mess.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "mess.cpp"
00004 
00005 // (c) OPAC Team, LIFL, August 2005
00006 
00007 /* This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011    
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020    
00021    Contact: paradiseo-help@lists.gforge.inria.fr
00022 */
00023 
00024 #include <mpi.h>
00025 #include <vector>
00026 
00027 #include "mess.h"
00028 #include "../../core/peo_debug.h"
00029 #include "node.h"
00030 
00031 #define MPI_BUF_SIZE 1024*64
00032         
00033 static char mpi_buf [MPI_BUF_SIZE];
00034         
00035 static int pos_buf ;
00036 
00037 static std :: vector <char *> act_buf; /* Active buffers */
00038 
00039 static std :: vector <MPI_Request *> act_req; /* Active requests */
00040 
00041 void cleanBuffers () {
00042 
00043   for (unsigned i = 0; i < act_req.size ();) {
00044        
00045     MPI_Status stat ;
00046     int flag ;
00047     MPI_Test (act_req [i], & flag, & stat) ;
00048     if (flag) {
00049       
00050       delete act_buf [i] ;
00051       delete act_req [i] ;
00052         
00053       act_buf [i] = act_buf.back () ;
00054       act_buf.pop_back () ;
00055       
00056       act_req [i] = act_req.back () ;
00057       act_req.pop_back () ;
00058     }
00059     else
00060       i ++;
00061   } 
00062 }
00063 
00064 void waitBuffers () {
00065 
00066   printDebugMessage ("waiting the termination of the asynchronous operations to complete");
00067 
00068   for (unsigned i = 0; i < act_req.size (); i ++) {
00069        
00070     MPI_Status stat ;
00071 
00072     MPI_Wait (act_req [i], & stat) ;
00073       
00074     delete act_buf [i] ;
00075     delete act_req [i] ;
00076   } 
00077 }
00078 
00079 bool probeMessage (int & __src, int & __tag) {
00080 
00081   int flag;
00082 
00083   MPI_Status stat;
00084 
00085   MPI_Iprobe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, & flag, & stat);
00086 
00087   __src = stat.MPI_SOURCE;
00088   __tag = stat.MPI_TAG;
00089 
00090   return flag;
00091 }
00092 
00093 void waitMessage () {
00094 
00095   MPI_Status stat;  
00096 
00097   MPI_Probe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, & stat);
00098 }
00099 
00100 void initMessage () {
00101   
00102   pos_buf = 0;
00103 }
00104 
00105 void sendMessage (int __to, int __tag) {
00106 
00107   cleanBuffers ();  
00108   act_buf.push_back (new char [pos_buf]);
00109   act_req.push_back (new MPI_Request);  
00110   memcpy (act_buf.back (), mpi_buf, pos_buf);  
00111   MPI_Isend (act_buf.back (), pos_buf, MPI_PACKED, __to, __tag, MPI_COMM_WORLD, act_req.back ()); 
00112 }
00113 
00114 void sendMessageToAll (int __tag) {
00115 
00116   for (int i = 0; i < getNumberOfNodes (); i ++)
00117     sendMessage (i, __tag);
00118 }
00119 
00120 void receiveMessage (int __from, int __tag) {
00121   
00122   MPI_Status stat;  
00123   MPI_Request req;
00124 
00125   MPI_Irecv (mpi_buf, MPI_BUF_SIZE, MPI_PACKED, __from, __tag, MPI_COMM_WORLD, & req) ;
00126   MPI_Wait (& req, & stat) ;
00127 }
00128 
00129 /* Char */
00130 void pack (const char & __c) {
00131 
00132   MPI_Pack ((void *) & __c, 1, MPI_CHAR, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00133 }
00134 
00135 /* Float */
00136 void pack (const float & __f, int __nitem) {
00137 
00138   MPI_Pack ((void *) & __f, __nitem, MPI_FLOAT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00139 }
00140 
00141 /* Double */
00142 void pack (const double & __d, int __nitem) {
00143 
00144   MPI_Pack ((void *) & __d, __nitem, MPI_DOUBLE, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00145 }
00146 
00147 /* Integer */
00148 void pack (const int & __i, int __nitem) {
00149 
00150   MPI_Pack ((void *) & __i, __nitem, MPI_INT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00151 }
00152 
00153 /* Unsigned int. */
00154 void pack (const unsigned int & __ui, int __nitem) {
00155 
00156   MPI_Pack ((void *) & __ui, __nitem, MPI_UNSIGNED, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00157 }
00158 
00159 /* Short int. */
00160 void pack (const short & __sh, int __nitem) {
00161 
00162   MPI_Pack ((void *) & __sh, __nitem, MPI_SHORT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00163 }
00164 
00165 /* Unsigned short */
00166 void pack (const unsigned short & __ush, int __nitem) {
00167 
00168   MPI_Pack ((void *) & __ush, __nitem, MPI_UNSIGNED_SHORT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00169 }
00170 
00171 /* Long */
00172 void pack (const long & __l, int __nitem) {
00173 
00174   MPI_Pack ((void *) & __l, __nitem, MPI_LONG, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00175 }
00176 
00177 /* Unsigned long */
00178 void pack (const unsigned long & __ul, int __nitem) {
00179 
00180   MPI_Pack ((void *) & __ul, __nitem, MPI_UNSIGNED_LONG, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00181 }
00182 
00183 /* String */
00184 void pack (const char * __str) {
00185   
00186   int len = strlen (__str) + 1;
00187   MPI_Pack (& len, 1, MPI_INT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00188   MPI_Pack ((void *) __str, len, MPI_CHAR, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
00189 }
00190 
00191 /* Char */
00192 void unpack (char & __c) {
00193 
00194   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __c, 1, MPI_CHAR, MPI_COMM_WORLD);
00195 }
00196 
00197 /* Float */
00198 void unpack (float & __f, int __nitem) {
00199 
00200   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __f, __nitem, MPI_FLOAT, MPI_COMM_WORLD);
00201 }
00202 
00203 /* Double */
00204 void unpack (double & __d, int __nitem) {
00205 
00206   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __d, __nitem, MPI_DOUBLE, MPI_COMM_WORLD);
00207 }
00208 
00209 /* Integer */
00210 void unpack (int & __i, int __nitem) {
00211 
00212   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __i, __nitem, MPI_INT, MPI_COMM_WORLD);
00213 }
00214 
00215 /* Unsigned int. */
00216 void unpack (unsigned int & __ui, int __nitem) {
00217 
00218   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ui, __nitem, MPI_UNSIGNED, MPI_COMM_WORLD);
00219 }
00220 
00221 /* Short int. */
00222 void unpack (short & __sh, int __nitem) {
00223 
00224   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __sh, __nitem, MPI_SHORT, MPI_COMM_WORLD);
00225 }
00226 
00227 /* Unsigned short */
00228 void unpack (unsigned short & __ush, int __nitem) {
00229 
00230   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ush, __nitem, MPI_UNSIGNED_SHORT, MPI_COMM_WORLD);
00231 }
00232 
00233 /* Long */
00234 void unpack (long & __l, int __nitem) {
00235 
00236   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __l, __nitem, MPI_LONG, MPI_COMM_WORLD);
00237 }
00238 
00239 /* Unsigned long */
00240 void unpack (unsigned long & __ul, int __nitem) {
00241 
00242   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ul, __nitem, MPI_UNSIGNED_LONG, MPI_COMM_WORLD);
00243 }
00244 
00245 /* String */
00246 void unpack (char * __str) {
00247 
00248   int len;
00249   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & len, 1, MPI_INT, MPI_COMM_WORLD);
00250   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, __str, len, MPI_CHAR, MPI_COMM_WORLD);    
00251 }
00252 

Generated on Sat Dec 30 14:55:29 2006 for ParadisEO-PEO by  doxygen 1.4.7