'ElMo-Knock'

mpi_utils.h

Go to the documentation of this file.
00001 
00002 #ifndef MPI_UTILS_H
00003 #define MPI_UTILS_H
00004 
00005 #ifdef PARALLEL
00006 #include <mpi.h>
00007 #endif
00008 #include <string>
00009 #include <vector>
00010 #include <assert.h>
00011 #include "util.h"
00012 
00013 #ifdef PARALLEL
00014 template<typename T> MPI_Datatype MPI_getType();
00015 template<> inline MPI_Datatype MPI_getType<bool>()          {
00016     switch(sizeof(bool)) {
00017     case 1: return MPI_CHAR;
00018     case 2: return MPI_SHORT;
00019     case 4: return MPI_INT;
00020     case 8: return MPI_LONG;
00021     default: assert(sizeof(bool)==1);
00022     }
00023 }
00024 
00025 template<> inline MPI_Datatype MPI_getType<char>()          {return MPI_CHAR;}
00026 template<> inline MPI_Datatype MPI_getType<unsigned char>() {return MPI_BYTE;}
00027 template<> inline MPI_Datatype MPI_getType<short>()         {return MPI_SHORT;}
00028 template<> inline MPI_Datatype MPI_getType<unsigned short>(){return MPI_UNSIGNED_SHORT;}
00029 template<> inline MPI_Datatype MPI_getType<int>()           {return MPI_INT;}
00030 template<> inline MPI_Datatype MPI_getType<unsigned int>()  {return MPI_UNSIGNED;}
00031 template<> inline MPI_Datatype MPI_getType<long>()          {return MPI_LONG;}
00032 template<> inline MPI_Datatype MPI_getType<unsigned long>() {return MPI_UNSIGNED_LONG;}
00033 template<> inline MPI_Datatype MPI_getType<unsigned long long>() {return MPI_UNSIGNED_LONG;}
00034 template<> inline MPI_Datatype MPI_getType<float>()         {return MPI_FLOAT;}
00035 template<> inline MPI_Datatype MPI_getType<double>()        {return MPI_DOUBLE;}
00036 #endif
00037 
00038 int mpi_bcast_vector_of_strings(vector<string> &vector_of_strings) ;         
00039 int mpi_bcast_string(std::string &ss);
00040 
00041 template<typename T> int mpi_bcast_scalar(T &elem) {
00042         #ifdef PARALLEL
00043                 MPI_Bcast( &elem, 1, MPI_getType<T>(), 0, MPI_COMM_WORLD );
00044         #endif
00045         return 0;
00046 }
00047 
00048 template<class T>  int mpi_bcast_vector( std::vector<T> &v) {
00049     typename std::vector<T>::iterator it;
00050     int proc_id=0,nump=1;
00051     util::get_proc_id_nump(proc_id,nump);   
00052     unsigned int sz;
00053 
00054     if (proc_id==0)
00055         sz = v.size();
00056 
00057     mpi_bcast_scalar(sz);
00058 
00059     if (proc_id!=0)
00060         v.resize(sz);
00061     for( it=v.begin(); it!=v.end(); it++ )
00062         mpi_bcast_scalar(*it);
00063     return 0;
00064 }
00065 
00066 template<class T> int mpi_bcast_matrix( std::vector< std::vector<T> > &m) {
00067 
00068 
00069     int proc_id=0,nump=1;
00070     util::get_proc_id_nump(proc_id,nump);   
00071     int rows, cols;     
00072     if (proc_id==0) {
00073         rows=m.size(); cols=m[0].size();
00074     }
00075     mpi_bcast_scalar(rows);
00076     mpi_bcast_scalar(cols);
00077     if (proc_id!=0) 
00078         m.resize(rows,vector<T>(cols));
00079     for (int i=0;i<rows;i++)
00080         mpi_bcast_vector(m[i]);
00081         
00082 }
00083 
00084 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines