Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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,v 1.7 2004/07/21 16:30:54 chrish42 Exp $ 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 #include <plearn/math/Mat.h> 00055 #include <plearn/base/plerror.h> 00056 #include <plearn/io/plstreams.h> 00057 // norman: changed to standard calls 00058 #include <iostream> 00059 #include <fstream> 00060 00061 namespace PLearn { 00062 using namespace std; 00063 00064 00214 // Let's define PLMPI_REAL to be either MPI_FLOAT or MPI_DOUBLE 00215 00216 #if USING_MPI 00217 #ifdef USEDOUBLE 00218 #define PLMPI_REAL MPI_DOUBLE 00219 #endif 00220 #ifdef USEFLOAT 00221 #define PLMPI_REAL MPI_FLOAT 00222 #endif 00223 #endif 00224 00226 00227 class PLMPI 00228 { 00229 protected: 00230 static streambuf* new_cin_buf; 00231 00232 public: 00233 static bool using_mpi; 00234 static int size; 00235 static int rank; 00236 00246 static bool synchronized; 00247 00252 // static oassignstream mycout; 00253 // static oassignstream mycerr; 00254 // static iassignstream mycin; 00255 static PStream mycout; 00256 static PStream mycerr; 00257 static PStream mycin; 00258 00261 static int tag; 00262 00263 inline static void init(int* argc, char*** argv); 00264 inline static void finalize(); 00265 00266 // exchange blocks of data among the PLMPI::size CPUs: 00267 // each CPU has the block starting at position PLMPI::rank*blocksize 00268 // and they must exchange the blocks so that, upon return, 00269 // data contains all of them on all the CPUs. A buffer 00270 // of length blocksize is provided by the caller. 00271 // The last block may potentially be shorter than blocksize. 00272 // (only for PLMPI::rank==PLMPI::size-1). This only happens 00273 // when blocksize*PLMPI::size != n. 00274 static void exchangeBlocks(double* data,int n,int blocksize,double* buffer=0); 00275 00276 // CPU i holds sourceBlock which is going to be 00277 // copied into the i-th block of columns of the destBlocks 00278 // matrix in all the CPUs. Note that the last block may be 00279 // smaller when destBlocks.width() is not a multiple of PLMPI::size. 00280 static void exchangeColumnBlocks(Mat sourceBlock, Mat destBlocks); 00281 00282 static void exchangeBlocks(float* data,int n,int blocksize,float* buffer=0); 00283 00284 00285 #if USING_MPI 00286 00290 inline static int wait_any(); 00291 00297 inline static int peek_any(); 00298 #endif 00299 00300 }; 00301 00303 00304 #if USING_MPI 00305 class pofstream: public ofstream 00306 { 00307 public: 00308 pofstream() {} 00309 pofstream(const char *name, ios::openmode mode=ios::out) 00310 { open(name, mode); } 00311 00312 void open(const char *name, ios::openmode mode=ios::out) 00313 { 00314 if(PLMPI::rank==0) 00315 ofstream::open(name, mode); 00316 else 00317 ios::rdbuf(nullout.rdbuf()); 00318 } 00319 00320 }; 00321 #else 00322 typedef ofstream pofstream; 00323 #endif 00324 00325 00327 00328 inline void PLMPI::init(int* argc, char*** argv) 00329 { 00330 mycout(&cout);//.rdbuf(cout.rdbuf()); 00331 mycerr(&cerr);//.rdbuf(cerr.rdbuf()); 00332 mycin(&cin);//.rdbuf(cin.rdbuf()); 00333 00334 #if USING_MPI 00335 MPI_Init( argc, argv ); 00336 MPI_Comm_size( MPI_COMM_WORLD, &size) ; 00337 MPI_Comm_rank( MPI_COMM_WORLD, &rank ); 00338 if(rank!=0) 00339 { 00340 cout.rdbuf(nullout.rdbuf()); 00341 cin.rdbuf(nullin.rdbuf()); 00342 } 00343 #endif 00344 } 00345 00346 inline void PLMPI::finalize() 00347 { 00348 #if USING_MPI 00349 MPI_Finalize(); 00350 #endif 00351 } 00352 00353 00354 #if USING_MPI 00355 inline int PLMPI::wait_any() 00356 { 00357 MPI_Status status; 00358 MPI_Probe(MPI_ANY_SOURCE, PLMPI::tag, MPI_COMM_WORLD, &status); 00359 return status.MPI_SOURCE; 00360 } 00361 00362 inline int PLMPI::peek_any() 00363 { 00364 int ready; 00365 MPI_Status status; 00366 MPI_Iprobe(MPI_ANY_SOURCE, PLMPI::tag, MPI_COMM_WORLD, &ready, &status); 00367 if(!ready) 00368 return -1; 00369 else 00370 return status.MPI_SOURCE; 00371 } 00372 #endif 00373 00374 } // end of namespace PLearn 00375 00376 00377 #endif 00378 00379 00380 00381 00382

Generated on Tue Aug 17 16:01:48 2004 for PLearn by doxygen 1.3.7