Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
33149902dfe71a19b09126c3f629791e29e1a12e
[simgrid.git] / src / simdag / sd_global.c
1 #include "simdag/simdag.h"
2 #include "private.h"
3 #include "xbt/sysdep.h"
4 #include "xbt/dynar.h"
5 #include "surf/surf.h"
6
7 SD_global_t sd_global = NULL;
8
9 /* Initialises SD internal data. This function should be called before any other SD function.
10  */
11 void SD_init(int *argc, char **argv) {
12   xbt_assert0(sd_global == NULL, "SD_init already called");
13
14   sd_global = xbt_new0(s_SD_global_t, 1);
15   sd_global->workstations = xbt_dict_new();
16   sd_global->workstation_count = 0;
17   sd_global->links = xbt_dict_new();
18   sd_global->tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
19
20   s_SD_task_t task;
21   sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
22   sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
23   sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
24   sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
25   sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
26
27   surf_init(argc, argv);
28 }
29
30 /* Creates the environnement described in a xml file of a platform description.
31  */
32 void SD_create_environment(const char *platform_file) {
33   xbt_dict_cursor_t cursor = NULL;
34   char *name = NULL;
35   void *surf_workstation = NULL;
36   void *surf_link = NULL;
37
38   SD_CHECK_INIT_DONE();
39
40   surf_timer_resource_init(platform_file);  /* tell Surf to create the environnement */
41
42   
43   /*printf("surf_workstation_resource = %p, workstation_set = %p\n", surf_workstation_resource, workstation_set);
44     printf("surf_network_resource = %p, network_link_set = %p\n", surf_network_resource, network_link_set);*/
45
46   /*surf_workstation_resource_init_KCCFLN05(platform_file);*/
47   surf_workstation_resource_init_CLM03(platform_file);
48
49   /*printf("surf_workstation_resource = %p, workstation_set = %p\n", surf_workstation_resource, workstation_set);
50     printf("surf_network_resource = %p, network_link_set = %p\n", surf_network_resource, network_link_set);*/
51
52
53   /* now let's create the SD wrappers for workstations and links */
54   xbt_dict_foreach(workstation_set, cursor, name, surf_workstation) {
55     __SD_workstation_create(surf_workstation, NULL);
56   }
57
58   xbt_dict_foreach(network_link_set, cursor, name, surf_link) {
59     __SD_link_create(surf_link, NULL);
60   }
61 }
62
63 /* Launches the simulation. Returns a NULL-terminated array of SD_task_t whose state has changed.
64  */
65 SD_task_t* SD_simulate(double how_long)
66 {
67   /* TODO */
68
69   double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
70   double elapsed_time = 0.0;
71   int watch_point_reached = 0;
72   int i;
73   SD_task_t task;
74   surf_action_t surf_action;
75
76   surf_solve(); /* Takes traces into account. Returns 0.0 */
77
78   /* main loop */
79   while (elapsed_time >= 0.0 && total_time < how_long && !watch_point_reached) {
80     for (i = 0 ; i < xbt_dynar_length(sd_global->tasks); i++) {
81       xbt_dynar_get_cpy(sd_global->tasks, i, &task);
82       printf("Examining task '%s'...\n", SD_task_get_name(task));
83
84       /* if the task is scheduled and the dependencies are satisfied,
85          we can execute the task */
86       if (SD_task_get_state(task) == SD_SCHEDULED) {
87         printf("Task '%s' is scheduled.\n", SD_task_get_name(task));
88
89         if (xbt_dynar_length(task->tasks_before) == 0) {
90           printf("The dependencies are satisfied. Executing task '%s'\n", SD_task_get_name(task));
91           surf_action = __SD_task_run(task);
92         }
93         else {
94           printf("Cannot execute task '%s' because some depencies are not satisfied.\n", SD_task_get_name(task));
95         }
96       }
97       else {
98         printf("Task '%s' is not scheduled. Nothing to do.\n", SD_task_get_name(task));
99       }
100     }
101     elapsed_time = surf_solve();
102     if (elapsed_time > 0.0)
103       total_time += elapsed_time;
104     printf("Total time: %f\n", total_time);
105   }
106
107   return NULL;
108 }
109
110 void SD_test() {
111   /* temporary test to explore the workstations and the links */
112   xbt_dict_cursor_t cursor = NULL;
113   char *name = NULL;
114   SD_workstation_t workstation = NULL;
115   double power, available_power, bandwidth, latency;
116   SD_link_t link = NULL;
117
118   surf_solve();
119
120   xbt_dict_foreach(sd_global->workstations, cursor, name, workstation) {
121     power = SD_workstation_get_power(workstation);
122     available_power = SD_workstation_get_available_power(workstation);
123     printf("Workstation name: %s, power: %f Mflop/s, available power: %f%%\n", name, power, (available_power*100));
124   }
125
126   xbt_dict_foreach(sd_global->links, cursor, name, link) {
127     bandwidth = SD_link_get_current_bandwidth(link);
128     latency = SD_link_get_current_latency(link);
129     printf("Link name: %s, bandwidth: %f, latency: %f\n", name, bandwidth, latency);
130   }
131
132   /* test the route between two workstations */
133   SD_workstation_t src, dst;
134   xbt_dict_cursor_first(sd_global->workstations, &cursor);
135   xbt_dict_cursor_get_or_free(&cursor, &name, (void**) &src);
136   xbt_dict_cursor_step(cursor);
137   xbt_dict_cursor_get_or_free(&cursor, &name, (void**) &dst);
138   xbt_dict_cursor_free(&cursor);
139
140   SD_link_t *route = SD_workstation_route_get_list(src, dst);
141   int route_size = SD_workstation_route_get_size(src, dst);
142
143   printf("Route between %s and %s (%d links) : ", SD_workstation_get_name(src), SD_workstation_get_name(dst), route_size);
144   int i;
145   for (i = 0; i < route_size; i++) {
146     printf("%s ", SD_link_get_name(route[i]));
147   }
148   printf("\n");
149 }
150
151 /* Destroys all SD internal data. This function should be called when the simulation is over.
152  * The tasks should have been destroyed first.
153  */
154 void SD_exit() {
155   if (sd_global != NULL) {
156     xbt_dict_free(&sd_global->workstations);
157     xbt_dict_free(&sd_global->links);
158     xbt_dynar_free(&sd_global->tasks);
159     xbt_free(sd_global);
160
161     xbt_swag_free(sd_global->not_scheduled_task_set);
162     xbt_swag_free(sd_global->scheduled_task_set);
163     xbt_swag_free(sd_global->running_task_set);
164     xbt_swag_free(sd_global->done_task_set);
165     xbt_swag_free(sd_global->failed_task_set);
166
167     surf_exit();
168   }
169 }