1 /* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/actor.h"
7 #include "simgrid/comm.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/exec.h"
10 #include "simgrid/host.h"
11 #include "simgrid/mailbox.h"
13 #include "xbt/asserts.h"
17 #include <stdio.h> /* snprintf */
19 #define FINALIZE 221297 /* a magic number to tell people to stop working */
21 XBT_LOG_NEW_DEFAULT_CATEGORY(platform_failures, "Messages specific for this example");
23 static void master(int argc, char* argv[])
25 xbt_assert(argc == 5);
26 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
27 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
28 long task_comm_size = xbt_str_parse_int(argv[3], "Invalid communication size: %s");
29 long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
31 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
33 for (int i = 0; i < number_of_tasks; i++) {
34 char mailbox_name[256];
35 snprintf(mailbox_name, 255, "worker-%ld", i % workers_count);
36 sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
38 XBT_INFO("Send a message to %s", mailbox_name);
39 double* payload = (double*)xbt_malloc(sizeof(double));
40 *payload = task_comp_size;
41 sg_comm_t comm = sg_mailbox_put_async(mailbox, payload, task_comm_size);
43 switch (sg_comm_wait_for(comm, 10.0)) {
45 XBT_INFO("Send to %s completed", mailbox_name);
47 case SG_ERROR_NETWORK:
48 XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox_name);
51 case SG_ERROR_TIMEOUT:
52 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox_name);
56 xbt_die("Unexpected behavior");
60 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
61 for (int i = 0; i < workers_count; i++) {
62 char mailbox_name[256];
63 snprintf(mailbox_name, 255, "worker-%ld", i % workers_count);
64 sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
65 double* payload = (double*)xbt_malloc(sizeof(double));
67 sg_comm_t comm = sg_mailbox_put_async(mailbox, payload, 0);
69 switch (sg_comm_wait_for(comm, 1.0)) {
70 case SG_ERROR_NETWORK:
71 XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox_name);
74 case SG_ERROR_TIMEOUT:
75 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox_name);
82 xbt_die("Unexpected behavior with '%s'", mailbox_name);
86 XBT_INFO("Goodbye now!");
89 static void worker(int argc, char* argv[])
91 xbt_assert(argc == 2);
92 char mailbox_name[80];
93 long id = xbt_str_parse_int(argv[1], "Invalid argument %s");
95 snprintf(mailbox_name, 79, "worker-%ld", id);
96 sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
99 XBT_INFO("Waiting a message on %s", mailbox_name);
101 sg_comm_t comm = sg_mailbox_get_async(mailbox, (void**)&payload);
102 sg_error_t retcode = sg_comm_wait(comm);
103 if (retcode == SG_OK) {
104 if (*payload == FINALIZE) {
108 double comp_size = *payload;
110 XBT_INFO("Start execution...");
111 sg_actor_execute(comp_size);
112 XBT_INFO("Execution complete.");
114 } else if (retcode == SG_ERROR_NETWORK) {
115 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
118 XBT_INFO("I'm done. See you!");
121 int main(int argc, char* argv[])
123 simgrid_init(&argc, argv);
125 "Usage: %s platform_file deployment_file\n"
126 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
129 simgrid_load_platform(argv[1]);
131 simgrid_register_function("master", master);
132 simgrid_register_function("worker", worker);
133 simgrid_load_deployment(argv[2]);
137 XBT_INFO("Simulation time %g", simgrid_get_clock());