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"
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
17 "Logging specific to this SimDag example");
19 typedef struct _WorkstationAttribute *WorkstationAttribute;
20 struct _WorkstationAttribute {
21 /* Earliest time at which a workstation is ready to execute a task */
23 SD_task_t last_scheduled_task;
26 static void sg_host_allocate_attribute(sg_host_t workstation)
29 data = calloc(1, sizeof(struct _WorkstationAttribute));
30 sg_host_user_set(workstation, data);
33 static void sg_host_free_attribute(sg_host_t workstation)
35 free(sg_host_user(workstation));
36 sg_host_user_set(workstation, NULL);
39 static double sg_host_get_available_at(sg_host_t workstation)
41 WorkstationAttribute attr =
42 (WorkstationAttribute) sg_host_user(workstation);
43 return attr->available_at;
46 static void sg_host_set_available_at(sg_host_t workstation,
49 WorkstationAttribute attr =
50 (WorkstationAttribute) sg_host_user(workstation);
51 attr->available_at = time;
52 sg_host_user_set(workstation, attr);
55 static SD_task_t sg_host_get_last_scheduled_task( sg_host_t workstation){
56 WorkstationAttribute attr =
57 (WorkstationAttribute) sg_host_user(workstation);
58 return attr->last_scheduled_task;
61 static void sg_host_set_last_scheduled_task(sg_host_t workstation,
63 WorkstationAttribute attr =
64 (WorkstationAttribute) sg_host_user(workstation);
65 attr->last_scheduled_task=task;
66 sg_host_user_set(workstation, attr);
69 static xbt_dynar_t get_ready_tasks(xbt_dynar_t dax)
72 xbt_dynar_t ready_tasks;
75 ready_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
76 xbt_dynar_foreach(dax, i, task) {
77 if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ &&
78 SD_task_get_state(task) == SD_SCHEDULABLE) {
79 xbt_dynar_push(ready_tasks, &task);
82 XBT_DEBUG("There are %lu ready tasks", xbt_dynar_length(ready_tasks));
87 static double finish_on_at(SD_task_t task, sg_host_t workstation)
89 volatile double result;
91 double data_available = 0.;
92 double redist_time = 0;
93 double last_data_available;
94 SD_task_t parent, grand_parent;
95 xbt_dynar_t parents, grand_parents;
97 sg_host_t *grand_parent_workstation_list;
99 parents = SD_task_get_parents(task);
101 if (!xbt_dynar_is_empty(parents)) {
102 /* compute last_data_available */
103 last_data_available = -1.0;
104 xbt_dynar_foreach(parents, i, parent) {
107 if (SD_task_get_kind(parent) == SD_TASK_COMM_E2E) {
108 grand_parents = SD_task_get_parents(parent);
110 xbt_assert(xbt_dynar_length(grand_parents) <2,
111 "Error: transfer %s has 2 parents",
112 SD_task_get_name(parent));
114 xbt_dynar_get_cpy(grand_parents, 0, &grand_parent);
116 grand_parent_workstation_list =
117 SD_task_get_workstation_list(grand_parent);
118 /* Estimate the redistribution time from this parent */
120 SD_route_get_communication_time(grand_parent_workstation_list
122 SD_task_get_amount(parent));
124 SD_task_get_finish_time(grand_parent) + redist_time;
126 xbt_dynar_free_container(&grand_parents);
129 /* no transfer, control dependency */
130 if (SD_task_get_kind(parent) == SD_TASK_COMP_SEQ) {
131 data_available = SD_task_get_finish_time(parent);
134 if (last_data_available < data_available)
135 last_data_available = data_available;
139 xbt_dynar_free_container(&parents);
141 result = MAX(sg_host_get_available_at(workstation),
142 last_data_available) +
143 sg_host_computation_time(workstation,
144 SD_task_get_amount(task));
146 xbt_dynar_free_container(&parents);
148 result = sg_host_get_available_at(workstation) +
149 sg_host_computation_time(workstation,
150 SD_task_get_amount(task));
155 static sg_host_t SD_task_get_best_workstation(SD_task_t task)
158 double EFT, min_EFT = -1.0;
159 const sg_host_t *workstations = sg_host_list();
160 int nworkstations = sg_host_count();
161 sg_host_t best_workstation;
163 best_workstation = workstations[0];
164 min_EFT = finish_on_at(task, workstations[0]);
166 for (i = 1; i < nworkstations; i++) {
167 EFT = finish_on_at(task, workstations[i]);
168 XBT_DEBUG("%s finishes on %s at %f",
169 SD_task_get_name(task),
170 sg_host_get_name(workstations[i]), EFT);
174 best_workstation = workstations[i];
178 return best_workstation;
181 static void output_xml(FILE * out, xbt_dynar_t dax)
183 unsigned int i, j, k;
184 int current_nworkstations;
185 const int nworkstations = sg_host_count();
186 const sg_host_t *workstations = sg_host_list();
190 fprintf(out, "<?xml version=\"1.0\"?>\n");
191 fprintf(out, "<grid_schedule>\n");
192 fprintf(out, " <grid_info>\n");
193 fprintf(out, " <info name=\"nb_clusters\" value=\"1\"/>\n");
194 fprintf(out, " <clusters>\n");
196 " <cluster id=\"1\" hosts=\"%d\" first_host=\"0\"/>\n",
198 fprintf(out, " </clusters>\n");
199 fprintf(out, " </grid_info>\n");
200 fprintf(out, " <node_infos>\n");
202 xbt_dynar_foreach(dax, i, task) {
203 fprintf(out, " <node_statistics>\n");
204 fprintf(out, " <node_property name=\"id\" value=\"%s\"/>\n",
205 SD_task_get_name(task));
206 fprintf(out, " <node_property name=\"type\" value=\"");
207 if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ)
208 fprintf(out, "computation\"/>\n");
209 if (SD_task_get_kind(task) == SD_TASK_COMM_E2E)
210 fprintf(out, "transfer\"/>\n");
213 " <node_property name=\"start_time\" value=\"%.3f\"/>\n",
214 SD_task_get_start_time(task));
216 " <node_property name=\"end_time\" value=\"%.3f\"/>\n",
217 SD_task_get_finish_time(task));
218 fprintf(out, " <configuration>\n");
220 current_nworkstations = SD_task_get_workstation_count(task);
223 " <conf_property name=\"host_nb\" value=\"%d\"/>\n",
224 current_nworkstations);
226 fprintf(out, " <host_lists>\n");
227 list = SD_task_get_workstation_list(task);
228 for (j = 0; j < current_nworkstations; j++) {
229 for (k = 0; k < nworkstations; k++) {
230 if (!strcmp(sg_host_get_name(workstations[k]),
231 sg_host_get_name(list[j]))) {
232 fprintf(out, " <hosts start=\"%u\" nb=\"1\"/>\n",
235 " <conf_property name=\"cluster_id\" value=\"0\"/>\n");
240 fprintf(out, " </host_lists>\n");
241 fprintf(out, " </configuration>\n");
242 fprintf(out, " </node_statistics>\n");
244 fprintf(out, " </node_infos>\n");
245 fprintf(out, "</grid_schedule>\n");
248 int main(int argc, char **argv)
251 double finish_time, min_finish_time = -1.0;
252 SD_task_t task, selected_task = NULL, last_scheduled_task;
253 xbt_dynar_t ready_tasks;
254 sg_host_t workstation, selected_workstation = NULL;
255 int total_nworkstations = 0;
256 const sg_host_t *workstations = NULL;
260 /* initialization of SD */
261 SD_init(&argc, argv);
263 /* Check our arguments */
264 xbt_assert(argc > 2, "Usage: %s platform_file dax_file [jedule_file]\n"
265 "\tExample: %s simulacrum_7_hosts.xml Montage_25.xml Montage_25.jed",
268 char *last = strrchr(argv[2], '.');
269 char * tracefilename = bprintf("%.*s.jed",(int) (last == NULL ?
271 last - argv[2]), argv[2]);
273 tracefilename = xbt_strdup(argv[3]);
275 /* creation of the environment */
276 SD_create_environment(argv[1]);
278 /* Allocating the workstation attribute */
279 total_nworkstations = sg_host_count();
280 workstations = sg_host_list();
282 for (cursor = 0; cursor < total_nworkstations; cursor++)
283 sg_host_allocate_attribute(workstations[cursor]);
286 /* load the DAX file */
287 dax = SD_daxload(argv[2]);
289 xbt_dynar_foreach(dax, cursor, task) {
290 SD_task_watch(task, SD_DONE);
293 /* Schedule the root first */
294 xbt_dynar_get_cpy(dax, 0, &task);
295 workstation = SD_task_get_best_workstation(task);
296 SD_task_schedulel(task, 1, workstation);
298 while (!xbt_dynar_is_empty(SD_simulate(-1.0))) {
299 /* Get the set of ready tasks */
300 ready_tasks = get_ready_tasks(dax);
301 if (xbt_dynar_is_empty(ready_tasks)) {
302 xbt_dynar_free_container(&ready_tasks);
303 /* there is no ready task, let advance the simulation */
306 /* For each ready task:
307 * get the workstation that minimizes the completion time.
308 * select the task that has the minimum completion time on
309 * its best workstation.
311 xbt_dynar_foreach(ready_tasks, cursor, task) {
312 XBT_DEBUG("%s is ready", SD_task_get_name(task));
313 workstation = SD_task_get_best_workstation(task);
314 finish_time = finish_on_at(task, workstation);
315 if (min_finish_time == -1. || finish_time < min_finish_time) {
316 min_finish_time = finish_time;
317 selected_task = task;
318 selected_workstation = workstation;
322 XBT_INFO("Schedule %s on %s", SD_task_get_name(selected_task),
323 sg_host_get_name(selected_workstation));
325 SD_task_schedulel(selected_task, 1, selected_workstation);
328 * SimDag allows tasks to be executed concurrently when they can by default.
329 * Yet schedulers take decisions assuming that tasks wait for resource
330 * availability to start.
331 * The solution (well crude hack is to keep track of the last task scheduled
332 * on a workstation and add a special type of dependency if needed to
333 * force the sequential execution meant by the scheduler.
334 * If the last scheduled task is already done, has failed or is a
335 * predecessor of the current task, no need for a new dependency
338 last_scheduled_task =
339 sg_host_get_last_scheduled_task(selected_workstation);
340 if (last_scheduled_task &&
341 (SD_task_get_state(last_scheduled_task) != SD_DONE) &&
342 (SD_task_get_state(last_scheduled_task) != SD_FAILED) &&
343 !SD_task_dependency_exists(
344 sg_host_get_last_scheduled_task(selected_workstation),
346 SD_task_dependency_add("resource", NULL,
347 last_scheduled_task, selected_task);
349 sg_host_set_last_scheduled_task(selected_workstation, selected_task);
351 sg_host_set_available_at(selected_workstation, min_finish_time);
353 xbt_dynar_free_container(&ready_tasks);
354 /* reset the min_finish_time for the next set of ready tasks */
355 min_finish_time = -1.;
358 XBT_INFO("Simulation Time: %f", SD_get_clock());
364 ("------------------- Produce the trace file---------------------------");
365 XBT_INFO("Producing the trace of the run into %s", tracefilename);
366 out = fopen(tracefilename, "w");
367 xbt_assert(out, "Cannot write to %s", tracefilename);
370 output_xml(out, dax);
375 xbt_dynar_free_container(&ready_tasks);
377 xbt_dynar_foreach(dax, cursor, task) {
378 SD_task_destroy(task);
380 xbt_dynar_free_container(&dax);
382 for (cursor = 0; cursor < total_nworkstations; cursor++)
383 sg_host_free_attribute(workstations[cursor]);