Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f7656af2dc8bea0912e7b461f5fb779df9ed892e
[simgrid.git] / examples / msg / platform-failures / platform-failures.c
1 /* Copyright (c) 2007-2018. 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/msg.h"
7
8 #include <stdio.h> /* snprintf */
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
13
14 static int master(int argc, char *argv[])
15 {
16   int i;
17
18   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
19   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
20   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
21   long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
22
23   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
24
25   for (i = 0; i < number_of_tasks; i++) {
26     char mailbox[256];
27     snprintf(mailbox, 255, "worker-%ld", i % workers_count);
28     XBT_INFO("Send a message to %s", mailbox);
29     msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1));
30     *((double *) task->data) = MSG_get_clock();
31
32     switch ( MSG_task_send_with_timeout(task,mailbox,10.0) ) {
33     case MSG_OK:
34       XBT_INFO("Send to %s completed", mailbox);
35       break;
36
37     case MSG_HOST_FAILURE:
38       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
39       free(task->data);
40       MSG_task_destroy(task);
41       return 0;
42
43     case MSG_TRANSFER_FAILURE:
44       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox);
45       free(task->data);
46       MSG_task_destroy(task);
47       break;
48
49     case MSG_TIMEOUT:
50       XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
51       free(task->data);
52       MSG_task_destroy(task);
53       break;
54
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 (i = 0; i < workers_count; i++) {
62     char mailbox[256];
63     snprintf(mailbox, 255, "worker-%ld", i % workers_count);
64     msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
65
66     switch (MSG_task_send_with_timeout(task,mailbox,1.0)) {
67     case MSG_HOST_FAILURE:
68       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
69       MSG_task_destroy(task);
70       break;
71
72     case MSG_TRANSFER_FAILURE:
73       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
74       MSG_task_destroy(task);
75       break;
76
77     case MSG_TIMEOUT:
78       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
79       MSG_task_destroy(task);
80       break;
81
82     case MSG_OK:
83       /* nothing */
84       break;
85
86     default:
87       xbt_die("Unexpected behavior with '%s'", mailbox);
88     }
89   }
90
91   XBT_INFO("Goodbye now!");
92   return 0;
93 }
94
95 static int worker(int argc, char *argv[])
96 {
97   char mailbox[80];
98
99   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
100
101   snprintf(mailbox, 79,"worker-%ld", id);
102
103   while (1) {
104     double time1 = MSG_get_clock();
105     msg_task_t task = NULL;
106     XBT_INFO("Waiting a message on %s", mailbox);
107     int retcode = MSG_task_receive( &(task), mailbox);
108     if (retcode == MSG_OK) {
109       if (MSG_task_get_data(task) == FINALIZE) {
110         MSG_task_destroy(task);
111         break;
112       }
113       if (time1 < *((double *) task->data))
114         time1 = *((double *) task->data);
115       XBT_INFO("Start execution...");
116       retcode = MSG_task_execute(task);
117       if (retcode == MSG_OK) {
118         XBT_INFO("Execution complete.");
119         free(task->data);
120         MSG_task_destroy(task);
121       } else if (retcode == MSG_HOST_FAILURE) {
122         XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
123         free(task->data);
124         MSG_task_destroy(task);
125         return 0;
126       } else {
127         XBT_INFO("Hey ?! What's up ? ");
128         xbt_die("Unexpected behavior");
129       }
130     } else if (retcode == MSG_HOST_FAILURE) {
131       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
132       return 0;
133     } else if (retcode == MSG_TRANSFER_FAILURE) {
134       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
135     } else {
136       xbt_die("Unexpected behavior");
137     }
138   }
139   XBT_INFO("I'm done. See you!");
140   return 0;
141 }
142
143 int main(int argc, char *argv[])
144 {
145   MSG_init(&argc, argv);
146   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
147              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
148
149   MSG_create_environment(argv[1]);
150
151   MSG_function_register("master", master);
152   MSG_function_register("worker", worker);
153   MSG_launch_application(argv[2]);
154
155   msg_error_t res = MSG_main();
156
157   XBT_INFO("Simulation time %g", MSG_get_clock());
158
159   return res != MSG_OK;
160 }