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. */
7 #include "simgrid/simdag.h"
11 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test, "Logging specific to this SimDag example");
13 int main(int argc, char **argv)
16 SD_task_t checkB, checkD;
17 xbt_dynar_t changed_tasks;
19 const int host_count = 2;
20 sg_host_t host_list[2];
21 double computation_amount[2];
22 double communication_amount[4] = { 0 };
25 /* initialization of SD */
28 xbt_assert(argc > 1, "Usage: %s platform_file\n\nExample: %s two_clusters.xml", argv[0], argv[0]);
29 SD_create_environment(argv[1]);
31 /* test the estimation functions */
32 const sg_host_t *hosts = sg_host_list();
33 sg_host_t h1 = hosts[0];
34 sg_host_t h2 = hosts[1];
35 const char *name1 = sg_host_get_name(h1);
36 const char *name2 = sg_host_get_name(h2);
37 double comp_amount1 = 2000000;
38 double comp_amount2 = 1000000;
39 double comm_amount12 = 2000000;
40 double comm_amount21 = 3000000;
41 XBT_INFO("Computation time for %f flops on %s: %f", comp_amount1, name1, comp_amount1/sg_host_speed(h1));
42 XBT_INFO("Computation time for %f flops on %s: %f", comp_amount2, name2, comp_amount2/sg_host_speed(h2));
44 XBT_INFO("Route between %s and %s:", name1, name2);
45 const SD_link_t *route = SD_route_get_list(h1, h2);
46 int route_size = SD_route_get_size(h1, h2);
47 for (int i = 0; i < route_size; i++) {
48 XBT_INFO(" Link %s: latency = %f, bandwidth = %f", sg_link_name(route[i]), sg_link_latency(route[i]),
49 sg_link_bandwidth(route[i]));
51 XBT_INFO("Route latency = %f, route bandwidth = %f", SD_route_get_latency(h1, h2), SD_route_get_bandwidth(h1, h2));
52 XBT_INFO("Communication time for %f bytes between %s and %s: %f", comm_amount12, name1, name2,
53 SD_route_get_latency(h1, h2) + comm_amount12 / SD_route_get_bandwidth(h1, h2));
54 XBT_INFO("Communication time for %f bytes between %s and %s: %f", comm_amount21, name2, name1,
55 SD_route_get_latency(h2, h1) + comm_amount21 / SD_route_get_bandwidth(h2, h1));
57 /* creation of the tasks and their dependencies */
58 SD_task_t taskA = SD_task_create("Task A", NULL, 10.0);
59 SD_task_t taskB = SD_task_create("Task B", NULL, 40.0);
60 SD_task_t taskC = SD_task_create("Task C", NULL, 30.0);
61 SD_task_t taskD = SD_task_create("Task D", NULL, 60.0);
63 /* try to attach and retrieve user data to a task */
64 SD_task_set_data(taskA, (void*) &comp_amount1);
65 if (comp_amount1 != (*((double*) SD_task_get_data(taskA))))
66 XBT_ERROR("User data was corrupted by a simple set/get");
68 SD_task_dependency_add(NULL, NULL, taskB, taskA);
69 SD_task_dependency_add(NULL, NULL, taskC, taskA);
70 SD_task_dependency_add(NULL, NULL, taskD, taskB);
71 SD_task_dependency_add(NULL, NULL, taskD, taskC);
72 SD_task_dependency_add(NULL, NULL, taskB, taskC);
75 SD_task_dependency_add(NULL, NULL, taskA, taskA); /* shouldn't work and must raise an exception */
76 xbt_die("Hey, I can add a dependency between Task A and Task A!");
79 if (ex.category != arg_error)
80 RETHROW; /* this is a serious error */
85 SD_task_dependency_add(NULL, NULL, taskB, taskA); /* shouldn't work and must raise an exception */
86 xbt_die("Oh oh, I can add an already existing dependency!");
89 if (ex.category != arg_error)
95 SD_task_dependency_remove(taskA, taskC); /* shouldn't work and must raise an exception */
96 xbt_die("Dude, I can remove an unknown dependency!");
99 if (ex.category != arg_error)
105 SD_task_dependency_remove(taskC, taskC); /* shouldn't work and must raise an exception */
106 xbt_die("Wow, I can remove a dependency between Task C and itself!");
109 if (ex.category != arg_error)
114 /* if everything is ok, no exception is forwarded or rethrown by main() */
117 SD_task_watch(taskD, SD_DONE);
118 SD_task_watch(taskB, SD_DONE);
119 SD_task_unwatch(taskD, SD_DONE);
121 /* scheduling parameters */
124 computation_amount[0] = comp_amount1;
125 computation_amount[1] = comp_amount2;
127 communication_amount[1] = comm_amount12;
128 communication_amount[2] = comm_amount21;
131 SD_task_t task = taskD;
132 XBT_INFO("Estimated time for '%s': %f", SD_task_get_name(task), SD_task_get_execution_time(task, host_count,
133 host_list, computation_amount, communication_amount));
135 SD_task_schedule(taskA, host_count, host_list, computation_amount, communication_amount, rate);
136 SD_task_schedule(taskB, host_count, host_list, computation_amount, communication_amount, rate);
137 SD_task_schedule(taskC, host_count, host_list, computation_amount, communication_amount, rate);
138 SD_task_schedule(taskD, host_count, host_list, computation_amount, communication_amount, rate);
140 changed_tasks = SD_simulate(-1.0);
141 xbt_dynar_foreach(changed_tasks, ctr, task) {
142 XBT_INFO("Task '%s' start time: %f, finish time: %f", SD_task_get_name(task),
143 SD_task_get_start_time(task), SD_task_get_finish_time(task));
146 xbt_dynar_get_cpy(changed_tasks, 0, &checkD);
147 xbt_dynar_get_cpy(changed_tasks, 1, &checkB);
149 xbt_assert(checkD == taskD && checkB == taskB, "Unexpected simulation results");
151 XBT_DEBUG("Destroying tasks...");
152 SD_task_destroy(taskA);
153 SD_task_destroy(taskB);
154 SD_task_destroy(taskC);
155 SD_task_destroy(taskD);
157 XBT_DEBUG("Tasks destroyed. Exiting SimDag...");