Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into CRTP
[simgrid.git] / examples / deprecated / msg / platform-failures / platform-failures.c
1 /* Copyright (c) 2007-2019. 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 void task_cleanup_handler(void* task)
15 {
16   if (task)
17     MSG_task_destroy(task);
18 }
19
20 static int master(int argc, char *argv[])
21 {
22   xbt_assert(argc == 5);
23   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
24   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
25   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
26   long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
27
28   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
29
30   for (int i = 0; i < number_of_tasks; i++) {
31     char mailbox[256];
32     snprintf(mailbox, 255, "worker-%ld", i % workers_count);
33     XBT_INFO("Send a message to %s", mailbox);
34     msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, NULL);
35
36     switch ( MSG_task_send_with_timeout(task,mailbox,10.0) ) {
37     case MSG_OK:
38       XBT_INFO("Send to %s completed", mailbox);
39       break;
40
41     case MSG_TRANSFER_FAILURE:
42       XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox);
43       MSG_task_destroy(task);
44       break;
45
46     case MSG_TIMEOUT:
47       XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
48       MSG_task_destroy(task);
49       break;
50
51     default:
52       xbt_die( "Unexpected behavior");
53     }
54   }
55
56   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
57   for (int i = 0; i < workers_count; i++) {
58     char mailbox[256];
59     snprintf(mailbox, 255, "worker-%ld", i % workers_count);
60     msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
61
62     switch (MSG_task_send_with_timeout(task,mailbox,1.0)) {
63
64     case MSG_TRANSFER_FAILURE:
65       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
66       MSG_task_destroy(task);
67       break;
68
69     case MSG_TIMEOUT:
70       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
71       MSG_task_destroy(task);
72       break;
73
74     case MSG_OK:
75       /* nothing */
76       break;
77
78     default:
79       xbt_die("Unexpected behavior with '%s'", mailbox);
80     }
81   }
82
83   XBT_INFO("Goodbye now!");
84   return 0;
85 }
86
87 static int worker(int argc, char *argv[])
88 {
89   xbt_assert(argc == 2);
90   char mailbox[80];
91
92   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
93
94   snprintf(mailbox, 79,"worker-%ld", id);
95
96   while (1) {
97     msg_task_t task = NULL;
98     XBT_INFO("Waiting a message on %s", mailbox);
99     int retcode = MSG_task_receive( &(task), mailbox);
100     if (retcode == MSG_OK) {
101       if (MSG_task_get_data(task) == FINALIZE) {
102         MSG_task_destroy(task);
103         break;
104       }
105       XBT_INFO("Start execution...");
106       MSG_process_set_data(MSG_process_self(), task);
107       retcode = MSG_task_execute(task);
108       MSG_process_set_data(MSG_process_self(), NULL);
109       if (retcode == MSG_OK) {
110         XBT_INFO("Execution complete.");
111         MSG_task_destroy(task);
112       } else {
113         XBT_INFO("Hey ?! What's up ? ");
114         xbt_die("Unexpected behavior");
115       }
116     } else if (retcode == MSG_TRANSFER_FAILURE) {
117       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
118     } else {
119       xbt_die("Unexpected behavior");
120     }
121   }
122   XBT_INFO("I'm done. See you!");
123   return 0;
124 }
125
126 int main(int argc, char *argv[])
127 {
128   MSG_init(&argc, argv);
129   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
130              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
131
132   MSG_create_environment(argv[1]);
133
134   MSG_function_register("master", master);
135   MSG_function_register("worker", worker);
136   MSG_process_set_data_cleanup(task_cleanup_handler);
137   MSG_launch_application(argv[2]);
138
139   msg_error_t res = MSG_main();
140
141   XBT_INFO("Simulation time %g", MSG_get_clock());
142
143   return res != MSG_OK;
144 }