Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Re-import the simple-token example (was named tokenS before)
[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   }
77   
78   TRY {
79     SD_task_dependency_add(NULL, NULL, taskA, taskB); /* shouldn't work and must raise an exception */
80     xbt_assert0(0, "Oh oh, I can add an already existing dependency!");
81   }
82   CATCH (ex) {
83   }
84
85   SD_task_dependency_remove(taskA, taskB);
86
87   TRY {
88     SD_task_dependency_remove(taskC, taskA); /* shouldn't work and must raise an exception */
89     xbt_assert0(0, "Dude, I can remove an unknown dependency!");
90   }
91   CATCH (ex) {
92   }
93
94   TRY {
95     SD_task_dependency_remove(taskC, taskC); /* shouldn't work and must raise an exception */
96     xbt_assert0(0, "Wow, I can remove a dependency between Task C and itself!");
97   }
98   CATCH (ex) {
99   }
100
101
102   /* if everything is ok, no exception is forwarded or rethrown by main() */
103
104   /* watch points */
105   SD_task_watch(taskD, SD_DONE);
106   SD_task_watch(taskB, SD_DONE);
107   SD_task_unwatch(taskD, SD_DONE);
108
109
110   /* scheduling parameters */
111
112   const int workstation_number = 2;
113   const SD_workstation_t workstation_list[] = {w1, w2};
114   double computation_amount[] = {computation_amount1, computation_amount2};
115   double communication_amount[] =
116     {
117       0, communication_amount12,
118       communication_amount21, 0
119     };
120   double rate = -1.0;
121
122   /* estimated time */
123   SD_task_t task = taskD;
124   INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
125         SD_task_get_execution_time(task, workstation_number, workstation_list,
126                                    computation_amount, communication_amount, rate));
127
128   /* let's launch the simulation! */
129
130   SD_task_schedule(taskA, workstation_number, workstation_list,
131                    computation_amount, communication_amount, rate);
132   SD_task_schedule(taskB, workstation_number, workstation_list,
133                    computation_amount, communication_amount, rate);
134   SD_task_schedule(taskC, workstation_number, workstation_list,
135                    computation_amount, communication_amount, rate);
136   SD_task_schedule(taskD, workstation_number, workstation_list,
137                    computation_amount, communication_amount, rate);
138
139   SD_task_t *changed_tasks;
140
141   changed_tasks = SD_simulate(-1.0);
142   xbt_assert0(changed_tasks[0] == taskD &&
143               changed_tasks[1] == taskB &&
144               changed_tasks[2] == taskC &&
145               changed_tasks[3] == NULL,
146               "Unexpected simulation results");
147   
148   xbt_free(changed_tasks);
149
150   DEBUG0("Destroying tasks...");
151
152   SD_task_destroy(taskA);
153   SD_task_destroy(taskB);
154   SD_task_destroy(taskC);
155   SD_task_destroy(taskD);
156
157   DEBUG0("Tasks destroyed. Exiting SimDag...");
158
159   SD_exit();
160   return 0;
161 }
162