Logo AND Algorithmique Numérique Distribuée

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