Logo AND Algorithmique Numérique Distribuée

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