Logo AND Algorithmique Numérique Distribuée

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