Logo AND Algorithmique Numérique Distribuée

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