Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
72748f0e1b90d8b7a9d9717ddea519d92176562e
[simgrid.git] / examples / c / platform-failures / platform-failures.c
1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
12
13 #include "xbt/log.h"
14 #include "xbt/str.h"
15 #include "xbt/sysdep.h"
16
17 #include <stdio.h> /* snprintf */
18
19 #define FINALIZE 221297 /* a magic number to tell people to stop working */
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(platform_failures, "Messages specific for this example");
22
23 static void master(int argc, char* argv[])
24 {
25   xbt_assert(argc == 5);
26   long number_of_tasks  = xbt_str_parse_int(argv[1], "Invalid amount of tasks");
27   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size");
28   long task_comm_size   = xbt_str_parse_int(argv[3], "Invalid communication size");
29   long workers_count    = xbt_str_parse_int(argv[4], "Invalid amount of workers");
30
31   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
32
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);
37
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);
42
43     switch (sg_comm_wait_for(comm, 10.0)) {
44       case SG_OK:
45         XBT_INFO("Send to %s completed", mailbox_name);
46         break;
47       case SG_ERROR_NETWORK:
48         XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox_name);
49         free(payload);
50         break;
51       case SG_ERROR_TIMEOUT:
52         XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox_name);
53         free(payload);
54         break;
55       default:
56         xbt_die("Unexpected behavior");
57     }
58   }
59
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));
66     *payload             = FINALIZE;
67     sg_comm_t comm       = sg_mailbox_put_async(mailbox, payload, 0);
68
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);
72         free(payload);
73         break;
74       case SG_ERROR_TIMEOUT:
75         XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox_name);
76         free(payload);
77         break;
78       case SG_OK:
79         /* nothing */
80         break;
81       default:
82         xbt_die("Unexpected behavior with '%s'", mailbox_name);
83     }
84   }
85
86   XBT_INFO("Goodbye now!");
87 }
88
89 static void worker(int argc, char* argv[])
90 {
91   xbt_assert(argc == 2);
92   char mailbox_name[80];
93   long id = xbt_str_parse_int(argv[1], "Invalid argument");
94
95   snprintf(mailbox_name, 79, "worker-%ld", id);
96   sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
97
98   while (1) {
99     XBT_INFO("Waiting a message on %s", mailbox_name);
100     double* payload;
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) {
105         free(payload);
106         break;
107       } else {
108         double comp_size = *payload;
109         free(payload);
110         XBT_INFO("Start execution...");
111         sg_actor_execute(comp_size);
112         XBT_INFO("Execution complete.");
113       }
114     } else if (retcode == SG_ERROR_NETWORK) {
115       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
116     }
117   }
118   XBT_INFO("I'm done. See you!");
119 }
120
121 int main(int argc, char* argv[])
122 {
123   simgrid_init(&argc, argv);
124   xbt_assert(argc > 2,
125              "Usage: %s platform_file deployment_file\n"
126              "\tExample: %s platform.xml deployment.xml\n",
127              argv[0], argv[0]);
128
129   simgrid_load_platform(argv[1]);
130
131   simgrid_register_function("master", master);
132   simgrid_register_function("worker", worker);
133   simgrid_load_deployment(argv[2]);
134
135   simgrid_run();
136
137   XBT_INFO("Simulation time %g", simgrid_get_clock());
138
139   return 0;
140 }