1 /* Copyright (c) 2006-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include "simgrid/simdag.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test,
14 "Logging specific to this SimDag example");
16 int main(int argc, char **argv)
20 const char *platform_file;
21 const SD_workstation_t *workstations;
24 double computation_amount1;
25 double computation_amount2;
26 double communication_amount12;
27 double communication_amount21;
28 const SD_link_t *route;
30 SD_task_t task, taskA, taskB, taskC, taskD, checkB, checkD;
31 xbt_dynar_t changed_tasks;
33 const int workstation_number = 2;
34 SD_workstation_t workstation_list[2];
35 double computation_amount[2];
36 double communication_amount[4] = { 0 };
38 SD_workstation_t w1, w2;
40 /* initialization of SD */
43 /* xbt_log_control_set("sd.thres=debug"); */
46 XBT_INFO("Usage: %s platform_file", argv[0]);
47 XBT_INFO("example: %s sd_platform.xml", argv[0]);
51 /* creation of the environment */
52 platform_file = argv[1];
53 SD_create_environment(platform_file);
55 /* test the estimation functions */
56 workstations = SD_workstation_get_list();
59 SD_workstation_set_access_mode(w2, SD_WORKSTATION_SEQUENTIAL_ACCESS);
60 name1 = SD_workstation_get_name(w1);
61 name2 = SD_workstation_get_name(w2);
62 computation_amount1 = 2000000;
63 computation_amount2 = 1000000;
64 communication_amount12 = 2000000;
65 communication_amount21 = 3000000;
66 XBT_INFO("Computation time for %f flops on %s: %f", computation_amount1,
67 name1, SD_workstation_get_computation_time(w1,
68 computation_amount1));
69 XBT_INFO("Computation time for %f flops on %s: %f", computation_amount2,
70 name2, SD_workstation_get_computation_time(w2,
71 computation_amount2));
73 XBT_INFO("Route between %s and %s:", name1, name2);
74 route = SD_route_get_list(w1, w2);
75 route_size = SD_route_get_size(w1, w2);
76 for (i = 0; i < route_size; i++) {
77 XBT_INFO(" Link %s: latency = %f, bandwidth = %f",
78 SD_link_get_name(route[i]),
79 SD_link_get_current_latency(route[i]),
80 SD_link_get_current_bandwidth(route[i]));
82 XBT_INFO("Route latency = %f, route bandwidth = %f",
83 SD_route_get_current_latency(w1, w2),
84 SD_route_get_current_bandwidth(w1, w2));
85 XBT_INFO("Communication time for %f bytes between %s and %s: %f",
86 communication_amount12, name1, name2,
87 SD_route_get_communication_time(w1, w2, communication_amount12));
88 XBT_INFO("Communication time for %f bytes between %s and %s: %f",
89 communication_amount21, name2, name1,
90 SD_route_get_communication_time(w2, w1, communication_amount21));
92 /* creation of the tasks and their dependencies */
93 taskA = SD_task_create("Task A", NULL, 10.0);
94 taskB = SD_task_create("Task B", NULL, 40.0);
95 taskC = SD_task_create("Task C", NULL, 30.0);
96 taskD = SD_task_create("Task D", NULL, 60.0);
98 /* try to attach and retrieve user data to a task */
99 SD_task_set_data(taskA, (void*) &computation_amount1);
100 if (computation_amount1 != (*((double*) SD_task_get_data(taskA))))
101 XBT_ERROR("User data was corrupted by a simple set/get");
103 SD_task_dependency_add(NULL, NULL, taskB, taskA);
104 SD_task_dependency_add(NULL, NULL, taskC, taskA);
105 SD_task_dependency_add(NULL, NULL, taskD, taskB);
106 SD_task_dependency_add(NULL, NULL, taskD, taskC);
107 /* SD_task_dependency_add(NULL, NULL, taskA, taskD); /\* deadlock */
112 SD_task_dependency_add(NULL, NULL, taskA, taskA); /* shouldn't work and must raise an exception */
113 xbt_die("Hey, I can add a dependency between Task A and Task A!");
116 if (ex.category != arg_error)
117 RETHROW; /* this is a serious error */
122 SD_task_dependency_add(NULL, NULL, taskB, taskA); /* shouldn't work and must raise an exception */
123 xbt_die("Oh oh, I can add an already existing dependency!");
126 if (ex.category != arg_error)
132 SD_task_dependency_remove(taskA, taskC); /* shouldn't work and must raise an exception */
133 xbt_die("Dude, I can remove an unknown dependency!");
136 if (ex.category != arg_error)
142 SD_task_dependency_remove(taskC, taskC); /* shouldn't work and must raise an exception */
143 xbt_die("Wow, I can remove a dependency between Task C and itself!");
146 if (ex.category != arg_error)
152 /* if everything is ok, no exception is forwarded or rethrown by main() */
155 SD_task_watch(taskD, SD_DONE);
156 SD_task_watch(taskB, SD_DONE);
157 SD_task_unwatch(taskD, SD_DONE);
160 /* scheduling parameters */
161 workstation_list[0] = w1;
162 workstation_list[1] = w2;
163 computation_amount[0] = computation_amount1;
164 computation_amount[1] = computation_amount2;
166 communication_amount[1] = communication_amount12;
167 communication_amount[2] = communication_amount21;
171 XBT_INFO("Estimated time for '%s': %f", SD_task_get_name(task),
172 SD_task_get_execution_time(task, workstation_number,
173 workstation_list, computation_amount,
174 communication_amount));
176 /* let's launch the simulation! */
178 SD_task_schedule(taskA, workstation_number, workstation_list,
179 computation_amount, communication_amount, rate);
180 SD_task_schedule(taskB, workstation_number, workstation_list,
181 computation_amount, communication_amount, rate);
182 SD_task_schedule(taskC, workstation_number, workstation_list,
183 computation_amount, communication_amount, rate);
184 SD_task_schedule(taskD, workstation_number, workstation_list,
185 computation_amount, communication_amount, rate);
187 changed_tasks = SD_simulate(-1.0);
188 xbt_dynar_foreach(changed_tasks, ctr, task) {
189 XBT_INFO("Task '%s' start time: %f, finish time: %f",
190 SD_task_get_name(task),
191 SD_task_get_start_time(task), SD_task_get_finish_time(task));
194 xbt_dynar_get_cpy(changed_tasks, 0, &checkD);
195 xbt_dynar_get_cpy(changed_tasks, 1, &checkB);
197 xbt_assert(checkD == taskD &&
198 checkB == taskB, "Unexpected simulation results");
200 xbt_dynar_free_container(&changed_tasks);
202 XBT_DEBUG("Destroying tasks...");
204 SD_task_destroy(taskA);
205 SD_task_destroy(taskB);
206 SD_task_destroy(taskC);
207 SD_task_destroy(taskD);
209 XBT_DEBUG("Tasks destroyed. Exiting SimDag...");