Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
let's be friendly with prehistoric compliers
[simgrid.git] / testsuite / 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"
9
10 #define VERBOSE
11 #include "messages.h"
12
13 int master(int argc, char *argv[]);
14 int slave(int argc, char *argv[]);
15 int forwarder(int argc, char *argv[]);
16 void test_all(const char *platform_file, const char *application_file);
17
18
19 typedef enum {
20   PORT_22 = 0,
21   MAX_CHANNEL
22 } channel_t;
23
24 void print_args(int argc, char** argv);
25 void print_args(int argc, char** argv)
26 {
27   int i ; 
28
29   fprintf(stderr,"<");
30   for(i=0; i<argc; i++) 
31     fprintf(stderr,"%s ",argv[i]);
32   fprintf(stderr,">\n");
33 }
34
35 int master(int argc, char *argv[])
36 {
37   int slaves_count = 0;
38   m_host_t *slaves = NULL;
39   m_task_t *todo = NULL;
40   int number_of_tasks = 0;
41   double task_comp_size = 0;
42   double task_comm_size = 0;
43
44
45   int i;
46
47   print_args(argc,argv);
48
49   ASSERT(sscanf(argv[1],"%d", &number_of_tasks),
50          "Invalid argument %s\n",argv[1]);
51   ASSERT(sscanf(argv[2],"%lg", &task_comp_size),
52          "Invalid argument %s\n",argv[2]);
53   ASSERT(sscanf(argv[3],"%lg", &task_comm_size),
54          "Invalid argument %s\n",argv[3]);
55
56   {                  /*  Task creation */
57     char sprintf_buffer[64];
58
59     todo = xbt_new0(m_task_t,number_of_tasks);
60
61     for (i = 0; i < number_of_tasks; i++) {
62       sprintf(sprintf_buffer, "Task_%d", i);
63       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
64     }
65   }
66
67   {                  /* Process organisation */
68     slaves_count = argc - 4;
69     slaves = calloc(slaves_count, sizeof(m_host_t));
70     
71     for (i = 4; i < argc; i++) {
72       slaves[i-4] = MSG_get_host_by_name(argv[i]);
73       if(slaves[i-4]==NULL) {
74         PRINT_MESSAGE("Unknown host %s. Stopping Now! \n", argv[i]);
75         abort();
76       }
77     }
78   }
79
80   PRINT_MESSAGE("Got %d slave(s) :\n", slaves_count);
81   for (i = 0; i < slaves_count; i++)
82     PRINT_MESSAGE("\t %s\n", slaves[i]->name);
83
84   PRINT_MESSAGE("Got %d task to process :\n", number_of_tasks);
85
86   for (i = 0; i < number_of_tasks; i++)
87     PRINT_MESSAGE("\t\"%s\"\n", todo[i]->name);
88
89   for (i = 0; i < number_of_tasks; i++) {
90     PRINT_MESSAGE("Sending \"%s\" to \"%s\"\n",
91                   todo[i]->name,
92                   slaves[i % slaves_count]->name);
93     MSG_task_put(todo[i], slaves[i % slaves_count],
94                  PORT_22);
95     PRINT_MESSAGE("Send completed\n");
96   }
97   
98   PRINT_MESSAGE("All tasks have been dispatched. Bye!\n");
99   free(slaves);
100   free(todo);
101   return 0;
102 }
103
104 int slave(int argc, char *argv[])
105 {
106   print_args(argc,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       PRINT_MESSAGE("Received \"%s\" \n", task->name);
114       PRINT_MESSAGE("Processing \"%s\" \n", task->name);
115       MSG_task_execute(task);
116       PRINT_MESSAGE("\"%s\" done \n", task->name);
117       MSG_task_destroy(task);
118     } else {
119       PRINT_MESSAGE("Hey ?! What's up ? \n");
120       DIE("Unexpected behaviour");
121     }
122   }
123   PRINT_MESSAGE("I'm done. See you!\n");
124   return 0;
125 }
126
127
128 int forwarder(int argc, char *argv[])
129 {
130   int i;
131   int slaves_count = argc - 1;
132   m_host_t *slaves = calloc(slaves_count, sizeof(m_host_t));
133
134   print_args(argc,argv);
135
136   {                  /* Process organisation */
137     slaves_count = argc - 1;
138     slaves = calloc(slaves_count, sizeof(m_host_t));
139     
140     for (i = 1; i < argc; i++) {
141       slaves[i-1] = MSG_get_host_by_name(argv[i]);
142       if(slaves[i-1]==NULL) {
143         PRINT_MESSAGE("Unknown host %s. Stopping Now! \n", argv[i]);
144         abort();
145       }
146     }
147   }
148
149
150   while(1) {
151     m_task_t task = NULL;
152     int a;
153     a = MSG_task_get(&(task), PORT_22);
154     if (a == MSG_OK) {
155       PRINT_MESSAGE("Received \"%s\" \n", task->name);
156       PRINT_MESSAGE("Sending \"%s\" to \"%s\"\n",
157                     task->name,
158                     slaves[i % slaves_count]->name);
159       MSG_task_put(task, slaves[i % slaves_count],
160                    PORT_22);
161     } else {
162       PRINT_MESSAGE("Hey ?! What's up ? \n");
163       DIE("Unexpected behaviour");
164     }
165   }
166
167   PRINT_MESSAGE("I'm done. See you!\n");
168   return 0;
169 }
170
171
172 void test_all(const char *platform_file,const char *application_file)
173 {
174   {                             /*  Simulation setting */
175     MSG_set_channel_number(MAX_CHANNEL);
176     MSG_create_environment(platform_file);
177   }
178   {                            /*   Application deployment */
179     MSG_function_register("master", master);
180     MSG_function_register("slave", slave);
181     MSG_function_register("forwarder", forwarder);
182     MSG_launch_application(application_file);
183   }
184   MSG_main();
185   printf("Simulation time %g\n",MSG_getClock());
186 }
187
188 int main(int argc, char *argv[])
189 {
190   MSG_global_init_args(&argc,argv);
191   test_all("msg_platform.xml","msg_deployment.xml");
192   MSG_clean();
193   return (0);
194 }