Logo AND Algorithmique Numérique Distribuée

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