Logo AND Algorithmique Numérique Distribuée

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