Logo AND Algorithmique Numérique Distribuée

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