Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge conflict resolved
[simgrid.git] / examples / msg / masterslave / masterslave_arg.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
20 #define task_comp_size 50000000
21 #define task_comm_size 1000000
22
23 long number_of_jobs;
24 long number_of_slaves;
25
26 static long my_random(long n)
27 {
28   return n * (rand() / ((double)RAND_MAX + 1));
29 }
30
31 /** Emitter function  */
32 int master(int argc, char *argv[])
33 {
34   int i;
35
36   for (i = 1; i <= number_of_jobs; i++) {
37     char mailbox[256];
38     char sprintf_buffer[256];
39     m_task_t task = NULL;
40
41     sprintf(mailbox, "slave-%ld", i % number_of_slaves);
42     sprintf(sprintf_buffer, "Task_%d", i);
43     task =
44         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
45                         NULL);
46     XBT_DEBUG("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
47           number_of_jobs, mailbox);
48
49     MSG_task_send(task, mailbox);
50   }
51
52   XBT_DEBUG
53       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
54   for (i = 0; i < number_of_slaves; i++) {
55     char mailbox[80];
56
57     sprintf(mailbox, "slave-%ld", i % number_of_slaves);
58     m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
59     MSG_task_send(finalize, mailbox);
60   }
61
62   XBT_DEBUG("Goodbye now!");
63   return 0;
64 }                               /* end_of_master */
65
66 /** Receiver function  */
67 int slave(int argc, char *argv[])
68 {
69   m_task_t task = NULL;
70   _XBT_GNUC_UNUSED int res;
71
72   XBT_DEBUG("mailbox: %s",MSG_process_get_name(MSG_process_self()));
73   while (1) {
74     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
75     xbt_assert(res == MSG_OK, "MSG_task_get failed");
76
77     XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
78     if (!strcmp(MSG_task_get_name(task), "finalize")) {
79       MSG_task_destroy(task);
80       break;
81     }
82     XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
83     MSG_task_execute(task);
84     XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
85     MSG_task_destroy(task);
86     task = NULL;
87   }
88   XBT_DEBUG("I'm done. See you!");
89   return 0;
90 }                               /* end_of_slave */                              /* end_of_test_all */
91
92 /** Main function */
93 int main(int argc, char *argv[])
94 {
95   MSG_error_t res = MSG_OK;
96   long i;
97
98   MSG_global_init(&argc, argv);
99   if (argc < 4) {
100     printf("Usage: %s platform_file number_of_jobs number_of_slaves\n", argv[0]);
101     printf("example: %s msg_platform.xml 10 5\n", argv[0]);
102     exit(1);
103   }
104
105   MSG_function_register("master", master);
106   MSG_function_register("slave", slave);
107
108   MSG_create_environment(argv[1]);
109
110   number_of_jobs = atol(argv[2]);
111   number_of_slaves = atol(argv[3]);
112   long number_max = MSG_get_host_number();
113   XBT_INFO("Got %ld slaves, %ld tasks to process, and %d hosts", number_of_slaves, number_of_jobs,MSG_get_host_number());
114
115   m_host_t *host_table =  MSG_get_host_table();
116
117   MSG_process_create( "master",
118                       master,
119                       NULL,
120                       host_table[my_random(number_max)]
121                       );
122
123   for(i = 0 ; i<number_of_slaves; i++)
124   {
125     char* name_host = bprintf("slave-%ld",i);
126       MSG_process_create( name_host,
127                           slave,
128                           NULL,
129                           host_table[my_random(number_max)]
130                           );
131       free(name_host);
132   }
133   xbt_free(host_table);
134
135   res = MSG_main();
136
137   XBT_INFO("Simulation time %g", MSG_get_clock());
138
139   MSG_clean();
140
141   if (res == MSG_OK)
142     return 0;
143   else
144     return 1;
145 }                               /* end_of_main */