Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
update the double extern declaration bis
[simgrid.git] / examples / msg / msg_test.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 "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
15
16 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 int forwarder(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
20
21 typedef enum {
22   PORT_22 = 0,
23   MAX_CHANNEL
24 } channel_t;
25
26 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
27
28 /** Emitter function  */
29 int master(int argc, char *argv[])
30 {
31   int slaves_count = 0;
32   m_host_t *slaves = NULL;
33   m_task_t *todo = 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   {                  /*  Task creation */
49     char sprintf_buffer[64];
50
51     todo = calloc(number_of_tasks, sizeof(m_task_t));
52
53     for (i = 0; i < number_of_tasks; i++) {
54       sprintf(sprintf_buffer, "Task_%d", i);
55       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
56     }
57   }
58
59   {                  /* Process organisation */
60     slaves_count = argc - 4;
61     slaves = calloc(slaves_count, sizeof(m_host_t));
62     
63     for (i = 4; i < argc; i++) {
64       slaves[i-4] = MSG_get_host_by_name(argv[i]);
65       if(slaves[i-4]==NULL) {
66         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
67         abort();
68       }
69     }
70   }
71
72   INFO1("Got %d slave(s) :", slaves_count);
73   for (i = 0; i < slaves_count; i++)
74     INFO1("\t %s", slaves[i]->name);
75
76   INFO1("Got %d task to process :", number_of_tasks);
77
78   for (i = 0; i < number_of_tasks; i++)
79     INFO1("\t\"%s\"", todo[i]->name);
80
81   for (i = 0; i < number_of_tasks; i++) {
82     INFO2("Sending \"%s\" to \"%s\"",
83                   todo[i]->name,
84                   slaves[i % slaves_count]->name);
85     if(MSG_host_self()==slaves[i % slaves_count]) {
86       INFO0("Hey ! It's me ! :)");
87     }
88
89     MSG_task_put(todo[i], slaves[i % slaves_count],
90                  PORT_22);
91     INFO0("Send completed");
92   }
93   
94   INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
95   for (i = 0; i < slaves_count; i++) 
96     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
97                  slaves[i], PORT_22);
98   
99   INFO0("Goodbye now!");
100   free(slaves);
101   free(todo);
102   return 0;
103 } /* end_of_master */
104
105 /** Receiver function  */
106 int slave(int argc, char *argv[])
107 {
108   while(1) {
109     m_task_t task = NULL;
110     int a;
111     a = MSG_task_get(&(task), PORT_22);
112     if (a == MSG_OK) {
113       INFO1("Received \"%s\" ", MSG_task_get_name(task));
114       if(MSG_task_get_data(task)==FINALIZE) {
115         MSG_task_destroy(task);
116         break;
117       }
118       INFO1("Processing \"%s\" ", MSG_task_get_name(task));
119       MSG_task_execute(task);
120       INFO1("\"%s\" done ", MSG_task_get_name(task));
121       MSG_task_destroy(task);
122     } else {
123       INFO0("Hey ?! What's up ? ");
124       xbt_assert0(0,"Unexpected behavior");
125     }
126   }
127   INFO0("I'm done. See you!");
128   return 0;
129 } /* end_of_slave */
130
131 /** Forwarder function */
132 int forwarder(int argc, char *argv[])
133 {
134   int i;
135   int slaves_count;
136   m_host_t *slaves;
137
138   {                  /* Process organisation */
139     slaves_count = argc - 1;
140     slaves = calloc(slaves_count, sizeof(m_host_t));
141     
142     for (i = 1; i < argc; i++) {
143       slaves[i-1] = MSG_get_host_by_name(argv[i]);
144       if(slaves[i-1]==NULL) {
145         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
146         abort();
147       }
148     }
149   }
150
151   i=0;
152   while(1) {
153     m_task_t task = NULL;
154     int a;
155     a = MSG_task_get(&(task), PORT_22);
156     if (a == MSG_OK) {
157       INFO1("Received \"%s\" ", MSG_task_get_name(task));
158       if(MSG_task_get_data(task)==FINALIZE) {
159         INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
160         for (i = 0; i < slaves_count; i++) 
161           MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
162                        slaves[i], PORT_22);
163         MSG_task_destroy(task);
164         break;
165       }
166       INFO2("Sending \"%s\" to \"%s\"",
167                     MSG_task_get_name(task),
168                     slaves[i% slaves_count]->name);
169       MSG_task_put(task, slaves[i % slaves_count],
170                    PORT_22);
171       i++;
172     } else {
173       INFO0("Hey ?! What's up ? ");
174       xbt_assert0(0,"Unexpected behavior");
175     }
176   }
177
178   INFO0("I'm done. See you!");
179   return 0;
180 } /* end_of_forwarder */
181
182 /** Test function */
183 MSG_error_t test_all(const char *platform_file,
184                             const char *application_file)
185 {
186   MSG_error_t res = MSG_OK;
187
188   /* MSG_config("surf_workstation_model","KCCFLN05"); */
189   {                             /*  Simulation setting */
190     MSG_set_channel_number(MAX_CHANNEL);
191     MSG_paje_output("msg_test.trace");
192     MSG_create_environment(platform_file);
193   }
194   {                            /*   Application deployment */
195     MSG_function_register("master", master);
196     MSG_function_register("slave", slave);
197     MSG_function_register("forwarder", forwarder);
198     MSG_launch_application(application_file);
199   }
200   res = MSG_main();
201   
202   INFO1("Simulation time %g",MSG_get_clock());
203   return res;
204 } /* end_of_test_all */
205
206
207 /** Main function */
208 int main(int argc, char *argv[])
209 {
210   MSG_error_t res = MSG_OK;
211
212   MSG_global_init(&argc,argv);
213   if (argc < 3) {
214      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
215      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
216      exit(1);
217   }
218   res = test_all(argv[1],argv[2]);
219   MSG_clean();
220
221   if(res==MSG_OK) return 0; 
222   else return 1;
223 } /* end_of_main */