Logo AND Algorithmique Numérique Distribuée

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