Logo AND Algorithmique Numérique Distribuée

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