Logo AND Algorithmique Numérique Distribuée

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