OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Pstream.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 // NOTE:
00049 // valid parallel options vary between implementations, but flag common ones.
00050 // if they are not removed by MPI_Init(), the subsequent argument processing
00051 // will notice that they are wrong
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     //signal(SIGABRT, stop);
00116 
00117     // Now that nprocs is known construct communication tables.
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         int myProcNo = Pstream::myProcNo();
00281         int nProcs = Pstream::nProcs();
00282 
00283         //
00284         // receive from children
00285         //
00286         int level = 1;
00287         int thisLevelOffset = 2;
00288         int childLevelOffset = thisLevelOffset/2;
00289         int childProcId = 0;
00290 
00291         while
00292         (
00293             (childLevelOffset < nProcs)
00294          && (myProcNo % thisLevelOffset) == 0
00295         )
00296         {
00297             childProcId = myProcNo + childLevelOffset;
00298 
00299             scalar value;
00300 
00301             if (childProcId < nProcs)
00302             {
00303                 if
00304                 (
00305                     MPI_Recv
00306                     (
00307                         &value,
00308                         1,
00309                         MPI_SCALAR,
00310                         Pstream::procID(childProcId),
00311                         Pstream::msgType(),
00312                         MPI_COMM_WORLD,
00313                         MPI_STATUS_IGNORE
00314                     )
00315                 )
00316                 {
00317                     FatalErrorIn
00318                     (
00319                         "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00320                     )   << "MPI_Recv failed"
00321                         << Foam::abort(FatalError);
00322                 }
00323 
00324                 Value = bop(Value, value);
00325             }
00326 
00327             level++;
00328             thisLevelOffset <<= 1;
00329             childLevelOffset = thisLevelOffset/2;
00330         }
00331 
00332         //
00333         // send and receive from parent
00334         //
00335         if (!Pstream::master())
00336         {
00337             int parentId = myProcNo - (myProcNo % thisLevelOffset);
00338 
00339             if
00340             (
00341                 MPI_Send
00342                 (
00343                     &Value,
00344                     1,
00345                     MPI_SCALAR,
00346                     Pstream::procID(parentId),
00347                     Pstream::msgType(),
00348                     MPI_COMM_WORLD
00349                 )
00350             )
00351             {
00352                 FatalErrorIn
00353                 (
00354                     "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00355                 )   << "MPI_Send failed"
00356                     << Foam::abort(FatalError);
00357             }
00358 
00359             if
00360             (
00361                 MPI_Recv
00362                 (
00363                     &Value,
00364                     1,
00365                     MPI_SCALAR,
00366                     Pstream::procID(parentId),
00367                     Pstream::msgType(),
00368                     MPI_COMM_WORLD,
00369                     MPI_STATUS_IGNORE
00370                 )
00371             )
00372             {
00373                 FatalErrorIn
00374                 (
00375                     "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00376                 )   << "MPI_Recv failed"
00377                     << Foam::abort(FatalError);
00378             }
00379         }
00380 
00381 
00382         //
00383         // distribute to my children
00384         //
00385         level--;
00386         thisLevelOffset >>= 1;
00387         childLevelOffset = thisLevelOffset/2;
00388 
00389         while (level > 0)
00390         {
00391             childProcId = myProcNo + childLevelOffset;
00392 
00393             if (childProcId < nProcs)
00394             {
00395                 if
00396                 (
00397                     MPI_Send
00398                     (
00399                         &Value,
00400                         1,
00401                         MPI_SCALAR,
00402                         Pstream::procID(childProcId),
00403                         Pstream::msgType(),
00404                         MPI_COMM_WORLD
00405                     )
00406                 )
00407                 {
00408                     FatalErrorIn
00409                     (
00410                         "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00411                     )   << "MPI_Send failed"
00412                         << Foam::abort(FatalError);
00413                 }
00414             }
00415 
00416             level--;
00417             thisLevelOffset >>= 1;
00418             childLevelOffset = thisLevelOffset/2;
00419         }
00420         */
00421     }
00422 }
00423 
00424 
00425 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00426 
00427 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd