Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update factor for model LegrandVelho.
[simgrid.git] / examples / msg / gtnets / gtnets.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 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 int timer(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file,
20                      const char *application_file);
21
22 int timer_start = 1;
23
24 //keep a pointer to all surf running tasks.
25 #define NTASKS 1500
26 int bool_printed = 0;
27 double start_time, end_time, elapsed_time;
28 double gl_data_size[NTASKS];
29 m_task_t gl_task_array[NTASKS];
30 const char *slavenames[NTASKS];
31 const char *masternames[NTASKS];
32 int gl_task_array_id = 0;
33 int count_finished = 0;
34
35 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
36
37 /** master */
38 int master(int argc, char *argv[])
39 {
40   char *slavename = NULL;
41   double task_comm_size = 0;
42   m_task_t todo;
43   char id_alias[10];
44   //unique id to control statistics
45   int id = -1;
46
47   if (argc != 4) {
48     XBT_INFO("Strange number of arguments expected 3 got %d", argc - 1);
49   }
50
51   /* data size */
52   int read;
53   read = sscanf(argv[1], "%lg", &task_comm_size);
54   xbt_assert(read, "Invalid argument %s\n", argv[1]);
55
56   /* slave name */
57   slavename = argv[2];
58   id = atoi(argv[3]);
59   sprintf(id_alias, "flow_%d", id);
60   slavenames[id] = slavename;
61   TRACE_category(id_alias);
62
63   masternames[id] = MSG_host_get_name(MSG_host_self());
64
65   {                             /*  Task creation.  */
66     char sprintf_buffer[64] = "Task_0";
67     todo = MSG_task_create(sprintf_buffer, 0, task_comm_size, NULL);
68     TRACE_msg_set_task_category(todo, id_alias);
69     //keep track of running tasks
70     gl_task_array[id] = todo;
71     gl_data_size[id] = task_comm_size;
72   }
73
74   count_finished++;
75
76   /* time measurement */
77   sprintf(id_alias, "%d", id);
78   start_time = MSG_get_clock();
79   MSG_task_send(todo, id_alias);
80   end_time = MSG_get_clock();
81
82
83   return 0;
84 }                               /* end_of_master */
85
86
87 /** Timer function  */
88 int timer(int argc, char *argv[])
89 {
90   int sleep_time;
91   int first_sleep;
92
93   if (argc != 3) {
94     XBT_INFO("Strange number of arguments expected 2 got %d", argc - 1);
95   }
96
97   sscanf(argv[1], "%d", &first_sleep);
98   sscanf(argv[2], "%d", &sleep_time);
99
100   if(first_sleep){
101       MSG_process_sleep(first_sleep);
102   }
103
104   while(timer_start){
105       MSG_process_sleep(sleep_time);
106   }
107
108   return 0;
109 }
110
111 /** Receiver function  */
112 int slave(int argc, char *argv[])
113 {
114
115   m_task_t task = NULL;
116   int a;
117   int id = 0;
118 #ifdef HAVE_LATENCY_BOUND_TRACKING
119   int limited_latency = 0;
120 #endif
121   double remaining = 0;
122   char id_alias[10];
123
124   if (argc != 2) {
125     XBT_INFO("Strange number of arguments expected 1 got %d", argc - 1);
126   }
127
128   id = atoi(argv[1]);
129   sprintf(id_alias, "%d", id);
130   int trace_id = id;
131
132   a = MSG_task_receive(&(task), id_alias);
133
134   count_finished--;
135   if(count_finished == 0){
136       timer_start = 0;
137   }
138
139
140
141   if (a != MSG_OK) {
142     XBT_INFO("Hey?! What's up?");
143     xbt_die("Unexpected behavior.");
144   }
145
146   elapsed_time = MSG_get_clock() - start_time;
147   
148   
149   if (!bool_printed) {
150     bool_printed = 1;
151     
152     for (id = 0; id < NTASKS; id++) {
153       if (gl_task_array[id] == NULL) continue;
154       if (gl_task_array[id] == task) {
155 #ifdef HAVE_LATENCY_BOUND_TRACKING
156         limited_latency = MSG_task_is_latency_bounded(gl_task_array[id]);
157         if (limited_latency) {
158           XBT_INFO("WARNING FLOW[%d] is limited by latency!!", id);
159         }
160 #endif
161         XBT_INFO
162             ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
163              id, gl_data_size[id] / elapsed_time, masternames[id],
164              slavenames[id], 0.0);
165         MSG_task_destroy(gl_task_array[id]);
166         gl_task_array[id]=NULL;
167       } else {
168         remaining =
169             MSG_task_get_remaining_communication(gl_task_array[id]);
170 #ifdef HAVE_LATENCY_BOUND_TRACKING
171         limited_latency = MSG_task_is_latency_bounded(gl_task_array[id]);
172
173         if (limited_latency) {
174           XBT_INFO("WARNING FLOW[%d] is limited by latency!!", id);
175         }
176 #endif
177         XBT_INFO
178             ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
179              id, (gl_data_size[id] - remaining) / elapsed_time,
180              masternames[id], slavenames[id], remaining);
181         if(remaining==0) {
182           MSG_task_destroy(gl_task_array[id]);
183           gl_task_array[id]=NULL;
184         }
185       }
186     }
187     bool_printed = 2;
188   }
189   char mark[100];
190   snprintf(mark, 100, "flow_%d_finished", trace_id);
191   TRACE_mark("endmark", mark);
192
193   if(bool_printed==2 && gl_task_array[trace_id]) MSG_task_destroy(gl_task_array[trace_id]);
194
195   return 0;
196 }                               /* end_of_slave */
197
198 /** Test function */
199 MSG_error_t test_all(const char *platform_file,
200                      const char *application_file)
201 {
202   MSG_error_t res = MSG_OK;
203
204   /* MSG_config("workstation/model", "GTNETS"); */
205   /* MSG_config("workstation/model","KCCFLN05"); */
206   {                             /*  Simulation setting */
207     MSG_create_environment(platform_file);
208   }
209
210   TRACE_declare_mark("endmark");
211
212   {                             /*   Application deployment */
213     MSG_function_register("master", master);
214     MSG_function_register("slave", slave);
215     MSG_function_register("timer", timer);
216
217     MSG_launch_application(application_file);
218   }
219   res = MSG_main();
220   return res;
221 }                               /* end_of_test_all */
222
223 /** Main function */
224 int main(int argc, char *argv[])
225 {
226   MSG_error_t res = MSG_OK;
227   bool_printed = 0;
228
229   MSG_global_init(&argc, argv);
230   if (argc < 3) {
231     printf("Usage: %s platform_file deployment_file\n", argv[0]);
232     exit(1);
233   }
234
235   res = test_all(argv[1], argv[2]);
236
237   MSG_clean();
238
239   if (res == MSG_OK)
240     return 0;
241   else
242     return 1;
243 }                               /* end_of_main */