Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add missing xbt_ex_free
[simgrid.git] / examples / simdag / sd_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "simdag/simdag.h"
4 #include "xbt/ex.h"
5 #include "xbt/log.h"
6
7 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test,
8                              "Logging specific to this SimDag example");
9
10 int main(int argc, char **argv) {
11   int i;
12
13   /* initialisation of SD */
14   SD_init(&argc, argv);
15
16   if (argc < 2) {
17     INFO1("Usage: %s platform_file", argv[0]);
18     INFO1("example: %s sd_platform.xml", argv[0]);
19     exit(1);
20   }
21
22   /* creation of the environment */
23   const char * platform_file = argv[1];
24   SD_create_environment(platform_file);
25
26   /* test the estimation functions */
27   const SD_workstation_t *workstations = SD_workstation_get_list();
28   SD_workstation_t w1 = workstations[0];
29   SD_workstation_t w2 = workstations[1];
30   const char *name1 = SD_workstation_get_name(w1);
31   const char *name2 = SD_workstation_get_name(w2);
32   const double computation_amount1 = 2000000;
33   const double computation_amount2 = 1000000;
34   const double communication_amount12 = 2000000;
35   const double communication_amount21 = 3000000;
36   INFO3("Computation time for %f flops on %s: %f", computation_amount1, name1,
37         SD_workstation_get_computation_time(w1, computation_amount1));
38   INFO3("Computation time for %f flops on %s: %f", computation_amount2, name2,
39         SD_workstation_get_computation_time(w2, computation_amount2));
40
41   INFO2("Route between %s and %s:", name1, name2);
42   SD_link_t *route = SD_route_get_list(w1, w2);
43   int route_size = SD_route_get_size(w1, w2);
44   for (i = 0; i < route_size; i++) {
45     INFO3("\tLink %s: latency = %f, bandwidth = %f", SD_link_get_name(route[i]),
46           SD_link_get_current_latency(route[i]), SD_link_get_current_bandwidth(route[i]));
47   }
48   INFO2("Route latency = %f, route bandwidth = %f", SD_route_get_current_latency(w1, w2),
49         SD_route_get_current_bandwidth(w1, w2));
50   INFO4("Communication time for %f bytes between %s and %s: %f", communication_amount12, name1, name2,
51         SD_route_get_communication_time(w1, w2, communication_amount12));
52   INFO4("Communication time for %f bytes between %s and %s: %f", communication_amount21, name2, name1,
53         SD_route_get_communication_time(w2, w1, communication_amount21));
54   xbt_free(route);
55
56   /* creation of the tasks and their dependencies */
57   SD_task_t taskA = SD_task_create("Task A", NULL, 10.0);
58   SD_task_t taskB = SD_task_create("Task B", NULL, 40.0);
59   SD_task_t taskC = SD_task_create("Task C", NULL, 30.0);
60   SD_task_t taskD = SD_task_create("Task D", NULL, 60.0);
61   
62
63   SD_task_dependency_add(NULL, NULL, taskB, taskA);
64   SD_task_dependency_add(NULL, NULL, taskC, taskA);
65   SD_task_dependency_add(NULL, NULL, taskD, taskB);
66   SD_task_dependency_add(NULL, NULL, taskD, taskC);
67   /*  SD_task_dependency_add(NULL, NULL, taskA, taskD); /\* deadlock */
68
69   xbt_ex_t ex;
70
71   TRY {
72     SD_task_dependency_add(NULL, NULL, taskA, taskA); /* shouldn't work and must raise an exception */
73     xbt_assert0(0, "Hey, I can add a dependency between Task A and Task A!");
74   }
75   CATCH (ex) {
76     xbt_ex_free(ex);
77   }
78   
79   TRY {
80     SD_task_dependency_add(NULL, NULL, taskA, taskB); /* shouldn't work and must raise an exception */
81     xbt_assert0(0, "Oh oh, I can add an already existing dependency!");
82   }
83   CATCH (ex) {
84     xbt_ex_free(ex);
85   }
86
87   SD_task_dependency_remove(taskA, taskB);
88
89   TRY {
90     SD_task_dependency_remove(taskC, taskA); /* shouldn't work and must raise an exception */
91     xbt_assert0(0, "Dude, I can remove an unknown dependency!");
92   }
93   CATCH (ex) {
94     xbt_ex_free(ex);
95   }
96
97   TRY {
98     SD_task_dependency_remove(taskC, taskC); /* shouldn't work and must raise an exception */
99     xbt_assert0(0, "Wow, I can remove a dependency between Task C and itself!");
100   }
101   CATCH (ex) {
102     xbt_ex_free(ex);
103   }
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
114   /* scheduling parameters */
115
116   const int workstation_number = 2;
117   const SD_workstation_t workstation_list[] = {w1, w2};
118   double computation_amount[] = {computation_amount1, computation_amount2};
119   double communication_amount[] =
120     {
121       0, communication_amount12,
122       communication_amount21, 0
123     };
124   double rate = -1.0;
125
126   /* estimated time */
127   SD_task_t task = taskD;
128   INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
129         SD_task_get_execution_time(task, workstation_number, workstation_list,
130                                    computation_amount, communication_amount, rate));
131
132   /* let's launch the simulation! */
133
134   SD_task_schedule(taskA, workstation_number, workstation_list,
135                    computation_amount, communication_amount, rate);
136   SD_task_schedule(taskB, workstation_number, workstation_list,
137                    computation_amount, communication_amount, rate);
138   SD_task_schedule(taskC, workstation_number, workstation_list,
139                    computation_amount, communication_amount, rate);
140   SD_task_schedule(taskD, workstation_number, workstation_list,
141                    computation_amount, communication_amount, rate);
142
143   SD_task_t *changed_tasks;
144
145   changed_tasks = SD_simulate(-1.0);
146   xbt_assert0(changed_tasks[0] == taskD &&
147               changed_tasks[1] == taskB &&
148               changed_tasks[2] == taskC &&
149               changed_tasks[3] == NULL,
150               "Unexpected simulation results");
151
152   for (i = 0; changed_tasks[i] != NULL; i++) {
153     INFO3("Task '%s' start time: %f, finish time: %f",
154           SD_task_get_name(changed_tasks[i]),
155           SD_task_get_start_time(changed_tasks[i]),
156           SD_task_get_finish_time(changed_tasks[i]));
157   }
158   
159   xbt_free(changed_tasks);
160
161   DEBUG0("Destroying tasks...");
162
163   SD_task_destroy(taskA);
164   SD_task_destroy(taskB);
165   SD_task_destroy(taskC);
166   SD_task_destroy(taskD);
167
168   DEBUG0("Tasks destroyed. Exiting SimDag...");
169
170   SD_exit();
171   return 0;
172 }
173