PLearn 0.1
PLMPI.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 2001 Pascal Vincent
00005 //
00006 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 
00036  
00037 
00038 /* *******************************************************      
00039  * $Id: PLMPI.h 8789 2008-04-09 19:44:18Z nouiz $
00040  * AUTHORS: Pascal Vincent 
00041  * This file is part of the PLearn library.
00042  ******************************************************* */
00043 
00044 
00047 #ifndef PLMPI_INC
00048 #define PLMPI_INC
00049 
00050 #if USING_MPI
00051 #include <mpi.h>
00052 #endif
00053 
00054 // norman: changed to standard calls
00055 #include <iostream>
00056 #include <fstream>
00057 
00058 namespace PLearn {
00059 using namespace std;
00060 
00062 class Mat;
00063 class PStream;
00213 //  Let's define PLMPI_REAL to be either MPI_FLOAT or MPI_DOUBLE
00214 
00215 #if USING_MPI
00216 #ifdef USEDOUBLE
00217 #define PLMPI_REAL MPI_DOUBLE
00218 #endif
00219 #ifdef USEFLOAT
00220 #define PLMPI_REAL MPI_FLOAT
00221 #endif
00222 #endif
00223 
00225 
00226 class PLMPI
00227 {
00228 protected:
00229     static streambuf* new_cin_buf;
00230 
00231 public:
00232     static bool using_mpi; 
00233     static int size;  
00234     static int rank;  
00235 
00245     static bool synchronized; 
00246 
00251 //  static oassignstream mycout;
00252 //  static oassignstream mycerr;
00253 //  static iassignstream mycin;
00254     static PStream mycout;
00255     static PStream mycerr;
00256     static PStream mycin;
00257 
00260     static int tag; 
00261 
00262     static void init(int* argc, char*** argv);
00263     static void finalize();
00264   
00265     // exchange blocks of data among the PLMPI::size CPUs:
00266     // each CPU has the block starting at position PLMPI::rank*blocksize
00267     // and they must exchange the blocks so that, upon return,
00268     // data contains all of them on all the CPUs. A buffer
00269     // of length blocksize is provided by the caller.
00270     // The last block may potentially be shorter than blocksize.
00271     // (only for PLMPI::rank==PLMPI::size-1). This only happens
00272     // when blocksize*PLMPI::size != n.
00273     static void exchangeBlocks(double* data,int n,int blocksize,double* buffer=0);  
00274 
00275     // CPU i holds sourceBlock which is going to be
00276     // copied into the i-th block of columns of the destBlocks
00277     // matrix in all the CPUs. Note that the last block may be
00278     // smaller when destBlocks.width() is not a multiple of PLMPI::size.
00279     static void exchangeColumnBlocks(Mat sourceBlock, Mat destBlocks);
00280 
00281     static void exchangeBlocks(float* data,int n,int blocksize,float* buffer=0);
00282 
00283     
00284 #if USING_MPI  
00285 
00289     inline static int wait_any();
00290 
00296     inline static int peek_any();
00297 #endif
00298 
00299 };
00300 
00302 
00303 #if USING_MPI
00304 class pofstream: public ofstream
00305 {
00306 public:
00307     pofstream() {}
00308     pofstream(const char *name, ios::openmode mode=ios::out)
00309     { open(name, mode); }
00310 
00311     void open(const char *name,  ios::openmode mode=ios::out)
00312     { 
00313         if(PLMPI::rank==0)
00314             ofstream::open(name, mode); 
00315         else
00316             ios::rdbuf(nullout.rdbuf());
00317     }
00318 
00319 };
00320 #else
00321 typedef ofstream pofstream;
00322 #endif
00323 
00324 
00325 
00326 #if USING_MPI
00327 inline int PLMPI::wait_any()
00328 {
00329     MPI_Status status;
00330     MPI_Probe(MPI_ANY_SOURCE, PLMPI::tag, MPI_COMM_WORLD, &status);
00331     return status.MPI_SOURCE;
00332 }
00333 
00334 inline int PLMPI::peek_any()
00335 {
00336     int ready;
00337     MPI_Status status;
00338     MPI_Iprobe(MPI_ANY_SOURCE, PLMPI::tag, MPI_COMM_WORLD, &ready, &status);
00339     if(!ready)
00340         return -1;
00341     else
00342         return status.MPI_SOURCE;
00343 }
00344 #endif
00345 
00346 } // end of namespace PLearn
00347 
00348 
00349 #endif
00350 
00351 
00352 /*
00353   Local Variables:
00354   mode:c++
00355   c-basic-offset:4
00356   c-file-style:"stroustrup"
00357   c-file-offsets:((innamespace . 0)(inline-open . 0))
00358   indent-tabs-mode:nil
00359   fill-column:79
00360   End:
00361 */
00362 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines