Logo AND Algorithmique Numérique Distribuée

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