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
1 /* Copyright (c) 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "msg/msg.h"
10 #include "surf/surf_private.h"
11
12 int host(int argc, char *argv[]);
13 unsigned int task_comp_size = 50000000;
14 unsigned int task_comm_size = 1000000;
15
16 int nb_hosts; /* All declared hosts */
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(ring,
19                              "Messages specific for this msg example");
20
21 /** @addtogroup MSG_examples
22  * 
23  *  @section MSG_ex_apps Examples of full applications
24  * 
25  * - <b>token_ring/ring_call.c</b>: Classical token ring
26  *   communication, where a token is exchanged along a ring to reach
27  *   every participant.
28  * 
29  */
30
31 int host(int argc, char *argv[])
32 {
33   int host_number = atoi(MSG_process_get_name(MSG_process_self()));
34   char mailbox[256];
35   m_task_t task = NULL;
36   _XBT_GNUC_UNUSED int res;
37   if (host_number == 0){ //master  send then receive
38     sprintf(mailbox, "%d", host_number+1);
39     task = MSG_task_create("Token", task_comp_size, task_comm_size, NULL);
40     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",host_number,task->name,mailbox);
41     MSG_task_send(task, mailbox);
42     task = NULL;
43     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
44     xbt_assert(res == MSG_OK, "MSG_task_get failed");
45     XBT_INFO("Host \"%d\" received \"%s\"",host_number, MSG_task_get_name(task));
46     MSG_task_destroy(task);
47   }
48   else{ //slave receive then send
49     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
50     xbt_assert(res == MSG_OK, "MSG_task_get failed");
51     XBT_INFO("Host \"%d\" received \"%s\"",host_number, MSG_task_get_name(task));
52
53     if(host_number+1 == nb_hosts)
54       sprintf(mailbox, "0");
55     else
56       sprintf(mailbox, "%d", host_number+1);
57     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",host_number,task->name,mailbox);
58     MSG_task_send(task, mailbox);
59   }
60   return 0;
61 }
62
63 int main(int argc, char **argv)
64 {
65   int i,res;
66   MSG_init(&argc, argv);
67   MSG_create_environment(argv[1]);
68   xbt_dynar_t hosts = MSG_hosts_as_dynar();
69   nb_hosts =  xbt_dynar_length(hosts);
70   MSG_function_register("host", host);
71
72   XBT_INFO("Number of host '%d'",nb_hosts);
73   for(i = 0 ; i<nb_hosts; i++)
74   {
75     char* name_host = bprintf("%d",i);
76     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,m_host_t) );
77     free(name_host);
78   }
79   xbt_dynar_free(&hosts);
80
81   res = MSG_main();
82   XBT_INFO("Simulation time %g", MSG_get_clock());
83   MSG_clean();
84   if (res == MSG_OK)
85     return 0;
86   else
87     return 1;
88
89 }