Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
compile simdag with g++ so that Surf cleaned of its C interface
[simgrid.git] / src / simdag / sd_workstation.cpp
1 /* Copyright (c) 2006-2016. 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 "src/simdag/simdag_private.h"
8 #include "simgrid/simdag.h"
9 #include "simgrid/host.h"
10 #include "xbt/dict.h"
11 #include "xbt/lib.h"
12 #include "xbt/sysdep.h"
13 #include "surf/surf.h"
14 #include "simgrid/msg.h" //FIXME: why?
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
17                                 "Logging specific to SimDag (workstation)");
18
19 /* Creates a workstation and registers it in SD.
20  */
21 SD_workstation_t __SD_workstation_create(const char *name)
22 {
23
24   SD_workstation_priv_t workstation;
25
26   workstation = xbt_new(s_SD_workstation_priv_t, 1);
27   workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS;      /* default mode is shared */
28   workstation->task_fifo = NULL;
29   workstation->current_task = NULL;
30
31   sg_host_t sg_host = sg_host_by_name(name);
32   sg_host_sd_set(sg_host,workstation);
33   return sg_host;
34 }
35
36 /* Creates a storage and registers it in SD.
37  */
38 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
39 {
40
41   SD_storage_priv_t storage;
42   const char *name;
43
44   storage = xbt_new(s_SD_storage_priv_t, 1);
45   storage->data = data;     /* user data */
46   name = surf_resource_name((surf_cpp_resource_t)surf_storage);
47   storage->host = (const char*)surf_storage_get_host( (surf_resource_t )surf_storage_resource_by_name(name));
48   xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
49   return xbt_lib_get_elm_or_null(storage_lib, name);
50 }
51
52 /* Destroys a storage.
53  */
54 void __SD_storage_destroy(void *storage)
55 {
56   SD_storage_priv_t s;
57
58   s = (SD_storage_priv_t) storage;
59   xbt_free(s);
60 }
61
62 /**
63  * \brief Returns a workstation given its name
64  *
65  * If there is no such workstation, the function returns \c NULL.
66  *
67  * \param name workstation name
68  * \return the workstation, or \c NULL if there is no such workstation
69  */
70 SD_workstation_t SD_workstation_get_by_name(const char *name)
71 {
72   return sg_host_by_name(name);
73 }
74
75 /**
76  * \brief Returns the workstation list
77  *
78  * Use SD_workstation_get_number() to know the array size.
79  * 
80  * \return an array of \ref SD_workstation_t containing all workstations
81  * \remark The workstation order in the returned array is generally different from the workstation creation/declaration order in the XML platform (we use a hash table internally).
82  * \see SD_workstation_get_number()
83  */
84 const SD_workstation_t *SD_workstation_get_list(void) {
85   xbt_assert(SD_workstation_get_number() > 0, "There is no workstation!");
86
87   if (sd_global->workstation_list == NULL)     /* this is the first time the function is called */
88     sd_global->workstation_list = (SD_workstation_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
89
90   return sd_global->workstation_list;
91 }
92
93 /**
94  * \brief Returns the number of workstations
95  *
96  * \return the number of existing workstations
97  * \see SD_workstation_get_list()
98  */
99 int SD_workstation_get_number(void)
100 {
101   return sg_host_count();
102 }
103
104 /**
105  * \brief Returns the user data of a workstation
106  *
107  * \param workstation a workstation
108  * \return the user data associated with this workstation (can be \c NULL)
109  * \see SD_workstation_set_data()
110  */
111 void *SD_workstation_get_data(SD_workstation_t workstation)
112 {
113   return sg_host_user(workstation);
114 }
115
116 /**
117  * \brief Sets the user data of a workstation
118  *
119  * The new data can be \c NULL. The old data should have been freed first
120  * if it was not \c NULL.
121  *
122  * \param workstation a workstation
123  * \param data the new data you want to associate with this workstation
124  * \see SD_workstation_get_data()
125  */
126 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
127 {
128         sg_host_user_set(workstation, data);
129 }
130
131 /**
132  * \brief Returns the name of a workstation
133  *
134  * \param workstation a workstation
135  * \return the name of this workstation (cannot be \c NULL)
136  */
137 const char *SD_workstation_get_name(SD_workstation_t workstation)
138 {
139   return sg_host_get_name(workstation);
140 }
141
142 /**
143  * \brief Returns the value of a given workstation property
144  *
145  * \param ws a workstation
146  * \param name a property name
147  * \return value of a property (or NULL if property not set)
148  */
149 const char *SD_workstation_get_property_value(SD_workstation_t ws,
150                                               const char *name)
151 {
152   return (const char*) xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
153 }
154
155
156 /**
157  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
158  *
159  * \param workstation a workstation
160  * \return the dictionary containing the properties associated with the workstation
161  */
162 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
163 {
164   return sg_host_get_properties(workstation);
165 }
166
167
168 /** @brief Displays debugging informations about a workstation */
169 void SD_workstation_dump(SD_workstation_t ws)
170 {
171   xbt_dict_t props;
172   xbt_dict_cursor_t cursor=NULL;
173   char *key,*data;
174   SD_task_t task = NULL;
175   
176   XBT_INFO("Displaying workstation %s", SD_workstation_get_name(ws));
177   XBT_INFO("  - power: %.0f", SD_workstation_get_power(ws));
178   XBT_INFO("  - available power: %.2f", SD_workstation_get_available_power(ws));
179   switch (sg_host_sd(ws)->access_mode){
180   case SD_WORKSTATION_SHARED_ACCESS:
181       XBT_INFO("  - access mode: Space shared");
182       break;
183   case SD_WORKSTATION_SEQUENTIAL_ACCESS:
184       XBT_INFO("  - access mode: Exclusive");
185     task = SD_workstation_get_current_task(ws);
186     if(task)
187       XBT_INFO("    current running task: %s",
188                SD_task_get_name(task));
189     else
190       XBT_INFO("    no task running");
191       break;
192   default: break;
193   }
194   props = SD_workstation_get_properties(ws);
195   
196   if (!xbt_dict_is_empty(props)){
197     XBT_INFO("  - properties:");
198
199     xbt_dict_foreach(props,cursor,key,data) {
200       XBT_INFO("    %s->%s",key,data);
201     }
202   }
203 }
204
205 /**
206  * \brief Returns the route between two workstations
207  *
208  * Use SD_route_get_size() to know the array size.
209  *
210  * \param src a workstation
211  * \param dst another workstation
212  * \return a new array of \ref SD_link_t representating the route between these two workstations
213  * \see SD_route_get_size(), SD_link_t
214  */
215 const SD_link_t *SD_route_get_list(SD_workstation_t src,
216                                    SD_workstation_t dst)
217 {
218   xbt_dynar_t surf_route;
219   void *surf_link;
220   unsigned int cpt;
221
222   if (sd_global->recyclable_route == NULL) {
223     /* first run */
224     sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
225   }
226
227   surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model, src, dst);
228
229   xbt_dynar_foreach(surf_route, cpt, surf_link) {
230     sd_global->recyclable_route[cpt] = (SD_link_t)surf_link;
231   }
232   return sd_global->recyclable_route;
233 }
234
235 /**
236  * \brief Returns the number of links on the route between two workstations
237  *
238  * \param src a workstation
239  * \param dst another workstation
240  * \return the number of links on the route between these two workstations
241  * \see SD_route_get_list()
242  */
243 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
244 {
245   return xbt_dynar_length(surf_host_model_get_route(
246                     (surf_host_model_t)surf_host_model, src, dst));
247 }
248
249 /**
250  * \brief Returns the total power of a workstation
251  *
252  * \param workstation a workstation
253  * \return the total power of this workstation
254  * \see SD_workstation_get_available_power()
255  */
256 double SD_workstation_get_power(SD_workstation_t workstation)
257 {
258   return sg_host_get_speed(workstation);
259 }
260 /**
261  * \brief Returns the amount of cores of a workstation
262  *
263  * \param workstation a workstation
264  * \return the amount of cores of this workstation
265  */
266 int SD_workstation_get_cores(SD_workstation_t workstation) {
267   return sg_host_get_core(workstation);
268 }
269
270 /**
271  * \brief Returns the proportion of available power in a workstation
272  *
273  * \param workstation a workstation
274  * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
275  * \see SD_workstation_get_power()
276  */
277 double SD_workstation_get_available_power(SD_workstation_t workstation)
278 {
279   return surf_host_get_available_speed(workstation);
280 }
281
282 /**
283  * \brief Returns an approximative estimated time for the given computation amount on a workstation
284  *
285  * \param workstation a workstation
286  * \param flops_amount the computation amount you want to evaluate (in flops)
287  * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
288  */
289 double SD_workstation_get_computation_time(SD_workstation_t workstation,
290                                            double flops_amount)
291 {
292   xbt_assert(flops_amount >= 0,
293               "flops_amount must be greater than or equal to zero");
294   return flops_amount / SD_workstation_get_power(workstation);
295 }
296
297 /**
298  * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
299  * between the workstations.
300  *
301  * \param src the first workstation
302  * \param dst the second workstation
303  * \return the latency of the route between the two workstations (in seconds)
304  * \see SD_route_get_current_bandwidth()
305  */
306 double SD_route_get_current_latency(SD_workstation_t src,
307                                     SD_workstation_t dst)
308 {
309
310   const SD_link_t *links;
311   int nb_links;
312   double latency;
313   int i;
314
315   links = SD_route_get_list(src, dst);
316   nb_links = SD_route_get_size(src, dst);
317   latency = 0.0;
318
319   for (i = 0; i < nb_links; i++) {
320     latency += SD_link_get_current_latency(links[i]);
321   }
322
323   return latency;
324 }
325
326 /**
327  * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
328  * between the workstations.
329  *
330  * \param src the first workstation
331  * \param dst the second workstation
332  * \return the bandwidth of the route between the two workstations (in bytes/second)
333  * \see SD_route_get_current_latency()
334  */
335 double SD_route_get_current_bandwidth(SD_workstation_t src,
336                                       SD_workstation_t dst)
337 {
338
339   const SD_link_t *links;
340   int nb_links;
341   double bandwidth;
342   double min_bandwidth;
343   int i;
344
345   links = SD_route_get_list(src, dst);
346   nb_links = SD_route_get_size(src, dst);
347   min_bandwidth = -1.0;
348
349   for (i = 0; i < nb_links; i++) {
350     bandwidth = SD_link_get_current_bandwidth(links[i]);
351     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
352       min_bandwidth = bandwidth;
353   }
354
355   return min_bandwidth;
356 }
357
358 /**
359  * \brief Returns an approximative estimated time for the given
360  * communication amount between two workstations
361  *
362  * \param src the first workstation
363  * \param dst the second workstation
364  * \param bytes_amount the communication amount you want to evaluate (in bytes)
365  * \return an approximative estimated communication time for the given bytes amount
366  * between the workstations (in seconds)
367  */
368 double SD_route_get_communication_time(SD_workstation_t src,
369                                        SD_workstation_t dst,
370                                        double bytes_amount)
371 {
372
373
374   /* total time = latency + transmission time of the slowest link
375      transmission time of a link = communication amount / link bandwidth */
376
377   const SD_link_t *links;
378   int nb_links;
379   double bandwidth, min_bandwidth;
380   double latency;
381   int i;
382
383   xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
384
385
386   if (bytes_amount == 0.0)
387     return 0.0;
388
389   links = SD_route_get_list(src, dst);
390   nb_links = SD_route_get_size(src, dst);
391   min_bandwidth = -1.0;
392   latency = 0;
393
394   for (i = 0; i < nb_links; i++) {
395     latency += SD_link_get_current_latency(links[i]);
396     bandwidth = SD_link_get_current_bandwidth(links[i]);
397     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
398       min_bandwidth = bandwidth;
399   }
400
401   return latency + (bytes_amount / min_bandwidth);
402 }
403
404 /**
405  * \brief Returns the access mode of this workstation.
406  *
407  * \param workstation a workstation
408  * \return the access mode for the tasks running on this workstation:
409  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
410  *
411  * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
412  */
413 e_SD_workstation_access_mode_t
414 SD_workstation_get_access_mode(SD_workstation_t workstation)
415 {
416   return sg_host_sd(workstation)->access_mode;
417 }
418
419 /**
420  * \brief Sets the access mode for the tasks that will be executed on a workstation
421  *
422  * By default, a workstation model is shared, i.e. several tasks
423  * can be executed at the same time on a workstation. The CPU power of
424  * the workstation is shared between the running tasks on the workstation.
425  * In sequential mode, only one task can use the workstation, and the other
426  * tasks wait in a FIFO.
427  *
428  * \param workstation a workstation
429  * \param access_mode the access mode you want to set to this workstation:
430  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
431  *
432  * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
433  */
434 void SD_workstation_set_access_mode(SD_workstation_t workstation,
435                                     e_SD_workstation_access_mode_t
436                                     access_mode)
437 {
438   xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
439              access_mode != SD_WORKSTATION_SHARED_ACCESS,
440              "Trying to set an invalid access mode");
441
442   if (access_mode == sg_host_sd(workstation)->access_mode) {
443     return;                     // nothing is changed
444   }
445
446   sg_host_sd(workstation)->access_mode = access_mode;
447
448   if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
449     xbt_fifo_free(sg_host_sd(workstation)->task_fifo);
450     sg_host_sd(workstation)->task_fifo = NULL;
451   } else {
452           sg_host_sd(workstation)->task_fifo = xbt_fifo_new();
453   }
454 }
455
456 /**
457  * \brief Return the list of mounted storages on a workstation.
458  *
459  * \param workstation a workstation
460  * \return a dynar containing all mounted storages on the workstation
461  */
462 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
463   return surf_host_get_mounted_storage_list(workstation);
464 }
465
466 /**
467  * \brief Return the list of mounted storages on a workstation.
468  *
469  * \param workstation a workstation
470  * \return a dynar containing all mounted storages on the workstation
471  */
472 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
473   return surf_host_get_attached_storage_list(workstation);
474 }
475
476 /**
477  * \brief Returns the host name the storage is attached to
478  *
479  * This functions checks whether a storage is a valid pointer or not and return its name.
480  */
481 const char *SD_storage_get_host(msg_storage_t storage) {
482   xbt_assert((storage != NULL), "Invalid parameters");
483   SD_storage_priv_t priv = SD_storage_priv(storage);
484   return priv->host;
485 }
486
487 /* Returns whether a task can start now on a workstation*/
488 /*
489   int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
490   SD_CHECK_INIT_DONE();
491   xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
492
493   return !__SD_workstation_is_busy(workstation) &&
494   (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
495   }
496 */
497
498 /* Returns whether a workstation is busy. A workstation is busy is it is
499  * in sequential mode and a task is running on it or the fifo is not empty.
500  */
501 int __SD_workstation_is_busy(SD_workstation_t workstation)
502 {
503   XBT_DEBUG
504       ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
505        SD_workstation_get_name(workstation),
506        (sg_host_sd(workstation)->access_mode ==
507         SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
508        (sg_host_sd(workstation)->current_task ?
509         SD_task_get_name(sg_host_sd(workstation)->current_task)
510         : "none"),
511        (sg_host_sd(workstation)->task_fifo ? xbt_fifo_size(sg_host_sd(workstation)->task_fifo) :
512         0));
513
514   return sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
515       (sg_host_sd(workstation)->current_task != NULL
516        || xbt_fifo_size(sg_host_sd(workstation)->task_fifo) > 0);
517 }
518
519 /* Destroys a workstation.
520  */
521 void __SD_workstation_destroy(void *workstation)
522 {
523
524   if (workstation==NULL)
525           return;
526   SD_workstation_priv_t w;
527
528   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
529
530   w = (SD_workstation_priv_t) workstation;
531
532   if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
533     xbt_fifo_free(w->task_fifo);
534   }
535   xbt_free(w);
536 }
537
538 /** 
539  * \brief Returns the kind of the task currently running on a workstation
540  * Only call this with sequential access mode set
541  * \param workstation a workstation */
542 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
543 {
544   xbt_assert(sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
545               "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
546               " to use this function");
547
548   return (sg_host_sd(workstation)->current_task);
549 }
550
551 /**
552  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
553  * or router
554  *
555  * \param AS, router name
556  * \return the xbt_dict_t properties of the AS
557  */
558 xbt_dict_t SD_as_router_get_properties(const char *asr)
559 {
560   return get_as_router_properties(asr);
561 }
562 /**
563  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to the AS
564  * or router
565  *
566  * \param AS, router name
567  * \param The name of a properties
568  * \return value of the properties
569  */
570 const char* SD_as_router_get_property_value(const char *asr, const char *name)
571 {
572   xbt_dict_t dict = get_as_router_properties(asr);
573   if(!dict) return NULL;
574   return (const char*)xbt_dict_get_or_null(dict,name);
575 }