Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move MSG_parallel_task_create() in msg_task.c.
[simgrid.git] / examples / msg / token_ring / ring_call.c
index 73ed6d7..243445c 100644 (file)
 #include "msg/msg.h"
 #include "surf/surf_private.h"
 
-extern routing_global_t global_routing;
-int totalHosts= 0;
-const m_host_t *hosts;
+int host(int argc, char *argv[]);
+unsigned int task_comp_size = 50000000;
+unsigned int task_comm_size = 1000000;
 
-int master(int argc, char *argv[]);
-int slave(int argc, char *argv[]);
+int nb_hosts; /* All declared hosts */
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(ring,
                              "Messages specific for this msg example");
 
-int master(int argc, char *argv[])
+/** @addtogroup MSG_examples
+ * 
+ *  @section MSG_ex_apps Examples of full applications
+ * 
+ * - <b>token_ring/ring_call.c</b>: Classical token ring
+ *   communication, where a token is exchanged along a ring to reach
+ *   every participant.
+ * 
+ */
+
+int host(int argc, char *argv[])
 {
-       m_task_t task_s = NULL;
-       m_task_t task_r = NULL;
-       unsigned int task_comp_size = 50000000;
-       unsigned int task_comm_size = 1000000;
-       char mailbox[80];
-       char buffer[20];
-       int num = atoi(argv[1]);
-
-    sprintf(mailbox, "host%d", num+1);
-    if(num == totalHosts-1)
-       sprintf(mailbox, "host%d", 0);
-    sprintf(buffer, "Token");
-
-    task_s = MSG_task_create(buffer,
-                                                       task_comp_size,
-                                                       task_comm_size,
-                                                       NULL);
-    MSG_task_send(task_s,mailbox);
-
-    //MSG_comm_wait(comm, -1);
-    INFO1("Send Data to \"%s\"", mailbox);
-
-       sprintf(mailbox, "host%d", num);
-       MSG_task_receive(&(task_r), mailbox);
-       //res = MSG_comm_wait(res_irecv, -1);
-       INFO1("Received \"%s\"", MSG_task_get_name(task_r));
-       //MSG_comm_destroy(res_irecv);
-       return 0;
-}
-
-int slave(int argc, char *argv[])
-{
-       m_task_t task_s = NULL;
-       m_task_t task_r = NULL;
-       unsigned int task_comp_size = 50000000;
-       unsigned int task_comm_size = 1000000;
-       char mailbox[80];
-       char buffer[20];
-       int num = atoi(argv[1]);
-
-       sprintf(mailbox, "host%d", num);
-       MSG_task_receive(&(task_r), mailbox);
-       //res = MSG_comm_wait(res_irecv, -1);
-       INFO1("Received \"%s\"", MSG_task_get_name(task_r));
-       //MSG_comm_destroy(res_irecv);
-       //Receive something now need to tell it!
-
-       sprintf(mailbox, "host%d", num+1);
-       if(num == totalHosts-1)
-               sprintf(mailbox, "host%d", 0);
-       sprintf(buffer, "Token");
-       task_s = MSG_task_create(buffer,
-                                                       task_comp_size,
-                                                       task_comm_size,
-                                                       NULL);
-       MSG_task_send(task_s, mailbox);
-       //MSG_comm_wait(comm, -1);
-       INFO1("Send Data to \"%s\"", mailbox);
-
-       return 0;
-}
-
-static int surf_parse_bypass_application(void)
-{
-       int i;
-       static int AX_ptr;
-       static int surfxml_bufferstack_size = 2048;
-       static int surfxml_buffer_stack_stack_ptr = 0;
-       static int surfxml_buffer_stack_stack[1024];
-       /* allocating memory to the buffer, I think 2MB should be enough */
-       surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
-
-       totalHosts = MSG_get_host_number();
-       hosts = MSG_get_host_table();
-
-       /* <platform> */
-       SURFXML_BUFFER_SET(platform_version, "3");
-
-       SURFXML_START_TAG(platform);
-
-       DEBUG1("process : %s en master",MSG_host_get_name(hosts[0]));
-       /*   <process host="host A" function="master"> */
-       SURFXML_BUFFER_SET(process_host, MSG_host_get_name(hosts[0]));
-       SURFXML_BUFFER_SET(process_function, "master");
-       SURFXML_BUFFER_SET(process_start_time, "-1.0");
-       SURFXML_BUFFER_SET(process_kill_time, "-1.0");
-       SURFXML_START_TAG(process);
-
-       /*      <argument value="0"/> */
-       SURFXML_BUFFER_SET(argument_value, "0");
-       SURFXML_START_TAG(argument);
-       SURFXML_END_TAG(argument);
-       SURFXML_END_TAG(process);
-
-       for(i=1;i<totalHosts;i++)
-       {
-       DEBUG1("process : %s en slave",MSG_host_get_name(hosts[i]));
-       /*   <process host="host A" function="slave"> */
-       SURFXML_BUFFER_SET(process_host,MSG_host_get_name(hosts[i]) );
-       SURFXML_BUFFER_SET(process_function, "slave");
-       SURFXML_BUFFER_SET(process_start_time, "-1.0");
-       SURFXML_BUFFER_SET(process_kill_time, "-1.0");
-       SURFXML_START_TAG(process);
-
-       /*      <argument value="num"/> */
-       SURFXML_BUFFER_SET(argument_value, bprintf("%d",i));
-       SURFXML_START_TAG(argument);
-       SURFXML_END_TAG(argument);
-       SURFXML_END_TAG(process);
-       }
-       /* </platform> */
-       SURFXML_END_TAG(platform);
-
-       free(surfxml_bufferstack);
-       return 0;
+  int host_number = atoi(MSG_process_get_name(MSG_process_self()));
+  char mailbox[256];
+  m_task_t task = NULL;
+  _XBT_GNUC_UNUSED int res;
+  if (host_number == 0){ //master  send then receive
+    sprintf(mailbox, "%d", host_number+1);
+    task = MSG_task_create("Token", task_comp_size, task_comm_size, NULL);
+    XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",host_number,task->name,mailbox);
+    MSG_task_send(task, mailbox);
+    task = NULL;
+    res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
+    xbt_assert(res == MSG_OK, "MSG_task_get failed");
+    XBT_INFO("Host \"%d\" received \"%s\"",host_number, MSG_task_get_name(task));
+    MSG_task_destroy(task);
+  }
+  else{ //slave receive then send
+    res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
+    xbt_assert(res == MSG_OK, "MSG_task_get failed");
+    XBT_INFO("Host \"%d\" received \"%s\"",host_number, MSG_task_get_name(task));
+
+    if(host_number+1 == nb_hosts)
+      sprintf(mailbox, "0");
+    else
+      sprintf(mailbox, "%d", host_number+1);
+    XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",host_number,task->name,mailbox);
+    MSG_task_send(task, mailbox);
+  }
+  return 0;
 }
 
-typedef enum {
-  PORT_22 = 20,
-  MAX_CHANNEL
-} channel_t;
-
 int main(int argc, char **argv)
 {
-       int res;
-  MSG_global_init(&argc, argv);
-  MSG_set_channel_number(MAX_CHANNEL);
+  int i,res;
+  MSG_init(&argc, argv);
   MSG_create_environment(argv[1]);
-
-  MSG_function_register("master", master);
-  MSG_function_register("slave", slave);
-  surf_parse = surf_parse_bypass_application;
-  MSG_launch_application(NULL);
+  xbt_dynar_t hosts = MSG_hosts_as_dynar();
+  nb_hosts =  xbt_dynar_length(hosts);
+  MSG_function_register("host", host);
+
+  XBT_INFO("Number of host '%d'",nb_hosts);
+  for(i = 0 ; i<nb_hosts; i++)
+  {
+    char* name_host = bprintf("%d",i);
+    MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,m_host_t) );
+    free(name_host);
+  }
+  xbt_dynar_free(&hosts);
 
   res = MSG_main();
-
-  INFO1("Simulation time %g", MSG_get_clock());
-
+  XBT_INFO("Simulation time %g", MSG_get_clock());
   MSG_clean();
-
   if (res == MSG_OK)
     return 0;
   else