00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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 }