Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Uniformize the test_{sg,rl} scripts, rename the wine variable to exenv (excution...
[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     INFO1("\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     INFO1("\t\"%s\"", todo[i]->name);
91
92   for (i = 0; i < number_of_tasks; i++) {
93     INFO2("Sending \"%s\" to \"%s\"",
94                   todo[i]->name,
95                   slaves[i % slaves_count]->name);
96     if(MSG_host_self()==slaves[i % slaves_count]) {
97       INFO0("Hey ! It's me ! :)");
98     }
99     MSG_task_put(todo[i], slaves[i % slaves_count],
100                  PORT_22);
101     INFO0("Send completed");
102   }
103   
104   INFO0("All tasks have been dispatched. Bye!");
105   free(slaves);
106   free(todo);
107   return 0;
108 } /* end_of_master */
109
110 /** Receiver function  */
111 int slave(int argc, char *argv[])
112 {
113   print_args(argc,argv);
114
115   while(1) {
116     m_task_t task = NULL;
117     int a;
118     a = MSG_task_get(&(task), PORT_22);
119     if (a == MSG_OK) {
120       INFO1("Received \"%s\" ", task->name);
121       INFO1("Processing \"%s\" ", task->name);
122       MSG_task_execute(task);
123       INFO1("\"%s\" done ", task->name);
124       MSG_task_destroy(task);
125     } else {
126       INFO0("Hey ?! What's up ? ");
127       xbt_assert0(0,"Unexpected behaviour");
128     }
129   }
130   INFO0("I'm done. See you!");
131   return 0;
132 } /* end_of_slave */
133
134 /** Receiver function */
135 int forwarder(int argc, char *argv[])
136 {
137   int i;
138   int slaves_count = argc - 1;
139   m_host_t *slaves = calloc(slaves_count, sizeof(m_host_t));
140
141   print_args(argc,argv);
142
143   {                  /* Process organisation */
144     slaves_count = argc - 1;
145     slaves = calloc(slaves_count, sizeof(m_host_t));
146     
147     for (i = 1; i < argc; i++) {
148       slaves[i-1] = MSG_get_host_by_name(argv[i]);
149       if(slaves[i-1]==NULL) {
150         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
151         abort();
152       }
153     }
154   }
155
156   i=0;
157   while(1) {
158     m_task_t task = NULL;
159     int a;
160     a = MSG_task_get(&(task), PORT_22);
161     if (a == MSG_OK) {
162       INFO1("Received \"%s\" ", task->name);
163       INFO2("Sending \"%s\" to \"%s\"",
164                     task->name,
165                     slaves[i % slaves_count]->name);
166       MSG_task_put(task, slaves[i % slaves_count],
167                    PORT_22);
168     } else {
169       INFO0("Hey ?! What's up ? ");
170       xbt_assert0(0,"Unexpected behaviour");
171     }
172   }
173
174   INFO0("I'm done. See you!");
175   return 0;
176 } /* end_of_forwarder */
177
178
179 /** Test function */
180 void test_all(const char *platform_file,const char *application_file)
181 {
182
183   /* MSG_config("surf_workstation_model","KCCFLN05"); */
184   {                             /*  Simulation setting */
185     MSG_set_channel_number(MAX_CHANNEL);
186     MSG_paje_output("msg_test.trace");
187     MSG_create_environment(platform_file);
188   }
189   {                            /*   Application deployment */
190     MSG_function_register("master", master);
191     MSG_function_register("slave", slave);
192     MSG_function_register("forwarder", forwarder);
193     MSG_launch_application(application_file);
194   }
195   MSG_main();
196   
197   INFO1("Simulation time %g",MSG_getClock());
198 } /* end_of_test_all */
199
200
201 /** Main function */
202 int main(int argc, char *argv[])
203 {
204   MSG_global_init_args(&argc,argv);
205   if (argc < 3) {
206      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
207      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
208      exit(1);
209   }
210   test_all(argv[1],argv[2]);
211   MSG_clean();
212   return (0);
213 } /* end_of_main */