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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
00058
#include <iostream>
00059
#include <fstream>
00060
00061
namespace PLearn {
00062
using namespace std;
00063
00064
00214
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
00253
00254
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
00267
00268
00269
00270
00271
00272
00273
00274
static void exchangeBlocks(
double* data,
int n,
int blocksize,
double* buffer=0);
00275
00276
00277
00278
00279
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);
00331
mycerr(&cerr);
00332
mycin(&cin);
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 }
00375
00376
00377
#endif
00378
00379
00380
00381
00382