Logo AND Algorithmique Numérique Distribuée

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