Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add documentation for workstations and fix a few bugs
[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 /* Initialises SD internal data. This function should be called before any other SD function.
13  */
14 void SD_init(int *argc, char **argv) {
15   xbt_assert0(sd_global == NULL, "SD_init already called");
16
17   sd_global = xbt_new0(s_SD_global_t, 1);
18   sd_global->workstations = xbt_dict_new();
19   sd_global->workstation_count = 0;
20   sd_global->links = xbt_dict_new();
21   sd_global->watch_point_reached = 0;
22
23   s_SD_task_t task;
24   sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
25   sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
26   sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
27   sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
28   sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
29   sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
30
31   surf_init(argc, argv);
32 }
33
34 /* Creates the environnement described in a xml file of a platform description.
35  */
36 void SD_create_environment(const char *platform_file) {
37   xbt_dict_cursor_t cursor = NULL;
38   char *name = NULL;
39   void *surf_workstation = NULL;
40   void *surf_link = NULL;
41
42   SD_CHECK_INIT_DONE();
43
44   surf_timer_resource_init(platform_file);  /* tell Surf to create the environnement */
45
46   
47   /*printf("surf_workstation_resource = %p, workstation_set = %p\n", surf_workstation_resource, workstation_set);
48     printf("surf_network_resource = %p, network_link_set = %p\n", surf_network_resource, network_link_set);*/
49
50   /*surf_workstation_resource_init_KCCFLN05(platform_file);*/
51   surf_workstation_resource_init_CLM03(platform_file);
52
53   /*printf("surf_workstation_resource = %p, workstation_set = %p\n", surf_workstation_resource, workstation_set);
54     printf("surf_network_resource = %p, network_link_set = %p\n", surf_network_resource, network_link_set);*/
55
56
57   /* now let's create the SD wrappers for workstations and links */
58   xbt_dict_foreach(workstation_set, cursor, name, surf_workstation) {
59     __SD_workstation_create(surf_workstation, NULL);
60   }
61
62   xbt_dict_foreach(network_link_set, cursor, name, surf_link) {
63     __SD_link_create(surf_link, NULL);
64   }
65 }
66
67 /* Launches the simulation. Returns a NULL-terminated array of SD_task_t whose state has changed.
68  */
69 SD_task_t* SD_simulate(double how_long)
70 {
71   double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
72   double elapsed_time = 0.0;
73   SD_task_t task;
74   surf_action_t action;
75   SD_task_t *changed_tasks = NULL;
76   int changed_task_number = 0;
77   int changed_task_capacity = 16; /* will be increased if necessary */
78
79   /* create the array that will be returned */
80   changed_tasks = xbt_new0(SD_task_t, changed_task_capacity);
81   changed_tasks[0] = NULL;
82
83   surf_solve(); /* Takes traces into account. Returns 0.0 */
84
85   sd_global->watch_point_reached = 0;
86
87   /* main loop */
88   while (elapsed_time >= 0.0 && total_time < how_long && !sd_global->watch_point_reached) {
89
90     /* explore the ready tasks */
91     xbt_swag_foreach(task, sd_global->ready_task_set) {
92       INFO1("Executing task '%s'", SD_task_get_name(task));
93       task->state_changed = 0;
94       action = __SD_task_run(task);
95       surf_workstation_resource->common_public->action_set_data(action, task);
96       task->state_changed = 1;
97       
98       changed_tasks[changed_task_number++] = task; /* replace NULL by the task */
99       if (changed_task_number == changed_task_capacity) {
100         changed_task_capacity *= 2;
101         changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
102       }
103       changed_tasks[changed_task_number] = NULL;
104     }
105
106     elapsed_time = surf_solve();
107     if (elapsed_time > 0.0)
108       total_time += elapsed_time;
109
110     /* let's see which tasks are done */
111     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.done_action_set))) {
112       task = action->data;
113       INFO1("Task '%s' done", SD_task_get_name(task));
114       __SD_task_set_state(task, SD_DONE);
115       __SD_task_remove_dependencies(task);
116       if (!task->state_changed) {
117         task->state_changed = 1;
118         changed_tasks[changed_task_number++] = task;
119       }
120     }
121
122     /* let's see which tasks have just failed */
123     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.failed_action_set))) {
124       task = action->data;
125       INFO1("Task '%s' failed", SD_task_get_name(task));
126       __SD_task_set_state(task, SD_FAILED);
127       if (!task->state_changed) {
128         task->state_changed = 1;
129         changed_tasks[changed_task_number++] = task;
130       }
131     }
132   }
133
134   INFO0("Simulation finished");
135   INFO1("Number of tasks whose state has changed: %d", changed_task_number);
136
137   return changed_tasks;
138 }
139
140 /* Destroys all SD internal data. This function should be called when the simulation is over.
141  * The tasks should have been destroyed first.
142  */
143 void SD_exit(void) {
144   if (sd_global != NULL) {
145     xbt_dict_free(&sd_global->workstations);
146     xbt_dict_free(&sd_global->links);
147     xbt_free(sd_global);
148
149     xbt_swag_free(sd_global->not_scheduled_task_set);
150     xbt_swag_free(sd_global->scheduled_task_set);
151     xbt_swag_free(sd_global->ready_task_set);
152     xbt_swag_free(sd_global->running_task_set);
153     xbt_swag_free(sd_global->done_task_set);
154     xbt_swag_free(sd_global->failed_task_set);
155
156     surf_exit();
157   }
158 }