Logo AND Algorithmique Numérique Distribuée

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