Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use action_free to free the actions
[simgrid.git] / src / simdag / sd_global.c
1 #include "private.h"
2 #include "xbt/sysdep.h"
3 #include "xbt/dynar.h"
4 #include "surf/surf.h"
5
6 XBT_LOG_NEW_CATEGORY(sd,"Logging specific to SimDag");
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel,sd,
8                                 "Logging specific to SimDag (kernel)");
9
10 SD_global_t sd_global = NULL;
11
12 /**
13  * \brief Initialises SD internal data
14  *
15  * This function must be called before any other SD function. Then you
16  * should call SD_create_environment().
17  *
18  * \param argc argument number
19  * \param argv argument list
20  * \see SD_create_environment(), SD_exit()
21  */
22 void SD_init(int *argc, char **argv) {
23   xbt_assert0(sd_global == NULL, "SD_init already called");
24
25   sd_global = xbt_new0(s_SD_global_t, 1);
26   sd_global->workstations = xbt_dict_new();
27   sd_global->workstation_count = 0;
28   sd_global->workstation_list = NULL;
29   sd_global->links = xbt_dict_new();
30   sd_global->link_count = 0;
31   sd_global->link_list = NULL;
32   sd_global->watch_point_reached = 0;
33
34   s_SD_task_t task;
35   sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
36   sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
37   sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
38   sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
39   sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
40   sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
41
42   surf_init(argc, argv);
43 }
44
45 /**
46  * \brief Creates the environment
47  *
48  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
49  * \ref SD_link_management "links") is created with the data stored in the given XML
50  * platform file.
51  *
52  * \param platform_file name of an XML file describing the environment to create
53  * \see SD_workstation_management, SD_link_management
54  *
55  * The XML file follows this DTD:
56  *
57  *     \include surfxml.dtd
58  *
59  * Here is a small example of such a platform: 
60  *
61  *     \include small_platform.xml
62  */
63 void SD_create_environment(const char *platform_file) {
64   xbt_dict_cursor_t cursor = NULL;
65   char *name = NULL;
66   void *surf_workstation = NULL;
67   void *surf_link = NULL;
68
69   SD_CHECK_INIT_DONE();
70
71   surf_timer_resource_init(platform_file);  /* tell Surf to create the environnement */
72
73   /*surf_workstation_resource_init_KCCFLN05(platform_file);*/
74   surf_workstation_resource_init_CLM03(platform_file);
75
76   /* now let's create the SD wrappers for workstations and links */
77   xbt_dict_foreach(workstation_set, cursor, name, surf_workstation) {
78     __SD_workstation_create(surf_workstation, NULL);
79   }
80
81   xbt_dict_foreach(network_link_set, cursor, name, surf_link) {
82     __SD_link_create(surf_link, NULL);
83   }
84 }
85
86 /**
87  * \brief Launches the simulation.
88  *
89  * The function will execute the \ref SD_READY ready tasks.
90  * The simulation will be stopped when its time reaches \a how_long,
91  * when a watch point is reached, or when no more task can be executed.
92  * Then you can call SD_simulate() again.
93  * 
94  * \param how_long maximum duration of the simulation (a negative value means no time limit)
95  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
96  * \see SD_task_schedule(), SD_task_watch()
97  */
98 SD_task_t* SD_simulate(double how_long)
99 {
100   double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
101   double elapsed_time = 0.0;
102   SD_task_t task, dst;
103   SD_dependency_t dependency;
104   surf_action_t action;
105   SD_task_t *changed_tasks = NULL;
106   int changed_task_number = 0;
107   int changed_task_capacity = 16; /* will be increased if necessary */
108   int i;
109   static int first_time = 1;
110
111   INFO0("Starting simulation...");
112
113   /* create the array that will be returned */
114   changed_tasks = xbt_new0(SD_task_t, changed_task_capacity);
115   changed_tasks[0] = NULL;
116
117   if (first_time) {
118     surf_solve(); /* Takes traces into account. Returns 0.0 */
119     first_time = 0;
120   }
121
122   sd_global->watch_point_reached = 0;
123
124   /* explore the ready tasks */
125   xbt_swag_foreach(task, sd_global->ready_task_set) {
126     INFO1("Executing task '%s'", SD_task_get_name(task));
127     task->surf_action = __SD_task_run(task);
128     surf_workstation_resource->common_public->action_set_data(task->surf_action, task);
129     task->state_changed = 1;
130     
131     changed_tasks[changed_task_number++] = task; /* replace NULL by the task */
132     if (changed_task_number == changed_task_capacity) {
133       changed_task_capacity *= 2;
134       changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
135     }
136     changed_tasks[changed_task_number] = NULL;
137   }
138
139   /* main loop */
140   elapsed_time = 0.0;
141   while (elapsed_time >= 0.0 &&
142          (how_long < 0.0 || total_time < how_long) &&
143          !sd_global->watch_point_reached) {
144
145     DEBUG1("Total time: %f", total_time);
146
147     elapsed_time = surf_solve();
148     DEBUG1("surf_solve() returns %f", elapsed_time);
149     if (elapsed_time > 0.0)
150       total_time += elapsed_time;
151
152     /* let's see which tasks are done */
153     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.done_action_set))) {
154       task = action->data;
155       INFO1("Task '%s' done", SD_task_get_name(task));
156       __SD_task_set_state(task, SD_DONE);
157       surf_workstation_resource->common_public->action_free(action);
158       task->surf_action = NULL;
159
160       /* the state has changed */
161       if (!task->state_changed) {
162         task->state_changed = 1;
163         changed_tasks[changed_task_number++] = task;
164         if (changed_task_number == changed_task_capacity) {
165           changed_task_capacity *= 2;
166           changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
167         }
168         changed_tasks[changed_task_number] = NULL;
169       }
170
171       /* remove the dependencies after this task */
172       while (xbt_dynar_length(task->tasks_after) > 0) {
173         xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
174         dst = dependency->dst;
175         SD_task_dependency_remove(task, dst);
176         
177         /* is dst ready now? */
178         if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
179           INFO1("Executing task '%s'", SD_task_get_name(dst));
180           dst->surf_action = __SD_task_run(dst);
181           surf_workstation_resource->common_public->action_set_data(dst->surf_action, dst);
182           dst->state_changed = 1;
183           
184           changed_tasks[changed_task_number++] = dst;
185           if (changed_task_number == changed_task_capacity) {
186             changed_task_capacity *= 2;
187             changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
188           }
189           changed_tasks[changed_task_number] = NULL;
190         }
191       }
192     }
193
194     /* let's see which tasks have just failed */
195     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.failed_action_set))) {
196       task = action->data;
197       INFO1("Task '%s' failed", SD_task_get_name(task));
198       __SD_task_set_state(task, SD_FAILED);
199       surf_workstation_resource->common_public->action_free(action);
200       task->surf_action = NULL;
201
202       if (!task->state_changed) {
203         task->state_changed = 1;
204         changed_tasks[changed_task_number++] = task;
205         if (changed_task_number == changed_task_capacity) {
206           changed_task_capacity *= 2;
207           changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
208         }
209         changed_tasks[changed_task_number] = NULL;
210       }
211     }
212   }
213
214   /* we must reset every task->state_changed */
215   i = 0;
216   while (changed_tasks[i] != NULL) {
217     changed_tasks[i]->state_changed = 0;
218     i++;
219   }
220
221   INFO0("Simulation finished");
222   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d", elapsed_time, total_time, sd_global->watch_point_reached);
223   DEBUG1("current time = %f", surf_get_clock());
224
225   return changed_tasks;
226 }
227
228 /**
229  * \brief Destroys all SD internal data.
230  *
231  * This function should be called when the simulation is over. Don't forget also to destroy
232  * the tasks.
233  *
234  * \see SD_init(), SD_task_destroy()
235  */
236 void SD_exit(void) {
237   if (sd_global != NULL) {
238     DEBUG0("Destroying workstation and link dictionaries...");
239     xbt_dict_free(&sd_global->workstations);
240     xbt_dict_free(&sd_global->links);
241
242     DEBUG0("Destroying workstation and link arrays if necessary...");
243     if (sd_global->workstation_list != NULL)
244       xbt_free(sd_global->workstation_list);
245
246     if (sd_global->link_list != NULL)
247       xbt_free(sd_global->link_list);
248
249     DEBUG0("Destroying the swags...");
250     xbt_swag_free(sd_global->not_scheduled_task_set);
251     xbt_swag_free(sd_global->scheduled_task_set);
252     xbt_swag_free(sd_global->ready_task_set);
253     xbt_swag_free(sd_global->running_task_set);
254     xbt_swag_free(sd_global->done_task_set);
255     xbt_swag_free(sd_global->failed_task_set);
256
257     xbt_free(sd_global);
258
259     DEBUG0("Exiting Surf...");
260     surf_exit();
261   }
262 }