Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add functions SD_link_get_list() and SD_link_get_number() + optimize SD_workstation_g...
[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     action = __SD_task_run(task);
128     surf_workstation_resource->common_public->action_set_data(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     if (elapsed_time > 0.0)
149       total_time += elapsed_time;
150
151     /* let's see which tasks are done */
152     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.done_action_set))) {
153       task = action->data;
154       INFO1("Task '%s' done", SD_task_get_name(task));
155       __SD_task_set_state(task, SD_DONE);
156
157       /* the state has changed */
158       if (!task->state_changed) {
159         task->state_changed = 1;
160         changed_tasks[changed_task_number++] = task;
161         if (changed_task_number == changed_task_capacity) {
162           changed_task_capacity *= 2;
163           changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
164         }
165         changed_tasks[changed_task_number] = NULL;
166       }
167
168       /* remove the dependencies after this task */
169       while (xbt_dynar_length(task->tasks_after) > 0) {
170         xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
171         dst = dependency->dst;
172         SD_task_dependency_remove(task, dst);
173         
174         /* is dst ready now? */
175         if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
176           INFO1("Executing task '%s'", SD_task_get_name(dst));
177           action = __SD_task_run(dst);
178           surf_workstation_resource->common_public->action_set_data(action, dst);
179           dst->state_changed = 1;
180           
181           changed_tasks[changed_task_number++] = dst;
182           if (changed_task_number == changed_task_capacity) {
183             changed_task_capacity *= 2;
184             changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
185           }
186           changed_tasks[changed_task_number] = NULL;
187         }
188       }
189     }
190
191     /* let's see which tasks have just failed */
192     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.failed_action_set))) {
193       task = action->data;
194       INFO1("Task '%s' failed", SD_task_get_name(task));
195       __SD_task_set_state(task, SD_FAILED);
196       if (!task->state_changed) {
197         task->state_changed = 1;
198         changed_tasks[changed_task_number++] = task;
199         if (changed_task_number == changed_task_capacity) {
200           changed_task_capacity *= 2;
201           changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
202         }
203         changed_tasks[changed_task_number] = NULL;
204       }
205     }
206   }
207
208   /* we must reset every task->state_changed */
209   i = 0;
210   while (changed_tasks[i] != NULL) {
211     changed_tasks[i]->state_changed = 0;
212     i++;
213   }
214
215   INFO0("Simulation finished");
216   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d", elapsed_time, total_time, sd_global->watch_point_reached);
217
218   return changed_tasks;
219 }
220
221 /**
222  * \brief Destroys all SD internal data.
223  *
224  * This function should be called when the simulation is over. Don't forget also to destroy
225  * the tasks.
226  *
227  * \see SD_init(), SD_task_destroy()
228  */
229 void SD_exit(void) {
230   if (sd_global != NULL) {
231     xbt_dict_free(&sd_global->workstations);
232     xbt_dict_free(&sd_global->links);
233
234     if (sd_global->workstation_list != NULL)
235       xbt_free(sd_global->workstation_list);
236
237     if (sd_global->link_list != NULL)
238       xbt_free(sd_global->link_list);
239
240     xbt_free(sd_global);
241
242     xbt_swag_free(sd_global->not_scheduled_task_set);
243     xbt_swag_free(sd_global->scheduled_task_set);
244     xbt_swag_free(sd_global->ready_task_set);
245     xbt_swag_free(sd_global->running_task_set);
246     xbt_swag_free(sd_global->done_task_set);
247     xbt_swag_free(sd_global->failed_task_set);
248
249     surf_exit();
250   }
251 }