Logo AND Algorithmique Numérique Distribuée

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