Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e9d26e54e53387d69d9f624915a41b2a4c5c84a2
[simgrid.git] / examples / msg / app-masterworker / app-masterworker.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 /** @addtogroup MSG_examples
12  *
13  *  - <b>app-masterworker/app-masterworker.c: Master/workers example</b>. This good old example is also very simple. Its
14  *    basic version is fully commented on this page: \ref MSG_ex_master_worker.
15  */
16
17 static int master(int argc, char *argv[])
18 {
19   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
20   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
21   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
22   long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
23
24   int i;
25
26   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
27
28   for (i = 0; i < number_of_tasks; i++) {
29     char mailbox[256];
30     char sprintf_buffer[256];
31     msg_task_t task = NULL;
32
33     sprintf(mailbox, "worker-%ld", i % workers_count);
34     sprintf(sprintf_buffer, "Task_%d", i);
35     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
36     if (number_of_tasks < 10000 || i % 10000 == 0)
37       XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
38
39     MSG_task_send(task, mailbox);
40   }
41
42   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
43   for (i = 0; i < workers_count; i++) {
44     char mailbox[80];
45
46     sprintf(mailbox, "worker-%ld", i % workers_count);
47     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
48     MSG_task_send(finalize, mailbox);
49   }
50
51   return 0;
52 }
53
54 static int worker(int argc, char *argv[])
55 {
56   msg_task_t task = NULL;
57   char mailbox[80];
58
59   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
60
61   sprintf(mailbox, "worker-%ld", id);
62
63   while (1) {
64     int res = MSG_task_receive(&(task), mailbox);
65     xbt_assert(res == MSG_OK, "MSG_task_get failed");
66
67 //  XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
68     if (!strcmp(MSG_task_get_name(task), "finalize")) {
69       MSG_task_destroy(task);
70       break;
71     }
72 //    XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
73     MSG_task_execute(task);
74 //    XBT_INFO("\"%s\" done", MSG_task_get_name(task));
75     MSG_task_destroy(task);
76     task = NULL;
77   }
78   XBT_INFO("I'm done. See you!");
79   return 0;
80 }
81
82 int main(int argc, char *argv[])
83 {
84   MSG_init(&argc, argv);
85   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
86              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
87
88   MSG_create_environment(argv[1]);
89
90   MSG_function_register("master", master);
91   MSG_function_register("worker", worker);
92   MSG_launch_application(argv[2]);
93
94   msg_error_t res = MSG_main();
95
96   XBT_INFO("Simulation time %g", MSG_get_clock());
97
98   return res != MSG_OK;
99 }