Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1413ad352efc4665ce830d8e516d6b9bd5b2cccf
[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 /* Creates a storage and registers it in SD.
18  */
19 SD_storage_t __SD_storage_create(void *surf_storage, void *data)
20 {
21
22   SD_storage_priv_t storage;
23   const char *name;
24
25   storage = xbt_new(s_SD_storage_priv_t, 1);
26   storage->data = data;     /* user data */
27   name = surf_resource_name((surf_cpp_resource_t)surf_storage);
28   storage->host = (const char*)surf_storage_get_host( (surf_resource_t )surf_storage_resource_by_name(name));
29   xbt_lib_set(storage_lib,name, SD_STORAGE_LEVEL, storage);
30   return xbt_lib_get_elm_or_null(storage_lib, name);
31 }
32
33 /* Destroys a storage.
34  */
35 void __SD_storage_destroy(void *storage)
36 {
37   SD_storage_priv_t s;
38
39   s = (SD_storage_priv_t) storage;
40   xbt_free(s);
41 }
42
43 double sg_host_computation_time(sg_host_t host, double flops_amount)
44 {
45   xbt_assert(flops_amount >= 0,
46               "flops_amount must be greater than or equal to zero");
47   return flops_amount / sg_host_speed(host);
48 }
49
50 /**
51  * \brief Returns the route between two workstations
52  *
53  * Use SD_route_get_size() to know the array size.
54  *
55  * \param src a workstation
56  * \param dst another workstation
57  * \return a new array of \ref SD_link_t representing the route between these two workstations
58  * \see SD_route_get_size(), SD_link_t
59  */
60 const SD_link_t *SD_route_get_list(sg_host_t src,
61                                    sg_host_t dst)
62 {
63   xbt_dynar_t surf_route;
64   void *surf_link;
65   unsigned int cpt;
66
67   if (sd_global->recyclable_route == NULL) {
68     /* first run */
69     sd_global->recyclable_route = xbt_new(SD_link_t, sg_link_count());
70   }
71
72   surf_route = surf_host_model_get_route((surf_host_model_t)surf_host_model, src, dst);
73
74   xbt_dynar_foreach(surf_route, cpt, surf_link) {
75     sd_global->recyclable_route[cpt] = (SD_link_t)surf_link;
76   }
77   return sd_global->recyclable_route;
78 }
79
80 /**
81  * \brief Returns the number of links on the route between two workstations
82  *
83  * \param src a workstation
84  * \param dst another workstation
85  * \return the number of links on the route between these two workstations
86  * \see SD_route_get_list()
87  */
88 int SD_route_get_size(sg_host_t src, sg_host_t dst)
89 {
90   return xbt_dynar_length(surf_host_model_get_route(
91                     (surf_host_model_t)surf_host_model, src, dst));
92 }
93
94 /**
95  * \brief Returns an approximative estimated time for the given computation amount on a workstation
96  *
97  * \param workstation a workstation
98  * \param flops_amount the computation amount you want to evaluate (in flops)
99  * \return an approximative estimated computation time for the given computation amount on this workstation (in seconds)
100  */
101 /**
102  * \brief Returns the latency of the route between two workstations.
103  *
104  * \param src the first workstation
105  * \param dst the second workstation
106  * \return the latency of the route between the two workstations (in seconds)
107  * \see SD_route_get_bandwidth()
108  */
109 double SD_route_get_latency(sg_host_t src, sg_host_t dst)
110 {
111   xbt_dynar_t route = NULL;
112   double latency = 0;
113
114   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
115                                     &route, &latency);
116
117   return latency;
118 }
119
120 /**
121  * \brief Returns the bandwidth of the route between two workstations,
122  * i.e. the minimum link bandwidth of all between the workstations.
123  *
124  * \param src the first workstation
125  * \param dst the second workstation
126  * \return the bandwidth of the route between the two workstations
127  * (in bytes/second)
128  * \see SD_route_get_latency()
129  */
130 double SD_route_get_bandwidth(sg_host_t src, sg_host_t dst)
131 {
132
133   const SD_link_t *links;
134   int nb_links;
135   double bandwidth;
136   double min_bandwidth;
137   int i;
138
139   links = SD_route_get_list(src, dst);
140   nb_links = SD_route_get_size(src, dst);
141   min_bandwidth = -1.0;
142
143   for (i = 0; i < nb_links; i++) {
144     bandwidth = sg_link_bandwidth(links[i]);
145     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
146       min_bandwidth = bandwidth;
147   }
148
149   return min_bandwidth;
150 }
151
152 /**
153  * \brief Returns an approximative estimated time for the given
154  * communication amount between two workstations
155  *
156  * \param src the first workstation
157  * \param dst the second workstation
158  * \param bytes_amount the communication amount you want to evaluate (in bytes)
159  * \return an approximative estimated communication time for the given bytes amount
160  * between the workstations (in seconds)
161  */
162 double SD_route_get_communication_time(sg_host_t src,
163                                        sg_host_t dst,
164                                        double bytes_amount)
165 {
166
167
168   /* total time = latency + transmission time of the slowest link
169      transmission time of a link = communication amount / link bandwidth */
170
171   const SD_link_t *links;
172   xbt_dynar_t route = NULL;
173   int nb_links;
174   double bandwidth, min_bandwidth;
175   double latency = 0;
176   int i;
177
178   xbt_assert(bytes_amount >= 0, "bytes_amount must be greater than or equal to zero");
179
180
181   if (bytes_amount == 0.0)
182     return 0.0;
183
184   routing_platf->getRouteAndLatency(src->pimpl_netcard, dst->pimpl_netcard,
185                                     &route, &latency);
186
187   links = SD_route_get_list(src, dst);
188   nb_links = SD_route_get_size(src, dst);
189   min_bandwidth = -1.0;
190
191   for (i = 0; i < nb_links; i++) {
192     bandwidth = sg_link_bandwidth(links[i]);
193     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
194       min_bandwidth = bandwidth;
195   }
196
197   return latency + (bytes_amount / min_bandwidth);
198 }
199
200 /**
201  * \brief Return the list of mounted storages on a workstation.
202  *
203  * \param workstation a workstation
204  * \return a dynar containing all mounted storages on the workstation
205  */
206 /**
207  * \brief Return the list of mounted storages on a workstation.
208  *
209  * \param workstation a workstation
210  * \return a dynar containing all mounted storages on the workstation
211  */
212 /**
213  * \brief Returns the host name the storage is attached to
214  *
215  * This functions checks whether a storage is a valid pointer or not and return its name.
216  */
217 const char *SD_storage_get_host(msg_storage_t storage) {
218   xbt_assert((storage != NULL), "Invalid parameters");
219   SD_storage_priv_t priv = SD_storage_priv(storage);
220   return priv->host;
221 }