Logo AND Algorithmique Numérique Distribuée

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