Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
029ceb9135a5c8af83fca076658c292a8888ebdb
[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 <stdio.h>
8 #include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int worker(int argc, char *argv[]);
19 msg_error_t test_all(const char *platform_file,
20                      const char *application_file);
21
22 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
23
24 static char * build_channel_name(char *buffer, const char *sender, const char* receiver)
25 {
26   strcpy(buffer, sender);
27   strcat(buffer, ":");
28   strcat(buffer, receiver);
29   return buffer;
30 }
31
32 /** Emitter function  */
33 int master(int argc, char *argv[])
34 {
35   int workers_count = 0;
36   msg_host_t *workers = NULL;
37   msg_task_t *todo = NULL;
38   msg_host_t host_self = MSG_host_self();
39   char *master_name = (char *) MSG_host_get_name(host_self);
40   double task_comp_size = 0;
41   double task_comm_size = 0;
42   char channel[1024];
43   double timeout = -1;
44
45   int i;
46
47   TRACE_category(master_name);
48
49   XBT_ATTRIB_UNUSED int res = sscanf(argv[1], "%lg", &timeout);
50   xbt_assert(res,"Invalid argument %s\n", argv[1]);
51   res = sscanf(argv[2], "%lg", &task_comp_size);
52   xbt_assert(res, "Invalid argument %s\n", argv[2]);
53   res = sscanf(argv[3], "%lg", &task_comm_size);
54   xbt_assert(res, "Invalid argument %s\n", argv[3]);
55
56   {                             /* Process organization */
57     workers_count = MSG_get_host_number();
58     workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
59     
60     for (i = 0; i < workers_count; i++)
61       if(host_self == workers[i]) {
62   workers[i] = workers[workers_count-1];
63   workers_count--;
64   break;
65       }
66
67     for (i = 0; i < workers_count; i++)
68   MSG_process_create("worker", worker, master_name, workers[i]);
69   }
70
71   XBT_INFO("Got %d workers and will send tasks for %g seconds!", 
72      workers_count, timeout);
73   xbt_dynar_t idle_hosts = xbt_dynar_new(sizeof(msg_host_t), NULL);
74   msg_host_t request_host = NULL;
75
76   for (i = 0; 1;) {
77     char sprintf_buffer[64];
78     msg_task_t task = NULL;
79
80     msg_task_t request = NULL;
81     while(MSG_task_listen(master_name)) {
82       res = MSG_task_receive(&(request),master_name);
83       xbt_assert(res == MSG_OK, "MSG_task_receive failed");
84       request_host = MSG_task_get_data(request);
85       xbt_dynar_push(idle_hosts, &request_host);
86       MSG_task_destroy(request);
87       request = NULL;
88     }
89
90     if(MSG_get_clock()>timeout) {
91       if(xbt_dynar_length(idle_hosts) == workers_count) break;
92       else {
93   MSG_process_sleep(.1);
94   continue;
95       }
96     }
97     
98     if(xbt_dynar_length(idle_hosts)<=0) {
99       /* No request. Let's wait... */
100       MSG_process_sleep(.1);
101       continue;
102     }
103
104     sprintf(sprintf_buffer, "Task_%d", i);
105     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
106          NULL);
107     MSG_task_set_category(task, master_name);
108
109     xbt_dynar_shift(idle_hosts, &request_host);
110
111     build_channel_name(channel,master_name, MSG_host_get_name(request_host));
112     
113     XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
114     MSG_task_send(task, channel);
115     XBT_DEBUG("Sent");
116     i++;
117   }
118
119   int task_num = i;
120
121   XBT_DEBUG
122       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
123   for (i = 0; i < workers_count; i++) {
124     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
125     MSG_task_send(finalize, build_channel_name(channel,master_name,
126         MSG_host_get_name(workers[i % workers_count])));
127   }
128
129   XBT_INFO("Sent %d tasks in total!", task_num);
130   free(workers);
131   free(todo);
132   return 0;
133 }                               /* end_of_master */
134
135 /** Receiver function  */
136 int worker(int argc, char *argv[])
137 {
138   msg_task_t task = NULL;
139   XBT_ATTRIB_UNUSED int res;
140   char channel[1024];
141
142   const char *my_master = MSG_process_get_data(MSG_process_self());
143   build_channel_name(channel, my_master, MSG_host_get_name(MSG_host_self()));
144
145   XBT_DEBUG("Receiving on channel \"%s\"", channel);
146
147   while (1) {
148     /* Send a request */
149     msg_task_t request = MSG_task_create("request", 0, 0, MSG_host_self());
150     MSG_task_send(request, my_master);
151
152     res = MSG_task_receive(&(task),channel);
153     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
154     
155     XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
156     if (!strcmp(MSG_task_get_name(task), "finalize")) {
157       MSG_task_destroy(task);
158       break;
159     }
160
161     XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
162     MSG_task_execute(task);
163     XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
164     MSG_task_destroy(task);
165     task = NULL;
166   }
167   XBT_DEBUG("I'm done. See you!");
168   return 0;
169 }                               /* end_of_worker */
170
171 /** Test function */
172 msg_error_t test_all(const char *platform_file,
173                      const char *application_file)
174 {
175   msg_error_t res = MSG_OK;
176
177   {                             /*  Simulation setting */
178     MSG_create_environment(platform_file);
179   }
180   {                             /*   Application deployment */
181     MSG_function_register("master", master);
182     MSG_function_register("worker", worker);
183     MSG_launch_application(application_file);
184   }
185   res = MSG_main();
186
187   XBT_INFO("Simulation time %g", MSG_get_clock());
188   return res;
189 }                               /* end_of_test_all */
190
191
192 /** Main function */
193 int main(int argc, char *argv[])
194 {
195   msg_error_t res = MSG_OK;
196
197   MSG_init(&argc, argv);
198   if (argc < 3) {
199     printf("Usage: %s platform_file deployment_file\n", argv[0]);
200     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
201     exit(1);
202   }
203   res = test_all(argv[1], argv[2]);
204
205   if (res == MSG_OK)
206     return 0;
207   else
208     return 1;
209 }                               /* end_of_main */