Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a masterslave example with arguments
[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 long my_random(long n) {
27   return n*(rand()/(RAND_MAX+1));
28 }
29
30 /** Emitter function  */
31 int master(int argc, char *argv[])
32 {
33   int i;
34
35   for (i = 1; i <= number_of_jobs; i++) {
36     char mailbox[256];
37     char sprintf_buffer[256];
38     m_task_t task = NULL;
39
40     sprintf(mailbox, "slave-%ld", i % number_of_slaves);
41     sprintf(sprintf_buffer, "Task_%d", i);
42     task =
43         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
44                         NULL);
45     XBT_DEBUG("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
46           number_of_jobs, mailbox);
47
48     MSG_task_send(task, mailbox);
49   }
50
51   XBT_DEBUG
52       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
53   for (i = 0; i < number_of_slaves; i++) {
54     char mailbox[80];
55
56     sprintf(mailbox, "slave-%ld", i % number_of_slaves);
57     m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
58     MSG_task_send(finalize, mailbox);
59   }
60
61   XBT_DEBUG("Goodbye now!");
62   return 0;
63 }                               /* end_of_master */
64
65 /** Receiver function  */
66 int slave(int argc, char *argv[])
67 {
68   m_task_t task = NULL;
69   _XBT_GNUC_UNUSED int res;
70   int id = -1;
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_set_channel_number(0);
106   MSG_function_register("master", master);
107   MSG_function_register("slave", slave);
108
109   MSG_create_environment(argv[1]);
110
111   number_of_jobs = atol(argv[2]);
112   number_of_slaves = atol(argv[3]);
113
114   XBT_INFO("Got %ld slaves, %ld tasks to process, and %d hosts", number_of_slaves, number_of_jobs,MSG_get_host_number());
115
116   m_host_t *host_table =  MSG_get_host_table();
117
118   MSG_process_create( "master",
119                       master,
120                       NULL,
121                       host_table[my_random(number_of_slaves)]
122                       );
123
124   for(i = 0 ; i<number_of_slaves; i++)
125   {
126       MSG_process_create(bprintf("slave-%ld",i),
127                           slave,
128                           NULL,
129                           host_table[my_random(number_of_slaves)]
130                           );
131   }
132
133   res = MSG_main();
134
135   XBT_INFO("Simulation time %g", MSG_get_clock());
136
137   MSG_clean();
138
139   if (res == MSG_OK)
140     return 0;
141   else
142     return 1;
143 }                               /* end_of_main */