Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
567cd0bd71c51e13f764dd27cd247338fc79d253
[simgrid.git] / src / simdag / sd_workstation.c
1 #include "private.h"
2 #include "simdag/simdag.h"
3 #include "xbt/dict.h"
4 #include "xbt/sysdep.h"
5 #include "surf/surf.h"
6
7 /* Creates a workstation and registers it in SD.
8  */
9 SD_workstation_t __SD_workstation_create(void *surf_workstation, void *data) {
10   SD_CHECK_INIT_DONE();
11   xbt_assert0(surf_workstation != NULL, "surf_workstation is NULL !");
12
13   SD_workstation_t workstation = xbt_new0(s_SD_workstation_t, 1);
14   workstation->surf_workstation = surf_workstation;
15   workstation->data = data; /* user data */
16   
17   const char *name = SD_workstation_get_name(workstation);
18   xbt_dict_set(sd_global->workstations, name, workstation, __SD_workstation_destroy); /* add the workstation to the dictionary */
19   sd_global->workstation_count++;
20
21   return workstation;
22 }
23
24 /* Returns a workstation given its name, or NULL if there is no such workstation.
25  */
26 SD_workstation_t SD_workstation_get_by_name(const char *name) {
27   SD_CHECK_INIT_DONE();
28
29   xbt_assert0(name != NULL, "Invalid parameter");
30
31   return xbt_dict_get_or_null(sd_global->workstations, name);
32 }
33
34 /* Returns a NULL-terminated array of existing workstations.
35  */
36 SD_workstation_t*  SD_workstation_get_list(void) {
37   SD_CHECK_INIT_DONE();
38
39   SD_workstation_t* array = xbt_new0(SD_workstation_t, sd_global->workstation_count + 1);
40   
41   xbt_dict_cursor_t cursor;
42   char *key;
43   void *data;
44   int i=0;
45
46   xbt_dict_foreach(sd_global->workstations, cursor, key, data) {
47     array[i++] = (SD_workstation_t) data;
48   }
49   array[i] = NULL;
50
51   return array;
52 }
53
54 /* Returns the number or workstations.
55  */
56 int SD_workstation_get_number(void) {
57   SD_CHECK_INIT_DONE();
58   return sd_global->workstation_count;
59 }
60
61 /* Sets the data of a workstation. The new data can be NULL. The old data should have been freed first if it was not NULL.
62  */
63 void SD_workstation_set_data(SD_workstation_t workstation, void *data) {
64   SD_CHECK_INIT_DONE();
65   xbt_assert0(workstation != NULL, "Invalid parameter");
66   workstation->data = data;
67 }
68
69 /* Returns the data of a workstation. The user data can be NULL.
70  */
71 void* SD_workstation_get_data(SD_workstation_t workstation) {
72   SD_CHECK_INIT_DONE();
73   xbt_assert0(workstation != NULL, "Invalid parameter");
74   return workstation->data;
75 }
76
77 /* Returns the name of a workstation.
78  */
79 const char* SD_workstation_get_name(SD_workstation_t workstation) {
80   SD_CHECK_INIT_DONE();
81   xbt_assert0(workstation != NULL, "Invalid parameter");
82   return surf_workstation_resource->common_public->get_resource_name(workstation->surf_workstation);
83 }
84
85 /* Returns an new array of links representating the route between two workstations.
86  */
87 SD_link_t* SD_workstation_route_get_list(SD_workstation_t src, SD_workstation_t dst) {
88   SD_CHECK_INIT_DONE();
89
90   void *surf_src = src->surf_workstation;
91   void *surf_dst = dst->surf_workstation;
92
93   const void **surf_route = surf_workstation_resource->extension_public->get_route(surf_src, surf_dst);
94   int route_size = surf_workstation_resource->extension_public->get_route_size(surf_src, surf_dst);
95
96   SD_link_t* route = xbt_new0(SD_link_t, route_size);
97   const char *link_name;
98   int i;
99   for (i = 0; i < route_size; i++) {
100     link_name = surf_workstation_resource->extension_public->get_link_name(surf_route[i]);
101     route[i] = xbt_dict_get(sd_global->links, link_name);
102   }
103
104   return route;
105 }
106
107 /* Returns the number of links on the route between two workstations.
108  */
109 int SD_workstation_route_get_size(SD_workstation_t src, SD_workstation_t dst) {
110   SD_CHECK_INIT_DONE();
111   return surf_workstation_resource->extension_public->
112     get_route_size(src->surf_workstation, dst->surf_workstation);
113 }
114
115 /* Returns the total power of a workstation.
116  */
117 double SD_workstation_get_power(SD_workstation_t workstation) {
118   SD_CHECK_INIT_DONE();
119   xbt_assert0(workstation != NULL, "Invalid parameter");
120   return surf_workstation_resource->extension_public->get_speed(workstation->surf_workstation, 1.0);
121 }
122
123 /* Returns the proportion of available power in a workstation (normally a number between 0 and 1).
124  */
125 double SD_workstation_get_available_power(SD_workstation_t workstation) {
126   SD_CHECK_INIT_DONE();
127   xbt_assert0(workstation != NULL, "Invalid parameter");
128   return surf_workstation_resource->extension_public->get_available_speed(workstation->surf_workstation);
129 }
130
131 /* Destroys a workstation. The user data (if any) should have been destroyed first.
132  */
133 void __SD_workstation_destroy(void *workstation) {
134   SD_CHECK_INIT_DONE();
135   xbt_assert0(workstation != NULL, "Invalid parameter");
136   /* workstation->surf_workstation is freed by surf_exit and workstation->data is freed by the user */
137   xbt_free(workstation);
138 }