Logo AND Algorithmique Numérique Distribuée

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