Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move the latency computation logic from surf models to the routing module.
[simgrid.git] / src / surf / network_vivaldi.c
1 /* Copyright (c) 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 "surf_private.h"
8 #include "surf/random_mgr.h"
9 #include "xbt/dict.h"
10 #include "xbt/str.h"
11 #include "xbt/log.h"
12
13 typedef struct surf_action_network_Vivaldi {
14   s_surf_action_t generic_action;
15   double latency;
16   double lat_init;
17   int suspended;
18 } s_surf_action_network_Vivaldi_t, *surf_action_network_Vivaldi_t;
19
20 typedef struct s_netviva_coords {
21   double x, y, h;
22 } s_netviva_coords_t, *netviva_coords_t;
23 xbt_dict_t coords;              /* Host name -> coordinates */
24
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
26 static random_data_t random_latency_viva = NULL;
27 static int host_number_int_viva = 0;
28
29 static void netviva_count_hosts(void)
30 {
31   host_number_int_viva++;
32 }
33
34 static void netviva_define_callbacks(const char *file)
35 {
36   surfxml_add_callback(STag_surfxml_host_cb_list, &netviva_count_hosts);
37 }
38
39 static int netviva_resource_used(void *resource_id)
40 {
41   /* nothing to do here */
42   return 0;
43 }
44
45 static int netviva_action_unref(surf_action_t action)
46 {
47   action->refcount--;
48   if (!action->refcount) {
49     xbt_swag_remove(action, action->state_set);
50 #ifdef HAVE_TRACING
51     if (action->category)
52       xbt_free(action->category);
53 #endif
54     free(action);
55     return 1;
56   }
57   return 0;
58 }
59
60 static void netviva_action_cancel(surf_action_t action)
61 {
62   return;
63 }
64
65 static void netviva_action_recycle(surf_action_t action)
66 {
67   return;
68 }
69
70 static double netviva_action_get_remains(surf_action_t action)
71 {
72   return action->remains;
73 }
74
75 /* look for the next event to finish */
76 static double netviva_share_resources(double now)
77 {
78   surf_action_network_Vivaldi_t action = NULL;
79   xbt_swag_t running_actions =
80       surf_network_model->states.running_action_set;
81   double min = -1.0;
82
83   xbt_swag_foreach(action, running_actions) {
84     if (action->latency > 0) {
85       if (min < 0)
86         min = action->latency;
87       else if (action->latency < min)
88         min = action->latency;
89     }
90   }
91
92   return min;
93 }
94
95 static void netviva_update_actions_state(double now, double delta)
96 {
97   /* Advance the actions by delta seconds */
98   surf_action_network_Vivaldi_t action = NULL;
99   surf_action_network_Vivaldi_t next_action = NULL;
100   xbt_swag_t running_actions =
101       surf_network_model->states.running_action_set;
102
103   xbt_swag_foreach_safe(action, next_action, running_actions) {
104     if (action->latency > 0) {
105       if (action->latency > delta) {
106         double_update(&(action->latency), delta);
107       } else {
108         action->latency = 0.0;
109       }
110     }
111     double_update(&(action->generic_action.remains),
112                   action->generic_action.cost * delta / action->lat_init);
113     if (action->generic_action.max_duration != NO_MAX_DURATION)
114       double_update(&(action->generic_action.max_duration), delta);
115
116     if (action->generic_action.remains <= 0) {  /* This action is done, inform SURF about it */
117       action->generic_action.finish = surf_get_clock();
118       surf_network_model->action_state_set((surf_action_t) action,
119                                            SURF_ACTION_DONE);
120     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
121                && (action->generic_action.max_duration <= 0)) {
122       action->generic_action.finish = surf_get_clock();
123       surf_network_model->action_state_set((surf_action_t) action,
124                                            SURF_ACTION_DONE);
125     }
126   }
127 }
128
129 static void netviva_update_resource_state(void *id,
130                                           tmgr_trace_event_t event_type,
131                                           double value, double time)
132 {                               /* Called as soon as there is a trace modification */
133   DIE_IMPOSSIBLE;
134 }
135
136 static surf_action_t netviva_communicate(const char *src_name,
137                                          const char *dst_name, double size,
138                                          double rate)
139 {
140   surf_action_network_Vivaldi_t action = NULL;
141   netviva_coords_t c1, c2;
142   c1 = xbt_dict_get(coords, src_name);
143   c2 = xbt_dict_get(coords, dst_name);
144
145   action =
146       surf_action_new(sizeof(s_surf_action_network_Vivaldi_t), size,
147                       surf_network_model, 0);
148
149   action->suspended = 0;
150
151   action->latency = sqrt((c1->x - c2->x) * (c1->x - c2->x) + (c1->y - c2->y) * (c1->y - c2->y)) + fabs(c1->h) + fabs(c2->h);    //random_generate(random_latency_viva);
152   action->lat_init = action->latency;
153
154   if (action->latency <= 0.0) {
155     action->generic_action.state_set =
156         surf_network_model->states.done_action_set;
157     xbt_swag_insert(action, action->generic_action.state_set);
158   }
159
160   return (surf_action_t) action;
161 }
162
163 /* returns an array of link_Vivaldi_t */
164 static xbt_dynar_t netviva_get_route(void *src, void *dst)
165 {
166   xbt_die("Calling this function does not make any sense");
167 }
168
169 static double netviva_get_link_bandwidth(const void *link)
170 {
171   DIE_IMPOSSIBLE;
172 }
173
174 static double netviva_get_link_latency(const void *link)
175 {
176   DIE_IMPOSSIBLE;
177 }
178
179 static int netviva_link_shared(const void *link)
180 {
181   DIE_IMPOSSIBLE;
182 }
183
184 static void netviva_action_suspend(surf_action_t action)
185 {
186   ((surf_action_network_Vivaldi_t) action)->suspended = 1;
187 }
188
189 static void netviva_action_resume(surf_action_t action)
190 {
191   if (((surf_action_network_Vivaldi_t) action)->suspended)
192     ((surf_action_network_Vivaldi_t) action)->suspended = 0;
193 }
194
195 static int netviva_action_is_suspended(surf_action_t action)
196 {
197   return ((surf_action_network_Vivaldi_t) action)->suspended;
198 }
199
200 static void netviva_action_set_max_duration(surf_action_t action,
201                                             double duration)
202 {
203   action->max_duration = duration;
204 }
205
206 static void netviva_finalize(void)
207 {
208   surf_model_exit(surf_network_model);
209   surf_network_model = NULL;
210 }
211
212 static void netviva_parse_host(void)
213 {
214   netviva_coords_t coord = xbt_new(s_netviva_coords_t, 1);
215
216   xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_host_vivaldi, " ");
217
218   coord->x = atof(xbt_dynar_get_as(ctn, 0, char *));
219   coord->y = atof(xbt_dynar_get_as(ctn, 1, char *));
220   coord->h = atof(xbt_dynar_get_as(ctn, 2, char *));
221
222 #ifdef HAVE_TRACING
223   TRACE_surf_host_vivaldi_parse(A_surfxml_host_id, coord->x, coord->y,
224                                 coord->h);
225 #endif
226
227   xbt_dynar_free(&ctn);
228   xbt_dict_set(coords, A_surfxml_host_id, coord, NULL);
229 }
230
231 #ifdef HAVE_LATENCY_BOUND_TRACKING
232 static int netviva_get_latency_limited(surf_action_t action)
233 {
234   return 0;
235 }
236 #endif
237
238 void surf_network_model_init_Vivaldi(const char *filename)
239 {
240   xbt_assert(surf_network_model == NULL);
241   if (surf_network_model)
242     return;
243   surf_network_model = surf_model_init();
244   coords = xbt_dict_new();
245
246   INFO0("Blih");
247   surf_network_model->name = "Vivaldi time network";
248   surf_network_model->action_unref = netviva_action_unref;
249   surf_network_model->action_cancel = netviva_action_cancel;
250   surf_network_model->action_recycle = netviva_action_recycle;
251   surf_network_model->get_remains = netviva_action_get_remains;
252 #ifdef HAVE_LATENCY_BOUND_TRACKING
253   surf_network_model->get_latency_limited = netviva_get_latency_limited;
254 #endif
255
256   surf_network_model->model_private->resource_used = netviva_resource_used;
257   surf_network_model->model_private->share_resources =
258       netviva_share_resources;
259   surf_network_model->model_private->update_actions_state =
260       netviva_update_actions_state;
261   surf_network_model->model_private->update_resource_state =
262       netviva_update_resource_state;
263   surf_network_model->model_private->finalize = netviva_finalize;
264
265   surf_network_model->suspend = netviva_action_suspend;
266   surf_network_model->resume = netviva_action_resume;
267   surf_network_model->is_suspended = netviva_action_is_suspended;
268   surf_cpu_model->set_max_duration = netviva_action_set_max_duration;
269
270   surf_network_model->extension.network.communicate = netviva_communicate;
271   surf_network_model->extension.network.get_link_bandwidth =
272       netviva_get_link_bandwidth;
273   surf_network_model->extension.network.get_link_latency =
274       netviva_get_link_latency;
275   surf_network_model->extension.network.link_shared = netviva_link_shared;
276
277   if (!random_latency_viva)
278     random_latency_viva = random_new(RAND, 100, 0.0, 1.0, .125, .034);
279   netviva_define_callbacks(filename);
280   xbt_dynar_push(model_list, &surf_network_model);
281
282   /* Callbacks settings */
283   surfxml_add_callback(STag_surfxml_host_cb_list, &netviva_parse_host);
284
285   update_model_description(surf_network_model_description,
286                            "Vivaldi", surf_network_model);
287
288 #ifdef HAVE_TRACING
289   TRACE_user_host_variable(0, "vivaldi_x", 0, "declare");
290   TRACE_user_host_variable(0, "vivaldi_y", 0, "declare");
291   TRACE_user_host_variable(0, "vivaldi_h", 0, "declare");
292 #endif
293
294   xbt_cfg_set_string(_surf_cfg_set, "routing", "none");
295   routing_model_create(sizeof(double), NULL, (double_f_pvoid_t)netviva_get_link_latency);
296 }