Logo AND Algorithmique Numérique Distribuée

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