Logo AND Algorithmique Numérique Distribuée

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