Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start rewriting the platform documentation now that AS are called netzones
[simgrid.git] / doc / msg-tuto-src / masterworker1.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 /** Emitter 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
33   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");    /** - Number of tasks      */
34   double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");  /** - Task compute cost    */
35   double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");  /** - Task communication size */
36
37   {                             /*  Task creation */
38     char sprintf_buffer[64];
39
40     todo = xbt_new0(msg_task_t, number_of_tasks);
41
42     for (i = 0; i < number_of_tasks; i++) {
43       sprintf(sprintf_buffer, "Task_%d", i);
44       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
45     }
46   }
47
48   {                             /* Process organization */
49     workers_count = MSG_get_host_number();
50     workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
51
52     for (i = 0; i < workers_count; i++)
53       if(host_self == workers[i]) {
54          workers[i] = workers[workers_count-1];
55          workers_count--;
56          break;
57       }
58
59     for (i = 0; i < workers_count; i++)
60   MSG_process_create("worker", worker, master_name, workers[i]);
61   }
62
63   XBT_INFO("Got %d workers and %Ld tasks to process", workers_count, number_of_tasks);
64
65   for (i = 0; i < number_of_tasks; i++) {
66     build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count]));
67
68     XBT_INFO("Sending \"%s\" to channel \"%s\"", todo[i]->name, channel);
69
70     MSG_task_send(todo[i], channel);
71     XBT_INFO("Sent");
72   }
73
74   XBT_INFO ("All tasks have been dispatched. Let's tell everybody the computation is over.");
75   for (i = 0; i < workers_count; i++) {
76     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
77     MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
78   }
79
80   XBT_INFO("Goodbye now!");
81   free(workers);
82   free(todo);
83   return 0;
84 }                               /* end_of_master */
85
86 /** Receiver function  */
87 static int worker(int argc, char *argv[])
88 {
89   msg_task_t task = NULL;
90   XBT_ATTRIB_UNUSED int res;
91   char channel[1024];
92
93   build_channel_name(channel,MSG_process_get_data(MSG_process_self()), MSG_host_get_name(MSG_host_self()));
94
95   XBT_INFO("Receiving on channel \"%s\"", channel);
96
97   while (1) {
98     res = MSG_task_receive(&(task),channel);
99     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
100     
101     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
102     if (!strcmp(MSG_task_get_name(task), "finalize")) {
103       MSG_task_destroy(task);
104       break;
105     }
106
107     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
108     MSG_task_execute(task);
109     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
110     MSG_task_destroy(task);
111     task = NULL;
112   }
113   XBT_INFO("I'm done. See you!");
114   return 0;
115 }                               /* end_of_worker */
116
117 /** Main function */
118 int main(int argc, char *argv[])
119 {
120   msg_error_t res = MSG_OK;
121
122   MSG_init(&argc, argv);
123   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
124              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
125   {                             /*  Simulation setting */
126     MSG_create_environment(argv[1]);
127   }
128   {                             /*   Application deployment */
129     MSG_function_register("master", master);
130     MSG_function_register("worker", worker);
131     MSG_launch_application(argv[2]);
132   }
133   res = MSG_main();
134
135   XBT_INFO("Simulation time %g", MSG_get_clock());
136   return (res != MSG_OK);
137 }                               /* end_of_main */