Logo AND Algorithmique Numérique Distribuée

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