Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[doc] TODO / Letter to Santa
[simgrid.git] / doc / msg-tuto-src / masterworker3.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 /** Master function  */
22 static int master(int argc, char *argv[])
23 {
24   int workers_count = 0;
25   msg_host_t *workers = NULL;
26   msg_task_t *todo = NULL;
27   msg_host_t host_self = MSG_host_self();
28   char *master_name = (char *) MSG_host_get_name(host_self);
29   char channel[1024];
30
31   int i;
32
33   TRACE_category(master_name);
34
35   double timeout   = xbt_str_parse_double(argv[1], "Invalid timeout: %s");             /** - timeout      */
36   double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");  /** - Task compute cost    */
37   double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");  /** - Task communication size */
38
39   {                             /* Process organization */
40     workers_count = MSG_get_host_number();
41     workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
42
43     for (i = 0; i < workers_count; i++)
44       if(host_self == workers[i]) {
45         workers[i] = workers[workers_count-1];
46         workers_count--;
47         break;
48       }
49
50     for (i = 0; i < workers_count; i++)
51       MSG_process_create("worker", worker, master_name, workers[i]);
52   }
53
54   XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout);
55
56   for (i = 0; 1; i++) {
57     char sprintf_buffer[64];
58     msg_task_t task = NULL;
59
60     if(MSG_get_clock()>timeout) break;
61
62     sprintf(sprintf_buffer, "Task_%d", i);
63     task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
64     MSG_task_set_category(task, master_name);
65
66     build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count]));
67
68     XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
69     MSG_task_send(task, channel);
70     XBT_DEBUG("Sent");
71   }
72
73   int task_num = i;
74
75   XBT_DEBUG ("All tasks have been dispatched. Let's tell everybody the computation is over.");
76   for (i = 0; i < workers_count; i++) {
77     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
78     MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
79   }
80
81   XBT_INFO("Sent %d tasks in total!", task_num);
82   free(workers);
83   free(todo);
84   return 0;
85 }                               /* end_of_master */
86
87 /** Worker function  */
88 static int worker(int argc, char *argv[])
89 {
90   msg_task_t task = NULL;
91   XBT_ATTRIB_UNUSED int res;
92   char channel[1024];
93
94   build_channel_name(channel,MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
95
96   XBT_DEBUG("Receiving on channel \"%s\"", channel);
97
98   while (1) {
99     res = MSG_task_receive(&(task),channel);
100     xbt_assert(res == MSG_OK, "MSG_task_get failed");
101     
102     XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
103     if (!strcmp(MSG_task_get_name(task), "finalize")) {
104       MSG_task_destroy(task);
105       break;
106     }
107
108     XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
109     MSG_task_execute(task);
110     XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
111     MSG_task_destroy(task);
112     task = NULL;
113   }
114   XBT_DEBUG("I'm done. See you!");
115   return 0;
116 }                               /* end_of_worker */
117
118 /** Main function */
119 int main(int argc, char *argv[])
120 {
121   msg_error_t res = MSG_OK;
122
123   MSG_init(&argc, argv);
124   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
125              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
126   {                             /*  Simulation setting */
127     MSG_create_environment(argv[1]);
128   }
129   {                             /*   Application deployment */
130     MSG_function_register("master", master);
131     MSG_function_register("worker", worker);
132     MSG_launch_application(argv[2]);
133   }
134   res = MSG_main();
135
136   XBT_INFO("Simulation time %g", MSG_get_clock());
137   return (res != MSG_OK);
138 }                               /* end_of_main */