Logo AND Algorithmique Numérique Distribuée

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