Logo AND Algorithmique Numérique Distribuée

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