Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill exception example
[simgrid.git] / examples / msg / failures / 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     sprintf(mailbox, "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     msg_error_t a = MSG_task_send_with_timeout(task,mailbox,10.0);
32
33     if (a == MSG_OK) {
34       XBT_INFO("Send completed");
35     } else if (a == MSG_HOST_FAILURE) {
36       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
37       free(task->data);
38       MSG_task_destroy(task);
39       return 0;
40     } else if (a == MSG_TRANSFER_FAILURE) {
41       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox);
42       free(task->data);
43       MSG_task_destroy(task);
44     } else if (a == MSG_TIMEOUT) {
45       XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
46       free(task->data);
47       MSG_task_destroy(task);
48     } else {
49       XBT_INFO("Hey ?! What's up ? ");
50       xbt_die( "Unexpected behavior");
51     }
52   }
53
54   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
55   for (i = 0; i < workers_count; i++) {
56     char mailbox[256];
57     sprintf(mailbox, "worker-%ld", i % workers_count);
58     msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
59     int a = MSG_task_send_with_timeout(task,mailbox,1.0);
60     if (a == MSG_OK)
61       continue;
62     if (a == MSG_HOST_FAILURE) {
63       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
64       MSG_task_destroy(task);
65       return 0;
66     } else if (a == MSG_TRANSFER_FAILURE) {
67       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
68       MSG_task_destroy(task);
69     } else if (a == MSG_TIMEOUT) {
70       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
71       MSG_task_destroy(task);
72     } else {
73       XBT_INFO("Hey ?! What's up ? ");
74       xbt_die("Unexpected behavior with '%s': %d", mailbox, a);
75     }
76   }
77
78   XBT_INFO("Goodbye now!");
79   return 0;
80 }
81
82 static int worker(int argc, char *argv[])
83 {
84   msg_task_t task = NULL;
85   char mailbox[80];
86
87   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
88
89   sprintf(mailbox, "worker-%ld", id);
90
91   while (1) {
92     int a;
93     double time1, time2;
94
95     time1 = MSG_get_clock();
96     a = MSG_task_receive( &(task), mailbox);
97     time2 = MSG_get_clock();
98     if (a == MSG_OK) {
99       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
100       if (MSG_task_get_data(task) == FINALIZE) {
101         MSG_task_destroy(task);
102         task = NULL;
103         break;
104       }
105       if (time1 < *((double *) task->data))
106         time1 = *((double *) task->data);
107       XBT_INFO("Communication time : \"%f\"", time2 - time1);
108       XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
109       a = MSG_task_execute(task);
110       if (a == MSG_OK) {
111         XBT_INFO("\"%s\" done", MSG_task_get_name(task));
112         free(task->data);
113         MSG_task_destroy(task);
114         task = NULL;
115       } else if (a == MSG_HOST_FAILURE) {
116         XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
117         free(task->data);
118         MSG_task_destroy(task);
119         task = NULL;
120         return 0;
121       } else {
122         XBT_INFO("Hey ?! What's up ? ");
123         xbt_die("Unexpected behavior");
124       }
125     } else if (a == MSG_HOST_FAILURE) {
126       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
127       return 0;
128     } else if (a == MSG_TRANSFER_FAILURE) {
129       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
130     } else {
131       XBT_INFO("Hey ?! What's up ? ");
132       xbt_die("Unexpected behavior");
133     }
134   }
135   XBT_INFO("I'm done. See you!");
136   return 0;
137 }
138
139 int main(int argc, char *argv[])
140 {
141   msg_error_t res = MSG_OK;
142
143   MSG_init(&argc, argv);
144   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
145              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
146
147   MSG_create_environment(argv[1]);
148
149   MSG_function_register("master", master);
150   MSG_function_register("worker", worker);
151   MSG_launch_application(argv[2]);
152
153   res = MSG_main();
154
155   XBT_INFO("Simulation time %g", MSG_get_clock());
156
157   return res != MSG_OK;
158 }