Logo AND Algorithmique Numérique Distribuée

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