Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
slave2worker cont'd
[simgrid.git] / examples / msg / masterworker / masterworker.c
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  * 
13  *  - <b>masterworker/masterworker.c: Master/workers example</b>. This good old example is also very simple. Its
14  *    basic version is fully commented on this page: \ref MSG_ex_master_worker, but several variants can be found in the
15  *    same directory.
16  */
17
18 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
19
20 static int master(int argc, char *argv[])
21 {
22   msg_host_t *workers = NULL;
23   msg_task_t *todo = NULL;
24   long workers_count = 0;
25   int i;
26
27   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
28   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
29   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
30
31   {                             /*  Task creation */
32     char sprintf_buffer[64];
33
34     todo = xbt_new0(msg_task_t, number_of_tasks);
35
36     for (i = 0; i < number_of_tasks; i++) {
37       sprintf(sprintf_buffer, "Task_%d", i);
38       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
39     }
40   }
41
42   {                             /* Process organization */
43     workers_count = argc - 4;
44     workers = xbt_new0(msg_host_t, workers_count);
45
46     for (i = 4; i < argc; i++) {
47       workers[i - 4] = MSG_host_by_name(argv[i]);
48       xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
49     }
50   }
51
52   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
53   for (i = 0; i < workers_count; i++)
54     XBT_DEBUG("%s", MSG_host_get_name(workers[i]));
55
56   for (i = 0; i < number_of_tasks; i++) {
57     XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name, MSG_host_get_name(workers[i % workers_count]));
58     if (MSG_host_self() == workers[i % workers_count]) {
59       XBT_INFO("Hey ! It's me ! :)");
60     }
61
62     MSG_task_send(todo[i], MSG_host_get_name(workers[i % workers_count]));
63     XBT_INFO("Sent");
64   }
65
66   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
67   for (i = 0; i < workers_count; i++) {
68     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
69     MSG_task_send(finalize, MSG_host_get_name(workers[i]));
70   }
71
72   XBT_INFO("Goodbye now!");
73   free(workers);
74   free(todo);
75   return 0;
76 }
77
78 static int worker(int argc, char *argv[])
79 {
80   msg_task_t task = NULL;
81   XBT_ATTRIB_UNUSED int res;
82   while (1) {
83     res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
84     xbt_assert(res == MSG_OK, "MSG_task_get failed");
85
86     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
87     if (!strcmp(MSG_task_get_name(task), "finalize")) {
88       MSG_task_destroy(task);
89       break;
90     }
91
92     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
93     MSG_task_execute(task);
94     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
95     MSG_task_destroy(task);
96     task = NULL;
97   }
98   XBT_INFO("I'm done. See you!");
99   return 0;
100 }
101
102 int main(int argc, char *argv[])
103 {
104   msg_error_t res = MSG_OK;
105
106   MSG_init(&argc, argv);
107   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
108              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
109
110   MSG_create_environment(argv[1]);
111
112   MSG_function_register("master", master);
113   MSG_function_register("worker", worker);
114   MSG_launch_application(argv[2]);
115
116   res = MSG_main();
117
118   XBT_INFO("Simulation time %g", MSG_get_clock());
119
120   return res != MSG_OK;
121 }