Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SimDag Revolution: SD_workstation becomes sg_host
[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/host.h"
11 #include <simgrid/s4u/host.hpp>
12 #include "xbt/dict.h"
13 #include "xbt/lib.h"
14 #include "xbt/sysdep.h"
15 #include "surf/surf.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
18                                 "Logging specific to SimDag (workstation)");
19
20 /* Creates a storage and registers it in SD.
21  */
22 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
23 {
24
25   SD_storage_priv_t storage;
26   const char *name;
27
28   storage = xbt_new(s_SD_storage_priv_t, 1);
29   storage->data = data;     /* user data */
30   name = surf_resource_name((surf_cpp_resource_t)surf_storage);
31   storage->host = (const char*)surf_storage_get_host( (surf_resource_t )surf_storage_resource_by_name(name));
32   xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
33   return xbt_lib_get_elm_or_null(storage_lib, name);
34 }
35
36 /* Destroys a storage.
37  */
38 void __SD_storage_destroy(void *storage)
39 {
40   SD_storage_priv_t s;
41
42   s = (SD_storage_priv_t) storage;
43   xbt_free(s);
44 }
45
46 /**
47  * \brief Returns the workstation list
48  *
49  * Use sg_host_count() to know the array size.
50  * 
51  * \return an array of \ref sg_host_t containing all workstations
52  * \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).
53  * \see sg_host_count()
54  */
55 const sg_host_t *sg_host_list(void) {
56   xbt_assert(sg_host_count() > 0, "There is no workstation!");
57
58   if (sd_global->host_list == NULL)     /* this is the first time the function is called */
59     sd_global->host_list = (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar());
60
61   return sd_global->host_list;
62 }
63
64 /** @brief Displays debugging informations about a workstation */
65 void sg_host_dump(sg_host_t ws)
66 {
67   xbt_dict_t props;
68   xbt_dict_cursor_t cursor=NULL;
69   char *key,*data;
70
71   XBT_INFO("Displaying workstation %s", sg_host_get_name(ws));
72   XBT_INFO("  - speed: %.0f", ws->speed());
73   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(ws));
74   props = sg_host_get_properties(ws);
75   
76   if (!xbt_dict_is_empty(props)){
77     XBT_INFO("  - properties:");
78
79     xbt_dict_foreach(props,cursor,key,data) {
80       XBT_INFO("    %s->%s",key,data);
81     }
82   }
83 }
84
85 /**
86  * \brief Returns the route between two workstations
87  *
88  * Use SD_route_get_size() to know the array size.
89  *
90  * \param src a workstation
91  * \param dst another workstation
92  * \return a new array of \ref SD_link_t representing the route between these two workstations
93  * \see SD_route_get_size(), SD_link_t
94  */
95 double sg_host_speed(sg_host_t workstation)
96 {
97   return workstation->speed();
98 }
99 int sg_host_core_count(sg_host_t workstation) {
100   return workstation->core_count();
101 }
102
103
104 double sg_host_computation_time(sg_host_t workstation,
105                                            double flops_amount)
106 {
107   xbt_assert(flops_amount >= 0,
108               "flops_amount must be greater than or equal to zero");
109   return flops_amount / workstation->speed();
110 }
111
112 xbt_dict_t sg_host_get_mounted_storage_list(sg_host_t workstation){
113   return workstation->extension<simgrid::surf::Host>()->getMountedStorageList();
114 }
115
116 xbt_dynar_t sg_host_get_attached_storage_list(sg_host_t workstation){
117   return surf_host_get_attached_storage_list(workstation);
118 }
119
120 const SD_link_t *SD_route_get_list(sg_host_t src,
121                                    sg_host_t dst)
122 {
123   xbt_dynar_t surf_route;
124   void *surf_link;
125   unsigned int cpt;
126
127   if (sd_global->recyclable_route == NULL) {
128     /* first run */
129     sd_global->recyclable_route = xbt_new(SD_link_t, sg_link_count());
130   }
131
132   surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model, src, dst);
133
134   xbt_dynar_foreach(surf_route, cpt, surf_link) {
135     sd_global->recyclable_route[cpt] = (SD_link_t)surf_link;
136   }
137   return sd_global->recyclable_route;
138 }
139
140 /**
141  * \brief Returns the number of links on the route between two workstations
142  *
143  * \param src a workstation
144  * \param dst another workstation
145  * \return the number of links on the route between these two workstations
146  * \see SD_route_get_list()
147  */
148 int SD_route_get_size(sg_host_t src, sg_host_t dst)
149 {
150   return xbt_dynar_length(surf_host_model_get_route(
151                     (surf_host_model_t)surf_host_model, src, dst));
152 }
153
154 /**
155  * \brief Returns the total speed of a workstation
156  *
157  * \param workstation a workstation
158  * \return the total speed of this workstation
159  * \see sg_host_get_available_speed()
160  */
161 /**
162  * \brief Returns the amount of cores of a workstation
163  *
164  * \param workstation a workstation
165  * \return the amount of cores of this workstation
166  */
167 /**
168  * \brief Returns an approximative estimated time for the given computation amount on a workstation
169  *
170  * \param workstation a workstation
171  * \param flops_amount the computation amount you want to evaluate (in flops)
172  * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
173  */
174 /**
175  * \brief Returns the latency of the route between two workstations.
176  *
177  * \param src the first workstation
178  * \param dst the second workstation
179  * \return the latency of the route between the two workstations (in seconds)
180  * \see SD_route_get_bandwidth()
181  */
182 double SD_route_get_latency(sg_host_t src, sg_host_t dst)
183 {
184   xbt_dynar_t route = NULL;
185   double latency = 0;
186
187   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
188                                     &route, &latency);
189
190   return latency;
191 }
192
193 /**
194  * \brief Returns the bandwidth of the route between two workstations,
195  * i.e. the minimum link bandwidth of all between the workstations.
196  *
197  * \param src the first workstation
198  * \param dst the second workstation
199  * \return the bandwidth of the route between the two workstations
200  * (in bytes/second)
201  * \see SD_route_get_latency()
202  */
203 double SD_route_get_bandwidth(sg_host_t src, sg_host_t dst)
204 {
205
206   const SD_link_t *links;
207   int nb_links;
208   double bandwidth;
209   double min_bandwidth;
210   int i;
211
212   links = SD_route_get_list(src, dst);
213   nb_links = SD_route_get_size(src, dst);
214   min_bandwidth = -1.0;
215
216   for (i = 0; i < nb_links; i++) {
217     bandwidth = sg_link_bandwidth(links[i]);
218     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
219       min_bandwidth = bandwidth;
220   }
221
222   return min_bandwidth;
223 }
224
225 /**
226  * \brief Returns an approximative estimated time for the given
227  * communication amount between two workstations
228  *
229  * \param src the first workstation
230  * \param dst the second workstation
231  * \param bytes_amount the communication amount you want to evaluate (in bytes)
232  * \return an approximative estimated communication time for the given bytes amount
233  * between the workstations (in seconds)
234  */
235 double SD_route_get_communication_time(sg_host_t src,
236                                        sg_host_t dst,
237                                        double bytes_amount)
238 {
239
240
241   /* total time = latency + transmission time of the slowest link
242      transmission time of a link = communication amount / link bandwidth */
243
244   const SD_link_t *links;
245   xbt_dynar_t route = NULL;
246   int nb_links;
247   double bandwidth, min_bandwidth;
248   double latency = 0;
249   int i;
250
251   xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
252
253
254   if (bytes_amount == 0.0)
255     return 0.0;
256
257   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
258                                     &route, &latency);
259
260   links = SD_route_get_list(src, dst);
261   nb_links = SD_route_get_size(src, dst);
262   min_bandwidth = -1.0;
263
264   for (i = 0; i < nb_links; i++) {
265     bandwidth = sg_link_bandwidth(links[i]);
266     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
267       min_bandwidth = bandwidth;
268   }
269
270   return latency + (bytes_amount / min_bandwidth);
271 }
272
273 /**
274  * \brief Return the list of mounted storages on a workstation.
275  *
276  * \param workstation a workstation
277  * \return a dynar containing all mounted storages on the workstation
278  */
279 /**
280  * \brief Return the list of mounted storages on a workstation.
281  *
282  * \param workstation a workstation
283  * \return a dynar containing all mounted storages on the workstation
284  */
285 /**
286  * \brief Returns the host name the storage is attached to
287  *
288  * This functions checks whether a storage is a valid pointer or not and return its name.
289  */
290 const char *SD_storage_get_host(msg_storage_t storage) {
291   xbt_assert((storage != NULL), "Invalid parameters");
292   SD_storage_priv_t priv = SD_storage_priv(storage);
293   return priv->host;
294 }