Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / doc / msg-tuto-src / masterworker4.c
1 /* Copyright (c) 2007-2010, 2013-2015. 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 "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
12
13 static char * build_channel_name(char *buffer, const char *sender, const char* receiver)
14 {
15   strcpy(buffer, sender);
16   strcat(buffer, ":");
17   strcat(buffer, receiver);
18   return buffer;
19 }
20
21 /** Worker function  */
22 static int master(int argc, char *argv[])
23 {
24   int workers_count = 0;
25   XBT_ATTRIB_UNUSED int res;
26   msg_host_t *workers = NULL;
27   msg_task_t *todo = NULL;
28   msg_host_t host_self = MSG_host_self();
29   char *master_name = (char *) MSG_host_get_name(host_self);
30   char channel[1024];
31
32   int i;
33
34   TRACE_category(master_name);
35
36   double timeout   = xbt_str_parse_double(argv[1], "Invalid timeout: %s");             /** - timeout      */
37   double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");  /** - Task compute cost    */
38   double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");  /** - Task communication size */
39
40   {                             /* Process organization */
41     workers_count = MSG_get_host_number();
42     workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
43     
44     for (i = 0; i < workers_count; i++)
45       if(host_self == workers[i]) {
46         workers[i] = workers[workers_count-1];
47         workers_count--;
48         break;
49       }
50
51     for (i = 0; i < workers_count; i++)
52       MSG_process_create("worker", worker, master_name, workers[i]);
53   }
54
55   XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout);
56   xbt_dynar_t idle_hosts = xbt_dynar_new(sizeof(msg_host_t), NULL);
57   msg_host_t request_host = NULL;
58
59   for (i = 0; 1;) {
60     char sprintf_buffer[64];
61     msg_task_t task = NULL;
62
63     msg_task_t request = NULL;
64     while(MSG_task_listen(master_name)) {
65       res = MSG_task_receive(&(request),master_name);
66       xbt_assert(res == MSG_OK, "MSG_task_receive failed");
67       request_host = MSG_task_get_data(request);
68       xbt_dynar_push(idle_hosts, &request_host);
69       MSG_task_destroy(request);
70       request = NULL;
71     }
72
73     if(MSG_get_clock()>timeout) {
74       if(xbt_dynar_length(idle_hosts) == workers_count) break;
75       else {
76         MSG_process_sleep(.1);
77         continue;
78       }
79     }
80
81     if(xbt_dynar_length(idle_hosts)<=0) {
82       /* No request. Let's wait... */
83       MSG_process_sleep(.1);
84       continue;
85     }
86
87     sprintf(sprintf_buffer, "Task_%d", i);
88     task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
89     MSG_task_set_category(task, master_name);
90
91     xbt_dynar_shift(idle_hosts, &request_host);
92
93     build_channel_name(channel,master_name, MSG_host_get_name(request_host));
94
95     XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
96     MSG_task_send(task, channel);
97     XBT_DEBUG("Sent");
98     i++;
99   }
100
101   int task_num = i;
102
103   XBT_DEBUG ("All tasks have been dispatched. Let's tell everybody the computation is over.");
104   for (i = 0; i < workers_count; i++) {
105     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
106     MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
107   }
108
109   XBT_INFO("Sent %d tasks in total!", task_num);
110   free(workers);
111   free(todo);
112   return 0;
113 }                               /* end_of_master */
114
115 /** Worker function  */
116 static int worker(int argc, char *argv[])
117 {
118   msg_task_t task = NULL;
119   XBT_ATTRIB_UNUSED int res;
120   char channel[1024];
121
122   const char *my_master = MSG_process_get_data(MSG_process_self());
123   build_channel_name(channel, my_master, MSG_host_get_name(MSG_host_self()));
124
125   XBT_DEBUG("Receiving on channel \"%s\"", channel);
126
127   while (1) {
128     /* Send a request */
129     msg_task_t request = MSG_task_create("request", 0, 0, MSG_host_self());
130     MSG_task_send(request, my_master);
131
132     res = MSG_task_receive(&(task),channel);
133     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
134     
135     XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
136     if (!strcmp(MSG_task_get_name(task), "finalize")) {
137       MSG_task_destroy(task);
138       break;
139     }
140
141     XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
142     MSG_task_execute(task);
143     XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
144     MSG_task_destroy(task);
145     task = NULL;
146   }
147   XBT_DEBUG("I'm done. See you!");
148   return 0;
149 }                               /* end_of_worker */
150
151 /** Main function */
152 int main(int argc, char *argv[])
153 {
154   msg_error_t res = MSG_OK;
155
156   MSG_init(&argc, argv);
157   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
158              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
159   {                             /*  Simulation setting */
160     MSG_create_environment(argv[1]);
161   }
162   {                             /*   Application deployment */
163     MSG_function_register("master", master);
164     MSG_function_register("worker", worker);
165     MSG_launch_application(argv[2]);
166   }
167   res = MSG_main();
168
169   XBT_INFO("Simulation time %g", MSG_get_clock());
170   return (res != MSG_OK);
171 }                               /* end_of_main */