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 <vector>
00038
00039 #include "runner.h"
00040 #include "reac_thread.h"
00041 #include "peo_debug.h"
00042 #include "messaging.h"
00043
00044 #include "../rmc/mpi/mess.h"
00045 #include "../rmc/mpi/tags.h"
00046
00047 #include "../rmc/mpi/node.h"
00048 #include "../rmc/mpi/schema.h"
00049
00050
00051 static std :: vector <pthread_t *> ll_threads;
00052
00053 static std :: vector <Runner *> the_runners;
00054
00055 static unsigned num_def_runners = 0;
00056
00057 static unsigned num_local_exec_runners = 0;
00058
00059 static unsigned num_exec_runners = 0;
00060
00061
00062 extern int getNodeRank ();
00063
00064 extern int getNumberOfNodes ();
00065
00066 extern void wakeUpCommunicator ();
00067
00068
00069 Runner :: Runner ()
00070 {
00071
00072 exec_id = 0;
00073 def_id = ++ num_def_runners;
00074
00075 the_runners.push_back (this);
00076
00077 sem_init (& sem_start, 0, 0);
00078 sem_init (& sem_cntxt, 0, 0);
00079 }
00080
00081 RUNNER_ID Runner :: getDefinitionID ()
00082 {
00083
00084 return def_id;
00085 }
00086
00087 RUNNER_ID Runner :: getExecutionID ()
00088 {
00089
00090 return exec_id;
00091 }
00092
00093 void Runner :: setExecutionID (const RUNNER_ID& execution_id)
00094 {
00095
00096 exec_id = execution_id;
00097 }
00098
00099 Runner * getRunner (RUNNER_ID __key)
00100 {
00101
00102 return dynamic_cast <Runner *> (getCommunicable (__key));
00103 }
00104
00105 void initializeContext ()
00106 {
00107
00108 num_local_exec_runners = 0;
00109
00110
00111 for (unsigned i = 0; i < the_runners.size (); i ++)
00112 {
00113 the_runners [i] -> setExecutionID ( my_node -> execution_id_run[ i ] );
00114 if (the_runners [i] -> isAssignedLocally ()) num_local_exec_runners ++;
00115 }
00116
00117 collectiveCountOfRunners( &num_local_exec_runners, &num_exec_runners );
00118
00119
00120
00121 for (unsigned i = 0; i < the_runners.size (); i ++)
00122 if (the_runners [i] -> isAssignedLocally ()) the_runners [i] -> notifyContextInitialized ();
00123 }
00124
00125 void Runner :: waitStarting ()
00126 {
00127
00128 sem_wait (& sem_start);
00129 }
00130
00131 void Runner :: waitContextInitialization ()
00132 {
00133
00134 sem_wait (& sem_cntxt);
00135 }
00136
00137 void Runner :: start ()
00138 {
00139
00140 setActive ();
00141
00142 sem_post (& sem_start);
00143
00144 waitContextInitialization ();
00145 run ();
00146 terminate ();
00147 }
00148
00149 void startRunners ()
00150 {
00151
00152
00153 for (unsigned i = 0; i < the_runners.size (); i ++)
00154 if (the_runners [i] -> isAssignedLocally ())
00155 {
00156 addThread (the_runners [i], ll_threads);
00157 the_runners [i] -> waitStarting ();
00158 }
00159
00160 printDebugMessage ("launched the parallel runners");
00161 }
00162
00163 void joinRunners ()
00164 {
00165
00166 joinThreads (ll_threads);
00167 the_runners.clear();
00168 }
00169
00170 bool atLeastOneActiveRunner ()
00171 {
00172
00173 return num_exec_runners;
00174 }
00175
00176 unsigned numberOfActiveRunners ()
00177 {
00178
00179 return num_exec_runners;
00180 }
00181
00182 void Runner :: notifyContextInitialized ()
00183 {
00184
00185 sem_post (& sem_cntxt);
00186 }
00187
00188 void Runner :: notifySendingTermination ()
00189 {
00190
00191 printDebugMessage ("I am informed that everyone received my termination notification.");
00192 setPassive ();
00193 }
00194
00195 void unpackTerminationOfRunner ()
00196 {
00197
00198 RUNNER_ID finished_id;
00199 unpack (finished_id);
00200
00201 num_exec_runners --;
00202
00203 printDebugMessage ("I'm noticed of the termination of a runner");
00204
00205 if (!num_exec_runners)
00206 {
00207
00208 printDebugMessage ("All the runners have terminated - now stopping the reactive threads.");
00209 stopReactiveThreads ();
00210 printDebugMessage ("Reactive threads stopped!");
00211 }
00212
00213 wakeUpCommunicator ();
00214 }
00215
00216 void initRunnersEnv ()
00217 {
00218
00219 ll_threads.clear ();
00220 the_runners.clear ();
00221
00222 num_def_runners = 0;
00223 num_local_exec_runners = 0;
00224 num_exec_runners = 0;
00225 }