Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e7f54da8129b55d4c7bf64b3d1e7c98e83d6659
[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
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
13
14 int master(int argc, char *argv[]);
15 int slave(int argc, char *argv[]);
16 int forwarder(int argc, char *argv[]);
17 void test_all(const char *platform_file, const char *application_file);
18
19 typedef enum {
20   PORT_22 = 0,
21   MAX_CHANNEL
22 } channel_t;
23
24 /* This function is just used so that users can check that each process
25  *  has received the arguments it was supposed to receive.
26  */
27 static void print_args(int argc, char** argv)
28 {
29   int i ; 
30
31   fprintf(stderr,"<");
32   for(i=0; i<argc; i++) 
33     fprintf(stderr,"%s ",argv[i]);
34   fprintf(stderr,">\n");
35 }
36
37 /** Emitter function  */
38 int master(int argc, char *argv[])
39 {
40   int slaves_count = 0;
41   m_host_t *slaves = NULL;
42   m_task_t *todo = NULL;
43   int number_of_tasks = 0;
44   double task_comp_size = 0;
45   double task_comm_size = 0;
46
47
48   int i;
49
50   print_args(argc,argv);
51
52   xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
53          "Invalid argument %s\n",argv[1]);
54   xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
55          "Invalid argument %s\n",argv[2]);
56   xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
57          "Invalid argument %s\n",argv[3]);
58
59   {                  /*  Task creation */
60     char sprintf_buffer[64];
61
62     todo = calloc(number_of_tasks, sizeof(m_task_t));
63
64     for (i = 0; i < number_of_tasks; i++) {
65       sprintf(sprintf_buffer, "Task_%d", i);
66       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
67     }
68   }
69
70   {                  /* Process organisation */
71     slaves_count = argc - 4;
72     slaves = calloc(slaves_count, sizeof(m_host_t));
73     
74     for (i = 4; i < argc; i++) {
75       slaves[i-4] = MSG_get_host_by_name(argv[i]);
76       if(slaves[i-4]==NULL) {
77         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
78         abort();
79       }
80     }
81   }
82
83   INFO1("Got %d slave(s) :", slaves_count);
84   for (i = 0; i < slaves_count; i++)
85     VERB1("\t %s", slaves[i]->name);
86
87   INFO1("Got %d task to process :", number_of_tasks);
88
89   for (i = 0; i < number_of_tasks; i++)
90     VERB1("\t\"%s\"", todo[i]->name);
91
92   for (i = 0; i < number_of_tasks; i++) {
93     VERB2("Sending \"%s\" to \"%s\"",
94                   todo[i]->name,
95                   slaves[i % slaves_count]->name);
96     MSG_task_put(todo[i], slaves[i % slaves_count],
97                  PORT_22);
98     DEBUG0("Send completed");
99   }
100   
101   INFO0("All tasks have been dispatched. Bye!");
102   free(slaves);
103   free(todo);
104   return 0;
105 } /* end_of_master */
106
107 /** Receiver function  */
108 int slave(int argc, char *argv[])
109 {
110   print_args(argc,argv);
111
112   while(1) {
113     m_task_t task = NULL;
114     int a;
115     a = MSG_task_get(&(task), PORT_22);
116     if (a == MSG_OK) {
117       INFO1("Received \"%s\" ", task->name);
118       DEBUG1("Processing \"%s\" ", task->name);
119       MSG_task_execute(task);
120       VERB1("\"%s\" done ", task->name);
121       MSG_task_destroy(task);
122     } else {
123       INFO0("Hey ?! What's up ? ");
124       xbt_assert0(0,"Unexpected behaviour");
125     }
126   }
127   INFO0("I'm done. See you!");
128   return 0;
129 } /* end_of_slave */
130
131 /** Receiver function */
132 int forwarder(int argc, char *argv[])
133 {
134   int i;
135   int slaves_count = argc - 1;
136   m_host_t *slaves = calloc(slaves_count, sizeof(m_host_t));
137
138   print_args(argc,argv);
139
140   {                  /* Process organisation */
141     slaves_count = argc - 1;
142     slaves = calloc(slaves_count, sizeof(m_host_t));
143     
144     for (i = 1; i < argc; i++) {
145       slaves[i-1] = MSG_get_host_by_name(argv[i]);
146       if(slaves[i-1]==NULL) {
147         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
148         abort();
149       }
150     }
151   }
152
153   i=0;
154   while(1) {
155     m_task_t task = NULL;
156     int a;
157     a = MSG_task_get(&(task), PORT_22);
158     if (a == MSG_OK) {
159       INFO1("Received \"%s\" ", task->name);
160       INFO2("Sending \"%s\" to \"%s\"",
161                     task->name,
162                     slaves[i % slaves_count]->name);
163       MSG_task_put(task, slaves[i % slaves_count],
164                    PORT_22);
165     } else {
166       INFO0("Hey ?! What's up ? ");
167       xbt_assert0(0,"Unexpected behaviour");
168     }
169   }
170
171   INFO0("I'm done. See you!");
172   return 0;
173 } /* end_of_forwarder */
174
175
176 /** Test function */
177 void test_all(const char *platform_file,const char *application_file)
178 {
179   {                             /*  Simulation setting */
180     MSG_set_channel_number(MAX_CHANNEL);
181     MSG_paje_output("msg_test.trace");
182     MSG_create_environment(platform_file);
183   }
184   {                            /*   Application deployment */
185     MSG_function_register("master", master);
186     MSG_function_register("slave", slave);
187     MSG_function_register("forwarder", forwarder);
188     MSG_launch_application(application_file);
189   }
190   MSG_main();
191   
192   INFO1("Simulation time %g",MSG_getClock());
193 } /* end_of_test_all */
194
195
196 /** Main function */
197 int main(int argc, char *argv[])
198 {
199   MSG_global_init_args(&argc,argv);
200   if (argc < 3) {
201      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
202      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
203      exit(1);
204   }
205   test_all(argv[1],argv[2]);
206   MSG_clean();
207   return (0);
208 } /* end_of_main */