Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a73be5f92e8aa65c60ed7f57847931340fdf3a19
[simgrid.git] / examples / simdag / test / sd_test.cpp
1 /* Copyright (c) 2006-2015. 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 #include "simgrid/simdag.h"
8 #include "xbt/ex.h"
9 #include <xbt/ex.hpp>
10 #include "xbt/log.h"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test, "Logging specific to this SimDag example");
13
14 int main(int argc, char **argv)
15 {
16   unsigned int ctr;
17   SD_task_t checkB, checkD;
18   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 };
23   double rate = -1.0;
24
25   /* initialization of SD */
26   SD_init(&argc, argv);
27
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]);
30
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));
43
44   XBT_INFO("Route between %s and %s:", name1, name2);
45   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]));
50   xbt_free(route);
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));
56
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);
62
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");
67
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);
73
74   try {
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!");
77   }
78   catch (xbt_ex& ex) {
79     if (ex.category != arg_error)
80       throw;                  /* this is a serious error */
81   }
82
83   try {
84     SD_task_dependency_add(NULL, NULL, taskB, taskA);   /* shouldn't work and must raise an exception */
85     xbt_die("Oh oh, I can add an already existing dependency!");
86   }
87   catch (xbt_ex& ex) {
88     if (ex.category != arg_error)
89       throw;
90   }
91
92   try {
93     SD_task_dependency_remove(taskA, taskC);    /* shouldn't work and must raise an exception */
94     xbt_die("Dude, I can remove an unknown dependency!");
95   }
96   catch (xbt_ex& ex) {
97     if (ex.category != arg_error)
98       throw;
99   }
100
101   try {
102     SD_task_dependency_remove(taskC, taskC);    /* shouldn't work and must raise an exception */
103     xbt_die("Wow, I can remove a dependency between Task C and itself!");
104   }
105   catch (xbt_ex& ex) {
106     if (ex.category != arg_error)
107       throw;
108   }
109
110   /* if everything is ok, no exception is forwarded or rethrown by main() */
111
112   /* watch points */
113   SD_task_watch(taskD, SD_DONE);
114   SD_task_watch(taskB, SD_DONE);
115   SD_task_unwatch(taskD, SD_DONE);
116
117   /* scheduling parameters */
118   host_list[0] = h1;
119   host_list[1] = h2;
120   computation_amount[0] = comp_amount1;
121   computation_amount[1] = comp_amount2;
122
123   communication_amount[1] = comm_amount12;
124   communication_amount[2] = comm_amount21;
125
126   /* estimated time */
127   SD_task_t task = taskD;
128   XBT_INFO("Estimated time for '%s': %f", SD_task_get_name(task), SD_task_get_execution_time(task, host_count,
129            host_list, computation_amount, communication_amount));
130
131   SD_task_schedule(taskA, host_count, host_list, computation_amount, communication_amount, rate);
132   SD_task_schedule(taskB, host_count, host_list, computation_amount, communication_amount, rate);
133   SD_task_schedule(taskC, host_count, host_list, computation_amount, communication_amount, rate);
134   SD_task_schedule(taskD, host_count, host_list, computation_amount, communication_amount, rate);
135
136   changed_tasks = SD_simulate(-1.0);
137   xbt_dynar_foreach(changed_tasks, ctr, task) {
138     XBT_INFO("Task '%s' start time: %f, finish time: %f", SD_task_get_name(task),
139           SD_task_get_start_time(task), SD_task_get_finish_time(task));
140   }
141
142   xbt_dynar_get_cpy(changed_tasks, 0, &checkD);
143   xbt_dynar_get_cpy(changed_tasks, 1, &checkB);
144
145   xbt_assert(checkD == taskD && checkB == taskB, "Unexpected simulation results");
146
147   XBT_DEBUG("Destroying tasks...");
148   SD_task_destroy(taskA);
149   SD_task_destroy(taskB);
150   SD_task_destroy(taskC);
151   SD_task_destroy(taskD);
152
153   XBT_DEBUG("Tasks destroyed. Exiting SimDag...");
154   SD_exit();
155   return 0;
156 }