Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] update examples with the new API for setting task category
[simgrid.git] / examples / msg / ns3 / ns3.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "msg/msg.h"
10 #include "xbt/log.h"
11 #include "xbt/asserts.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16
17 /** @addtogroup MSG_examples
18  * 
19  *  @section MSG_ex_models Models-related examples
20  * 
21  *  @subsection MSG_ex_PLS Packet level simulators
22  * 
23  *  These examples demonstrate how to use the bindings to classical
24  *  Packet-Level Simulators (PLS), as explained in \ref pls. The most
25  *  interesting is probably not the C files since they are unchanged
26  *  from the other simulations, but the associated files, such as the
27  *  platform files to see how to declare a platform to be used with
28  *  the PLS bindings of SimGrid and the tesh files to see how to
29  *  actually start a simulation in these settings.
30  * 
31  * - <b>ns3</b>: Simple ping-pong using ns3 instead of the SimGrid network models. 
32  * 
33  */
34
35 int master(int argc, char *argv[]);
36 int slave(int argc, char *argv[]);
37 int timer(int argc, char *argv[]);
38 MSG_error_t test_all(const char *platform_file,
39                      const char *application_file);
40
41 int timer_start; //set as 1 in the master process
42
43 //keep a pointer to all surf running tasks.
44 #define NTASKS 1500
45 int bool_printed = 0;
46 double start_time, end_time, elapsed_time;
47 double gl_data_size[NTASKS];
48 m_task_t gl_task_array[NTASKS];
49 const char *slavenames[NTASKS];
50 const char *masternames[NTASKS];
51 int gl_task_array_id = 0;
52 int count_finished = 0;
53
54 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
55
56 /** master */
57 int master(int argc, char *argv[])
58 {
59   char *slavename = NULL;
60   double task_comm_size = 0;
61   m_task_t todo;
62   char id_alias[10];
63   //unique id to control statistics
64   int id = -1;
65
66   xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
67
68   XBT_DEBUG ("Master started");
69
70   /* data size */
71   int read;
72   read = sscanf(argv[1], "%lg", &task_comm_size);
73   xbt_assert(read, "Invalid argument %s\n", argv[1]);
74
75   /* slave name */
76   slavename = argv[2];
77   id = atoi(argv[3]);
78   sprintf(id_alias, "flow_%d", id);
79   slavenames[id] = slavename;
80   TRACE_category(id_alias);
81
82   masternames[id] = MSG_host_get_name(MSG_host_self());
83
84   {                             /*  Task creation.  */
85     char sprintf_buffer[64] = "Task_0";
86     todo = MSG_task_create(sprintf_buffer, 100*task_comm_size, task_comm_size, NULL);
87     MSG_task_set_category(todo, id_alias);
88     //keep track of running tasks
89     gl_task_array[id] = todo;
90     gl_data_size[id] = task_comm_size;
91   }
92
93   {                             /* Process organisation */
94     MSG_get_host_by_name(slavename);
95   }
96
97   count_finished++;
98   timer_start = 1 ;
99
100   /* time measurement */
101   sprintf(id_alias, "%d", id);
102   start_time = MSG_get_clock();
103   //MSG_task_execute(todo);
104   MSG_task_send(todo, id_alias);
105   end_time = MSG_get_clock();
106
107   XBT_DEBUG ("Finished");
108   return 0;
109 }                               /* end_of_master */
110
111
112 /** Timer function  */
113 int timer(int argc, char *argv[])
114 {
115   double sleep_time;
116   double first_sleep;
117
118   xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
119
120   sscanf(argv[1], "%lf", &first_sleep);
121   sscanf(argv[2], "%lf", &sleep_time);
122
123   XBT_DEBUG ("Timer started");
124
125   if(first_sleep){
126       MSG_process_sleep(first_sleep);
127   }
128
129   do {
130     XBT_DEBUG ("Get sleep");
131       MSG_process_sleep(sleep_time);
132   } while(timer_start);
133
134   XBT_DEBUG ("Finished");
135   return 0;
136 }
137
138 /** Receiver function  */
139 int slave(int argc, char *argv[])
140 {
141
142   m_task_t task = NULL;
143   int a = MSG_OK;
144   int id = 0;
145   char id_alias[10];
146
147   xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
148
149   XBT_DEBUG ("Slave started");
150
151   id = atoi(argv[1]);
152   sprintf(id_alias, "%d", id);
153
154   a = MSG_task_receive(&(task), id_alias);
155
156   count_finished--;
157   if(count_finished == 0){
158       timer_start = 0;
159   }
160
161
162
163   if (a != MSG_OK) {
164     XBT_INFO("Hey?! What's up?");
165     xbt_die("Unexpected behavior.");
166   }
167
168   elapsed_time = MSG_get_clock() - start_time;
169   
170   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s",
171                   id,
172                   MSG_task_get_data_size(task),
173        masternames[id],
174        slavenames[id]);
175 //  MSG_task_execute(task);
176   MSG_task_destroy(task);
177
178   XBT_DEBUG ("Finished");
179   return 0;
180 }                               /* end_of_slave */
181
182 /** Test function */
183 MSG_error_t test_all(const char *platform_file,
184                      const char *application_file)
185 {
186   MSG_error_t res = MSG_OK;
187
188   /* MSG_config("workstation/model", "GTNETS"); */
189   /* MSG_config("workstation/model","KCCFLN05"); */
190   {                             /*  Simulation setting */
191     MSG_create_environment(platform_file);
192   }
193
194   TRACE_declare_mark("endmark");
195
196   {                             /*   Application deployment */
197     MSG_function_register("master", master);
198     MSG_function_register("slave", slave);
199     MSG_function_register("timer", timer);
200
201     MSG_launch_application(application_file);
202   }
203   res = MSG_main();
204   return res;
205 }                               /* end_of_test_all */
206
207 /** Main function */
208 int main(int argc, char *argv[])
209 {
210   MSG_error_t res = MSG_OK;
211   bool_printed = 0;
212
213   MSG_global_init(&argc, argv);
214   if (argc < 3) {
215     printf("Usage: %s platform_file deployment_file\n", argv[0]);
216     exit(1);
217   }
218
219   res = test_all(argv[1], argv[2]);
220
221   MSG_clean();
222
223   if (res == MSG_OK)
224     return 0;
225   else
226     return 1;
227 }                               /* end_of_main */