Logo AND Algorithmique Numérique Distribuée

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