# ParadisEO-PEOMovingObjects: mess.cpp Source File

# mess.cpp

    00001 /*
    00002 * <mess.cpp>
    00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
    00004 * (C) OPAC Team, LIFL, 2002-2007
    00005 *
    00006 * Sebastien Cahon, Alexandru-Adrian Tantar
    00007 *
    00008 * This software is governed by the CeCILL license under French law and
    00009 * abiding by the rules of distribution of free software.  You can  use,
    00010 * modify and/ or redistribute the software under the terms of the CeCILL
    00011 * license as circulated by CEA, CNRS and INRIA at the following URL
    00012 * "http://www.cecill.info".
    00013 *
    00014 * As a counterpart to the access to the source code and  rights to copy,
    00015 * modify and redistribute granted by the license, users are provided only
    00016 * with a limited warranty  and the software's author,  the holder of the
    00017 * economic rights,  and the successive licensors  have only  limited liability.
    00018 *
    00019 * In this respect, the user's attention is drawn to the risks associated
    00020 * with loading,  using,  modifying and/or developing or reproducing the
    00021 * software by the user in light of its specific status of free software,
    00022 * that may mean  that it is complicated to manipulate,  and  that  also
    00023 * therefore means  that it is reserved for developers  and  experienced
    00024 * professionals having in-depth computer knowledge. Users are therefore
    00025 * encouraged to load and test the software's suitability as regards their
    00026 * requirements in conditions enabling the security of their systems and/or
    00027 * data to be ensured and,  more generally, to use and operate it in the
    00028 * same conditions as regards security.
    00029 * The fact that you are presently reading this means that you have had
    00030 * knowledge of the CeCILL license and that you accept its terms.
    00031 *
    00032 * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
    00033 * Contact: paradiseo-help@lists.gforge.inria.fr
    00034 *
    00035 */
    00036
    00037 #include <mpi.h>
    00038 #include <vector>
    00039
    00040 #include "mess.h"
    00041 #include "../../core/peo_debug.h"
    00042 #include "node.h"
    00043
    00044 #define MPI_BUF_SIZE 1024*64
    00045
    00046 static char mpi_buf [MPI_BUF_SIZE];
    00047
    00048 static int pos_buf ;
    00049
    00050 static std :: vector <char *> act_buf; /* Active buffers */
    00051
    00052 static std :: vector <MPI_Request *> act_req; /* Active requests */
    00053
    00054 void cleanBuffers () {
    00055
    00056   for (unsigned i = 0; i < act_req.size ();) {
    00057
    00058     MPI_Status stat ;
    00059     int flag ;
    00060     MPI_Test (act_req [i], & flag, & stat) ;
    00061     if (flag) {
    00062
    00063       delete act_buf [i] ;
    00064       delete act_req [i] ;
    00065
    00066       act_buf [i] = act_buf.back () ;
    00067       act_buf.pop_back () ;
    00068
    00069       act_req [i] = act_req.back () ;
    00070       act_req.pop_back () ;
    00071     }
    00072     else
    00073       i ++;
    00074   }
    00075 }
    00076
    00077 void waitBuffers () {
    00078
    00079   printDebugMessage ("waiting the termination of the asynchronous operations to complete");
    00080
    00081   for (unsigned i = 0; i < act_req.size (); i ++) {
    00082
    00083     MPI_Status stat ;
    00084
    00085     MPI_Wait (act_req [i], & stat) ;
    00086
    00087     delete act_buf [i] ;
    00088     delete act_req [i] ;
    00089   }
    00090 }
    00091
    00092 bool probeMessage (int & __src, int & __tag) {
    00093
    00094   int flag;
    00095
    00096   MPI_Status stat;
    00097
    00098   MPI_Iprobe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, & flag, & stat);
    00099
    00100   __src = stat.MPI_SOURCE;
    00101   __tag = stat.MPI_TAG;
    00102
    00103   return flag;
    00104 }
    00105
    00106 void waitMessage () {
    00107
    00108   MPI_Status stat;
    00109
    00110   MPI_Probe (MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, & stat);
    00111 }
    00112
    00113 void initMessage () {
    00114
    00115   pos_buf = 0;
    00116 }
    00117
    00118 void sendMessage (int __to, int __tag) {
    00119
    00120   cleanBuffers ();
    00121   act_buf.push_back (new char [pos_buf]);
    00122   act_req.push_back (new MPI_Request);
    00123   memcpy (act_buf.back (), mpi_buf, pos_buf);
    00124   MPI_Isend (act_buf.back (), pos_buf, MPI_PACKED, __to, __tag, MPI_COMM_WORLD, act_req.back ());
    00125 }
    00126
    00127 void sendMessageToAll (int __tag) {
    00128
    00129   for (int i = 0; i < getNumberOfNodes (); i ++)
    00130     sendMessage (i, __tag);
    00131 }
    00132
    00133 void receiveMessage (int __from, int __tag) {
    00134
    00135   MPI_Status stat;
    00136   MPI_Request req;
    00137
    00138   MPI_Irecv (mpi_buf, MPI_BUF_SIZE, MPI_PACKED, __from, __tag, MPI_COMM_WORLD, & req) ;
    00139   MPI_Wait (& req, & stat) ;
    00140 }
    00141
    00142 /* Char */
    00143 void pack (const char & __c) {
    00144
    00145   MPI_Pack ((void *) & __c, 1, MPI_CHAR, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00146 }
    00147
    00148 /* Float */
    00149 void pack (const float & __f, int __nitem) {
    00150
    00151   MPI_Pack ((void *) & __f, __nitem, MPI_FLOAT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00152 }
    00153
    00154 /* Double */
    00155 void pack (const double & __d, int __nitem) {
    00156
    00157   MPI_Pack ((void *) & __d, __nitem, MPI_DOUBLE, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00158 }
    00159
    00160 /* Integer */
    00161 void pack (const int & __i, int __nitem) {
    00162
    00163   MPI_Pack ((void *) & __i, __nitem, MPI_INT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00164 }
    00165
    00166 /* Unsigned int. */
    00167 void pack (const unsigned int & __ui, int __nitem) {
    00168
    00169   MPI_Pack ((void *) & __ui, __nitem, MPI_UNSIGNED, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00170 }
    00171
    00172 /* Short int. */
    00173 void pack (const short & __sh, int __nitem) {
    00174
    00175   MPI_Pack ((void *) & __sh, __nitem, MPI_SHORT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00176 }
    00177
    00178 /* Unsigned short */
    00179 void pack (const unsigned short & __ush, int __nitem) {
    00180
    00181   MPI_Pack ((void *) & __ush, __nitem, MPI_UNSIGNED_SHORT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00182 }
    00183
    00184 /* Long */
    00185 void pack (const long & __l, int __nitem) {
    00186
    00187   MPI_Pack ((void *) & __l, __nitem, MPI_LONG, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00188 }
    00189
    00190 /* Unsigned long */
    00191 void pack (const unsigned long & __ul, int __nitem) {
    00192
    00193   MPI_Pack ((void *) & __ul, __nitem, MPI_UNSIGNED_LONG, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00194 }
    00195
    00196 /* String */
    00197 void pack (const char * __str) {
    00198
    00199   int len = strlen (__str) + 1;
    00200   MPI_Pack (& len, 1, MPI_INT, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00201   MPI_Pack ((void *) __str, len, MPI_CHAR, mpi_buf, MPI_BUF_SIZE, & pos_buf, MPI_COMM_WORLD);
    00202 }
    00203
    00204 /* Char */
    00205 void unpack (char & __c) {
    00206
    00207   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __c, 1, MPI_CHAR, MPI_COMM_WORLD);
    00208 }
    00209
    00210 /* Float */
    00211 void unpack (float & __f, int __nitem) {
    00212
    00213   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __f, __nitem, MPI_FLOAT, MPI_COMM_WORLD);
    00214 }
    00215
    00216 /* Double */
    00217 void unpack (double & __d, int __nitem) {
    00218
    00219   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __d, __nitem, MPI_DOUBLE, MPI_COMM_WORLD);
    00220 }
    00221
    00222 /* Integer */
    00223 void unpack (int & __i, int __nitem) {
    00224
    00225   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __i, __nitem, MPI_INT, MPI_COMM_WORLD);
    00226 }
    00227
    00228 /* Unsigned int. */
    00229 void unpack (unsigned int & __ui, int __nitem) {
    00230
    00231   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ui, __nitem, MPI_UNSIGNED, MPI_COMM_WORLD);
    00232 }
    00233
    00234 /* Short int. */
    00235 void unpack (short & __sh, int __nitem) {
    00236
    00237   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __sh, __nitem, MPI_SHORT, MPI_COMM_WORLD);
    00238 }
    00239
    00240 /* Unsigned short */
    00241 void unpack (unsigned short & __ush, int __nitem) {
    00242
    00243   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ush, __nitem, MPI_UNSIGNED_SHORT, MPI_COMM_WORLD);
    00244 }
    00245
    00246 /* Long */
    00247 void unpack (long & __l, int __nitem) {
    00248
    00249   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __l, __nitem, MPI_LONG, MPI_COMM_WORLD);
    00250 }
    00251
    00252 /* Unsigned long */
    00253 void unpack (unsigned long & __ul, int __nitem) {
    00254
    00255   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & __ul, __nitem, MPI_UNSIGNED_LONG, MPI_COMM_WORLD);
    00256 }
    00257
    00258 /* String */
    00259 void unpack (char * __str) {
    00260
    00261   int len;
    00262   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, & len, 1, MPI_INT, MPI_COMM_WORLD);
    00263   MPI_Unpack (mpi_buf, MPI_BUF_SIZE, & pos_buf, __str, len, MPI_CHAR, MPI_COMM_WORLD);
    00264 }
    00265