Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8d889c6f9ed2aa755214ea70e42bd31dfead2c65
[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 /** @addtogroup MSG_examples
17  * 
18  * - <b>gtnets</b> Simple ping-pong using GTNeTs instead of the SimGrid network models.
19  */
20
21 int master(int argc, char *argv[]);
22 int slave(int argc, char *argv[]);
23 int timer(int argc, char *argv[]);
24 MSG_error_t test_all(const char *platform_file,
25                      const char *application_file);
26
27 int timer_start = 1;
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   char id_alias[10];
49   //unique id to control statistics
50   int id = -1;
51
52   if (argc != 4) {
53     XBT_INFO("Strange number of arguments expected 3 got %d", argc - 1);
54   }
55
56   /* data size */
57   int read;
58   read = sscanf(argv[1], "%lg", &task_comm_size);
59   xbt_assert(read, "Invalid argument %s\n", argv[1]);
60
61   /* slave name */
62   slavename = argv[2];
63   id = atoi(argv[3]);
64   sprintf(id_alias, "flow_%d", id);
65   slavenames[id] = slavename;
66   TRACE_category(id_alias);
67
68   masternames[id] = MSG_host_get_name(MSG_host_self());
69
70   {                             /*  Task creation.  */
71     char sprintf_buffer[64] = "Task_0";
72     todo = MSG_task_create(sprintf_buffer, 0, task_comm_size, NULL);
73     MSG_task_set_category(todo, id_alias);
74     //keep track of running tasks
75     gl_task_array[id] = todo;
76     gl_data_size[id] = task_comm_size;
77   }
78
79   count_finished++;
80
81   /* time measurement */
82   sprintf(id_alias, "%d", id);
83   start_time = MSG_get_clock();
84   MSG_task_send(todo, id_alias);
85   end_time = MSG_get_clock();
86
87
88   return 0;
89 }                               /* end_of_master */
90
91
92 /** Timer function  */
93 int timer(int argc, char *argv[])
94 {
95   int sleep_time;
96   int first_sleep;
97
98   if (argc != 3) {
99     XBT_INFO("Strange number of arguments expected 2 got %d", argc - 1);
100   }
101
102   sscanf(argv[1], "%d", &first_sleep);
103   sscanf(argv[2], "%d", &sleep_time);
104
105   if(first_sleep){
106       MSG_process_sleep(first_sleep);
107   }
108
109   while(timer_start){
110       MSG_process_sleep(sleep_time);
111   }
112
113   return 0;
114 }
115
116 /** Receiver function  */
117 int slave(int argc, char *argv[])
118 {
119
120   m_task_t task = NULL;
121   int a;
122   int id = 0;
123 #ifdef HAVE_LATENCY_BOUND_TRACKING
124   int limited_latency = 0;
125 #endif
126   double remaining = 0;
127   char id_alias[10];
128
129   if (argc != 2) {
130     XBT_INFO("Strange number of arguments expected 1 got %d", argc - 1);
131   }
132
133   id = atoi(argv[1]);
134   sprintf(id_alias, "%d", id);
135   int trace_id = id;
136
137   a = MSG_task_receive(&(task), id_alias);
138
139   count_finished--;
140   if(count_finished == 0){
141       timer_start = 0;
142   }
143
144
145
146   if (a != MSG_OK) {
147     XBT_INFO("Hey?! What's up?");
148     xbt_die("Unexpected behavior.");
149   }
150
151   elapsed_time = MSG_get_clock() - start_time;
152   
153   
154   if (!bool_printed) {
155     bool_printed = 1;
156     
157     for (id = 0; id < NTASKS; id++) {
158       if (gl_task_array[id] == NULL) continue;
159       if (gl_task_array[id] == task) {
160 #ifdef HAVE_LATENCY_BOUND_TRACKING
161         limited_latency = MSG_task_is_latency_bounded(gl_task_array[id]);
162         if (limited_latency) {
163           XBT_INFO("WARNING FLOW[%d] is limited by latency!!", id);
164         }
165 #endif
166         XBT_INFO
167             ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
168              id, gl_data_size[id] / elapsed_time, masternames[id],
169              slavenames[id], 0.0);
170         MSG_task_destroy(gl_task_array[id]);
171         gl_task_array[id]=NULL;
172       } else {
173         remaining =
174             MSG_task_get_remaining_communication(gl_task_array[id]);
175 #ifdef HAVE_LATENCY_BOUND_TRACKING
176         limited_latency = MSG_task_is_latency_bounded(gl_task_array[id]);
177
178         if (limited_latency) {
179           XBT_INFO("WARNING FLOW[%d] is limited by latency!!", id);
180         }
181 #endif
182         XBT_INFO
183             ("===> Estimated Bw of FLOW[%d] : %f ;  message from %s to %s  with remaining : %f",
184              id, (gl_data_size[id] - remaining) / elapsed_time,
185              masternames[id], slavenames[id], remaining);
186         if(remaining==0) {
187           MSG_task_destroy(gl_task_array[id]);
188           gl_task_array[id]=NULL;
189         }
190       }
191     }
192     bool_printed = 2;
193   }
194   char mark[100];
195   snprintf(mark, 100, "flow_%d_finished", trace_id);
196   TRACE_mark("endmark", mark);
197
198   if(bool_printed==2 && gl_task_array[trace_id]) MSG_task_destroy(gl_task_array[trace_id]);
199
200   return 0;
201 }                               /* end_of_slave */
202
203 /** Test function */
204 MSG_error_t test_all(const char *platform_file,
205                      const char *application_file)
206 {
207   MSG_error_t res = MSG_OK;
208
209   /* MSG_config("workstation/model", "GTNETS"); */
210   /* MSG_config("workstation/model","KCCFLN05"); */
211   {                             /*  Simulation setting */
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_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 */