Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
test of sd_test
[simgrid.git] / src / simdag / sd_workstation.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. 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 "private.h"
8 #include "simdag/simdag.h"
9 #include "xbt/dict.h"
10 #include "xbt/sysdep.h"
11 #include "surf/surf.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_workstation, sd,
14                                 "Logging specific to SimDag (workstation)");
15
16 /* Creates a workstation and registers it in SD.
17  */
18 SD_workstation_t __SD_workstation_create(void *surf_workstation, void *data)
19 {
20
21   SD_workstation_t workstation;
22   const char *name;
23   SD_CHECK_INIT_DONE();
24   xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
25
26   workstation = xbt_new(s_SD_workstation_t, 1);
27   workstation->surf_workstation = surf_workstation;
28   workstation->data = data;     /* user data */
29   workstation->access_mode = SD_WORKSTATION_SHARED_ACCESS;      /* default mode is shared */
30   workstation->task_fifo = NULL;
31   workstation->current_task = NULL;
32
33   name = SD_workstation_get_name(workstation);
34   xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy);   /* add the workstation to the dictionary */
35   sd_global->workstation_count++;
36
37   return workstation;
38 }
39
40 /**
41  * \brief Returns a workstation given its name
42  *
43  * If there is no such workstation, the function returns \c NULL.
44  *
45  * \param name workstation name
46  * \return the workstation, or \c NULL if there is no such workstation
47  */
48 SD_workstation_t SD_workstation_get_by_name(const char *name)
49 {
50   SD_CHECK_INIT_DONE();
51
52   xbt_assert0(name != NULL, "Invalid parameter");
53
54   return xbt_dict_get_or_null(sd_global->workstations, name);
55 }
56
57 /**
58  * \brief Returns the workstation list
59  *
60  * Use SD_workstation_get_number() to know the array size.
61  *
62  * \return an array of \ref SD_workstation_t containing all workstations
63  * \see SD_workstation_get_number()
64  */
65 const SD_workstation_t *SD_workstation_get_list(void)
66 {
67
68   xbt_dict_cursor_t cursor;
69   char *key;
70   void *data;
71   int i;
72
73   SD_CHECK_INIT_DONE();
74   xbt_assert0(SD_workstation_get_number() > 0, "There is no workstation!");
75
76   if (sd_global->workstation_list == NULL) {    /* this is the first time the function is called */
77     sd_global->workstation_list =
78       xbt_new(SD_workstation_t, sd_global->workstation_count);
79
80     i = 0;
81     xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
82       sd_global->workstation_list[i++] = (SD_workstation_t) data;
83     }
84   }
85   return sd_global->workstation_list;
86 }
87
88 /**
89  * \brief Returns the number of workstations
90  *
91  * \return the number of existing workstations
92  * \see SD_workstation_get_list()
93  */
94 int SD_workstation_get_number(void)
95 {
96   SD_CHECK_INIT_DONE();
97   return sd_global->workstation_count;
98 }
99
100 /**
101  * \brief Returns the user data of a workstation
102  *
103  * \param workstation a workstation
104  * \return the user data associated with this workstation (can be \c NULL)
105  * \see SD_workstation_set_data()
106  */
107 void *SD_workstation_get_data(SD_workstation_t workstation)
108 {
109   SD_CHECK_INIT_DONE();
110   xbt_assert0(workstation != NULL, "Invalid parameter");
111   return workstation->data;
112 }
113
114 /**
115  * \brief Sets the user data of a workstation
116  *
117  * The new data can be \c NULL. The old data should have been freed first
118  * if it was not \c NULL.
119  *
120  * \param workstation a workstation
121  * \param data the new data you want to associate with this workstation
122  * \see SD_workstation_get_data()
123  */
124 void SD_workstation_set_data(SD_workstation_t workstation, void *data)
125 {
126   SD_CHECK_INIT_DONE();
127   xbt_assert0(workstation != NULL, "Invalid parameter");
128   workstation->data = 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   SD_CHECK_INIT_DONE();
140   xbt_assert0(workstation != NULL, "Invalid parameter");
141   return surf_resource_name(workstation->surf_workstation);
142 }
143
144 /**
145  * \brief Returns the value of a given workstation property
146  *
147  * \param ws a workstation
148  * \param name a property name
149  * \return value of a property (or NULL if property not set)
150  */
151 const char *SD_workstation_get_property_value(SD_workstation_t ws,
152                                               const char *name)
153 {
154   return xbt_dict_get_or_null(SD_workstation_get_properties(ws), name);
155 }
156
157
158 /**
159  * \brief Returns a #xbt_dict_t consisting of the list of properties assigned to this workstation
160  *
161  * \param workstation a workstation
162  * \return the dictionary containing the properties associated with the workstation
163  */
164 xbt_dict_t SD_workstation_get_properties(SD_workstation_t workstation)
165 {
166   SD_CHECK_INIT_DONE();
167   xbt_assert0((workstation != NULL), "Invalid parameters");
168
169   return surf_workstation_model->extension.workstation.get_properties(workstation->surf_workstation);
170
171 }
172
173
174 /**
175  * \brief Returns the route between two workstations
176  *
177  * Use SD_route_get_size() to know the array size.
178  *
179  * \param src a workstation
180  * \param dst another workstation
181  * \return a new array of \ref SD_link_t representating the route between these two workstations
182  * \see SD_route_get_size(), SD_link_t
183  */
184 const SD_link_t *SD_route_get_list(SD_workstation_t src, SD_workstation_t dst)
185 {
186   void *surf_src;
187   void *surf_dst;
188   xbt_dynar_t surf_route;
189   const char *link_name;
190   void *surf_link;
191   unsigned int cpt;
192
193   SD_CHECK_INIT_DONE();
194
195   if (sd_global->recyclable_route == NULL) {
196     /* first run */
197     sd_global->recyclable_route = xbt_new(SD_link_t, SD_link_get_number());
198   }
199
200   surf_src = &src->surf_workstation;
201   surf_dst = &dst->surf_workstation;
202
203   surf_route = surf_workstation_model->extension.workstation.get_route(surf_src,surf_dst);
204
205   if(surf_route == NULL)
206   {
207           INFO0("PBLM1 surf_route == NULL");
208   }
209
210   xbt_dynar_foreach(surf_route, cpt, surf_link) {
211     link_name = surf_resource_name(surf_link);
212     sd_global->recyclable_route[cpt] =
213       xbt_dict_get(sd_global->links, link_name);
214   }
215   INFO0("FIN");
216   return sd_global->recyclable_route;
217 }
218
219 /**
220  * \brief Returns the number of links on the route between two workstations
221  *
222  * \param src a workstation
223  * \param dst another workstation
224  * \return the number of links on the route between these two workstations
225  * \see SD_route_get_list()
226  */
227 int SD_route_get_size(SD_workstation_t src, SD_workstation_t dst)
228 {
229   SD_CHECK_INIT_DONE();
230   return xbt_dynar_length(surf_workstation_model->extension.workstation.get_route(
231         src->surf_workstation,dst->surf_workstation));
232 }
233
234 /**
235  * \brief Returns the total power of a workstation
236  *
237  * \param workstation a workstation
238  * \return the total power of this workstation
239  * \see SD_workstation_get_available_power()
240  */
241 double SD_workstation_get_power(SD_workstation_t workstation)
242 {
243   SD_CHECK_INIT_DONE();
244   xbt_assert0(workstation != NULL, "Invalid parameter");
245   return surf_workstation_model->extension.workstation.
246     get_speed(workstation->surf_workstation, 1.0);
247 }
248
249 /**
250  * \brief Returns the proportion of available power in a workstation
251  *
252  * \param workstation a workstation
253  * \return the proportion of power currently available in this workstation (normally a number between 0 and 1)
254  * \see SD_workstation_get_power()
255  */
256 double SD_workstation_get_available_power(SD_workstation_t workstation)
257 {
258   SD_CHECK_INIT_DONE();
259   xbt_assert0(workstation != NULL, "Invalid parameter");
260   return surf_workstation_model->extension.
261     workstation.get_available_speed(workstation->surf_workstation);
262 }
263
264 /**
265  * \brief Returns an approximative estimated time for the given computation amount on a workstation
266  *
267  * \param workstation a workstation
268  * \param computation_amount the computation amount you want to evaluate (in flops)
269  * \return an approximative astimated computation time for the given computation amount on this workstation (in seconds)
270  */
271 double SD_workstation_get_computation_time(SD_workstation_t workstation,
272                                            double computation_amount)
273 {
274   SD_CHECK_INIT_DONE();
275   xbt_assert0(workstation != NULL, "Invalid parameter");
276   xbt_assert0(computation_amount >= 0,
277               "computation_amount must be greater than or equal to zero");
278   return computation_amount / SD_workstation_get_power(workstation);
279 }
280
281 /**
282  * \brief Returns the latency of the route between two workstations, i.e. the sum of all link latencies
283  * between the workstations.
284  *
285  * \param src the first workstation
286  * \param dst the second workstation
287  * \return the latency of the route between the two workstations (in seconds)
288  * \see SD_route_get_current_bandwidth()
289  */
290 double SD_route_get_current_latency(SD_workstation_t src,
291                                     SD_workstation_t dst)
292 {
293
294   const SD_link_t *links;
295   int nb_links;
296   double latency;
297   int i;
298
299   SD_CHECK_INIT_DONE();
300   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
301   links = SD_route_get_list(src, dst);
302   nb_links = SD_route_get_size(src, dst);
303   latency = 0.0;
304
305   for (i = 0; i < nb_links; i++) {
306     latency += SD_link_get_current_latency(links[i]);
307   }
308
309   return latency;
310 }
311
312 /**
313  * \brief Returns the bandwidth of the route between two workstations, i.e. the minimum link bandwidth of all
314  * between the workstations.
315  *
316  * \param src the first workstation
317  * \param dst the second workstation
318  * \return the bandwidth of the route between the two workstations (in bytes/second)
319  * \see SD_route_get_current_latency()
320  */
321 double SD_route_get_current_bandwidth(SD_workstation_t src,
322                                       SD_workstation_t dst)
323 {
324
325   const SD_link_t *links;
326   int nb_links;
327   double bandwidth;
328   double min_bandwidth;
329   int i;
330
331   SD_CHECK_INIT_DONE();
332   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
333
334   links = SD_route_get_list(src, dst);
335   nb_links = SD_route_get_size(src, dst);
336   bandwidth = min_bandwidth = -1.0;
337
338
339   for (i = 0; i < nb_links; i++) {
340     bandwidth = SD_link_get_current_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 communication_amount the communication amount you want to evaluate (in bytes)
355  * \return an approximative astimated computation time for the given communication 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 communication_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   int nb_links;
369   double bandwidth, min_bandwidth;
370   double latency;
371   int i;
372
373   SD_CHECK_INIT_DONE();
374   xbt_assert0(src != NULL && dst != NULL, "Invalid parameter");
375   xbt_assert0(communication_amount >= 0,
376               "communication_amount must be greater than or equal to zero");
377
378
379
380   if (communication_amount == 0.0)
381     return 0.0;
382
383   links = SD_route_get_list(src, dst);
384   nb_links = SD_route_get_size(src, dst);
385   min_bandwidth = -1.0;
386   latency = 0;
387
388   for (i = 0; i < nb_links; i++) {
389     latency += SD_link_get_current_latency(links[i]);
390     bandwidth = SD_link_get_current_bandwidth(links[i]);
391     if (bandwidth < min_bandwidth || min_bandwidth == -1.0)
392       min_bandwidth = bandwidth;
393   }
394
395   return latency + (communication_amount / min_bandwidth);
396 }
397
398 /**
399  * \brief Returns the access mode of this workstation.
400  *
401  * \param workstation a workstation
402  * \return the access mode for the tasks running on this workstation:
403  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
404  *
405  * \see SD_workstation_set_access_mode(), e_SD_workstation_access_mode_t
406  */
407 e_SD_workstation_access_mode_t SD_workstation_get_access_mode(SD_workstation_t
408                                                               workstation)
409 {
410   SD_CHECK_INIT_DONE();
411   xbt_assert0(workstation != NULL, "Invalid parameter");
412   return workstation->access_mode;
413 }
414
415 /**
416  * \brief Sets the access mode for the tasks that will be executed on a workstation
417  *
418  * By default, a workstation model is shared, i.e. several tasks
419  * can be executed at the same time on a workstation. The CPU power of
420  * the workstation is shared between the running tasks on the workstation.
421  * In sequential mode, only one task can use the workstation, and the other
422  * tasks wait in a FIFO.
423  *
424  * \param workstation a workstation
425  * \param access_mode the access mode you want to set to this workstation:
426  * SD_WORKSTATION_SHARED_ACCESS or SD_WORKSTATION_SEQUENTIAL_ACCESS
427  *
428  * \see SD_workstation_get_access_mode(), e_SD_workstation_access_mode_t
429  */
430 void SD_workstation_set_access_mode(SD_workstation_t workstation,
431                                     e_SD_workstation_access_mode_t
432                                     access_mode)
433 {
434   SD_CHECK_INIT_DONE();
435   xbt_assert0(workstation != NULL, "Invalid parameter");
436
437   if (access_mode == workstation->access_mode) {
438     return;                     // nothing is changed
439   }
440
441   workstation->access_mode = access_mode;
442
443   if (access_mode == SD_WORKSTATION_SHARED_ACCESS) {
444     xbt_fifo_free(workstation->task_fifo);
445     workstation->task_fifo = NULL;
446   } else {
447     workstation->task_fifo = xbt_fifo_new();
448   }
449 }
450
451 /* Returns whether a task can start now on a workstation.
452                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               *//*
453                                                                                                                                                                                                                                                                                                                                                                                                    int __SD_workstation_can_start(SD_workstation_t workstation, SD_task_t task) {
454                                                                                                                                                                                                                                                                                                                                                                                                    SD_CHECK_INIT_DONE();
455                                                                                                                                                                                                                                                                                                                                                                                                    xbt_assert0(workstation != NULL && task != NULL, "Invalid parameter");
456
457                                                                                                                                                                                                                                                                                                                                                                                                    return !__SD_workstation_is_busy(workstation) &&
458                                                                                                                                                                                                                                                                                                                                                                                                    (xbt_fifo_size(workstation->task_fifo) == 0) || xbt_fifo_get_first_item(workstation->task_fifo) == task);
459                                                                                                                                                                                                                                                                                                                                                                                                    }
460                                                                                                                                                                                                                                                                                                                                                                                                  */
461
462 /* Returns whether a workstation is busy. A workstation is busy is it is
463  * in sequential mode and a task is running on it or the fifo is not empty.
464  */
465 int __SD_workstation_is_busy(SD_workstation_t workstation)
466 {
467   SD_CHECK_INIT_DONE();
468   xbt_assert0(workstation != NULL, "Invalid parameter");
469
470   DEBUG4
471     ("Workstation '%s' access mode: '%s', current task: %s, fifo size: %d",
472      SD_workstation_get_name(workstation),
473      (workstation->access_mode ==
474       SD_WORKSTATION_SHARED_ACCESS) ? "SHARED" : "FIFO",
475      (workstation->current_task ? SD_task_get_name(workstation->current_task)
476       : "none"),
477      (workstation->task_fifo ? xbt_fifo_size(workstation->task_fifo) : 0));
478
479   return workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS &&
480     (workstation->current_task != NULL
481      || xbt_fifo_size(workstation->task_fifo) > 0);
482 }
483
484 /* Destroys a workstation.
485  */
486 void __SD_workstation_destroy(void *workstation)
487 {
488
489   SD_workstation_t w;
490
491   SD_CHECK_INIT_DONE();
492   xbt_assert0(workstation != NULL, "Invalid parameter");
493   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
494
495   w = (SD_workstation_t) workstation;
496
497   if (w->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS) {
498     xbt_fifo_free(w->task_fifo);
499   }
500   xbt_free(w);
501 }
502
503 /** 
504  * \brief Returns the kind of the task currently running on a workstation
505  * Only call this with sequential access mode set
506  * \param workstation a workstation */
507 SD_task_t SD_workstation_get_current_task(SD_workstation_t workstation)
508 {
509   SD_CHECK_INIT_DONE();
510   xbt_assert0(workstation != NULL, "Invalid parameter");
511   xbt_assert0(workstation->access_mode == SD_WORKSTATION_SEQUENTIAL_ACCESS, 
512               "Access mode must be set to SD_WORKSTATION_SEQUENTIAL_ACCESS"
513               " to use this function");
514   
515   return (workstation->current_task);
516 }