send.cpp

00001 /*
00002 * <send.cpp>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008
00004 * (C) OPAC Team, LIFL, 2002-2008
00005 *
00006 * Sebastien Cahon, Alexandru-Adrian Tantar, Clive Canape
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 <semaphore.h>
00039 #include <queue>
00040 
00041 #include "tags.h"
00042 #include "comm.h"
00043 #include "worker.h"
00044 #include "scheduler.h"
00045 #include "mess.h"
00046 #include "node.h"
00047 #include "../../core/cooperative.h"
00048 #include "../../core/peo_debug.h"
00049 
00050 #define TO_ALL -1
00051 
00052 
00053 typedef struct
00054   {
00055 
00056     Communicable * comm;
00057     int to;
00058     int tag;
00059 
00060   }
00061 SEND_REQUEST;
00062 
00063 
00064 static std :: queue <SEND_REQUEST> mess;
00065 
00066 static sem_t sem_send;
00067 
00068 static bool contextInitialized = false;
00069 
00070 
00071 void initSending ()
00072 {
00073 
00074   static bool initializedSemaphore = false;
00075 
00076   mess = std :: queue <SEND_REQUEST> ();
00077 
00078   if (initializedSemaphore)
00079     {
00080       sem_destroy(& sem_send);
00081     }
00082 
00083   sem_init (& sem_send, 0, 1);
00084   initializedSemaphore = true;
00085 
00086   contextInitialized = false;
00087 }
00088 
00089 void send (Communicable * __comm, int __to, int __tag)
00090 {
00091 
00092   SEND_REQUEST req;
00093   req.comm = __comm;
00094   req.to = __to;
00095   req.tag = __tag;
00096 
00097   sem_wait (& sem_send);
00098   mess.push (req);
00099   sem_post (& sem_send);
00100   wakeUpCommunicator ();
00101 }
00102 
00103 void sendToAll (Communicable * __comm, int __tag)
00104 {
00105 
00106   send (__comm, TO_ALL, __tag);
00107 }
00108 
00109 extern void initializeContext ();
00110 
00111 void sendMessages ()
00112 {
00113 
00114   if (! contextInitialized)
00115     {
00116       contextInitialized = true;
00117       initializeContext();
00118     }
00119 
00120   sem_wait (& sem_send);
00121 
00122   while (! mess.empty ())
00123     {
00124 
00125       SEND_REQUEST req = mess.front ();
00126 
00127       Communicable * comm = req.comm;
00128 
00129       initMessage ();
00130 
00131       switch (req.tag)
00132         {
00133 
00134         case RUNNER_STOP_TAG:
00135           dynamic_cast <Runner *> (comm) -> packTermination ();
00136           dynamic_cast <Runner *> (comm) -> notifySendingTermination ();
00137           break;
00138 
00139         case COOP_TAG:
00140           dynamic_cast <Cooperative *> (comm) -> pack ();
00141           dynamic_cast <Cooperative *> (comm) -> notifySending ();
00142           break;
00143 
00144         case SYNCHRONIZE_REQ_TAG:
00145           dynamic_cast <Cooperative *> (comm) -> packSynchronizeReq ();
00146           dynamic_cast <Cooperative *> (comm) -> notifySendingSyncReq ();
00147           break;
00148 
00149         case SCHED_REQUEST_TAG:
00150           dynamic_cast <Service *> (comm) -> packResourceRequest ();
00151           dynamic_cast <Service *> (comm) -> notifySendingResourceRequest ();
00152           break;
00153 
00154         case TASK_RESULT_TAG:
00155           dynamic_cast <Worker *> (comm) -> packResult ();
00156           dynamic_cast <Worker *> (comm) -> notifySendingResult ();
00157           break;
00158 
00159         case TASK_DONE_TAG:
00160           dynamic_cast <Worker *> (comm) -> packTaskDone ();
00161           dynamic_cast <Worker *> (comm) -> notifySendingTaskDone ();
00162           break;
00163 
00164         default :
00165           break;
00166 
00167         };
00168 
00169       if (req.to == TO_ALL)
00170         sendMessageToAll (req.tag);
00171       else
00172         sendMessage (req.to, req.tag);
00173 
00174       mess.pop ();
00175     }
00176 
00177   sem_post (& sem_send);
00178 }

Generated on Thu Mar 13 15:55:03 2008 for ParadisEO-PEO-ParallelanddistributedEvolvingObjects by  doxygen 1.4.7