Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4aaade0948afd20f4ccb8d0668c1c5af35d41022
[simgrid.git] / doc / tuto-msg / masterworker-sol4.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   = MSG_get_host_number();
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   int task_num = 0;
53   while (MSG_get_clock() < timeout) {
54
55     /* Retrieve the next incomming request */
56     XBT_DEBUG("Retrieve the next incomming request on %s", master_name);
57     msg_task_t request = NULL;
58     int res            = MSG_task_receive(&(request), master_name);
59     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
60     msg_host_t requester = MSG_task_get_data(request);
61     MSG_task_destroy(request);
62
63     /* Prepare the task to be sent */
64     char sprintf_buffer[64];
65     sprintf(sprintf_buffer, "Task_%d", task_num);
66     msg_task_t task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
67     MSG_task_set_category(task, master_name);
68
69     /* Send this out */
70     build_channel_name(channel, master_name, MSG_host_get_name(requester));
71
72     XBT_DEBUG("Sending '%s' to channel '%s'", task->name, channel);
73     MSG_task_send(task, channel);
74     XBT_DEBUG("Sent");
75     task_num++;
76   }
77
78   XBT_DEBUG("Time is up. Let's tell everybody the computation is over.");
79   for (int i = 0; i < workers_count; i++) { /* We don't write in order, but the total amount is right */
80
81     /* Don't write to a worker that did not request for work, or it will deadlock: both would be sending something */
82     msg_task_t request = NULL;
83     int res            = MSG_task_receive(&(request), master_name);
84     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
85     msg_host_t requester = MSG_task_get_data(request);
86     MSG_task_destroy(request);
87
88     XBT_DEBUG("Stop worker %s", MSG_host_get_name(requester));
89     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
90     MSG_task_send(finalize, build_channel_name(channel, master_name, MSG_host_get_name(requester)));
91   }
92
93   XBT_INFO("Sent %d tasks in total!", task_num);
94   free(workers);
95   return 0;
96 }
97
98 /** Worker function  */
99 static int worker(int argc, char* argv[])
100 {
101   char channel[1024];
102
103   const char* my_master = MSG_process_get_data(MSG_process_self());
104   build_channel_name(channel, my_master, MSG_host_get_name(MSG_host_self()));
105
106   XBT_DEBUG("Receiving on channel \"%s\"", channel);
107
108   while (1) {
109     /* Send a request */
110     XBT_DEBUG("Sent a request to my master on %s", my_master);
111     msg_task_t request = MSG_task_create("request", 0, 0, MSG_host_self());
112     MSG_task_send(request, my_master);
113
114     /* Wait for the answer */
115     msg_task_t task = NULL;
116     int res         = MSG_task_receive(&(task), channel);
117     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
118
119     XBT_DEBUG("Received '%s'", MSG_task_get_name(task));
120     if (!strcmp(MSG_task_get_name(task), "finalize")) {
121       MSG_task_destroy(task);
122       break;
123     }
124
125     XBT_DEBUG("Processing '%s'", MSG_task_get_name(task));
126     MSG_task_execute(task);
127     XBT_DEBUG("'%s' done", MSG_task_get_name(task));
128     MSG_task_destroy(task);
129   }
130   XBT_DEBUG("I'm done. See you!");
131   return 0;
132 }
133
134 /** Main function */
135 int main(int argc, char* argv[])
136 {
137   MSG_init(&argc, argv);
138   xbt_assert(argc > 2,
139              "Usage: %s platform_file deployment_file\n"
140              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
141              argv[0], argv[0]);
142
143   /*  Create a simulated platform */
144   MSG_create_environment(argv[1]);
145
146   /*   Application deployment */
147   MSG_function_register("master", master);
148   MSG_function_register("worker", worker);
149   MSG_launch_application(argv[2]);
150
151   /* Run the simulation */
152   msg_error_t res = MSG_main();
153
154   XBT_INFO("Simulation time %g", MSG_get_clock());
155   return (res != MSG_OK);
156 }