Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2bccb8854750dd0d3840fd4cc4fe6ffe17ec02a0
[simgrid.git] / src / surf / surf_interface.cpp
1 #include "surf.hpp"
2 #include "workstation.hpp"
3 #include "network.hpp"
4 #include "surf_routing_cluster.hpp"
5 #include "instr/instr_private.h"
6
7 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
8
9 /*********
10  * TOOLS *
11  *********/
12 extern double NOW;
13
14 static CpuPtr get_casted_cpu(surf_resource_t resource){
15   return dynamic_cast<CpuPtr>(static_cast<ResourcePtr>(surf_cpu_resource_priv(resource)));
16 }
17
18 static WorkstationCLM03Ptr get_casted_workstation(surf_resource_t resource){
19   return dynamic_cast<WorkstationCLM03Ptr>(static_cast<ResourcePtr>(surf_workstation_resource_priv(resource)));
20 }
21
22 char *surf_routing_edge_name(sg_routing_edge_t edge){
23   return edge->p_name;
24 }
25
26 #ifdef CONTEXT_THREADS
27 static xbt_parmap_t surf_parmap = NULL; /* parallel map on models */
28 #endif
29
30 static double *surf_mins = NULL; /* return value of share_resources for each model */
31 static int surf_min_index;       /* current index in surf_mins */
32 static double surf_min;               /* duration determined by surf_solve */
33
34 void surf_presolve(void)
35 {
36   double next_event_date = -1.0;
37   tmgr_trace_event_t event = NULL;
38   double value = -1.0;
39   ResourcePtr resource = NULL;
40   ModelPtr model = NULL;
41   unsigned int iter;
42
43   XBT_DEBUG
44       ("First Run! Let's \"purge\" events and put models in the right state");
45   while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
46     if (next_event_date > NOW)
47       break;
48     while ((event =
49             tmgr_history_get_next_event_leq(history, next_event_date,
50                                             &value,
51                                             (void **) &resource))) {
52       if (value >= 0){
53         resource->updateState(event, value, NOW);
54       }
55     }
56   }
57   xbt_dynar_foreach(model_list, iter, model)
58       model->updateActionsState(NOW, 0.0);
59 }
60
61 static void surf_share_resources(surf_model_t model)
62 {
63   double next_action_end = -1.0;
64   int i = __sync_fetch_and_add(&surf_min_index, 1);
65   if (strcmp(model->m_name.c_str(), "network NS3")) {
66     XBT_DEBUG("Running for Resource [%s]", model->m_name.c_str());
67     next_action_end = model->shareResources(NOW);
68     XBT_DEBUG("Resource [%s] : next action end = %f",
69         model->m_name.c_str(), next_action_end);
70   }
71   surf_mins[i] = next_action_end;
72 }
73
74 static void surf_update_actions_state(surf_model_t model)
75 {
76   model->updateActionsState(NOW, surf_min);
77 }
78
79 double surf_solve(double max_date)
80 {
81   surf_min = -1.0; /* duration */
82   double next_event_date = -1.0;
83   double model_next_action_end = -1.0;
84   double value = -1.0;
85   ResourcePtr resource = NULL;
86   ModelPtr model = NULL;
87   tmgr_trace_event_t event = NULL;
88   unsigned int iter;
89
90   if (max_date != -1.0 && max_date != NOW) {
91     surf_min = max_date - NOW;
92   }
93
94   XBT_DEBUG("Looking for next action end for all models except NS3");
95
96   if (surf_mins == NULL) {
97     surf_mins = xbt_new(double, xbt_dynar_length(model_list));
98   }
99   surf_min_index = 0;
100
101   /* sequential version */
102   xbt_dynar_foreach(model_list, iter, model) {
103     surf_share_resources(static_cast<ModelPtr>(model));
104   }
105
106   unsigned i;
107   for (i = 0; i < xbt_dynar_length(model_list); i++) {
108     if ((surf_min < 0.0 || surf_mins[i] < surf_min)
109         && surf_mins[i] >= 0.0) {
110       surf_min = surf_mins[i];
111     }
112   }
113
114   XBT_DEBUG("Min for resources (remember that NS3 don't update that value) : %f", surf_min);
115
116   XBT_DEBUG("Looking for next trace event");
117
118   do {
119     XBT_DEBUG("Next TRACE event : %f", next_event_date);
120
121     next_event_date = tmgr_history_next_date(history);
122
123     if(!strcmp(surf_network_model->m_name.c_str(), "network NS3")){//FIXME: add surf_network_model->m_name &&
124       if(next_event_date!=-1.0 && surf_min!=-1.0) {
125         surf_min = MIN(next_event_date - NOW, surf_min);
126       } else{
127         surf_min = MAX(next_event_date - NOW, surf_min);
128       }
129
130       XBT_DEBUG("Run for network at most %f", surf_min);
131       // run until min or next flow
132       model_next_action_end = surf_network_model->shareResources(surf_min);
133
134       XBT_DEBUG("Min for network : %f", model_next_action_end);
135       if(model_next_action_end>=0.0)
136         surf_min = model_next_action_end;
137     }
138
139     if (next_event_date < 0.0) {
140       XBT_DEBUG("no next TRACE event. Stop searching for it");
141       break;
142     }
143
144     if ((surf_min == -1.0) || (next_event_date > NOW + surf_min)) break;
145
146     XBT_DEBUG("Updating models (min = %g, NOW = %g, next_event_date = %g)", surf_min, NOW, next_event_date);
147     while ((event =
148             tmgr_history_get_next_event_leq(history, next_event_date,
149                                             &value,
150                                             (void **) &resource))) {
151       if (resource->isUsed()) {
152         surf_min = next_event_date - NOW;
153         XBT_DEBUG
154             ("This event will modify model state. Next event set to %f",
155              surf_min);
156       }
157       /* update state of model_obj according to new value. Does not touch lmm.
158          It will be modified if needed when updating actions */
159       XBT_DEBUG("Calling update_resource_state for resource %s with min %lf",
160              resource->p_model->m_name.c_str(), surf_min);
161       resource->updateState(event, value, next_event_date);
162     }
163   } while (1);
164
165   /* FIXME: Moved this test to here to avoid stopping simulation if there are actions running on cpus and all cpus are with availability = 0.
166    * This may cause an infinite loop if one cpu has a trace with periodicity = 0 and the other a trace with periodicity > 0.
167    * The options are: all traces with same periodicity(0 or >0) or we need to change the way how the events are managed */
168   if (surf_min == -1.0) {
169   XBT_DEBUG("No next event at all. Bail out now.");
170     return -1.0;
171   }
172
173   XBT_DEBUG("Duration set to %f", surf_min);
174
175   NOW = NOW + surf_min;
176
177   /* sequential version */
178   xbt_dynar_foreach(model_list, iter, model) {
179     surf_update_actions_state(model);
180   }
181
182 #ifdef HAVE_TRACING
183   TRACE_paje_dump_buffer (0);
184 #endif
185
186   return surf_min;
187 }
188
189 XBT_INLINE double surf_get_clock(void)
190 {
191   return NOW;
192 }
193
194 void routing_get_route_and_latency(sg_routing_edge_t src, sg_routing_edge_t dst,
195                               xbt_dynar_t * route, double *latency){
196   routing_platf->getRouteAndLatency(src, dst, route, latency);
197 }
198
199 /*********
200  * MODEL *
201  *********/
202
203 void *surf_as_cluster_get_backbone(AS_t as){
204   return static_cast<AsClusterPtr>(as)->p_backbone;
205 }
206
207 void surf_as_cluster_set_backbone(AS_t as, void* backbone){
208   static_cast<AsClusterPtr>(as)->p_backbone = dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(backbone));
209 }
210
211 const char *surf_model_name(surf_model_t model){
212   return model->m_name.c_str();
213 }
214
215 xbt_swag_t surf_model_done_action_set(surf_model_t model){
216   return model->p_doneActionSet;
217 }
218
219 xbt_swag_t surf_model_failed_action_set(surf_model_t model){
220   return model->p_failedActionSet;
221 }
222
223 xbt_swag_t surf_model_ready_action_set(surf_model_t model){
224   return model->p_readyActionSet;
225 }
226
227 xbt_swag_t surf_model_running_action_set(surf_model_t model){
228   return model->p_runningActionSet;
229 }
230
231 surf_action_t surf_workstation_model_execute_parallel_task(surf_workstation_model_t model,
232                                                     int workstation_nb,
233                                             void **workstation_list,
234                                             double *computation_amount,
235                                             double *communication_amount,
236                                             double rate){
237   return static_cast<ActionPtr>(model->executeParallelTask(workstation_nb, workstation_list, computation_amount, communication_amount, rate));
238 }
239
240 surf_action_t surf_workstation_model_communicate(surf_workstation_model_t model, surf_resource_t src, surf_resource_t dst, double size, double rate){
241   return model->communicate(get_casted_workstation(src), get_casted_workstation(dst), size, rate);
242 }
243
244 xbt_dynar_t surf_workstation_model_get_route(surf_workstation_model_t model,
245                                                      surf_resource_t src, surf_resource_t dst){
246   return model->getRoute(get_casted_workstation(src), get_casted_workstation(dst));
247 }
248
249 surf_action_t surf_network_model_communicate(surf_network_model_t model, sg_routing_edge_t src, sg_routing_edge_t dst, double size, double rate){
250   return model->communicate(src, dst, size, rate);
251 }
252
253 const char *surf_resource_name(surf_cpp_resource_t resource){
254   return resource->m_name;
255 }
256
257 xbt_dict_t surf_resource_get_properties(surf_cpp_resource_t resource){
258   return resource->getProperties();
259 }
260
261 e_surf_resource_state_t surf_resource_get_state(surf_cpp_resource_t resource){
262   return resource->getState();
263 }
264
265 surf_action_t surf_workstation_sleep(surf_resource_t resource, double duration){
266   return get_casted_workstation(resource)->sleep(duration);
267 }
268
269 double surf_workstation_get_speed(surf_resource_t resource, double load){
270   return get_casted_workstation(resource)->getSpeed(load);
271 }
272
273 double surf_workstation_get_available_speed(surf_resource_t resource){
274   return get_casted_workstation(resource)->getAvailableSpeed();
275 }
276
277 int surf_workstation_get_core(surf_resource_t resource){
278   return get_casted_workstation(resource)->getCore();
279 }
280
281 surf_action_t surf_workstation_execute(surf_resource_t resource, double size){
282   return get_casted_workstation(resource)->execute(size);
283 }
284
285 surf_action_t surf_workstation_open(surf_resource_t workstation, const char* mount, const char* path){
286   return get_casted_workstation(workstation)->open(mount, path);
287 }
288
289 surf_action_t surf_workstation_close(surf_resource_t workstation, surf_file_t fd){
290   return get_casted_workstation(workstation)->close(fd);
291 }
292
293 int surf_workstation_unlink(surf_resource_t workstation, surf_file_t fd){
294   return get_casted_workstation(workstation)->unlink(fd);
295 }
296
297 surf_action_t surf_workstation_ls(surf_resource_t workstation, const char* mount, const char *path){
298   return get_casted_workstation(workstation)->ls(mount, path);
299 }
300
301 size_t surf_workstation_get_size(surf_resource_t workstation, surf_file_t fd){
302   return get_casted_workstation(workstation)->getSize(fd);
303 }
304
305 surf_action_t surf_workstation_read(surf_resource_t resource, void *ptr, size_t size, surf_file_t fd){
306   return get_casted_workstation(resource)->read(ptr, size, fd);
307 }
308
309 surf_action_t surf_workstation_write(surf_resource_t resource, const void *ptr, size_t size, surf_file_t fd){
310   return get_casted_workstation(resource)->write(ptr, size, fd);
311 }
312
313 int surf_network_link_is_shared(surf_cpp_resource_t link){
314   return dynamic_cast<NetworkCm02LinkPtr>(link)->isShared();
315 }
316
317 double surf_network_link_get_bandwidth(surf_cpp_resource_t link){
318   return dynamic_cast<NetworkCm02LinkPtr>(link)->getBandwidth();
319 }
320
321 double surf_network_link_get_latency(surf_cpp_resource_t link){
322   return dynamic_cast<NetworkCm02LinkPtr>(link)->getLatency();
323 }
324
325 surf_action_t surf_cpu_execute(surf_resource_t cpu, double size){
326   return get_casted_cpu(cpu)->execute(size);
327 }
328
329 surf_action_t surf_cpu_sleep(surf_resource_t cpu, double duration){
330   return get_casted_cpu(cpu)->sleep(duration);
331 }
332
333 double surf_action_get_start_time(surf_action_t action){
334   return action->m_start;
335 }
336
337 double surf_action_get_finish_time(surf_action_t action){
338   return action->m_finish;
339 }
340
341 double surf_action_get_remains(surf_action_t action){
342   return action->m_remains;
343 }
344
345 void surf_action_unref(surf_action_t action){
346   action->unref();
347 }
348
349 void surf_action_suspend(surf_action_t action){
350   action->suspend();
351 }
352
353 void surf_action_resume(surf_action_t action){
354   action->resume();
355 }
356
357 void surf_action_cancel(surf_action_t action){
358   action->cancel();
359 }
360
361 void surf_action_set_priority(surf_action_t action, double priority){
362   action->setPriority(priority);
363 }
364
365 void surf_action_set_category(surf_action_t action, const char *category){
366   action->setCategory(category);
367 }
368
369 void *surf_action_get_data(surf_action_t action){
370   return action->p_data;
371 }
372
373 void surf_action_set_data(surf_action_t action, void *data){
374   action->p_data = data;
375 }
376
377 e_surf_action_state_t surf_action_get_state(surf_action_t action){
378   return action->getState();
379 }
380
381 int surf_action_get_cost(surf_action_t action){
382   return action->m_cost;
383 }
384
385 surf_file_t surf_storage_action_get_file(surf_storage_action_lmm_t action){
386   return action->p_file;
387 }
388
389 xbt_dict_t surf_storage_action_get_ls_dict(surf_storage_action_lmm_t action){
390   return action->p_lsDict;
391 }