Logo AND Algorithmique Numérique Distribuée

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