www.cemf.ir
processors.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 
21 #include "phasicFlowConfig.H"
22 
23 #ifdef pFlow_Build_MPI
24  #include "mpiCommunication.hpp"
25 #endif
26 
27 // from PhasicFlow
28 #include "error.hpp"
29 #include "processors.hpp"
30 #include "streams.hpp"
31 
32 
33 #ifdef pFlow_Build_MPI
34 
35  pFlow::MPI::DataType pFlow::MPI::realx3Type__;
36  pFlow::MPI::DataType pFlow::MPI::realx4Type__;
37  pFlow::MPI::DataType pFlow::MPI::int32x3Type__;
38  pFlow::MPI::DataType pFlow::MPI::uint32x3Type__;
39 
40 #endif
41 
42 void pFlow::processors::initProcessors(int argc, char *argv[])
43 {
44 
45 #ifdef pFlow_Build_MPI
47  {
48  CheckMPI(MPI_Init(&argc, &argv), true);
49  initProcessorsCelled_ = true;
50 
51  argc_ = argc;
52  argv_ = argv;
53 
54  MPI_Comm_rank(MPI_COMM_WORLD, &processors::globalRank_);
55  MPI_Comm_size(MPI_COMM_WORLD, &processors::globalSize_);
56 
57 
59  {
64  }
65 
67 
68 
69  MPI_Type_contiguous(3, pFlow::MPI::Type<real>(), &pFlow::MPI::realx3Type__);
70  MPI_Type_commit(&pFlow::MPI::realx3Type__);
71 
72  MPI_Type_contiguous(4, pFlow::MPI::Type<real>(), &pFlow::MPI::realx4Type__);
73  MPI_Type_commit(&pFlow::MPI::realx3Type__);
74 
75  MPI_Type_contiguous(3, pFlow::MPI::Type<int32>(), &pFlow::MPI::int32x3Type__);
76  MPI_Type_commit(&pFlow::MPI::int32x3Type__);
77 
78  MPI_Type_contiguous(3, pFlow::MPI::Type<uint32>(), &pFlow::MPI::uint32x3Type__);
79  MPI_Type_commit(&pFlow::MPI::uint32x3Type__);
80 
81  }
82 #else
83 
84 #endif
85 
86 }
87 
89 {
90 
91 #ifdef pFlow_Build_MPI
92  if(initProcessorsCelled_ && !isFinalized())
93  {
94  MPI::TypeFree(&pFlow::MPI::realx3Type__);
95  MPI::TypeFree(&pFlow::MPI::realx4Type__);
96  MPI::TypeFree(&pFlow::MPI::int32x3Type__);
97  MPI::TypeFree(&pFlow::MPI::uint32x3Type__);
98  CheckMPI(MPI_Finalize(), true);
99  }
100 #else
101 
102 #endif
103 }
104 
106 {
107 
108 #ifdef pFlow_Build_MPI
109  if(globalParallel() && !isInitialized())
110  {
112  "MPI communication is not initialized yet!"<<endl;
113  processors::abort(MPI_ERR_OP);
114  }
115 #endif
116 
117 }
118 
120 {}
121 
122 
124 {
125 
126 #ifdef pFlow_Build_MPI
127  int res;
128  MPI_Initialized(&res);
129  return res;
130 #endif
131  return false;
132 
133 }
134 
136 {
137 
138 #ifdef pFlow_Build_MPI
139  int res;
140  MPI_Finalized(&res);
141  return res;
142 #endif
143  return false;
144 
145 }
146 
148 {
149 
150 #ifdef pFlow_Build_MPI
151  int flag=0;
152  MPI_Initialized(&flag);
153  if(flag == 1)
154  {
155  MPI_Abort(MPI_COMM_WORLD, error);
156  MPI_Finalize();
157  }
158  exit(error);
159 #else
160  exit(error);
161 #endif
162 
163 }
164 
165 bool pFlow::checkMPI(const char* funcName, int error, bool forceAbort, const char* fileName, int lineNumebr)
166 {
167 
168 #ifdef pFlow_Build_MPI
169 
170  if(error == MPI_SUCCESS) return true;
171  fatalErrorInMessage(funcName, fileName, lineNumebr);
172 
173  if(!forceAbort) return false;
174  reportAndExit(error);
175 
176  return false;
177 
178 #else
179 
180  return true;
181 
182 #endif
183 }
184 
185 
186 
187 
188 
189 
pFlow::processors::processors
processors()
Constructor.
Definition: processors.cpp:105
pFlow::processors::globalSize_
static int globalSize_
The global size of all processors.
Definition: processors.hpp:65
pFlow::checkMPI
bool checkMPI(const char *funcName, int error, bool forceAbort, const char *fileName, int lineNumebr)
Definition: processors.cpp:165
pFlow::pOutput
processorOstream pOutput
pFlow::processors::initProcessors
static void initProcessors(int argc, char *argv[])
Initialize MPI processors.
Definition: processors.cpp:42
pFlow::processorOstream::activatePrefix
void activatePrefix()
Activate prefix for this stream.
Definition: processorOstream.hpp:83
processors.hpp
pFlow::mOutput
masterOstream mOutput
pFlow::processors::~processors
~processors()
Destructor.
Definition: processors.cpp:119
pFlow::processors::finalizeProcessors
static void finalizeProcessors()
Finalize MPI processors.
Definition: processors.cpp:88
pFlow::processors::globalMaster
static bool globalMaster()
Is this processor the master processor?
Definition: processors.hpp:125
fatalErrorInMessage
pFlow::iOstream & fatalErrorInMessage(const char *fnName, const char *fileName, int linNumber)
Definition: error.cpp:44
pFlow::processors::globalParallel
static bool globalParallel()
Is this a parallel MPI run.
Definition: processors.hpp:98
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
reportAndExit
pFlow::iOstream & reportAndExit(int errorCode)
Definition: error.cpp:111
pFlow::processors::isFinalized
static bool isFinalized()
Is MPI finalized?
Definition: processors.cpp:135
pFlow::errReport
processorOstream errReport
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::processors::argc
static int argc()
Definition: processors.hpp:145
pFlow::processors::argv
static char ** argv()
Definition: processors.hpp:151
streams.hpp
pFlow::processors::argc_
static int argc_
Definition: processors.hpp:68
pFlow::processorOstream::setPrefixNum
word setPrefixNum(int procNum)
Set processor number to be used in the prefix.
Definition: processorOstream.hpp:76
pFlow::processors::globalRank_
static int globalRank_
Global rank of the current processor.
Definition: processors.hpp:61
pFlow::masterOstream::setMasterSlave
void setMasterSlave(bool master)
Set if this processor is slave or master.
Definition: masterOstream.hpp:68
pFlow::processors::initProcessorsCelled_
static bool initProcessorsCelled_
Is the static member initProcessors is called.
Definition: processors.hpp:57
pFlow::processors::abort
static void abort(int error)
Abort MPI run or regular run.
Definition: processors.cpp:147
pFlow::processors::argv_
static char ** argv_
Definition: processors.hpp:71
error.hpp
pFlow::processors::isInitialized
static bool isInitialized()
Is MPI initialized?
Definition: processors.cpp:123