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 Description
00026     Pstream for GAMMA
00027 
00028     GAMMA has a (polling) receive handler which gets called every time a
00029     received message is complete. Ours stores the length of the currently
00030     received message and sets up the next buffer to store the next message
00031     in.
00032     Note that the pattern between two processors can be
00033     - send
00034     - receive
00035     - receive
00036     - send
00037     since the first swap might belong to a local exchange and the second to
00038     a reduce. Since gamma has to have the receive buffers already set up we
00039     have to allocate them big enough. To prevent excessive amounts needed we
00040     dynamically resize them (never shrink) by sending special 'resize' messages
00041     before sending a largish message.
00042 
00043     Because of this we actually need four receive buffers:
00044     - send
00045     - receive resize message
00046     - receive normal message
00047     - receive resize message
00048     - receive normal message
00049     - send
00050 
00051     The special resize message is a message with a special header which
00052     (hopefully) should never appear in normal exchanges (it actually checks
00053     for this in the OPstream::send)
00054 
00055 \*---------------------------------------------------------------------------*/
00056 
00057 #include "Pstream.H"
00058 #include "PstreamReduceOps.H"
00059 #include "OSspecific.H"
00060 #include "PstreamGlobals.H"
00061 
00062 #include <cstring>
00063 #include <cstdlib>
00064 #include <csignal>
00065 
00066 extern "C"
00067 {
00068 #   include <linux/gamma/libgamma.h>
00069 }
00070 
00071 
00072 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00073 
00074 namespace Foam
00075 {
00076 
00077 // Receive handler to copy out received message length and switch buffers.
00078 static void handler(void)
00079 {
00080     label current = PstreamGlobals::recvIndex[gamma_active_port];
00081 
00082     List<char>& buf = PstreamGlobals::recvBuf[current][gamma_active_port];
00083     label bufLen = PstreamGlobals::recvBufLen[current][gamma_active_port];
00084 
00085     if (bufLen != -1)
00086     {
00087         FatalErrorIn("Pstream::handler(void)")
00088             << "Buffer length not reset : "
00089             << bufLen
00090             << " when receiving message of size " << gamma_msglen
00091             << " from processor " << gamma_active_port << endl
00092             << "This means that the existing data has not been consumed yet"
00093             << " (by IPstream::read) and means your communication pattern"
00094             << " is probably not balanced (a receive for every send)"
00095             << endl
00096             << "This can happen if you have e.g. gather without scatter."
00097             << endl
00098             << "A workaround is to increase the depth of the circular"
00099             << " receive buffers in PstreamGlobals.H"
00100             << abort(FatalError);
00101     }
00102 
00103 
00104     // Some checks
00105     if
00106     (
00107         gamma_msglen < 0
00108      || gamma_msglen > buf.size()
00109     )
00110     {
00111         FatalErrorIn("Pstream::handler(void)")
00112             << "Received message of size " << gamma_msglen
00113             << " from processor " << gamma_active_port
00114             << Foam::endl
00115             << "but global receive buffer is only of size "
00116             << buf.size()
00117             << abort(FatalError);
00118     }
00119 
00120     // Check for resize message
00121     label resizeLen = PstreamGlobals::getSizeFromHeader
00122     (
00123         buf.begin(),
00124         gamma_msglen
00125     );
00126 
00127     if (resizeLen != -1)
00128     {
00129         if (Pstream::debug)
00130         {
00131             Pout<< "Pstream::handler : Resize message:" << resizeLen
00132                 << " from proc " << gamma_active_port
00133                 << " current size:"
00134                 << PstreamGlobals::getMaxBufSize(gamma_active_port)
00135                 << Foam::endl;
00136         }
00137 
00138         // Saved current buffer.
00139         List<char> savedBuf;
00140 
00141         if (resizeLen > PstreamGlobals::getMaxBufSize(gamma_active_port))
00142         {
00143             if (Pstream::debug)
00144             {
00145                 Pout<< "Pstream::handler :"
00146                     << " resizing receive buffer for processor "
00147                     << gamma_active_port
00148                     << " from "
00149                     << PstreamGlobals::getMaxBufSize(gamma_active_port)
00150                     << " to " << resizeLen << Foam::endl;
00151             }
00152 
00153             // Save the pointer (that gamma knows about) so we can safely
00154             // gamma_switch_to_buffer with a valid pointer.
00155             // Not sure if nessecary but do anyway.
00156             savedBuf.transfer(buf);
00157 
00158             // Resize all the buffers
00159             forAll(PstreamGlobals::recvBuf, i)
00160             {
00161                 List<char>& chars =
00162                     PstreamGlobals::recvBuf[i][gamma_active_port];
00163 
00164 //                gamma_munlock(chars.begin(), chars.size());
00165                 chars.setSize(resizeLen);
00166 //                gamma_mlock(chars.begin(), chars.size());
00167             }
00168         }
00169 
00170         // Update length with special value to denote resize was done.
00171         PstreamGlobals::recvBufLen[current][gamma_active_port] = -2;
00172     }
00173     else
00174     {
00175         // Update length with actual message length
00176         PstreamGlobals::recvBufLen[current][gamma_active_port] = gamma_msglen;
00177     }
00178 
00179     // Go to next buffer.
00180     label next = PstreamGlobals::recvBuf.fcIndex(current);
00181     PstreamGlobals::recvIndex[gamma_active_port] = next;
00182 
00183 //    gamma_switch_to_buffer
00184     gamma_post_recv
00185     (
00186         gamma_active_port,
00187         PstreamGlobals::recvBuf[next][gamma_active_port].begin(),
00188         PstreamGlobals::recvBuf[next][gamma_active_port].size()
00189     );
00190 }
00191 
00192 
00193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00194 
00195 void Pstream::addValidParOptions(HashTable<string>& validParOptions)
00196 {
00197     validParOptions.insert("np", "");
00198     validParOptions.insert("p4pg", "PI file");
00199     validParOptions.insert("p4wd", "directory");
00200     validParOptions.insert("p4amslave", "");
00201     validParOptions.insert("p4yourname", "hostname");
00202 
00203     validParOptions.insert("machinefile", "machine file");
00204     validParOptions.insert("GAMMANP", "numProcs");
00205     validParOptions.insert("GAMMAHOME", "gamma cwd");
00206     validParOptions.insert("GAMMA", "1(enable) or 0(disable)");
00207 }
00208 
00209 
00210 bool Pstream::init(int& argc, char**& argv)
00211 {
00212     int numprocs = 0;
00213 
00214     string npString("-GAMMANP");
00215 
00216     for (label i = 0; i < argc; i++)
00217     {
00218         if (argv[i] == npString)
00219         {
00220             if (i+1 < argc)
00221             {
00222                 numprocs = atoi(argv[i+1]);
00223                 break;
00224             }
00225         }
00226     }
00227 
00228     // Initialize GAMMA
00229     unsigned char smallNumprocs = numprocs;
00230 
00231     gamma_init(smallNumprocs, argc, argv);
00232 
00233     myProcNo_ = gamma_my_node();
00234 
00235     // Make sure printing with prefix.
00236     setParRun();
00237 
00238     procIDs_.setSize(numprocs);
00239 
00240     forAll(procIDs_, procNo)
00241     {
00242         procIDs_[procNo] = procNo;
00243     }
00244 
00245 
00246     // Allocate receive buffers.
00247     // ~~~~~~~~~~~~~~~~~~~~~~~~~
00248 
00249     // Make sure each receive buffer is at least large enough to receive
00250     // the resize message.
00251 
00252     // Current active buffer
00253     PstreamGlobals::recvIndex.setSize(numprocs);
00254     PstreamGlobals::recvIndex = 0;
00255     PstreamGlobals::consumeIndex.setSize(numprocs);
00256     PstreamGlobals::consumeIndex = 0;
00257 
00258     forAll(PstreamGlobals::recvBuf, i)
00259     {
00260         PstreamGlobals::recvBufLen[i].setSize(numprocs);
00261         PstreamGlobals::recvBufLen[i] = -1;
00262 
00263         List<List<char> >& buffers = PstreamGlobals::recvBuf[i];
00264 
00265         buffers.setSize(numprocs);
00266         forAll(buffers, procNo)
00267         {
00268             if (procNo != myProcNo_)
00269             {
00270                 buffers[procNo].setSize(PstreamGlobals::initialBufferLen);
00271 
00272                 // Acc. to gamma sources all buffers need to be in memory.
00273                 // Either locked or "write touched".
00274 //                gamma_mlock
00275  //               (
00276   //                  buffers[procNo].begin(),
00277    //                 buffers[procNo].size()
00278     //            );
00279             }
00280         }
00281     }
00282 
00283 
00284     // Lock the special resize message
00285 //    gamma_mlock
00286 //    (
00287  //       reinterpret_cast<char*>(PstreamGlobals::resizeMessage),
00288   //      PstreamGlobals::resizeMessageLen*sizeof(uint64_t)
00289    // );
00290 
00291 
00292     // Attach current receive buffers
00293     forAll(procIDs_, procNo)
00294     {
00295         if (procNo != myProcNo_)
00296         {
00297             // Buffer index (always 0 at this point)
00298             label current = PstreamGlobals::recvIndex[procNo];
00299 
00300             // Current buffer for this processor.
00301             List<char>& buf = PstreamGlobals::recvBuf[current][procNo];
00302 
00303             gamma_set_active_port
00304             (
00305                 procNo,             //unsigned short port,
00306                 procNo,             //unsigned short dest_node,
00307                 gamma_my_par_pid(), //unsigned char dest_par_pid,
00308                 myProcNo_,          //unsigned short dest_port,
00309                 handler,            //callback
00310             procNo,             //unsigned short semaphore,
00311             GO_BACK,            //unsigned char buffer_kind,
00312                 buf.begin(),
00313             buf.size()
00314             );
00315         }
00316     }
00317 
00318 
00319     // Make sure all have allocated the ports (so set the receive buffers)
00320     gamma_sync();
00321 
00322     Info<< "GAMMA Pstream initialized with:" << nl
00323         << "    floatTransfer         : " << floatTransfer << nl
00324         << "    nProcsSimpleSum       : " << nProcsSimpleSum << nl
00325         << "    scheduledTransfer     : " << Pstream::scheduledTransfer << nl
00326         << Foam::endl;
00327 
00328     // Now that nprocs is known construct communication tables.
00329     initCommunicationSchedule();
00330 
00331     return true;
00332 }
00333 
00334 
00335 void Pstream::exit(int errnum)
00336 {
00337 //    gamma_munlockall();
00338     gamma_exit();
00339     //gamma_abort();
00340 }
00341 
00342 
00343 void Pstream::abort()
00344 {
00345 Pout<< "**Pstream::abort()**" << endl;
00346 //    gamma_munlockall();
00347     gamma_abort();
00348 }
00349 
00350 
00351 void reduce(scalar& Value, const sumOp<scalar>& bop)
00352 {
00353     if (!Pstream::parRun())
00354     {
00355         return;
00356     }
00357 
00358     if (Pstream::debug)
00359     {
00360         Pout<< "**entering Pstream::reduce for " << Value << Foam::endl;
00361     }
00362 
00363 
00364     if (Pstream::master())
00365     {
00366         for
00367         (
00368             int slave=Pstream::firstSlave();
00369             slave<=Pstream::lastSlave();
00370             slave++
00371         )
00372         {
00373             scalar value;
00374 
00375             if
00376             (
00377                !IPstream::read
00378                 (
00379                     slave,
00380                     reinterpret_cast<char*>(&value),    // buf
00381                     sizeof(Value)                       // bufSize
00382                 )
00383             )
00384             {
00385                 FatalErrorIn
00386                 (
00387                     "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00388                 )   << "IPstream::read failed"
00389                     << Foam::abort(FatalError);
00390             }
00391 
00392             Value = bop(Value, value);
00393         }
00394     }
00395     else
00396     {
00397         if
00398         (
00399            !OPstream::write
00400             (
00401                 Pstream::masterNo(),
00402                 reinterpret_cast<const char*>(&Value),  // buf
00403                 sizeof(Value),                          // bufSize
00404                 false                                   // non-buffered
00405             )
00406         )
00407         {
00408             FatalErrorIn
00409             (
00410                 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00411             )   << "OPstream::write failed"
00412                 << Foam::abort(FatalError);
00413         }
00414     }
00415 
00416     if (Pstream::master())
00417     {
00418         for
00419         (
00420             int slave=Pstream::firstSlave();
00421             slave<=Pstream::lastSlave();
00422             slave++
00423         )
00424         {
00425             if
00426             (
00427                !OPstream::write
00428                 (
00429                     slave,
00430                     reinterpret_cast<const char*>(&Value),  // buf
00431                     sizeof(Value),                          // bufSize,
00432                     false                                   // non-buffered
00433                 )
00434             )
00435             {
00436                 FatalErrorIn
00437                 (
00438                     "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00439                 )   << "OPstream::write failed"
00440                     << Foam::abort(FatalError);
00441             }
00442         }
00443     }
00444     else
00445     {
00446         if
00447         (
00448            !IPstream::read
00449             (
00450                 Pstream::masterNo(),
00451                 reinterpret_cast<char*>(&Value),    // buf
00452                 sizeof(Value)                       // bufSize
00453             )
00454         )
00455         {
00456             FatalErrorIn
00457             (
00458                 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00459             )   << "IPstream::read failed"
00460                 << Foam::abort(FatalError);
00461         }
00462     }
00463 
00464     if (Pstream::debug)
00465     {
00466         Pout<< "**exiting Pstream::reduce with " << Value << Foam::endl;
00467     }
00468 }
00469 
00470 
00471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00472 
00473 } // End namespace Foam
00474 
00475 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd