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 
00045 
00048 
#ifndef MPIStream_INC
00049 
#define MPIStream_INC
00050 
00051 
#if USING_MPI
00052 
00053 
#include <iostream>
00054 
#include <plearn/sys/PLMPI.h>
00055 
00056 
namespace PLearn {
00057 
using namespace std;
00058 
00059   
class MPIStreambuf : 
public streambuf
00060   {
00061   
protected:
00062     
int tag; 
00063     
int peerrank; 
00064   
00065     
char* inbuf;
00066     
int inbuf_capacity;
00067   
00068   
protected:
00069 
00071     
inline void reserveInputBuffer(
int buflen)
00072     {
00073       
if(buflen>inbuf_capacity)
00074         {
00075           
if(inbuf)
00076             
delete[] inbuf;
00077           inbuf = 
new char[buflen];
00078           inbuf_capacity = buflen;
00079         }
00080     }
00081 
00083     
virtual int overflow(
int c=EOF);
00084     
virtual int underflow();
00085     
00087     
virtual int showmanyc();
00088     
virtual streamsize xsputn(
const char* s, streamsize n);
00089     
virtual streamsize xsgetn(
char* s, streamsize n);
00092     
virtual streambuf* setbuf(
char* p, 
int len);
00093     
virtual int sync();
00094 
00095   
public:
00096     MPIStreambuf(
int the_peerrank, 
int inbufsize);
00097     
virtual ~MPIStreambuf();
00098 
00099   };
00100   
00101   
class MPIStream: 
public iostream
00102   {
00103   
protected:
00104     
char* outbuffer;
00105 
00106   
public:
00107 
00108     MPIStream()
00109       :
iostream(0), outbuffer(0)
00110     {}
00111 
00113     MPIStream(
int the_peerrank, 
int inbufsize=200, 
int outbufsize=200)
00114       :
iostream(0), outbuffer(0)
00115     { init(the_peerrank, inbufsize, outbufsize); }
00116 
00118     
void init(
int the_peerrank, 
int inbufsize= 0, 
int outbufsize= 0);
00119 
00120     ~MPIStream();
00121   };
00122 
00123 
00124   
00125   
class MPIStreams
00126   {
00127   
protected:
00128     MPIStream* mpistreams;
00129 
00130   
public:
00131     MPIStreams(
int inbufsize=200, 
int outbufsize=200);
00132 
00133     
int size()
 const { 
return PLMPI::size; } 
00134     
int length()
 const { 
return PLMPI::size; } 
00135 
00136     
iostream& operator[](
int i)
00137     {
00138 
#ifdef BOUNDCHECK
00139 
      if(i<0 || i>=PLMPI::size)
00140         
PLERROR(
"In MPIStreams OUT OF BOUND ACCESS");
00141 
#endif      
00142 
      return mpistreams[i];
00143     }
00144 
00145     ~MPIStreams() 
00146     { 
if(mpistreams) 
delete[] mpistreams; }
00147   };
00148 
00149 } 
00150 
00152 
#endif 
00153 
00155 
#endif
00156