Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add/update copyright notices.
[simgrid.git] / doc / msg-tuto-src / masterworker2.c
1 /* Copyright (c) 2007-2010, 2013-2014. 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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/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   _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%lg", &timeout);
48   xbt_assert(res,"Invalid argument %s\n", argv[1]);
49   res = sscanf(argv[2], "%lg", &task_comp_size);
50   xbt_assert(res, "Invalid argument %s\n", argv[2]);
51   res = sscanf(argv[3], "%lg", &task_comm_size);
52   xbt_assert(res, "Invalid argument %s\n", argv[3]);
53
54   {                             /* Process organisation */
55     workers_count = MSG_get_host_number();
56     workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
57     
58     for (i = 0; i < workers_count; i++)
59       if(host_self == workers[i]) {
60         workers[i] = workers[workers_count-1];
61         workers_count--;
62         break;
63       }
64
65     for (i = 0; i < workers_count; i++)
66         MSG_process_create("worker", worker, master_name, workers[i]);
67   }
68
69   XBT_INFO("Got %d workers and will send tasks for %g seconds!", 
70            workers_count, timeout);
71
72   for (i = 0; 1; i++) {
73     char sprintf_buffer[64];
74     msg_task_t task = NULL;
75
76     if(MSG_get_clock()>timeout) break;
77
78     sprintf(sprintf_buffer, "Task_%d", i);
79     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
80                            NULL);
81
82     build_channel_name(channel,master_name,
83                        MSG_host_get_name(workers[i % workers_count]));
84     
85     XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
86     MSG_task_send(task, channel);
87     XBT_DEBUG("Sent");
88   }
89
90   int task_num = i;
91
92   XBT_DEBUG
93       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
94   for (i = 0; i < workers_count; i++) {
95     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
96     MSG_task_send(finalize, build_channel_name(channel,master_name,
97                     MSG_host_get_name(workers[i % workers_count])));
98   }
99
100   XBT_INFO("Sent %d tasks in total!", task_num);
101   free(workers);
102   free(todo);
103   return 0;
104 }                               /* end_of_master */
105
106 /** Receiver function  */
107 int worker(int argc, char *argv[])
108 {
109   msg_task_t task = NULL;
110   _XBT_GNUC_UNUSED int res;
111   char channel[1024];
112
113   build_channel_name(channel,MSG_process_get_data(MSG_process_self()),
114                      MSG_host_get_name(MSG_host_self()));
115
116   XBT_DEBUG("Receiving on channel \"%s\"", channel);
117
118   while (1) {
119     res = MSG_task_receive(&(task),channel);
120     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
121     
122     XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
123     if (!strcmp(MSG_task_get_name(task), "finalize")) {
124       MSG_task_destroy(task);
125       break;
126     }
127
128     XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
129     MSG_task_execute(task);
130     XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
131     MSG_task_destroy(task);
132     task = NULL;
133   }
134   XBT_DEBUG("I'm done. See you!");
135   return 0;
136 }                               /* end_of_worker */
137
138 /** Test function */
139 msg_error_t test_all(const char *platform_file,
140                      const char *application_file)
141 {
142   msg_error_t res = MSG_OK;
143
144   {                             /*  Simulation setting */
145     MSG_create_environment(platform_file);
146   }
147   {                             /*   Application deployment */
148     MSG_function_register("master", master);
149     MSG_function_register("worker", worker);
150     MSG_launch_application(application_file);
151   }
152   res = MSG_main();
153
154   XBT_INFO("Simulation time %g", MSG_get_clock());
155   return res;
156 }                               /* end_of_test_all */
157
158
159 /** Main function */
160 int main(int argc, char *argv[])
161 {
162   msg_error_t res = MSG_OK;
163
164   MSG_init(&argc, argv);
165   if (argc < 3) {
166     printf("Usage: %s platform_file deployment_file\n", argv[0]);
167     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
168     exit(1);
169   }
170   res = test_all(argv[1], argv[2]);
171
172   if (res == MSG_OK)
173     return 0;
174   else
175     return 1;
176 }                               /* end_of_main */