Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9d9a3f96962700c1dec11b774ca1e6e6ecd0c012
[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 "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11
12 /** @addtogroup MSG_examples
13  * 
14  *  @section MSG_ex_models Models-related examples
15  * 
16  *  @subsection MSG_ex_PLS Packet level simulators
17  * 
18  *  These examples demonstrate how to use the bindings to classicalPacket-Level Simulators (PLS), as explained in
19  *  \ref pls. The most interesting is probably not the C files since they are unchanged from the other simulations,
20  *  but the associated files, such as the platform files to see how to declare a platform to be used with the PLS
21  *  bindings of SimGrid and the tesh files to see how to actually start a simulation in these settings.
22  * 
23  * - <b>ns3</b>: Simple ping-pong using ns3 instead of the SimGrid network models. 
24  */
25
26 int timer_start; //set as 1 in the master process
27
28 //keep a pointer to all surf running tasks.
29 #define NTASKS 1500
30 int bool_printed = 0;
31 double start_time, end_time, elapsed_time;
32 double gl_data_size[NTASKS];
33 msg_task_t gl_task_array[NTASKS];
34 const char *slavenames[NTASKS];
35 const char *masternames[NTASKS];
36 int gl_task_array_id = 0;
37 int count_finished = 0;
38
39 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
40
41 static int master(int argc, char *argv[])
42 {
43   msg_task_t todo;
44
45   xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
46
47   XBT_DEBUG ("Master started");
48
49   /* data size */
50   double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
51
52   /* slave name */
53   char *slavename = argv[2];
54   int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s");   //unique id to control statistics
55   char *id_alias = bprintf("flow_%d", id);
56   slavenames[id] = slavename;
57   TRACE_category(id_alias);
58
59   masternames[id] = MSG_host_get_name(MSG_host_self());
60
61   {                             /*  Task creation.  */
62     todo = MSG_task_create("Task_0", 100*task_comm_size, task_comm_size, NULL);
63     MSG_task_set_category(todo, id_alias);
64     //keep track of running tasks
65     gl_task_array[id] = todo;
66     gl_data_size[id] = task_comm_size;
67   }
68
69   MSG_host_by_name(slavename);
70
71   count_finished++;
72   timer_start = 1 ;
73
74   /* time measurement */
75   sprintf(id_alias, "%d", id);
76   start_time = MSG_get_clock();
77   MSG_task_send(todo, id_alias);
78   end_time = MSG_get_clock();
79
80   XBT_DEBUG ("Finished");
81   xbt_free(id_alias);
82   return 0;
83 }
84
85 static int timer(int argc, char *argv[])
86 {
87   double sleep_time;
88   double first_sleep;
89
90   xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
91
92   sscanf(argv[1], "%lf", &first_sleep);
93   sscanf(argv[2], "%lf", &sleep_time);
94
95   XBT_DEBUG ("Timer started");
96
97   if(first_sleep){
98       MSG_process_sleep(first_sleep);
99   }
100
101   do {
102     XBT_DEBUG ("Get sleep");
103     MSG_process_sleep(sleep_time);
104   } while(timer_start);
105
106   XBT_DEBUG ("Finished");
107   return 0;
108 }
109
110 static int slave(int argc, char *argv[])
111 {
112   msg_task_t task = NULL;
113   char id_alias[10];
114
115   xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
116
117   XBT_DEBUG ("Slave started");
118
119   int id = xbt_str_parse_int(argv[1], "Invalid id: %s");
120   sprintf(id_alias, "%d", id);
121
122   msg_error_t a = MSG_task_receive(&(task), id_alias);
123
124   count_finished--;
125   if(count_finished == 0){
126       timer_start = 0;
127   }
128
129   if (a != MSG_OK) {
130     XBT_INFO("Hey?! What's up?");
131     xbt_die("Unexpected behavior.");
132   }
133
134   elapsed_time = MSG_get_clock() - start_time;
135
136   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, MSG_task_get_bytes_amount(task), masternames[id],
137            slavenames[id]);
138 //  MSG_task_execute(task);
139   MSG_task_destroy(task);
140
141   XBT_DEBUG ("Finished");
142   return 0;
143 }
144
145 int main(int argc, char *argv[])
146 {
147   msg_error_t res = MSG_OK;
148   bool_printed = 0;
149
150   MSG_init(&argc, argv);
151   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
152              "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
153
154   MSG_create_environment(argv[1]);
155   TRACE_declare_mark("endmark");
156
157   MSG_function_register("master", master);
158   MSG_function_register("slave", slave);
159   MSG_function_register("timer", timer);
160
161   MSG_launch_application(argv[2]);
162
163   res = MSG_main();
164
165   return res != MSG_OK;
166 }