Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename configuration variables *_model into */model (to start a hierarchy)
[simgrid.git] / examples / msg / actions / actions.c
index 44bfb41..2f8958d 100644 (file)
@@ -8,10 +8,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
+#include "simix/simix.h"        /* semaphores for the barrier */
 #include "xbt.h"                /* calloc, printf */
 #include "simgrid_config.h"     /* getline */
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
+XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
                  "Messages specific for this msg example");
 int communicator_size=0;
 
@@ -34,19 +35,25 @@ static double parse_double(const char *string) {
 
 
 /* My actions */
-static void send(xbt_dynar_t action)
+static void action_send(xbt_dynar_t action)
 {
-  char *name = xbt_str_join(action, " ");
+  char *name = NULL;
   char to[250];
   char *size = xbt_dynar_get_as(action, 3, char *);
   double clock = MSG_get_clock();
   sprintf (to,"%s_%s", MSG_process_get_name(MSG_process_self()),
           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))
+    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);
-  free(name);
+
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    free(name);
 }
 
 
@@ -88,9 +95,9 @@ static void Isend(xbt_dynar_t action)
 }
 
 
-static void recv(xbt_dynar_t action)
+static void action_recv(xbt_dynar_t action)
 {
-  char *name = xbt_str_join(action, " ");
+  char *name = NULL;
     char mailbox_name[250];
   m_task_t task = NULL;
   double clock = MSG_get_clock();
@@ -98,12 +105,18 @@ static void recv(xbt_dynar_t action)
   //char *from=xbt_dynar_get_as(action,2,char*);
   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))
+    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);
   MSG_task_destroy(task);
-  free(name);
+
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    free(name);
 }
 
 static int spawned_recv(int argc, char *argv[])
@@ -123,7 +136,7 @@ static int spawned_recv(int argc, char *argv[])
 
 static void Irecv(xbt_dynar_t action)
 {
-  char *name = xbt_str_join(action, " ");
+  char *name;
   m_process_t comm_helper;
   char mailbox_name[250];
   char **myargv;
@@ -133,7 +146,7 @@ static void Irecv(xbt_dynar_t action)
 
   sprintf (mailbox_name,"%s_%s", xbt_dynar_get_as(action, 2, char *),
           MSG_process_get_name(MSG_process_self()));
-  sprintf(name,"%s_wait",MSG_process_get_name(MSG_process_self()));
+  name = bprintf("%s_wait",MSG_process_get_name(MSG_process_self()));
   myargv = (char**) calloc (2, sizeof (char*));
   
   myargv[0] = xbt_strdup(mailbox_name);
@@ -143,35 +156,57 @@ static void Irecv(xbt_dynar_t action)
                                                  1, myargv);
 
   DEBUG2("%s %f",  xbt_str_join(action, " "), 
-       MSG_get_clock()-clock);
+      MSG_get_clock()-clock);
  
   free(name);
 }
 
 
-static void wait_action(xbt_dynar_t action)
+static void action_wait(xbt_dynar_t action)
 {
-  char *name = xbt_str_join(action, " ");
+  char *name = NULL;
   char task_name[80];
   m_task_t task = NULL;
   double clock = MSG_get_clock();
   
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    name = xbt_str_join(action, " ");
+
   DEBUG1("Entering %s", name);
   sprintf(task_name,"%s_wait",MSG_process_get_name(MSG_process_self()));
   DEBUG1("wait: %s", task_name);
   MSG_task_receive(&task,task_name);
   MSG_task_destroy(task);
   DEBUG2("%s %f", name,        MSG_get_clock()-clock);
-  free(name);
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    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 = xbt_str_join(action, " ");
-  DEBUG1("barrier: %s", name);
-  
+  char *name = NULL;
 
-  free(name);
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    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 (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);
+  }
+
+  DEBUG1("Exiting barrier: %s", name);
+
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    free(name);
 
 }
 
@@ -311,15 +346,21 @@ static void bcast (xbt_dynar_t action)
 }
 
 
-static void sleep(xbt_dynar_t action)
+static void action_sleep(xbt_dynar_t action)
 {
-  char *name = xbt_str_join(action, " ");
+  char *name = NULL;
   char *duration = xbt_dynar_get_as(action, 2, char *);
   double clock = MSG_get_clock();
+
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    name = xbt_str_join(action, " ");
+
   DEBUG1("Entering %s", name);
   MSG_process_sleep(parse_double(duration));
   DEBUG2("%s %f ", name, MSG_get_clock()-clock);
-  free(name);
+
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    free(name);
 }
 
 static void allReduce(xbt_dynar_t action)
@@ -432,16 +473,19 @@ static void comm_size(xbt_dynar_t action)
 
 static void compute(xbt_dynar_t action)
 {
-  char *name = xbt_str_join(action, " ");
+  char *name=NULL;
   char *amout = xbt_dynar_get_as(action, 2, char *);
   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))
+    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);
-  free(name);
+  if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
+    free(name);
 }
 
 /** Main function */
@@ -468,16 +512,16 @@ int main(int argc, char *argv[])
 
   /*   Action registration */
   MSG_action_register("comm_size", comm_size);
-  MSG_action_register("send", send);
+  MSG_action_register("send", action_send);
   MSG_action_register("Isend", Isend);
-  MSG_action_register("recv", recv);
+  MSG_action_register("recv", action_recv);
   MSG_action_register("Irecv", Irecv);
-  MSG_action_register("wait", wait_action);
+  MSG_action_register("wait", action_wait);
   MSG_action_register("barrier", barrier);
   MSG_action_register("bcast", bcast);
   MSG_action_register("reduce", reduce);
   MSG_action_register("allReduce", allReduce);
-  MSG_action_register("sleep", sleep);
+  MSG_action_register("sleep", action_sleep);
   MSG_action_register("compute", compute);