www.cemf.ir
localProcessors.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 #include "localProcessors.hpp"
21 
23 :
24  processors(),
25  localSize_(processors::globalSize()),
26  localRank_(processors::globalRank()),
27  name_(name)
28 {
29 #ifdef pFlow_Build_MPI
30  parrentCommunicator_ = MPI_COMM_WORLD;
31  localCommunicator_ = MPI_COMM_WORLD;
32 #endif
33 
34 }
35 
37 (
38  const word &name,
39  const std::vector<int> &ranks
40 )
41 :
42  processors(),
43  name_(name)
44 {
45 #ifdef pFlow_Build_MPI
46  parrentCommunicator_ = MPI_COMM_WORLD;
47 
48  if(ranks.size()> this->globalSize() )
49  {
51  "the size of ranks is larger than parrent communicator size "<<endl;
52  fatalExit;
53  }
54 
55  MPI_Group globalGroup;
56  MPI_Comm_group(parrentCommunicator_, &globalGroup);
57 
58  MPI_Group localGroup;
59  MPI_Group_incl(globalGroup, ranks.size(), ranks.data(), &localGroup);
60 
61  // Create the new communicator from that group of processes.
62 
63  CheckMPI( MPI_Comm_create(parrentCommunicator_, localGroup, &localCommunicator_), true);
64 
65  isPartOfLocal_ = localCommunicator_ != MPI_COMM_NULL;
66 
67  if(isPartOfLocal_)
68  {
69  MPI_Comm_set_name(localCommunicator_, name.c_str());
70 
71  MPI_Comm_size(localCommunicator_, &localSize_);
72  MPI_Comm_rank(localCommunicator_, &localRank_);
73  }
74 
75 #else
76 
77  if(ranks.size()> 1 )
78  {
80  "rank size for processors should not be greater than 1.";
81  fatalExit;
82  }
83  localSize_ = processors::globalSize();
84  localRank_ = processors::globalRank();
85 
86 #endif
87 }
88 
89 #ifdef pFlow_Build_MPI
90 
92 (
93  MPI_Comm parrentComm,
94  const word &name,
95  const std::vector<int> &ranks
96 )
97 :
98  processors(),
99  name_(name)
100 {
101  parrentCommunicator_ = parrentComm;
102 
103  int parSize;
104 
105  MPI_Comm_size(parrentCommunicator_, &parSize);
106 
107  if(ranks.size()> parSize )
108  {
110  "the size of ranks is larger than parrent communicator size "<<endl;
111  fatalExit;
112  }
113 
114  MPI_Group parGroup;
115  MPI_Comm_group(parrentCommunicator_, &parGroup);
116 
117  MPI_Group localGroup;
118  MPI_Group_incl(parGroup, ranks.size(), ranks.data(), &localGroup);
119 
120  // Create the new communicator from that group of processes.
121 
122  CheckMPI( MPI_Comm_create(parrentCommunicator_, localGroup, &localCommunicator_), true);
123 
124  isPartOfLocal_ = localCommunicator_ != MPI_COMM_NULL;
125 
126  if(isPartOfLocal_)
127  {
128  MPI_Comm_set_name(localCommunicator_, name.c_str());
129 
130  MPI_Comm_size(localCommunicator_, &localSize_);
131  MPI_Comm_rank(localCommunicator_, &localRank_);
132  }
133 
134 }
135 
136 #endif
pFlow::localProcessors::name
const auto & name() const
return name of the local communicator
Definition: localProcessors.hpp:82
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::localProcessors::localRank_
int localRank_
Definition: localProcessors.hpp:49
pFlow::processors::globalRank
static int globalRank()
Rank of the processor in the global MPI.
Definition: processors.hpp:139
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::processors
This class holds the information about the global execution world and number of processors in communi...
Definition: processors.hpp:51
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::processors::globalSize
static int globalSize()
Global size of processors.
Definition: processors.hpp:132
pFlow::localProcessors::localProcessors
localProcessors(const word &name)
Construct a local processor communication similar to global with specified name.
Definition: localProcessors.cpp:22
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
CheckMPI
#define CheckMPI(caller, fAbort)
Definition: processors.hpp:39
pFlow::localProcessors::localSize_
int localSize_
Definition: localProcessors.hpp:47
localProcessors.hpp
pFlow::localProcessors::isPartOfLocal_
bool isPartOfLocal_
Definition: localProcessors.hpp:51