Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use a msg_barrier instead of smx synchro in this example
[simgrid.git] / examples / msg / actions-comm / actions-comm.c
index 126936b..20b4990 100644 (file)
@@ -5,7 +5,7 @@
 
 #include "simgrid/msg.h"
 #include "simgrid/simix.h"      /* semaphores for the barrier */
-#include <xbt/replay.h>
+#include <xbt/replay.hpp>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
 int communicator_size = 0;
@@ -19,7 +19,9 @@ typedef struct {
   /* Used to implement irecv+wait */
   xbt_dynar_t irecvs;           /* of msg_comm_t */
   xbt_dynar_t tasks;            /* of msg_task_t */
-} s_process_globals_t, *process_globals_t;
+} s_process_globals_t;
+
+typedef s_process_globals_t *process_globals_t;
 
 /* Helper function */
 static double parse_double(const char *string)
@@ -54,10 +56,12 @@ static void asynchronous_cleanup(void)
   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
 
   /* Destroy any isend which correspond to completed communications */
-  int found;
   msg_comm_t comm;
-  while ((found = MSG_comm_testany(globals->isends)) != -1) {
-    xbt_dynar_remove_at(globals->isends, found, &comm);
+  while (1/*true*/) {
+    int pos_found = MSG_comm_testany(globals->isends);
+    if (pos_found == -1) /* none remaining */
+      break;
+    xbt_dynar_remove_at(globals->isends, pos_found, &comm);
     MSG_comm_destroy(comm);
   }
 }
@@ -158,33 +162,26 @@ static void action_wait(const char *const *action)
 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
 static void action_barrier(const char *const *action)
 {
-  static smx_mutex_t mutex = NULL;
-  static smx_cond_t cond = NULL;
+  static msg_bar_t barrier           = NULL;
   static int processes_arrived_sofar = 0;
 
-  if (mutex == NULL) {          // first arriving on the barrier
-    mutex = simcall_mutex_init();
-    cond = simcall_cond_init();
-    processes_arrived_sofar = 0;
+  if (barrier == NULL) {                                    // first arriving on the barrier
+    msg_bar_t newbar = MSG_barrier_init(communicator_size); // This is a simcall, unscheduling the current process
+    if (barrier == NULL)                                    // Still null, commit our new value
+      barrier = newbar;
+    else // some other process commited a new barrier before me, so dismiss mine
+      MSG_barrier_destroy(newbar);
   }
-  ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
 
-  simcall_mutex_lock(mutex);
-  if (++processes_arrived_sofar == communicator_size) {
-    simcall_cond_broadcast(cond);
-    simcall_mutex_unlock(mutex);
-  } else {
-    simcall_cond_wait(cond, mutex);
-    simcall_mutex_unlock(mutex);
-  }
+  processes_arrived_sofar++;
+  MSG_barrier_wait(barrier);
 
   ACT_DEBUG("Exiting barrier: %s", NAME);
 
   processes_arrived_sofar--;
   if (processes_arrived_sofar<=0) {
-    SIMIX_cond_destroy(cond);
-    SIMIX_mutex_destroy(mutex);
-    mutex = NULL;
+    MSG_barrier_destroy(barrier);
+    barrier = NULL;
   }
 }
 
@@ -201,7 +198,8 @@ static void action_bcast(const char *const *action)
 
   const char * process_name = MSG_process_get_name(MSG_process_self());
 
-  char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
+  char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter);
+  counters->bcast_counter++;
 
   if (!strcmp(process_name, "p0")) {
     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
@@ -274,8 +272,6 @@ static void action_finalize(const char *const *action)
 
 int main(int argc, char *argv[])
 {
-  msg_error_t res = MSG_OK;
-
   /* Check the given arguments */
   MSG_init(&argc, argv);
   /* Explicit initialization of the action module is required now*/
@@ -306,7 +302,7 @@ int main(int argc, char *argv[])
   xbt_replay_action_register("compute", action_compute);
 
   /* Actually do the simulation using MSG_action_trace_run */
-  res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
+  msg_error_t res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
 
   XBT_INFO("Simulation time %g", MSG_get_clock());