Logo AND Algorithmique Numérique Distribuée

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