00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "mpi.h"
00028
00029 #include "Pstream.H"
00030 #include "PstreamReduceOps.H"
00031 #include "OSspecific.H"
00032
00033 #include <cstring>
00034 #include <cstdlib>
00035 #include <csignal>
00036
00037 #if defined(WM_SP)
00038 # define MPI_SCALAR MPI_FLOAT
00039 #elif defined(WM_DP)
00040 # define MPI_SCALAR MPI_DOUBLE
00041 #endif
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 void Foam::Pstream::addValidParOptions(HashTable<string>& validParOptions)
00053 {
00054 validParOptions.insert("np", "");
00055 validParOptions.insert("p4pg", "PI file");
00056 validParOptions.insert("p4wd", "directory");
00057 validParOptions.insert("p4amslave", "");
00058 validParOptions.insert("p4yourname", "hostname");
00059 validParOptions.insert("GAMMANP", "number of instances");
00060 validParOptions.insert("machinefile", "machine file");
00061 }
00062
00063
00064 bool Foam::Pstream::init(int& argc, char**& argv)
00065 {
00066 MPI_Init(&argc, &argv);
00067
00068 int numprocs;
00069 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
00070 MPI_Comm_rank(MPI_COMM_WORLD, &myProcNo_);
00071
00072 if (numprocs <= 1)
00073 {
00074 FatalErrorIn("Pstream::init(int& argc, char**& argv)")
00075 << "bool Pstream::init(int& argc, char**& argv) : "
00076 "attempt to run parallel on 1 processor"
00077 << Foam::abort(FatalError);
00078 }
00079
00080 procIDs_.setSize(numprocs);
00081
00082 forAll(procIDs_, procNo)
00083 {
00084 procIDs_[procNo] = procNo;
00085 }
00086
00087 setParRun();
00088
00089 # ifndef SGIMPI
00090 string bufferSizeName = getEnv("MPI_BUFFER_SIZE");
00091
00092 if (bufferSizeName.size())
00093 {
00094 int bufferSize = atoi(bufferSizeName.c_str());
00095
00096 if (bufferSize)
00097 {
00098 MPI_Buffer_attach(new char[bufferSize], bufferSize);
00099 }
00100 }
00101 else
00102 {
00103 FatalErrorIn("Pstream::init(int& argc, char**& argv)")
00104 << "Pstream::init(int& argc, char**& argv) : "
00105 << "environment variable MPI_BUFFER_SIZE not defined"
00106 << Foam::abort(FatalError);
00107 }
00108 # endif
00109
00110 int processorNameLen;
00111 char processorName[MPI_MAX_PROCESSOR_NAME];
00112
00113 MPI_Get_processor_name(processorName, &processorNameLen);
00114
00115
00116
00117
00118 initCommunicationSchedule();
00119
00120 return true;
00121 }
00122
00123
00124 void Foam::Pstream::exit(int errnum)
00125 {
00126 # ifndef SGIMPI
00127 int size;
00128 char* buff;
00129 MPI_Buffer_detach(&buff, &size);
00130 delete[] buff;
00131 # endif
00132
00133 if (errnum == 0)
00134 {
00135 MPI_Finalize();
00136 ::exit(errnum);
00137 }
00138 else
00139 {
00140 MPI_Abort(MPI_COMM_WORLD, errnum);
00141 }
00142 }
00143
00144
00145 void Foam::Pstream::abort()
00146 {
00147 MPI_Abort(MPI_COMM_WORLD, 1);
00148 }
00149
00150
00151 void Foam::reduce(scalar& Value, const sumOp<scalar>& bop)
00152 {
00153 if (!Pstream::parRun())
00154 {
00155 return;
00156 }
00157
00158 if (Pstream::nProcs() <= Pstream::nProcsSimpleSum)
00159 {
00160 if (Pstream::master())
00161 {
00162 for
00163 (
00164 int slave=Pstream::firstSlave();
00165 slave<=Pstream::lastSlave();
00166 slave++
00167 )
00168 {
00169 scalar value;
00170
00171 if
00172 (
00173 MPI_Recv
00174 (
00175 &value,
00176 1,
00177 MPI_SCALAR,
00178 Pstream::procID(slave),
00179 Pstream::msgType(),
00180 MPI_COMM_WORLD,
00181 MPI_STATUS_IGNORE
00182 )
00183 )
00184 {
00185 FatalErrorIn
00186 (
00187 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00188 ) << "MPI_Recv failed"
00189 << Foam::abort(FatalError);
00190 }
00191
00192 Value = bop(Value, value);
00193 }
00194 }
00195 else
00196 {
00197 if
00198 (
00199 MPI_Send
00200 (
00201 &Value,
00202 1,
00203 MPI_SCALAR,
00204 Pstream::procID(Pstream::masterNo()),
00205 Pstream::msgType(),
00206 MPI_COMM_WORLD
00207 )
00208 )
00209 {
00210 FatalErrorIn
00211 (
00212 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00213 ) << "MPI_Send failed"
00214 << Foam::abort(FatalError);
00215 }
00216 }
00217
00218
00219 if (Pstream::master())
00220 {
00221 for
00222 (
00223 int slave=Pstream::firstSlave();
00224 slave<=Pstream::lastSlave();
00225 slave++
00226 )
00227 {
00228 if
00229 (
00230 MPI_Send
00231 (
00232 &Value,
00233 1,
00234 MPI_SCALAR,
00235 Pstream::procID(slave),
00236 Pstream::msgType(),
00237 MPI_COMM_WORLD
00238 )
00239 )
00240 {
00241 FatalErrorIn
00242 (
00243 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00244 ) << "MPI_Send failed"
00245 << Foam::abort(FatalError);
00246 }
00247 }
00248 }
00249 else
00250 {
00251 if
00252 (
00253 MPI_Recv
00254 (
00255 &Value,
00256 1,
00257 MPI_SCALAR,
00258 Pstream::procID(Pstream::masterNo()),
00259 Pstream::msgType(),
00260 MPI_COMM_WORLD,
00261 MPI_STATUS_IGNORE
00262 )
00263 )
00264 {
00265 FatalErrorIn
00266 (
00267 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00268 ) << "MPI_Recv failed"
00269 << Foam::abort(FatalError);
00270 }
00271 }
00272 }
00273 else
00274 {
00275 scalar sum;
00276 MPI_Allreduce(&Value, &sum, 1, MPI_SCALAR, MPI_SUM, MPI_COMM_WORLD);
00277 Value = sum;
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 }
00422 }
00423
00424
00425
00426
00427