Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8c4c100720ea51617c9b908b8e059bb5d99561e1
[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"
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 /** Sender function  */
21 int master(int argc, char *argv[])
22 {
23   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
24   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
25   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
26   long slaves_count = xbt_str_parse_int(argv[4], "Invalid amount of slaves: %s");
27
28   int i;
29
30   XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count, number_of_tasks);
31
32   for (i = 0; i < number_of_tasks; i++) {
33     char mailbox[256];
34     char sprintf_buffer[256];
35     msg_task_t task = NULL;
36
37     sprintf(mailbox, "slave-%ld", i % slaves_count);
38     sprintf(sprintf_buffer, "Task_%d", i);
39     task =
40         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
41                         NULL);
42     if (number_of_tasks < 10000 || i % 10000 == 0)
43       XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
44             number_of_tasks, mailbox);
45
46     MSG_task_send(task, mailbox);
47   }
48
49   XBT_INFO
50       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
51   for (i = 0; i < slaves_count; i++) {
52     char mailbox[80];
53
54     sprintf(mailbox, "slave-%ld", i % slaves_count);
55     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
56     MSG_task_send(finalize, mailbox);
57   }
58
59 //  XBT_INFO("Goodbye now!");
60   return 0;
61 }                               /* end_of_master */
62
63 /** Receiver function  */
64 int slave(int argc, char *argv[])
65 {
66   msg_task_t task = NULL;
67   XBT_ATTRIB_UNUSED int res;
68   int id = -1;
69   char mailbox[80];
70   XBT_ATTRIB_UNUSED int read;
71
72   read = sscanf(argv[1], "%d", &id);
73   xbt_assert(read, "Invalid argument %s\n", argv[1]);
74
75   sprintf(mailbox, "slave-%d", id);
76
77   while (1) {
78     res = MSG_task_receive(&(task), mailbox);
79     xbt_assert(res == MSG_OK, "MSG_task_get failed");
80
81 //  XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
82     if (!strcmp(MSG_task_get_name(task), "finalize")) {
83       MSG_task_destroy(task);
84       break;
85     }
86 //    XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
87     MSG_task_execute(task);
88 //    XBT_INFO("\"%s\" done", MSG_task_get_name(task));
89     MSG_task_destroy(task);
90     task = NULL;
91   }
92   XBT_INFO("I'm done. See you!");
93   return 0;
94 }                               /* end_of_slave */
95
96 /** Main function */
97 int main(int argc, char *argv[])
98 {
99   msg_error_t res;
100   const char *platform_file;
101   const char *application_file;
102
103   MSG_init(&argc, argv);
104   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
105             "\tExample: %s msg_platform.xml msg_deployment.xml\n", 
106             argv[0], argv[0]);
107   
108   platform_file = argv[1];
109   application_file = argv[2];
110
111   {                             /*  Simulation setting */
112     MSG_create_environment(platform_file);
113   }
114   {                             /*   Application deployment */
115     MSG_function_register("master", master);
116     MSG_function_register("slave", slave);
117     MSG_launch_application(application_file);
118   }
119   res = MSG_main();
120
121   XBT_INFO("Simulation time %g", MSG_get_clock());
122
123   return res != MSG_OK;
124 }