Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cover with a test Mailbox::ready() method introduced in commit 1ed0e64dc405fbb627ff8a...
[simgrid.git] / doc / tuto-msg / masterworker-sol4.c
1 /* Copyright (c) 2007-2018. 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 #include <stdio.h> /* sprintf */
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
13
14 static char* build_channel_name(char* buffer, const char* sender, const char* receiver)
15 {
16   strcpy(buffer, sender);
17   strcat(buffer, ":");
18   strcat(buffer, receiver);
19   return buffer;
20 }
21
22 /* forward definitions */
23 static int master(int argc, char* argv[]);
24 static int worker(int argc, char* argv[]);
25
26 static int master(int argc, char* argv[])
27 {
28   msg_host_t host_self = MSG_host_self();
29   char* master_name    = (char*)MSG_host_get_name(host_self);
30   char channel[1024];
31
32   TRACE_category(master_name);
33
34   double timeout   = xbt_str_parse_double(argv[1], "Invalid timeout: %s");            /** - timeout      */
35   double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost    */
36   double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
37
38   /* Get the info about the worker processes */
39   int worker_count    = MSG_get_host_number();
40   msg_host_t* workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
41
42   for (int i = 0; i < worker_count; i++) // Remove my host from the list
43     if (host_self == workers[i]) {
44       workers[i] = workers[worker_count - 1];
45       worker_count--;
46       break;
47     }
48
49   for (int i = 0; i < worker_count; i++)
50     MSG_process_create("worker", worker, (void*)master_name, workers[i]);
51   XBT_INFO("Got %d workers and will send tasks for %g seconds", worker_count, timeout);
52
53   /* Dispatch the tasks */
54   int task_num = 0;
55   while (MSG_get_clock() < timeout) {
56
57     /* Retrieve the next incomming request */
58     XBT_DEBUG("Retrieve the next incomming request on %s", master_name);
59     msg_task_t request = NULL;
60     int res            = MSG_task_receive(&(request), master_name);
61     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
62     msg_host_t requester = MSG_task_get_data(request);
63     MSG_task_destroy(request);
64
65     /* Prepare the task to be sent */
66     char sprintf_buffer[64];
67     sprintf(sprintf_buffer, "Task_%d", task_num);
68     msg_task_t task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
69     MSG_task_set_category(task, master_name);
70
71     /* Send this out */
72     build_channel_name(channel, master_name, MSG_host_get_name(requester));
73
74     XBT_DEBUG("Sending '%s' to channel '%s'", task->name, channel);
75     MSG_task_send(task, channel);
76     XBT_DEBUG("Sent");
77     task_num++;
78   }
79
80   XBT_DEBUG("Time is up. Let's tell everybody the computation is over.");
81   for (int i = 0; i < worker_count; i++) { /* We don't write in order, but the total amount is right */
82
83     /* Don't write to a worker that did not request for work, or it will deadlock: both would be sending something */
84     msg_task_t request = NULL;
85     int res            = MSG_task_receive(&(request), master_name);
86     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
87     msg_host_t requester = MSG_task_get_data(request);
88     MSG_task_destroy(request);
89
90     XBT_DEBUG("Stop worker %s", MSG_host_get_name(requester));
91     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
92     MSG_task_send(finalize, build_channel_name(channel, master_name, MSG_host_get_name(requester)));
93   }
94
95   XBT_INFO("Sent %d tasks in total!", task_num);
96   free(workers);
97   return 0;
98 }
99
100 /** Worker function  */
101 static int worker(int argc, char* argv[])
102 {
103   char channel[1024];
104
105   const char* my_master = MSG_process_get_data(MSG_process_self());
106   build_channel_name(channel, my_master, MSG_host_get_name(MSG_host_self()));
107
108   XBT_DEBUG("Receiving on channel \"%s\"", channel);
109
110   while (1) {
111     /* Send a request */
112     XBT_DEBUG("Sent a request to my master on %s", my_master);
113     msg_task_t request = MSG_task_create("request", 0, 0, MSG_host_self());
114     MSG_task_send(request, my_master);
115
116     /* Wait for the answer */
117     msg_task_t task = NULL;
118     int res         = MSG_task_receive(&(task), channel);
119     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
120
121     XBT_DEBUG("Received '%s'", MSG_task_get_name(task));
122     if (!strcmp(MSG_task_get_name(task), "finalize")) {
123       MSG_task_destroy(task);
124       break;
125     }
126
127     XBT_DEBUG("Processing '%s'", MSG_task_get_name(task));
128     MSG_task_execute(task);
129     XBT_DEBUG("'%s' done", MSG_task_get_name(task));
130     MSG_task_destroy(task);
131   }
132   XBT_DEBUG("I'm done. See you!");
133   return 0;
134 }
135
136 /** Main function */
137 int main(int argc, char* argv[])
138 {
139   MSG_init(&argc, argv);
140   xbt_assert(argc > 2,
141              "Usage: %s platform_file deployment_file\n"
142              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
143              argv[0], argv[0]);
144
145   /*  Create a simulated platform */
146   MSG_create_environment(argv[1]);
147
148   /*   Application deployment */
149   MSG_function_register("master", master);
150   MSG_function_register("worker", worker);
151   MSG_launch_application(argv[2]);
152
153   /* Run the simulation */
154   msg_error_t res = MSG_main();
155
156   XBT_INFO("Simulation time %g", MSG_get_clock());
157   return (res != MSG_OK);
158 }