Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
39d2cec69cf4f7b129800d96f3cd0dadd5f8e994
[simgrid.git] / doc / msg-tuto-src / masterworker3.c
1 /* Copyright (c) 2007-2010, 2013-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 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
12
13 static char * build_channel_name(char *buffer, const char *sender, const char* receiver)
14 {
15   strcpy(buffer, sender);
16   strcat(buffer, ":");
17   strcat(buffer, receiver);
18   return buffer;
19 }
20
21 /* forward definitions */
22 static int master(int argc, char* argv[]);
23 static int worker(int argc, char* argv[]);
24
25 static int master(int argc, char *argv[])
26 {
27   int workers_count = 0;
28   msg_host_t *workers = NULL;
29   msg_task_t *todo = NULL;
30   msg_host_t host_self = MSG_host_self();
31   char *master_name = (char *) MSG_host_get_name(host_self);
32   char channel[1024];
33
34   int i;
35
36   TRACE_category(master_name);
37
38   double timeout   = xbt_str_parse_double(argv[1], "Invalid timeout: %s");             /** - timeout      */
39   double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");  /** - Task compute cost    */
40   double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");  /** - Task communication size */
41
42   {                             /* Process organization */
43     workers_count = MSG_get_host_number();
44     workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
45
46     for (i = 0; i < workers_count; i++)
47       if(host_self == workers[i]) {
48         workers[i] = workers[workers_count-1];
49         workers_count--;
50         break;
51       }
52
53     for (i = 0; i < workers_count; i++)
54       MSG_process_create("worker", worker, master_name, workers[i]);
55   }
56
57   XBT_INFO("Got %d workers and will send tasks for %g seconds!", workers_count, timeout);
58
59   for (i = 0; 1; i++) {
60     char sprintf_buffer[64];
61     msg_task_t task = NULL;
62
63     if(MSG_get_clock()>timeout) break;
64
65     sprintf(sprintf_buffer, "Task_%d", i);
66     task = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
67     MSG_task_set_category(task, master_name);
68
69     build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count]));
70
71     XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
72     MSG_task_send(task, channel);
73     XBT_DEBUG("Sent");
74   }
75
76   int task_num = i;
77
78   XBT_DEBUG ("All tasks have been dispatched. Let's tell everybody the computation is over.");
79   for (i = 0; i < workers_count; i++) {
80     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
81     MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
82   }
83
84   XBT_INFO("Sent %d tasks in total!", task_num);
85   free(workers);
86   free(todo);
87   return 0;
88 }                               /* end_of_master */
89
90 /** Worker function  */
91 static int worker(int argc, char *argv[])
92 {
93   msg_task_t task = NULL;
94   XBT_ATTRIB_UNUSED int res;
95   char channel[1024];
96
97   build_channel_name(channel,MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
98
99   XBT_DEBUG("Receiving on channel \"%s\"", channel);
100
101   while (1) {
102     res = MSG_task_receive(&(task),channel);
103     xbt_assert(res == MSG_OK, "MSG_task_get failed");
104     
105     XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
106     if (!strcmp(MSG_task_get_name(task), "finalize")) {
107       MSG_task_destroy(task);
108       break;
109     }
110
111     XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
112     MSG_task_execute(task);
113     XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
114     MSG_task_destroy(task);
115     task = NULL;
116   }
117   XBT_DEBUG("I'm done. See you!");
118   return 0;
119 }                               /* end_of_worker */
120
121 /** Main function */
122 int main(int argc, char *argv[])
123 {
124   msg_error_t res = MSG_OK;
125
126   MSG_init(&argc, argv);
127   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
128              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
129   {                             /*  Simulation setting */
130     MSG_create_environment(argv[1]);
131   }
132   {                             /*   Application deployment */
133     MSG_function_register("master", master);
134     MSG_function_register("worker", worker);
135     MSG_launch_application(argv[2]);
136   }
137   res = MSG_main();
138
139   XBT_INFO("Simulation time %g", MSG_get_clock());
140   return (res != MSG_OK);
141 }                               /* end_of_main */