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-sol2.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   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 (directly from SimGrid) */
37   int worker_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 < worker_count; i++) // Remove my host from the list
41     if (host_self == workers[i]) {
42       workers[i] = workers[worker_count - 1];
43       worker_count--;
44       break;
45     }
46
47   for (int i = 0; i < worker_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", worker_count, timeout);
50
51   /* Dispatch the tasks */
52   int task_num = 0;
53   while (1) {
54     if (MSG_get_clock() > timeout)
55       break;
56
57     char sprintf_buffer[64];
58     sprintf(sprintf_buffer, "Task_%d", task_num);
59     msg_task_t task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
60
61     build_channel_name(channel, master_name, MSG_host_get_name(workers[task_num % worker_count]));
62
63     XBT_DEBUG("Sending '%s' to channel '%s'", task->name, channel);
64     MSG_task_send(task, channel);
65     XBT_DEBUG("Sent");
66     task_num++;
67   }
68
69   XBT_DEBUG("All tasks have been dispatched. Let's tell everybody the computation is over.");
70   for (int i = 0; i < worker_count; i++) {
71     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
72     MSG_task_send(finalize, build_channel_name(channel, master_name, MSG_host_get_name(workers[i % worker_count])));
73   }
74
75   XBT_DEBUG("Sent %d tasks in total!", task_num);
76   free(workers);
77   return 0;
78 }
79
80 /** Worker function  */
81 static int worker(int argc, char* argv[])
82 {
83   char channel[1024];
84
85   build_channel_name(channel, MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
86
87   XBT_DEBUG("Receiving on channel '%s'", channel);
88
89   while (1) {
90     msg_task_t task = NULL;
91     int res         = MSG_task_receive(&(task), channel);
92     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
93
94     XBT_DEBUG("Received '%s'", MSG_task_get_name(task));
95     if (!strcmp(MSG_task_get_name(task), "finalize")) {
96       MSG_task_destroy(task);
97       break;
98     }
99
100     XBT_DEBUG("Processing '%s'", MSG_task_get_name(task));
101     MSG_task_execute(task);
102     XBT_DEBUG("'%s' done", MSG_task_get_name(task));
103     MSG_task_destroy(task);
104   }
105   XBT_DEBUG("I'm done. See you!");
106   return 0;
107 }
108
109 /** Main function */
110 int main(int argc, char* argv[])
111 {
112   MSG_init(&argc, argv);
113   xbt_assert(argc > 2,
114              "Usage: %s platform_file deployment_file\n"
115              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
116              argv[0], argv[0]);
117
118   /*  Create a simulated platform */
119   MSG_create_environment(argv[1]);
120
121   /*   Application deployment */
122   MSG_function_register("master", master);
123   MSG_function_register("worker", worker);
124   MSG_launch_application(argv[2]);
125
126   /* Run the simulation */
127   msg_error_t res = MSG_main();
128
129   XBT_INFO("Simulation time %g", MSG_get_clock());
130   return (res != MSG_OK);
131 }