Logo AND Algorithmique Numérique Distribuée

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