Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change the prototype of xbt_thread_create(), sorry.
[simgrid.git] / examples / msg / masterslave / masterslave_failure.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 #include "xbt/sysdep.h"         /* calloc, printf */
11
12 /* Create a log channel to have nice outputs. */
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
16                              "Messages specific for this msg example");
17
18 int master(int argc, char *argv[]);
19 int slave(int argc, char *argv[]);
20 int forwarder(int argc, char *argv[]);
21 MSG_error_t test_all(const char *platform_file, const char *application_file);
22
23 typedef enum {
24   PORT_22 = 0,
25   MAX_CHANNEL
26 } channel_t;
27
28 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
29
30 /** Emitter function  */
31 int master(int argc, char *argv[])
32 {
33   int slaves_count = 0;
34   m_host_t *slaves = NULL;
35   int number_of_tasks = 0;
36   double task_comp_size = 0;
37   double task_comm_size = 0;
38
39
40   int i;
41
42   xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
43               "Invalid argument %s\n", argv[1]);
44   xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
45               "Invalid argument %s\n", argv[2]);
46   xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
47               "Invalid argument %s\n", argv[3]);
48
49   {                             /* Process organisation */
50     slaves_count = argc - 4;
51     slaves = xbt_new0(m_host_t, slaves_count);
52
53     for (i = 4; i < argc; i++) {
54       slaves[i - 4] = MSG_get_host_by_name(argv[i]);
55       if (slaves[i - 4] == NULL) {
56         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
57         abort();
58       }
59     }
60   }
61
62   INFO1("Got %d slave(s) :", slaves_count);
63   for (i = 0; i < slaves_count; i++)
64     INFO1("%s", slaves[i]->name);
65
66   INFO1("Got %d task to process :", number_of_tasks);
67
68   for (i = 0; i < number_of_tasks; i++) {
69     m_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
70                                     xbt_new0(double, 1));
71     int a;
72     *((double *) task->data) = MSG_get_clock();
73
74     a =
75       MSG_task_put_with_timeout(task, slaves[i % slaves_count], PORT_22,
76                                 10.0);
77     if (a == MSG_OK) {
78       INFO0("Send completed");
79     } else if (a == MSG_HOST_FAILURE) {
80       INFO0
81         ("Gloups. The cpu on which I'm running just turned off!. See you!");
82       free(slaves);
83       return 0;
84     } else if (a == MSG_TRANSFER_FAILURE) {
85       INFO1
86         ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
87          slaves[i % slaves_count]->name);
88       MSG_task_destroy(task);
89     } else {
90       INFO0("Hey ?! What's up ? ");
91       xbt_assert0(0, "Unexpected behavior");
92     }
93   }
94
95   INFO0
96     ("All tasks have been dispatched. Let's tell everybody the computation is over.");
97   for (i = 0; i < slaves_count; i++) {
98     m_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
99     int a = MSG_task_put_with_timeout(task, slaves[i], PORT_22, 1.0);
100     if (a == MSG_OK)
101       continue;
102     if (a == MSG_HOST_FAILURE) {
103       INFO0
104         ("Gloups. The cpu on which I'm running just turned off!. See you!");
105       return 0;
106     } else if (a == MSG_TRANSFER_FAILURE) {
107       INFO1("Mmh. Can't reach '%s'! Nevermind. Let's keep going!",
108             slaves[i]->name);
109       MSG_task_destroy(task);
110     } else {
111       INFO0("Hey ?! What's up ? ");
112       xbt_assert2(0, "Unexpected behavior with '%s': %d", slaves[i]->name, a);
113     }
114   }
115
116   INFO0("Goodbye now!");
117   free(slaves);
118   return 0;
119 }                               /* end_of_master */
120
121 /** Receiver function  */
122 int slave(int argc, char *argv[])
123 {
124   while (1) {
125     m_task_t task = NULL;
126     int a;
127     double time1, time2;
128
129     time1 = MSG_get_clock();
130     a = MSG_task_get(&(task), PORT_22);
131     time2 = MSG_get_clock();
132     if (a == MSG_OK) {
133       INFO1("Received \"%s\"", MSG_task_get_name(task));
134       if (MSG_task_get_data(task) == FINALIZE) {
135         MSG_task_destroy(task);
136         break;
137       }
138       if (time1 < *((double *) task->data))
139         time1 = *((double *) task->data);
140       INFO1("Communication time : \"%f\"", time2 - time1);
141       INFO1("Processing \"%s\"", MSG_task_get_name(task));
142       a = MSG_task_execute(task);
143       if (a == MSG_OK) {
144         INFO1("\"%s\" done", MSG_task_get_name(task));
145         free(task->data);
146         MSG_task_destroy(task);
147       } else if (a == MSG_HOST_FAILURE) {
148         INFO0
149           ("Gloups. The cpu on which I'm running just turned off!. See you!");
150         return 0;
151       } else {
152         INFO0("Hey ?! What's up ? ");
153         xbt_assert0(0, "Unexpected behavior");
154       }
155     } else if (a == MSG_HOST_FAILURE) {
156       INFO0
157         ("Gloups. The cpu on which I'm running just turned off!. See you!");
158       return 0;
159     } else if (a == MSG_TRANSFER_FAILURE) {
160       INFO0("Mmh. Something went wrong. Nevermind. Let's keep going!");
161     } else {
162       INFO0("Hey ?! What's up ? ");
163       xbt_assert0(0, "Unexpected behavior");
164     }
165   }
166   INFO0("I'm done. See you!");
167   return 0;
168 }                               /* end_of_slave */
169
170 /** Test function */
171 MSG_error_t test_all(const char *platform_file, const char *application_file)
172 {
173   MSG_error_t res = MSG_OK;
174
175   /* MSG_config("workstation_model","KCCFLN05"); */
176   {                             /*  Simulation setting */
177     MSG_set_channel_number(MAX_CHANNEL);
178     MSG_paje_output("msg_test.trace");
179     MSG_create_environment(platform_file);
180   }
181   {                             /*   Application deployment */
182     MSG_function_register("master", master);
183     MSG_function_register("slave", slave);
184     MSG_launch_application(application_file);
185   }
186   res = MSG_main();
187
188   INFO1("Simulation time %g", MSG_get_clock());
189   return res;
190 }                               /* end_of_test_all */
191
192
193 /** Main function */
194 int main(int argc, char *argv[])
195 {
196   MSG_error_t res = MSG_OK;
197
198   MSG_global_init(&argc, argv);
199   if (argc < 3) {
200     printf("Usage: %s platform_file deployment_file\n", argv[0]);
201     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
202     exit(1);
203   }
204   res = test_all(argv[1], argv[2]);
205   MSG_clean();
206
207   if (res == MSG_OK)
208     return 0;
209   else
210     return 1;
211 }                               /* end_of_main */