Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stop that misuse of semaphores and use conditions where they are expected (ok, Arnaud...
[simgrid.git] / examples / msg / actions / actions.c
index 5ab23f0..d8439fb 100644 (file)
@@ -45,14 +45,14 @@ static void action_send(xbt_dynar_t action)
           xbt_dynar_get_as(action, 2, char *));
   //  char *to =  xbt_dynar_get_as(action, 2, char *);
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     name = xbt_str_join(action, " ");
 
   DEBUG2("Entering Send: %s (size: %lg)", name, parse_double(size));
   MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
-  DEBUG2("%s %f", name, MSG_get_clock() - clock);
+  VERB2("%s %f", name, MSG_get_clock() - clock);
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     free(name);
 }
 
@@ -91,7 +91,7 @@ static void Isend(xbt_dynar_t action)
   comm_helper =
       MSG_process_create_with_arguments(spawn_name, spawned_send,
                                         NULL, MSG_host_self(), 2, myargv);
-  DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
+  VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
 }
 
 
@@ -106,16 +106,16 @@ static void action_recv(xbt_dynar_t action)
   sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
           MSG_process_get_name(MSG_process_self()));
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     name = xbt_str_join(action, " ");
 
   DEBUG1("Receiving: %s", name);
   MSG_task_receive(&task, mailbox_name);
   //  MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
-  DEBUG2("%s %f", name, MSG_get_clock() - clock);
+  VERB2("%s %f", name, MSG_get_clock() - clock);
   MSG_task_destroy(task);
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     free(name);
 }
 
@@ -155,7 +155,7 @@ static void Irecv(xbt_dynar_t action)
                                                   NULL, MSG_host_self(),
                                                   1, myargv);
 
-  DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
+  VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
 
   free(name);
 }
@@ -168,7 +168,7 @@ static void action_wait(xbt_dynar_t action)
   m_task_t task = NULL;
   double clock = MSG_get_clock();
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     name = xbt_str_join(action, " ");
 
   DEBUG1("Entering %s", name);
@@ -176,35 +176,48 @@ static void action_wait(xbt_dynar_t action)
   DEBUG1("wait: %s", task_name);
   MSG_task_receive(&task, task_name);
   MSG_task_destroy(task);
-  DEBUG2("%s %f", name, MSG_get_clock() - clock);
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  VERB2("%s %f", name, MSG_get_clock() - clock);
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     free(name);
 }
 
 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
-smx_sem_t barrier_semaphore = NULL;
 static void barrier(xbt_dynar_t action)
 {
   char *name = NULL;
+  static smx_mutex_t mutex = NULL;
+  static smx_cond_t cond = NULL;
+  static int processes_arrived_sofar=0;
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     name = xbt_str_join(action, " ");
 
-  DEBUG1("Entering barrier: %s", name);
-  if (barrier_semaphore == NULL)        // first arriving on the barrier
-    barrier_semaphore = SIMIX_sem_init(0);
+  if (mutex == NULL) {       // first arriving on the barrier
+    mutex = SIMIX_mutex_init();
+    cond = SIMIX_cond_init();
+    processes_arrived_sofar=0;
+  }
+  DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
 
-  if (SIMIX_sem_get_capacity(barrier_semaphore) == -communicator_size + 1) {    // last arriving
-    SIMIX_sem_release_forever(barrier_semaphore);
-    SIMIX_sem_destroy(barrier_semaphore);
-    barrier_semaphore = NULL;
-  } else {                      // not last
-    SIMIX_sem_acquire(barrier_semaphore);
+  SIMIX_mutex_lock(mutex);
+  if (++processes_arrived_sofar == communicator_size) {
+    SIMIX_cond_broadcast(cond);
+    SIMIX_mutex_unlock(mutex);
+  } else {
+    SIMIX_cond_wait(cond,mutex);
+    SIMIX_mutex_unlock(mutex);
   }
 
   DEBUG1("Exiting barrier: %s", name);
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  processes_arrived_sofar--;
+  if (!processes_arrived_sofar) {
+    SIMIX_cond_destroy(cond);
+    SIMIX_mutex_destroy(mutex);
+    mutex=NULL;
+  }
+
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     free(name);
 
 }
@@ -276,7 +289,7 @@ static void reduce(xbt_dynar_t action)
   }
 
   MSG_process_set_data(MSG_process_self(), (void *) counters);
-  DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
+  VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
   free(name);
 }
 
@@ -341,7 +354,7 @@ static void bcast(xbt_dynar_t action)
   }
 
   MSG_process_set_data(MSG_process_self(), (void *) counters);
-  DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
+  VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
   free(name);
 }
 
@@ -352,14 +365,14 @@ static void action_sleep(xbt_dynar_t action)
   char *duration = xbt_dynar_get_as(action, 2, char *);
   double clock = MSG_get_clock();
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     name = xbt_str_join(action, " ");
 
   DEBUG1("Entering %s", name);
   MSG_process_sleep(parse_double(duration));
-  DEBUG2("%s %f ", name, MSG_get_clock() - clock);
+  VERB2("%s %f ", name, MSG_get_clock() - clock);
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     free(name);
 }
 
@@ -463,14 +476,22 @@ static void allReduce(xbt_dynar_t action)
   }
 
   MSG_process_set_data(MSG_process_self(), (void *) counters);
-  DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
+  VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
   free(name);
 }
 
 static void comm_size(xbt_dynar_t action)
 {
+  char *name = NULL;
   char *size = xbt_dynar_get_as(action, 2, char *);
+  double clock = MSG_get_clock();
+
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
+    name = xbt_str_join(action, " ");
   communicator_size = parse_double(size);
+  VERB2("%s %f", name, MSG_get_clock() - clock);
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
+    free(name);
 }
 
 static void compute(xbt_dynar_t action)
@@ -480,13 +501,13 @@ static void compute(xbt_dynar_t action)
   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
   double clock = MSG_get_clock();
 
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     name = xbt_str_join(action, " ");
   DEBUG1("Entering %s", name);
   MSG_task_execute(task);
   MSG_task_destroy(task);
-  DEBUG2("%s %f", name, MSG_get_clock() - clock);
-  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_debug))
+  VERB2("%s %f", name, MSG_get_clock() - clock);
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
     free(name);
 }