Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7a5ddbbae29478b83d3d8bb316ec34bb84d0d4e7
[simgrid.git] / examples / msg / masterslave / masterslave_mailbox.c
1 /* Copyright (c) 2010-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 <stdio.h>
8 #include "simgrid/msg.h"        /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   long number_of_tasks = atol(argv[1]);
24   double task_comp_size = atof(argv[2]);
25   double task_comm_size = atof(argv[3]);
26   long slaves_count = atol(argv[4]);
27
28   int i;
29
30   XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count,
31         number_of_tasks);
32
33   for (i = 0; i < number_of_tasks; i++) {
34     char mailbox[256];
35     char sprintf_buffer[256];
36     msg_task_t task = NULL;
37
38     sprintf(mailbox, "slave-%ld", i % slaves_count);
39     sprintf(sprintf_buffer, "Task_%d", i);
40     task =
41         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
42                         NULL);
43     if (number_of_tasks < 10000 || i % 10000 == 0)
44       XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
45             number_of_tasks, mailbox);
46
47     MSG_task_send(task, mailbox);
48   }
49
50   XBT_INFO
51       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
52   for (i = 0; i < slaves_count; i++) {
53     char mailbox[80];
54
55     sprintf(mailbox, "slave-%ld", i % slaves_count);
56     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
57     MSG_task_send(finalize, mailbox);
58   }
59
60 //  XBT_INFO("Goodbye now!");
61   return 0;
62 }                               /* end_of_master */
63
64 /** Receiver function  */
65 int slave(int argc, char *argv[])
66 {
67   msg_task_t task = NULL;
68   XBT_ATTRIB_UNUSED int res;
69   int id = -1;
70   char mailbox[80];
71   XBT_ATTRIB_UNUSED int read;
72
73   read = sscanf(argv[1], "%d", &id);
74   xbt_assert(read, "Invalid argument %s\n", argv[1]);
75
76   sprintf(mailbox, "slave-%d", id);
77
78   while (1) {
79     res = MSG_task_receive(&(task), mailbox);
80     xbt_assert(res == MSG_OK, "MSG_task_get failed");
81
82 //  XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
83     if (!strcmp(MSG_task_get_name(task), "finalize")) {
84       MSG_task_destroy(task);
85       break;
86     }
87 //    XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
88     MSG_task_execute(task);
89 //    XBT_INFO("\"%s\" done", MSG_task_get_name(task));
90     MSG_task_destroy(task);
91     task = NULL;
92   }
93   XBT_INFO("I'm done. See you!");
94   return 0;
95 }                               /* end_of_slave */
96
97 /** Main function */
98 int main(int argc, char *argv[])
99 {
100   msg_error_t res;
101   const char *platform_file;
102   const char *application_file;
103
104   MSG_init(&argc, argv);
105   if (argc < 3) {
106     printf("Usage: %s platform_file deployment_file\n", argv[0]);
107     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
108     exit(1);
109   }
110   platform_file = argv[1];
111   application_file = argv[2];
112
113   {                             /*  Simulation setting */
114     MSG_create_environment(platform_file);
115   }
116   {                             /*   Application deployment */
117     MSG_function_register("master", master);
118     MSG_function_register("slave", slave);
119     MSG_launch_application(application_file);
120   }
121   res = MSG_main();
122
123   XBT_INFO("Simulation time %g", MSG_get_clock());
124
125   if (res == MSG_OK)
126     return 0;
127   else
128     return 1;
129 }                               /* end_of_main */