Logo AND Algorithmique Numérique Distribuée

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