Logo AND Algorithmique Numérique Distribuée

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