Logo AND Algorithmique Numérique Distribuée

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