1 /* simple test to schedule a DAX file with the Min-Min algorithm. */
3 /* Copyright (c) 2009-2015. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "simgrid/simdag.h"
17 #include "simgrid/jedule/jedule_sd_binding.h"
20 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
21 "Logging specific to this SimDag example");
23 typedef struct _WorkstationAttribute *WorkstationAttribute;
24 struct _WorkstationAttribute {
25 /* Earliest time at which a workstation is ready to execute a task */
27 SD_task_t last_scheduled_task;
30 static void sg_host_allocate_attribute(sg_host_t workstation)
33 data = calloc(1, sizeof(struct _WorkstationAttribute));
34 sg_host_user_set(workstation, data);
37 static void sg_host_free_attribute(sg_host_t workstation)
39 free(sg_host_user(workstation));
40 sg_host_user_set(workstation, NULL);
43 static double sg_host_get_available_at(sg_host_t workstation)
45 WorkstationAttribute attr =
46 (WorkstationAttribute) sg_host_user(workstation);
47 return attr->available_at;
50 static void sg_host_set_available_at(sg_host_t workstation,
53 WorkstationAttribute attr =
54 (WorkstationAttribute) sg_host_user(workstation);
55 attr->available_at = time;
56 sg_host_user_set(workstation, attr);
59 static SD_task_t sg_host_get_last_scheduled_task( sg_host_t workstation){
60 WorkstationAttribute attr =
61 (WorkstationAttribute) sg_host_user(workstation);
62 return attr->last_scheduled_task;
65 static void sg_host_set_last_scheduled_task(sg_host_t workstation,
67 WorkstationAttribute attr =
68 (WorkstationAttribute) sg_host_user(workstation);
69 attr->last_scheduled_task=task;
70 sg_host_user_set(workstation, attr);
73 static xbt_dynar_t get_ready_tasks(xbt_dynar_t dax)
76 xbt_dynar_t ready_tasks;
79 ready_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
80 xbt_dynar_foreach(dax, i, task) {
81 if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ &&
82 SD_task_get_state(task) == SD_SCHEDULABLE) {
83 xbt_dynar_push(ready_tasks, &task);
86 XBT_DEBUG("There are %lu ready tasks", xbt_dynar_length(ready_tasks));
91 static double finish_on_at(SD_task_t task, sg_host_t workstation)
93 volatile double result;
95 double data_available = 0.;
96 double redist_time = 0;
97 double last_data_available;
98 SD_task_t parent, grand_parent;
99 xbt_dynar_t parents, grand_parents;
101 sg_host_t *grand_parent_workstation_list;
103 parents = SD_task_get_parents(task);
105 if (!xbt_dynar_is_empty(parents)) {
106 /* compute last_data_available */
107 last_data_available = -1.0;
108 xbt_dynar_foreach(parents, i, parent) {
111 if (SD_task_get_kind(parent) == SD_TASK_COMM_E2E) {
112 grand_parents = SD_task_get_parents(parent);
114 xbt_assert(xbt_dynar_length(grand_parents) <2,
115 "Error: transfer %s has 2 parents",
116 SD_task_get_name(parent));
118 xbt_dynar_get_cpy(grand_parents, 0, &grand_parent);
120 grand_parent_workstation_list =
121 SD_task_get_workstation_list(grand_parent);
122 /* Estimate the redistribution time from this parent */
123 if (SD_task_get_amount(parent) == 0){
127 SD_route_get_latency(grand_parent_workstation_list[0],
129 SD_task_get_amount(parent) /
130 SD_route_get_bandwidth(grand_parent_workstation_list[0],
134 SD_task_get_finish_time(grand_parent) + redist_time;
136 xbt_dynar_free_container(&grand_parents);
139 /* no transfer, control dependency */
140 if (SD_task_get_kind(parent) == SD_TASK_COMP_SEQ) {
141 data_available = SD_task_get_finish_time(parent);
144 if (last_data_available < data_available)
145 last_data_available = data_available;
149 xbt_dynar_free_container(&parents);
151 result = MAX(sg_host_get_available_at(workstation),
152 last_data_available) +
153 SD_task_get_amount(task)/sg_host_speed(workstation);
155 xbt_dynar_free_container(&parents);
157 result = sg_host_get_available_at(workstation) +
158 SD_task_get_amount(task)/sg_host_speed(workstation);
163 static sg_host_t SD_task_get_best_workstation(SD_task_t task)
166 double EFT, min_EFT = -1.0;
167 const sg_host_t *workstations = sg_host_list();
168 int nworkstations = sg_host_count();
169 sg_host_t best_workstation;
171 best_workstation = workstations[0];
172 min_EFT = finish_on_at(task, workstations[0]);
174 for (i = 1; i < nworkstations; i++) {
175 EFT = finish_on_at(task, workstations[i]);
176 XBT_DEBUG("%s finishes on %s at %f",
177 SD_task_get_name(task),
178 sg_host_get_name(workstations[i]), EFT);
182 best_workstation = workstations[i];
186 return best_workstation;
189 int main(int argc, char **argv)
192 double finish_time, min_finish_time = -1.0;
193 SD_task_t task, selected_task = NULL, last_scheduled_task;
194 xbt_dynar_t ready_tasks;
195 sg_host_t workstation, selected_workstation = NULL;
196 int total_nworkstations = 0;
197 const sg_host_t *workstations = NULL;
198 char * tracefilename = NULL;
201 /* initialization of SD */
202 SD_init(&argc, argv);
204 /* Check our arguments */
205 xbt_assert(argc > 2, "Usage: %s platform_file dax_file [jedule_file]\n"
206 "\tExample: %s simulacrum_7_hosts.xml Montage_25.xml Montage_25.jed", argv[0], argv[0]);
209 tracefilename = xbt_strdup(argv[3]);
211 /* creation of the environment */
212 SD_create_environment(argv[1]);
214 /* Allocating the workstation attribute */
215 total_nworkstations = sg_host_count();
216 workstations = sg_host_list();
218 for (cursor = 0; cursor < total_nworkstations; cursor++)
219 sg_host_allocate_attribute(workstations[cursor]);
222 /* load the DAX file */
223 dax = SD_daxload(argv[2]);
225 xbt_dynar_foreach(dax, cursor, task) {
226 SD_task_watch(task, SD_DONE);
229 /* Schedule the root first */
230 xbt_dynar_get_cpy(dax, 0, &task);
231 workstation = SD_task_get_best_workstation(task);
232 SD_task_schedulel(task, 1, workstation);
234 while (!xbt_dynar_is_empty(SD_simulate(-1.0))) {
235 /* Get the set of ready tasks */
236 ready_tasks = get_ready_tasks(dax);
237 if (xbt_dynar_is_empty(ready_tasks)) {
238 xbt_dynar_free_container(&ready_tasks);
239 /* there is no ready task, let advance the simulation */
242 /* For each ready task:
243 * get the workstation that minimizes the completion time.
244 * select the task that has the minimum completion time on
245 * its best workstation.
247 xbt_dynar_foreach(ready_tasks, cursor, task) {
248 XBT_DEBUG("%s is ready", SD_task_get_name(task));
249 workstation = SD_task_get_best_workstation(task);
250 finish_time = finish_on_at(task, workstation);
251 if (min_finish_time == -1. || finish_time < min_finish_time) {
252 min_finish_time = finish_time;
253 selected_task = task;
254 selected_workstation = workstation;
258 XBT_INFO("Schedule %s on %s", SD_task_get_name(selected_task),
259 sg_host_get_name(selected_workstation));
261 SD_task_schedulel(selected_task, 1, selected_workstation);
264 * SimDag allows tasks to be executed concurrently when they can by default.
265 * Yet schedulers take decisions assuming that tasks wait for resource
266 * availability to start.
267 * The solution (well crude hack is to keep track of the last task scheduled
268 * on a workstation and add a special type of dependency if needed to
269 * force the sequential execution meant by the scheduler.
270 * If the last scheduled task is already done, has failed or is a
271 * predecessor of the current task, no need for a new dependency
274 last_scheduled_task =
275 sg_host_get_last_scheduled_task(selected_workstation);
276 if (last_scheduled_task &&
277 (SD_task_get_state(last_scheduled_task) != SD_DONE) &&
278 (SD_task_get_state(last_scheduled_task) != SD_FAILED) &&
279 !SD_task_dependency_exists(
280 sg_host_get_last_scheduled_task(selected_workstation),
282 SD_task_dependency_add("resource", NULL,
283 last_scheduled_task, selected_task);
285 sg_host_set_last_scheduled_task(selected_workstation, selected_task);
287 sg_host_set_available_at(selected_workstation, min_finish_time);
289 xbt_dynar_free_container(&ready_tasks);
290 /* reset the min_finish_time for the next set of ready tasks */
291 min_finish_time = -1.;
294 XBT_INFO("Simulation Time: %f", SD_get_clock());
296 XBT_INFO("------------------- Produce the trace file---------------------------");
297 XBT_INFO("Producing a jedule output (if active) of the run into %s", tracefilename?tracefilename:"./minmin_test.jed");
299 jedule_sd_dump(tracefilename);
303 xbt_dynar_free_container(&ready_tasks);
305 xbt_dynar_foreach(dax, cursor, task) {
306 SD_task_destroy(task);
308 xbt_dynar_free_container(&dax);
310 for (cursor = 0; cursor < total_nworkstations; cursor++)
311 sg_host_free_attribute(workstations[cursor]);