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 typedef struct {
00053
00054 Communicable * comm;
00055 int to;
00056 int tag;
00057
00058 } SEND_REQUEST;
00059
00060 static std :: queue <SEND_REQUEST> mess;
00061
00062 static sem_t sem_send;
00063
00064 void initSending () {
00065
00066 sem_init (& sem_send, 0, 1);
00067 }
00068
00069 void send (Communicable * __comm, int __to, int __tag) {
00070
00071 SEND_REQUEST req;
00072 req.comm = __comm;
00073 req.to = __to;
00074 req.tag = __tag;
00075
00076 sem_wait (& sem_send);
00077 mess.push (req);
00078 sem_post (& sem_send);
00079 wakeUpCommunicator ();
00080 }
00081
00082 void sendToAll (Communicable * __comm, int __tag) {
00083
00084 send (__comm, TO_ALL, __tag);
00085 }
00086
00087 void sendMessages () {
00088
00089 sem_wait (& sem_send);
00090
00091 while (! mess.empty ()) {
00092
00093 SEND_REQUEST req = mess.front ();
00094
00095
00096
00097
00098
00099
00100 Communicable * comm = req.comm;
00101
00102 initMessage ();
00103
00104 switch (req.tag) {
00105
00106 case RUNNER_STOP_TAG:
00107 dynamic_cast <Runner *> (comm) -> packTermination ();
00108 dynamic_cast <Runner *> (comm) -> notifySendingTermination ();
00109 break;
00110
00111 case COOP_TAG:
00112 dynamic_cast <Cooperative *> (comm) -> pack ();
00113 dynamic_cast <Cooperative *> (comm) -> notifySending ();
00114 break;
00115
00116 case SCHED_REQUEST_TAG:
00117 dynamic_cast <Service *> (comm) -> packResourceRequest ();
00118 dynamic_cast <Service *> (comm) -> notifySendingResourceRequest ();
00119 break;
00120
00121 case TASK_RESULT_TAG:
00122 dynamic_cast <Worker *> (comm) -> packResult ();
00123 dynamic_cast <Worker *> (comm) -> notifySendingResult ();
00124 break;
00125
00126 case TASK_DONE_TAG:
00127 dynamic_cast <Worker *> (comm) -> packTaskDone ();
00128 dynamic_cast <Worker *> (comm) -> notifySendingTaskDone ();
00129 break;
00130
00131 default :
00132 break;
00133
00134 };
00135
00136 if (req.to == TO_ALL)
00137 sendMessageToAll (req.tag);
00138 else
00139 sendMessage (req.to, req.tag);
00140 mess.pop ();
00141 }
00142
00143 sem_post (& sem_send);
00144 }