Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
some extra cleanups in simdag examples
[simgrid.git] / examples / simdag / scheduling / minmin_test.c
1 /* Copyright (c) 2009-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* simple test to schedule a DAX file with the Min-Min algorithm.           */
8 #include <string.h>
9 #include "simgrid/simdag.h"
10
11 #ifdef HAVE_JEDULE
12 #include "simgrid/jedule/jedule_sd_binding.h"
13 #endif
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this SimDag example");
16
17 typedef struct _HostAttribute *HostAttribute;
18 struct _HostAttribute {
19   /* Earliest time at which a host is ready to execute a task */
20   double available_at;
21   SD_task_t last_scheduled_task;
22 };
23
24 static void sg_host_allocate_attribute(sg_host_t host)
25 {
26   void *data;
27   data = calloc(1, sizeof(struct _HostAttribute));
28   sg_host_user_set(host, data);
29 }
30
31 static void sg_host_free_attribute(sg_host_t host)
32 {
33   free(sg_host_user(host));
34   sg_host_user_set(host, NULL);
35 }
36
37 static double sg_host_get_available_at(sg_host_t host)
38 {
39   HostAttribute attr = (HostAttribute) sg_host_user(host);
40   return attr->available_at;
41 }
42
43 static void sg_host_set_available_at(sg_host_t host, double time)
44 {
45   HostAttribute attr = (HostAttribute) sg_host_user(host);
46   attr->available_at = time;
47   sg_host_user_set(host, attr);
48 }
49
50 static SD_task_t sg_host_get_last_scheduled_task( sg_host_t host){
51   HostAttribute attr = (HostAttribute) sg_host_user(host);
52   return attr->last_scheduled_task;
53 }
54
55 static void sg_host_set_last_scheduled_task(sg_host_t host, SD_task_t task){
56   HostAttribute attr = (HostAttribute) sg_host_user(host);
57   attr->last_scheduled_task=task;
58   sg_host_user_set(host, attr);
59 }
60
61 static xbt_dynar_t get_ready_tasks(xbt_dynar_t dax)
62 {
63   unsigned int i;
64   xbt_dynar_t ready_tasks;
65   SD_task_t task;
66
67   ready_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
68   xbt_dynar_foreach(dax, i, task) {
69     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ && SD_task_get_state(task) == SD_SCHEDULABLE) {
70       xbt_dynar_push(ready_tasks, &task);
71     }
72   }
73   XBT_DEBUG("There are %lu ready tasks", xbt_dynar_length(ready_tasks));
74
75   return ready_tasks;
76 }
77
78 static double finish_on_at(SD_task_t task, sg_host_t host)
79 {
80   volatile double result;
81   unsigned int i;
82   double data_available = 0.;
83   double redist_time = 0;
84   double last_data_available;
85   SD_task_t parent, grand_parent;
86   xbt_dynar_t parents, grand_parents;
87
88   sg_host_t *grand_parent_host_list;
89
90   parents = SD_task_get_parents(task);
91
92   if (!xbt_dynar_is_empty(parents)) {
93     /* compute last_data_available */
94     last_data_available = -1.0;
95     xbt_dynar_foreach(parents, i, parent) {
96       /* normal case */
97       if (SD_task_get_kind(parent) == SD_TASK_COMM_E2E) {
98         grand_parents = SD_task_get_parents(parent);
99
100         xbt_assert(xbt_dynar_length(grand_parents) <2, "Error: transfer %s has 2 parents", SD_task_get_name(parent));
101         
102         xbt_dynar_get_cpy(grand_parents, 0, &grand_parent);
103
104         grand_parent_host_list = SD_task_get_workstation_list(grand_parent);
105         /* Estimate the redistribution time from this parent */
106         if (SD_task_get_amount(parent) == 0){
107           redist_time= 0;
108         } else {
109           redist_time = SD_route_get_latency(grand_parent_host_list[0], host) +
110                         SD_task_get_amount(parent) / SD_route_get_bandwidth(grand_parent_host_list[0], host);
111         }
112         data_available = SD_task_get_finish_time(grand_parent) + redist_time;
113
114         xbt_dynar_free_container(&grand_parents);
115       }
116
117       /* no transfer, control dependency */
118       if (SD_task_get_kind(parent) == SD_TASK_COMP_SEQ) {
119         data_available = SD_task_get_finish_time(parent);
120       }
121
122       if (last_data_available < data_available)
123         last_data_available = data_available;
124     }
125
126     xbt_dynar_free_container(&parents);
127
128     result = MAX(sg_host_get_available_at(host), last_data_available) + SD_task_get_amount(task)/sg_host_speed(host);
129   } else {
130     xbt_dynar_free_container(&parents);
131
132     result = sg_host_get_available_at(host) + SD_task_get_amount(task)/sg_host_speed(host);
133   }
134   return result;
135 }
136
137 static sg_host_t SD_task_get_best_host(SD_task_t task)
138 {
139   int i;
140   double EFT, min_EFT = -1.0;
141   const sg_host_t *hosts = sg_host_list();
142   int nhosts = sg_host_count();
143   sg_host_t best_host;
144
145   best_host = hosts[0];
146   min_EFT = finish_on_at(task, hosts[0]);
147
148   for (i = 1; i < nhosts; i++) {
149     EFT = finish_on_at(task, hosts[i]);
150     XBT_DEBUG("%s finishes on %s at %f", SD_task_get_name(task), sg_host_get_name(hosts[i]), EFT);
151
152     if (EFT < min_EFT) {
153       min_EFT = EFT;
154       best_host = hosts[i];
155     }
156   }
157   return best_host;
158 }
159
160 int main(int argc, char **argv)
161 {
162   unsigned int cursor;
163   double finish_time, min_finish_time = -1.0;
164   SD_task_t task, selected_task = NULL, last_scheduled_task;
165   xbt_dynar_t ready_tasks;
166   sg_host_t host, selected_host = NULL;
167   int total_nhosts = 0;
168   const sg_host_t *hosts = NULL;
169   char * tracefilename = NULL;
170   xbt_dynar_t dax;
171
172   /* initialization of SD */
173   SD_init(&argc, argv);
174
175   /* Check our arguments */
176   xbt_assert(argc > 2, "Usage: %s platform_file dax_file [jedule_file]\n"
177              "\tExample: %s simulacrum_7_hosts.xml Montage_25.xml Montage_25.jed", argv[0], argv[0]);
178
179   if (argc == 4)
180     tracefilename = xbt_strdup(argv[3]);
181
182   /* creation of the environment */
183   SD_create_environment(argv[1]);
184
185   /*  Allocating the host attribute */
186   total_nhosts = sg_host_count();
187   hosts = sg_host_list();
188
189   for (cursor = 0; cursor < total_nhosts; cursor++)
190     sg_host_allocate_attribute(hosts[cursor]);
191
192   /* load the DAX file */
193   dax = SD_daxload(argv[2]);
194
195   xbt_dynar_foreach(dax, cursor, task) {
196     SD_task_watch(task, SD_DONE);
197   }
198
199   /* Schedule the root first */
200   xbt_dynar_get_cpy(dax, 0, &task);
201   host = SD_task_get_best_host(task);
202   SD_task_schedulel(task, 1, host);
203
204   while (!xbt_dynar_is_empty(SD_simulate(-1.0))) {
205     /* Get the set of ready tasks */
206     ready_tasks = get_ready_tasks(dax);
207     if (xbt_dynar_is_empty(ready_tasks)) {
208       xbt_dynar_free_container(&ready_tasks);
209       /* there is no ready task, let advance the simulation */
210       continue;
211     }
212     /* For each ready task:
213      * get the host that minimizes the completion time.
214      * select the task that has the minimum completion time on its best host.
215      */
216     xbt_dynar_foreach(ready_tasks, cursor, task) {
217       XBT_DEBUG("%s is ready", SD_task_get_name(task));
218       host = SD_task_get_best_host(task);
219       finish_time = finish_on_at(task, host);
220       if (min_finish_time == -1. || finish_time < min_finish_time) {
221         min_finish_time = finish_time;
222         selected_task = task;
223         selected_host = host;
224       }
225     }
226
227     XBT_INFO("Schedule %s on %s", SD_task_get_name(selected_task), sg_host_get_name(selected_host));
228     SD_task_schedulel(selected_task, 1, selected_host);
229
230     /*
231      * SimDag allows tasks to be executed concurrently when they can by default.
232      * Yet schedulers take decisions assuming that tasks wait for resource availability to start.
233      * The solution (well crude hack is to keep track of the last task scheduled on a host and add a special type of
234      * dependency if needed to force the sequential execution meant by the scheduler.
235      * If the last scheduled task is already done, has failed or is a predecessor of the current task, no need for a
236      * new dependency
237     */
238
239     last_scheduled_task = sg_host_get_last_scheduled_task(selected_host);
240     if (last_scheduled_task && (SD_task_get_state(last_scheduled_task) != SD_DONE) &&
241         (SD_task_get_state(last_scheduled_task) != SD_FAILED) &&
242         !SD_task_dependency_exists(sg_host_get_last_scheduled_task(selected_host), selected_task))
243       SD_task_dependency_add("resource", NULL, last_scheduled_task, selected_task);
244
245     sg_host_set_last_scheduled_task(selected_host, selected_task);
246     sg_host_set_available_at(selected_host, min_finish_time);
247
248     xbt_dynar_free_container(&ready_tasks);
249     /* reset the min_finish_time for the next set of ready tasks */
250     min_finish_time = -1.;
251   }
252
253   XBT_INFO("Simulation Time: %f", SD_get_clock());
254   XBT_INFO("------------------- Produce the trace file---------------------------");
255   XBT_INFO("Producing a jedule output (if active) of the run into %s", tracefilename?tracefilename:"minmin_test.jed");
256 #ifdef HAVE_JEDULE
257   jedule_sd_dump(tracefilename);
258 #endif
259   free(tracefilename);
260
261   xbt_dynar_free_container(&ready_tasks);
262
263   xbt_dynar_foreach(dax, cursor, task) {
264     SD_task_destroy(task);
265   }
266   xbt_dynar_free_container(&dax);
267
268   for (cursor = 0; cursor < total_nhosts; cursor++)
269     sg_host_free_attribute(hosts[cursor]);
270
271   /* exit */
272   SD_exit();
273   return 0;
274 }