Logo AND Algorithmique Numérique Distribuée

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