Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d37ceadd37448e56df7480e3ab335cffd0c2738e
[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/surf/host_interface.hpp"
8 #include "src/simdag/simdag_private.h"
9 #include "simgrid/simdag.h"
10 #include <simgrid/s4u/host.hpp>
11 #include "xbt/dict.h"
12 #include "xbt/lib.h"
13 #include "xbt/sysdep.h"
14 #include "surf/surf.h"
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_count() > 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_count(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("  - speed: %.0f", SD_workstation_get_speed(ws));
178   XBT_INFO("  - available speed: %.2f", SD_workstation_get_available_speed(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 representing 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, sg_link_count());
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 speed of a workstation
251  *
252  * \param workstation a workstation
253  * \return the total speed of this workstation
254  * \see SD_workstation_get_available_speed()
255  */
256 double SD_workstation_get_speed(SD_workstation_t workstation)
257 {
258   return workstation->speed();
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 workstation->core_count();
268 }
269
270 /**
271  * \brief Returns the proportion of available speed in a workstation
272  *
273  * \param workstation a workstation
274  * \return the proportion of speed currently available in this workstation (normally a number between 0 and 1)
275  * \see SD_workstation_get_speed()
276  */
277 double SD_workstation_get_available_speed(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_speed(workstation);
295 }
296
297 /**
298  * \brief Returns the latency of the route between two workstations.
299  *
300  * \param src the first workstation
301  * \param dst the second workstation
302  * \return the latency of the route between the two workstations (in seconds)
303  * \see SD_route_get_bandwidth()
304  */
305 double SD_route_get_latency(SD_workstation_t src, SD_workstation_t dst)
306 {
307   xbt_dynar_t route = NULL;
308   double latency = 0;
309
310   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
311                                     &route, &latency);
312
313   return latency;
314 }
315
316 /**
317  * \brief Returns the bandwidth of the route between two workstations,
318  * i.e. the minimum link bandwidth of all between the workstations.
319  *
320  * \param src the first workstation
321  * \param dst the second workstation
322  * \return the bandwidth of the route between the two workstations
323  * (in bytes/second)
324  * \see SD_route_get_latency()
325  */
326 double SD_route_get_bandwidth(SD_workstation_t src, SD_workstation_t dst)
327 {
328
329   const SD_link_t *links;
330   int nb_links;
331   double bandwidth;
332   double min_bandwidth;
333   int i;
334
335   links = SD_route_get_list(src, dst);
336   nb_links = SD_route_get_size(src, dst);
337   min_bandwidth = -1.0;
338
339   for (i = 0; i < nb_links; i++) {
340     bandwidth = sg_link_bandwidth(links[i]);
341     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
342       min_bandwidth = bandwidth;
343   }
344
345   return min_bandwidth;
346 }
347
348 /**
349  * \brief Returns an approximative estimated time for the given
350  * communication amount between two workstations
351  *
352  * \param src the first workstation
353  * \param dst the second workstation
354  * \param bytes_amount the communication amount you want to evaluate (in bytes)
355  * \return an approximative estimated communication time for the given bytes amount
356  * between the workstations (in seconds)
357  */
358 double SD_route_get_communication_time(SD_workstation_t src,
359                                        SD_workstation_t dst,
360                                        double bytes_amount)
361 {
362
363
364   /* total time = latency + transmission time of the slowest link
365      transmission time of a link = communication amount / link bandwidth */
366
367   const SD_link_t *links;
368   xbt_dynar_t route = NULL;
369   int nb_links;
370   double bandwidth, min_bandwidth;
371   double latency = 0;
372   int i;
373
374   xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
375
376
377   if (bytes_amount == 0.0)
378     return 0.0;
379
380   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
381                                     &route, &latency);
382
383   links = SD_route_get_list(src, dst);
384   nb_links = SD_route_get_size(src, dst);
385   min_bandwidth = -1.0;
386
387   for (i = 0; i < nb_links; i++) {
388     bandwidth = sg_link_bandwidth(links[i]);
389     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
390       min_bandwidth = bandwidth;
391   }
392
393   return latency + (bytes_amount / min_bandwidth);
394 }
395
396 /**
397  * \brief Returns the access mode of this workstation.
398  *
399  * \param workstation a workstation
400  * \return the access mode for the tasks running on this workstation:
401  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
402  *
403  * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
404  */
405 e_SD_workstation_access_mode_t
406 SD_workstation_get_access_mode(SD_workstation_t workstation)
407 {
408   return sg_host_sd(workstation)->access_mode;
409 }
410
411 /**
412  * \brief Sets the access mode for the tasks that will be executed on a workstation
413  *
414  * By default, a workstation model is shared, i.e. several tasks
415  * can be executed at the same time on a workstation. The CPU power of
416  * the workstation is shared between the running tasks on the workstation.
417  * In sequential mode, only one task can use the workstation, and the other
418  * tasks wait in a FIFO.
419  *
420  * \param workstation a workstation
421  * \param access_mode the access mode you want to set to this workstation:
422  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
423  *
424  * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
425  */
426 void SD_workstation_set_access_mode(SD_workstation_t workstation,
427                                     e_SD_workstation_access_mode_t
428                                     access_mode)
429 {
430   xbt_assert(access_mode != SD_WORKSTATION_SEQUENTIAL_ACCESS ||
431              access_mode != SD_WORKSTATION_SHARED_ACCESS,
432              "Trying to set an invalid access mode");
433
434   if (access_mode == sg_host_sd(workstation)->access_mode) {
435     return;                     // nothing is changed
436   }
437
438   sg_host_sd(workstation)->access_mode = access_mode;
439
440   if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
441     xbt_fifo_free(sg_host_sd(workstation)->task_fifo);
442     sg_host_sd(workstation)->task_fifo = NULL;
443   } else {
444           sg_host_sd(workstation)->task_fifo = xbt_fifo_new();
445   }
446 }
447
448 /**
449  * \brief Return the list of mounted storages on a workstation.
450  *
451  * \param workstation a workstation
452  * \return a dynar containing all mounted storages on the workstation
453  */
454 xbt_dict_t SD_workstation_get_mounted_storage_list(SD_workstation_t workstation){
455   return workstation->extension<simgrid::surf::Host>()->getMountedStorageList();
456 }
457
458 /**
459  * \brief Return the list of mounted storages on a workstation.
460  *
461  * \param workstation a workstation
462  * \return a dynar containing all mounted storages on the workstation
463  */
464 xbt_dynar_t SD_workstation_get_attached_storage_list(SD_workstation_t workstation){
465   return surf_host_get_attached_storage_list(workstation);
466 }
467
468 /**
469  * \brief Returns the host name the storage is attached to
470  *
471  * This functions checks whether a storage is a valid pointer or not and return its name.
472  */
473 const char *SD_storage_get_host(msg_storage_t storage) {
474   xbt_assert((storage != NULL), "Invalid parameters");
475   SD_storage_priv_t priv = SD_storage_priv(storage);
476   return priv->host;
477 }
478
479 /* Returns whether a task can start now on a workstation*/
480 /*
481   int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
482   SD_CHECK_INIT_DONE();
483   xbt_assert(workstation != NULL && task != NULL, "Invalid parameter");
484
485   return !__SD_workstation_is_busy(workstation) &&
486   (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
487   }
488 */
489
490 /* Returns whether a workstation is busy. A workstation is busy is it is
491  * in sequential mode and a task is running on it or the fifo is not empty.
492  */
493 int __SD_workstation_is_busy(SD_workstation_t workstation)
494 {
495   XBT_DEBUG
496       ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
497        SD_workstation_get_name(workstation),
498        (sg_host_sd(workstation)->access_mode ==
499         SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
500        (sg_host_sd(workstation)->current_task ?
501         SD_task_get_name(sg_host_sd(workstation)->current_task)
502         : "none"),
503        (sg_host_sd(workstation)->task_fifo ? xbt_fifo_size(sg_host_sd(workstation)->task_fifo) :
504         0));
505
506   return sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
507       (sg_host_sd(workstation)->current_task != NULL
508        || xbt_fifo_size(sg_host_sd(workstation)->task_fifo) > 0);
509 }
510
511 /* Destroys a workstation.
512  */
513 void __SD_workstation_destroy(void *workstation)
514 {
515
516   if (workstation==NULL)
517           return;
518   SD_workstation_priv_t w;
519
520   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
521
522   w = (SD_workstation_priv_t) workstation;
523
524   if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
525     xbt_fifo_free(w->task_fifo);
526   }
527   xbt_free(w);
528 }
529
530 /** 
531  * \brief Returns the kind of the task currently running on a workstation
532  * Only call this with sequential access mode set
533  * \param workstation a workstation */
534 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
535 {
536   xbt_assert(sg_host_sd(workstation)->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS,
537               "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
538               " to use this function");
539
540   return (sg_host_sd(workstation)->current_task);
541 }
542