Logo AND Algorithmique Numérique Distribuée

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