Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
catch some bugs and smells
[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 #include <math.h>
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test, "Logging specific to this SimDag example");
14
15 int main(int argc, char **argv)
16 {
17   unsigned int ctr;
18   SD_task_t checkB;
19   SD_task_t checkD;
20   xbt_dynar_t changed_tasks;
21   sg_host_t host_list[2];
22   double computation_amount[2];
23   double communication_amount[4] = { 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, static_cast<void*>(&comp_amount1));
65   if (fabs(comp_amount1 - (*(static_cast<double*>(SD_task_get_data(taskA))))) > 1e-12)
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   } catch (xbt_ex& ex) {
78     if (ex.category != arg_error)
79       throw;                  /* this is a serious error */
80   }
81
82   try {
83     SD_task_dependency_add(NULL, NULL, taskB, taskA);   /* shouldn't work and must raise an exception */
84     xbt_die("Oh oh, I can add an already existing dependency!");
85   } catch (xbt_ex& ex) {
86     if (ex.category != arg_error)
87       throw;
88   }
89
90   try {
91     SD_task_dependency_remove(taskA, taskC);    /* shouldn't work and must raise an exception */
92     xbt_die("Dude, I can remove an unknown dependency!");
93   } catch (xbt_ex& ex) {
94     if (ex.category != arg_error)
95       throw;
96   }
97
98   try {
99     SD_task_dependency_remove(taskC, taskC);    /* shouldn't work and must raise an exception */
100     xbt_die("Wow, I can remove a dependency between Task C and itself!");
101   } catch (xbt_ex& ex) {
102     if (ex.category != arg_error)
103       throw;
104   }
105
106   /* if everything is ok, no exception is forwarded or rethrown by main() */
107
108   /* watch points */
109   SD_task_watch(taskD, SD_DONE);
110   SD_task_watch(taskB, SD_DONE);
111   SD_task_unwatch(taskD, SD_DONE);
112
113   /* scheduling parameters */
114   host_list[0] = h1;
115   host_list[1] = h2;
116   computation_amount[0] = comp_amount1;
117   computation_amount[1] = comp_amount2;
118
119   communication_amount[1] = comm_amount12;
120   communication_amount[2] = comm_amount21;
121
122   /* estimated time */
123   SD_task_t task = taskD;
124   XBT_INFO("Estimated time for '%s': %f", SD_task_get_name(task), SD_task_get_execution_time(task, 2, host_list,
125            computation_amount, communication_amount));
126
127   SD_task_schedule(taskA, 2, host_list, computation_amount, communication_amount, -1);
128   SD_task_schedule(taskB, 2, host_list, computation_amount, communication_amount, -1);
129   SD_task_schedule(taskC, 2, host_list, computation_amount, communication_amount, -1);
130   SD_task_schedule(taskD, 2, host_list, computation_amount, communication_amount, -1);
131
132   changed_tasks = SD_simulate(-1.0);
133   xbt_dynar_foreach(changed_tasks, ctr, task) {
134     XBT_INFO("Task '%s' start time: %f, finish time: %f", SD_task_get_name(task),
135           SD_task_get_start_time(task), SD_task_get_finish_time(task));
136   }
137
138   xbt_dynar_get_cpy(changed_tasks, 0, &checkD);
139   xbt_dynar_get_cpy(changed_tasks, 1, &checkB);
140
141   xbt_assert(checkD == taskD && checkB == taskB, "Unexpected simulation results");
142
143   XBT_DEBUG("Destroying tasks...");
144   SD_task_destroy(taskA);
145   SD_task_destroy(taskB);
146   SD_task_destroy(taskC);
147   SD_task_destroy(taskD);
148
149   XBT_DEBUG("Tasks destroyed. Exiting SimDag...");
150   SD_exit();
151   return 0;
152 }