Logo AND Algorithmique Numérique Distribuée

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