Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #2 from mquinson/master
[simgrid.git] / examples / msg / ns3 / ns3.c
1 /* Copyright (c) 2007-2015. 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 "simgrid/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 msg_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   msg_task_t todo;
60
61   xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
62
63   XBT_DEBUG ("Master started");
64
65   /* data size */
66   double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
67
68   /* slave name */
69   char *slavename = argv[2];
70   int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s");   //unique id to control statistics
71   char *id_alias = bprintf("flow_%d", id);
72   slavenames[id] = slavename;
73   TRACE_category(id_alias);
74
75   masternames[id] = MSG_host_get_name(MSG_host_self());
76
77   {                             /*  Task creation.  */
78     todo = MSG_task_create("Task_0", 100*task_comm_size, task_comm_size, NULL);
79     MSG_task_set_category(todo, id_alias);
80     //keep track of running tasks
81     gl_task_array[id] = todo;
82     gl_data_size[id] = task_comm_size;
83   }
84
85   {                             /* Process organization */
86     MSG_host_by_name(slavename);
87   }
88
89   count_finished++;
90   timer_start = 1 ;
91
92   /* time measurement */
93   sprintf(id_alias, "%d", id);
94   start_time = MSG_get_clock();
95   MSG_task_send(todo, id_alias);
96   end_time = MSG_get_clock();
97
98   XBT_DEBUG ("Finished");
99   xbt_free(id_alias);
100   return 0;
101 }                               /* end_of_master */
102
103
104 /** Timer function  */
105 int timer(int argc, char *argv[])
106 {
107   double sleep_time;
108   double first_sleep;
109
110   xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
111
112   sscanf(argv[1], "%lf", &first_sleep);
113   sscanf(argv[2], "%lf", &sleep_time);
114
115   XBT_DEBUG ("Timer started");
116
117   if(first_sleep){
118       MSG_process_sleep(first_sleep);
119   }
120
121   do {
122     XBT_DEBUG ("Get sleep");
123       MSG_process_sleep(sleep_time);
124   } while(timer_start);
125
126   XBT_DEBUG ("Finished");
127   return 0;
128 }
129
130 /** Receiver function  */
131 int slave(int argc, char *argv[])
132 {
133
134   msg_task_t task = NULL;
135   int a = MSG_OK;
136   int id = 0;
137   char id_alias[10];
138
139   xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
140
141   XBT_DEBUG ("Slave started");
142
143   id = xbt_str_parse_int(argv[1], "Invalid id: %s");
144   sprintf(id_alias, "%d", id);
145
146   a = MSG_task_receive(&(task), id_alias);
147
148   count_finished--;
149   if(count_finished == 0){
150       timer_start = 0;
151   }
152
153
154
155   if (a != MSG_OK) {
156     XBT_INFO("Hey?! What's up?");
157     xbt_die("Unexpected behavior.");
158   }
159
160   elapsed_time = MSG_get_clock() - start_time;
161   
162   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s",
163       id,
164       MSG_task_get_bytes_amount(task),
165        masternames[id],
166        slavenames[id]);
167 //  MSG_task_execute(task);
168   MSG_task_destroy(task);
169
170   XBT_DEBUG ("Finished");
171   return 0;
172 }                               /* end_of_slave */
173
174 /** Test function */
175 msg_error_t test_all(const char *platform_file,
176                      const char *application_file)
177 {
178   msg_error_t res = MSG_OK;
179
180   {                             /*  Simulation setting */
181     MSG_create_environment(platform_file);
182   }
183
184   TRACE_declare_mark("endmark");
185
186   {                             /*   Application deployment */
187     MSG_function_register("master", master);
188     MSG_function_register("slave", slave);
189     MSG_function_register("timer", timer);
190
191     MSG_launch_application(application_file);
192   }
193   res = MSG_main();
194   return res;
195 }                               /* end_of_test_all */
196
197 /** Main function */
198 int main(int argc, char *argv[])
199 {
200   msg_error_t res = MSG_OK;
201   bool_printed = 0;
202
203   MSG_init(&argc, argv);
204   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
205             "\tExample: %s platform.xml deployment.xml\n", 
206             argv[0], argv[0]);
207    
208
209   res = test_all(argv[1], argv[2]);
210
211   return res != MSG_OK;
212 }