Logo AND Algorithmique Numérique Distribuée

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