Logo AND Algorithmique Numérique Distribuée

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