runner.cpp

00001 /*
00002 * <runner.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 <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; /* Low-level runner threads */
00052 
00053 static std :: vector <Runner *> the_runners;
00054 
00055 static unsigned num_def_runners = 0; /* Number of defined runners */
00056 
00057 static unsigned num_local_exec_runners = 0; /* Number of locally executing runners */
00058 
00059 static unsigned num_exec_runners = 0; /* Number of globally executing runners */
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   // setting up the execution IDs & counting the number of local exec. runners
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   // synchronizeNodes ();
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   /* Runners */
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 }

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