00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <mpi.h>
00010 #include <vector>
00011
00012 #include "mess.h"
00013 #include "../../core/peo_debug.h"
00014 #include "node.h"
00015
00016 #define MPI_BUF_SIZE 1024*64
00017
00018 static char mpi_buf [MPI_BUF_SIZE];
00019
00020 static int pos_buf ;
00021
00022 static std :: vector <char *> act_buf;
00023
00024 static std :: vector <MPI_Request *> act_req;
00025
00026 void cleanBuffers () {
00027
00028 for (unsigned i = 0; i < act_req.size ();) {
00029
00030 MPI_Status stat ;
00031 int flag ;
00032 MPI_Test (act_req [i], & flag, & stat) ;
00033 if (flag) {
00034
00035 delete act_buf [i] ;
00036 delete act_req [i] ;
00037
00038 act_buf [i] = act_buf.back () ;
00039 act_buf.pop_back () ;
00040
00041 act_req [i] = act_req.back () ;
00042 act_req.pop_back () ;
00043 }
00044 else
00045 i ++;
00046 }
00047 }
00048
00049 void waitBuffers () {
00050
00051 printDebugMessage ("waiting the termination of the asynchronous operations to complete");
00052
00053 for (unsigned i = 0; i < act_req.size (); i ++) {
00054
00055 MPI_Status stat ;
00056
00057 MPI_Wait (act_req [i], & stat) ;
00058
00059 delete act_buf [i] ;
00060 delete act_req [i] ;
00061 }
00062 }
00063
00064 bool probeMessage (int & __src, int & __tag) {
00065
00066 int flag;
00067
00068 MPI_Status stat;
00069
00070 MPI_Iprobe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, & flag, & stat);
00071
00072 __src = stat.MPI_SOURCE;
00073 __tag = stat.MPI_TAG;
00074
00075 return flag;
00076 }
00077
00078 void waitMessage () {
00079
00080 MPI_Status stat;
00081
00082 MPI_Probe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, & stat);
00083 }
00084
00085 void initMessage () {
00086
00087 pos_buf = 0;
00088 }
00089
00090 void sendMessage (int __to, int __tag) {
00091
00092 cleanBuffers ();
00093 act_buf.push_back (new char [pos_buf]);
00094 act_req.push_back (new MPI_Request);
00095 memcpy (act_buf.back (), mpi_buf, pos_buf);
00096 MPI_Isend (act_buf.back (), pos_buf, MPI_PACKED, __to, __tag, MPI_COMM_WORLD, act_req.back ());
00097 }
00098
00099 void sendMessageToAll (int __tag) {
00100
00101 for (int i = 0; i < getNumberOfNodes (); i ++)
00102 sendMessage (i, __tag);
00103 }
00104
00105 void receiveMessage (int __from, int __tag) {
00106
00107 MPI_Status stat;
00108 MPI_Request req;
00109
00110 MPI_Irecv (mpi_buf, MPI_BUF_SIZE, MPI_PACKED, __from, __tag, MPI_COMM_WORLD, & req) ;
00111 MPI_Wait (& req, & stat) ;
00112 }
00113
00114
00115 void pack (const char & __c) {
00116
00117 MPI_Pack ((void *) & __c, 1, MPI_CHAR, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00118 }
00119
00120
00121 void pack (const float & __f, int __nitem) {
00122
00123 MPI_Pack ((void *) & __f, __nitem, MPI_FLOAT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00124 }
00125
00126
00127 void pack (const double & __d, int __nitem) {
00128
00129 MPI_Pack ((void *) & __d, __nitem, MPI_DOUBLE, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00130 }
00131
00132
00133 void pack (const int & __i, int __nitem) {
00134
00135 MPI_Pack ((void *) & __i, __nitem, MPI_INT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00136 }
00137
00138
00139 void pack (const unsigned int & __ui, int __nitem) {
00140
00141 MPI_Pack ((void *) & __ui, __nitem, MPI_UNSIGNED, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00142 }
00143
00144
00145 void pack (const short & __sh, int __nitem) {
00146
00147 MPI_Pack ((void *) & __sh, __nitem, MPI_SHORT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00148 }
00149
00150
00151 void pack (const unsigned short & __ush, int __nitem) {
00152
00153 MPI_Pack ((void *) & __ush, __nitem, MPI_UNSIGNED_SHORT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00154 }
00155
00156
00157 void pack (const long & __l, int __nitem) {
00158
00159 MPI_Pack ((void *) & __l, __nitem, MPI_LONG, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00160 }
00161
00162
00163 void pack (const unsigned long & __ul, int __nitem) {
00164
00165 MPI_Pack ((void *) & __ul, __nitem, MPI_UNSIGNED_LONG, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00166 }
00167
00168
00169 void pack (const char * __str) {
00170
00171 int len = strlen (__str) + 1;
00172 MPI_Pack (& len, 1, MPI_INT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00173 MPI_Pack ((void *) __str, len, MPI_CHAR, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00174 }
00175
00176
00177 void unpack (char & __c) {
00178
00179 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __c, 1, MPI_CHAR, MPI_COMM_WORLD);
00180 }
00181
00182
00183 void unpack (float & __f, int __nitem) {
00184
00185 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __f, __nitem, MPI_FLOAT, MPI_COMM_WORLD);
00186 }
00187
00188
00189 void unpack (double & __d, int __nitem) {
00190
00191 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __d, __nitem, MPI_DOUBLE, MPI_COMM_WORLD);
00192 }
00193
00194
00195 void unpack (int & __i, int __nitem) {
00196
00197 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __i, __nitem, MPI_INT, MPI_COMM_WORLD);
00198 }
00199
00200
00201 void unpack (unsigned int & __ui, int __nitem) {
00202
00203 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ui, __nitem, MPI_UNSIGNED, MPI_COMM_WORLD);
00204 }
00205
00206
00207 void unpack (short & __sh, int __nitem) {
00208
00209 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __sh, __nitem, MPI_SHORT, MPI_COMM_WORLD);
00210 }
00211
00212
00213 void unpack (unsigned short & __ush, int __nitem) {
00214
00215 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ush, __nitem, MPI_UNSIGNED_SHORT, MPI_COMM_WORLD);
00216 }
00217
00218
00219 void unpack (long & __l, int __nitem) {
00220
00221 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __l, __nitem, MPI_LONG, MPI_COMM_WORLD);
00222 }
00223
00224
00225 void unpack (unsigned long & __ul, int __nitem) {
00226
00227 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ul, __nitem, MPI_UNSIGNED_LONG, MPI_COMM_WORLD);
00228 }
00229
00230
00231 void unpack (char * __str) {
00232
00233 int len;
00234 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & len, 1, MPI_INT, MPI_COMM_WORLD);
00235 MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, __str, len, MPI_CHAR, MPI_COMM_WORLD);
00236 }
00237