Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c5c9d18e8b7da5782d440fb8e53920ef05bbf459
[simgrid.git] / testsuite / msg / msg_test.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2004,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 /** \file msg_test.c 
9  *  \brief Test program for msg.
10 */
11
12 #include "msg/msg.h"
13
14 /** This flag enable the debugging messages from PRINT_DEBUG_MESSAGE() */
15 #define VERBOSE
16 #include "messages.h"
17
18 int unix_emitter(int argc, char *argv[]);
19 int unix_receiver(int argc, char *argv[]);
20 int unix_forwarder(int argc, char *argv[]);
21 void test_all(const char *platform_file, const char *application_file, double sharing);
22
23
24 /** The names of the channels we will use in this simulation. There is
25     only one channel identified by the name PORT_22. */
26 typedef enum {
27   PORT_22 = 0,
28   MAX_CHANNEL
29 } channel_t;
30
31 void print_args(int argc, char** argv);
32 void print_args(int argc, char** argv)
33 {
34   int i ; 
35
36   fprintf(stderr,"<");
37   for(i=0; i<argc; i++) 
38     fprintf(stderr,"%s ",argv[i]);
39   fprintf(stderr,">\n");
40 }
41
42 /** The number of task each slave will process */
43 #define NB_TASK 3
44 int unix_emitter(int argc, char *argv[])
45 {
46   int slaves_count = 0;
47   m_host_t *slaves = NULL;
48   int todo_count = 0;
49   m_task_t *todo = NULL;
50
51   int i;
52   PRINT_MESSAGE("Hello !");
53   print_args(argc,argv);
54
55   {                  /* Process organisation */
56     slaves_count = argc - 1;
57     slaves = calloc(slaves_count, sizeof(m_host_t));
58     
59     for (i = 1; i < argc; i++) {
60       slaves[i-1] = MSG_get_host_by_name(argv[i]);
61       if(slaves[i-1]==NULL) {
62         PRINT_MESSAGE("Unknown host %s. Stopping Now! \n", argv[i]);
63         abort();
64       }
65     }
66   }
67
68   {                  /*  Task creation */
69     char sprintf_buffer[64];
70     int slave  = slaves_count;
71
72     todo = calloc(NB_TASK * slave, sizeof(m_task_t));
73     todo_count = NB_TASK * slave;
74
75     for (i = 0; i < NB_TASK * slave; i++) {
76       sprintf(sprintf_buffer, "Task_%d", i);
77       todo[i] = MSG_task_create(sprintf_buffer, 5000, 10, NULL);
78     }
79   }
80
81   PRINT_MESSAGE("Got %d slave(s) :\n", slaves_count);
82   for (i = 0; i < slaves_count; i++)
83     PRINT_MESSAGE("\t %s\n", slaves[i]->name);
84
85   PRINT_MESSAGE("Got %d task to process :\n", todo_count);
86
87   for (i = 0; i < todo_count; i++)
88     PRINT_MESSAGE("\t\"%s\"\n", todo[i]->name);
89
90   for (i = 0; i < todo_count; i++) {
91     PRINT_MESSAGE("Sending \"%s\" to \"%s\"\n",
92                   todo[i]->name,
93                   slaves[i % slaves_count]->name);
94     MSG_task_put(todo[i], slaves[i % slaves_count],
95                  PORT_22);
96     PRINT_MESSAGE("Send completed\n");
97   }
98   
99   free(slaves);
100   free(todo);
101   return 0;
102 }
103
104 int unix_receiver(int argc, char *argv[])
105 {
106   int todo_count = 0;
107   m_task_t *todo = (m_task_t *) calloc(NB_TASK, sizeof(m_task_t));
108   int i;
109
110   PRINT_MESSAGE("Hello !");
111   print_args(argc,argv);
112
113   for (i = 0; i < NB_TASK;) {
114     int a;
115     PRINT_MESSAGE("Awaiting Task %d \n", i);
116     a = MSG_task_get(&(todo[i]), PORT_22);
117     if (a == MSG_OK) {
118       todo_count++;
119       PRINT_MESSAGE("Received \"%s\" \n", todo[i]->name);
120       PRINT_MESSAGE("Processing \"%s\" \n", todo[i]->name);
121       MSG_task_execute(todo[i]);
122       PRINT_MESSAGE("\"%s\" done \n", todo[i]->name);
123       MSG_task_destroy(todo[i]);
124       i++;
125     } else {
126       PRINT_MESSAGE("Hey ?! What's up ? \n");
127       DIE("Unexpected behaviour");
128     }
129   }
130   free(todo);
131   PRINT_MESSAGE("I'm done. See you!\n");
132   return 0;
133 }
134
135
136 int unix_forwarder(int argc, char *argv[])
137 {
138   int todo_count = 0;
139   m_task_t *todo = (m_task_t *) calloc(NB_TASK, sizeof(m_task_t));
140   int i;
141
142   PRINT_MESSAGE("Hello !");
143   print_args(argc,argv);
144
145   for (i = 0; i < NB_TASK;) {
146     int a;
147     PRINT_MESSAGE("Awaiting Task %d \n", i);
148     a = MSG_task_get(&(todo[i]), PORT_22);
149     if (a == MSG_OK) {
150       todo_count++;
151       PRINT_MESSAGE("Received \"%s\" \n", todo[i]->name);
152       PRINT_MESSAGE("Processing \"%s\" \n", todo[i]->name);
153       MSG_task_execute(todo[i]);
154       PRINT_MESSAGE("\"%s\" done \n", todo[i]->name);
155       MSG_task_destroy(todo[i]);
156       i++;
157     } else {
158       PRINT_MESSAGE("Hey ?! What's up ? \n");
159       DIE("Unexpected behaviour");
160     }
161   }
162   free(todo);
163   PRINT_MESSAGE("I'm done. See you!\n");
164   return 0;
165 }
166
167
168 void test_all(const char *platform_file,const char *application_file, double sharing)
169 {
170   {                             /*  Simulation setting */
171     MSG_global_init();
172     MSG_set_verbosity(MSG_SILENT);
173     MSG_set_channel_number(MAX_CHANNEL);
174     if(sharing<=0) {
175       MSG_set_sharing_policy(MSG_TCP,.1);
176     } else {
177       MSG_set_sharing_policy(MSG_STORE_AND_FORWARD,sharing);
178     }
179     MSG_create_environment(platform_file);
180   }
181   {                            /*   Application deployment */
182     MSG_function_register("master", unix_emitter);
183     MSG_function_register("slave", unix_receiver);
184     MSG_launch_application(application_file);
185   }
186   MSG_main();
187   printf("Simulation time %Lg\n",MSG_getClock());
188   MSG_clean();
189 }
190
191 int main(int argc, char *argv[])
192 {
193   test_all("msg_platform.xml","msg_deployment.xml",-.1);
194 /*   test_all("msg_platform.txt","msg_deployment.txt",.1); */
195   return (0);
196 }