00001 
00002 
00003 
00004 #ifndef MPI_UTILITIES_H_
00005 #define MPI_UTILITIES_H_
00006 
00007 #ifdef MPI_ENABLED
00008 
00009 #include <mpi.h>
00010 
00011 namespace MPI
00012 {
00040 
00041         inline const Datatype& type(bool) { return BOOL; }
00042         inline const Datatype& type(const bool*) { return BOOL; }
00043         inline const Datatype& type(char) { return CHAR; }
00044         inline const Datatype& type(const char*) { return CHAR; }
00045         inline const Datatype& type(unsigned char) { return UNSIGNED_CHAR; }
00046         inline const Datatype& type(const unsigned char*) { return UNSIGNED_CHAR; }
00047         inline const Datatype& type(short int) { return SHORT; }
00048         inline const Datatype& type(const short int*) { return SHORT; }
00049         inline const Datatype& type(unsigned short int) { return UNSIGNED_SHORT; }
00050         inline const Datatype& type(const unsigned short int*) { return UNSIGNED_SHORT; }
00051         inline const Datatype& type(int) { return INT; }
00052         inline const Datatype& type(const int*) { return INT; }
00053         inline const Datatype& type(unsigned int) { return UNSIGNED; }
00054         inline const Datatype& type(const unsigned int*) { return UNSIGNED; }
00055         inline const Datatype& type(long) { return LONG_INT; }
00056         inline const Datatype& type(const long*) { return LONG_INT; }
00057         inline const Datatype& type(unsigned long) { return UNSIGNED_LONG; }
00058         inline const Datatype& type(const unsigned long*) { return UNSIGNED_LONG; }
00059         inline const Datatype& type(sys_float) { return FLOAT; }
00060         inline const Datatype& type(const sys_float*) { return FLOAT; }
00061         inline const Datatype& type(double) { return DOUBLE; }
00062         inline const Datatype& type(const double*) { return DOUBLE; }
00063         inline const Datatype& type(complex<sys_float>) { return COMPLEX; }
00064         inline const Datatype& type(const complex<sys_float>*) { return COMPLEX; }
00065         inline const Datatype& type(complex<double>) { return DOUBLE_COMPLEX; }
00066         inline const Datatype& type(const complex<double>*) { return DOUBLE_COMPLEX; }
00067 }
00068 
00069 #else 
00070 
00071 namespace MPI
00072 {
00073         
00074         
00075         
00076         
00077         
00078         struct t_MPI
00079         {
00080                 int total_insanity() { return TotalInsanityAsStub<int>(); }
00081         };
00082         extern t_MPI COMM_WORLD;
00083 }
00084 
00085 
00086 
00087 #define Barrier() total_insanity()
00088 #define Bcast(W,X,Y,Z) total_insanity()
00089 #define Finalize() COMM_WORLD.total_insanity()
00090 #define Get_size() total_insanity()
00091 #define Get_rank() total_insanity()
00092 #define Init(Y,Z) COMM_WORLD.total_insanity()
00093 #define Reduce(U,V,W,X,Y,Z) total_insanity()
00094 
00095 #endif 
00096 
00097 class load_balance
00098 {
00099         vector<int> p_jobs;
00100         unsigned int p_ptr;
00101         void p_clear0()
00102         {
00103                 p_jobs.clear();
00104         }
00105         void p_clear1()
00106         {
00107                 p_ptr = 0;
00108         }
00109 public:
00110         load_balance()
00111         {
00112                 p_clear1();
00113         }
00114         explicit load_balance( int nJobs )
00115         {
00116                 p_clear1();
00117                 init( nJobs );
00118         }
00119         ~load_balance()
00120         {
00121                 p_clear0();
00122         }
00123         void clear()
00124         {
00125                 p_clear0();
00126                 p_clear1();
00127         }
00128         void init( int nJobs );
00129         int next_job()
00130         {
00131                 if( p_ptr < p_jobs.size() )
00132                 {
00133                         int res = p_jobs[p_ptr];
00134                         if( cpu.i().lgMPI() )
00135                                 p_ptr += MPI::COMM_WORLD.Get_size();
00136                         else
00137                                 p_ptr++;
00138                         return res;
00139                 }
00140                 else
00141                         return -1;
00142         }
00143         void finalize()
00144         {
00145                 
00146                 if( cpu.i().lgMPI() )
00147                         MPI::COMM_WORLD.Barrier();
00148         }
00149 };
00150 
00152 inline string GridPointPrefix(int n)
00153 {
00154         ostringstream oss;
00155         oss << "grid" << setfill( '0' ) << setw(9) << n << "_";
00156         return oss.str();
00157 }
00158 
00160 void process_output();
00161 
00163 void append_file( FILE*, const char* );
00164 
00165 #endif