Logo AND Algorithmique Numérique Distribuée

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