Logo AND Algorithmique Numérique Distribuée

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