Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move GRAS tests to TESH
[simgrid.git] / testsuite / 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   char * platform_file = argv[1];
24   SD_create_environment(platform_file);
25
26   /* test the estimation functions (use small_platform.xml) */
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   const SD_link_t *route = SD_route_get_list(w1, w2);
37   int route_size = SD_route_get_size(w1, w2);
38   SD_task_t taskA = SD_task_create("Task A", NULL, 10.0);
39   SD_task_t taskB = SD_task_create("Task B", NULL, 40.0);
40   SD_task_t taskC = SD_task_create("Task C", NULL, 30.0);
41   SD_task_t taskD = SD_task_create("Task D", NULL, 60.0);
42   xbt_ex_t ex;
43
44   INFO3("Computation time for %f flops on %s: %f", computation_amount1, name1,
45         SD_workstation_get_computation_time(w1, computation_amount1));
46   INFO3("Computation time for %f flops on %s: %f", computation_amount2, name2,
47         SD_workstation_get_computation_time(w2, computation_amount2));
48
49   INFO2("Route between %s and %s:", name1, name2);
50   for (i = 0; i < route_size; i++) {
51     INFO3("\tLink %s: latency = %f, bandwidth = %f", SD_link_get_name(route[i]),
52           SD_link_get_current_latency(route[i]), SD_link_get_current_bandwidth(route[i]));
53   }
54   INFO2("Route latency = %f, route bandwidth = %f", SD_route_get_current_latency(w1, w2),
55         SD_route_get_current_bandwidth(w1, w2));
56   INFO4("Communication time for %f bytes between %s and %s: %f", communication_amount12, name1, name2,
57         SD_route_get_communication_time(w1, w2, communication_amount12));
58   INFO4("Communication time for %f bytes between %s and %s: %f", communication_amount21, name2, name1,
59         SD_route_get_communication_time(w2, w1, communication_amount21));
60
61   /* creation of the tasks and their dependencies */  
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   TRY {
70     SD_task_dependency_add(NULL, NULL, taskA, taskA); /* shouldn't work and must raise an exception */
71     xbt_assert0(0, "Hey, I can add a dependency between Task A and Task A!");
72   }
73   CATCH (ex) {
74   }
75   
76   TRY {
77     SD_task_dependency_add(NULL, NULL, taskA, taskB); /* shouldn't work and must raise an exception */
78     xbt_assert0(0, "Oh oh, I can add an already existing dependency!");
79   }
80   CATCH (ex) {
81   }
82
83   SD_task_dependency_remove(taskA, taskB);
84
85   TRY {
86     SD_task_dependency_remove(taskC, taskA); /* shouldn't work and must raise an exception */
87     xbt_assert0(0, "Dude, I can remove an unknown dependency!");
88   }
89   CATCH (ex) {
90   }
91
92   TRY {
93     SD_task_dependency_remove(taskC, taskC); /* shouldn't work and must raise an exception */
94     xbt_assert0(0, "Wow, I can remove a dependency between Task C and itself!");
95   }
96   CATCH (ex) {
97   }
98
99
100   /* if everything is ok, no exception is forwarded or rethrown by main() */
101
102   /* watch points */
103   SD_task_watch(taskD, SD_DONE);
104   SD_task_watch(taskB, SD_DONE);
105   SD_task_unwatch(taskD, SD_DONE);
106
107
108   /* scheduling parameters */
109   {
110     const int workstation_number = 2;
111     const SD_workstation_t workstation_list[] = {w1, w2};
112     double computation_amount[] = {computation_amount1, computation_amount2};
113     double communication_amount[] =
114       {
115         0, communication_amount12,
116         communication_amount21, 0
117       };
118     SD_task_t *changed_tasks;
119     double rate = -1.0;
120         
121     /* estimated time */
122     SD_task_t task = taskD;
123     INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
124           SD_task_get_execution_time(task, workstation_number, workstation_list,
125                                      computation_amount, communication_amount, rate));
126     
127     /* let's launch the simulation! */
128     
129     SD_task_schedule(taskA, workstation_number, workstation_list,
130                      computation_amount, communication_amount, rate);
131     SD_task_schedule(taskB, workstation_number, workstation_list,
132                      computation_amount, communication_amount, rate);
133     SD_task_schedule(taskC, workstation_number, workstation_list,
134                      computation_amount, communication_amount, rate);
135     SD_task_schedule(taskD, workstation_number, workstation_list,
136                      computation_amount, communication_amount, rate);
137     
138     changed_tasks = SD_simulate(-1.0);
139     xbt_assert0(changed_tasks[0] == taskD &&
140                 changed_tasks[1] == taskB &&
141                 changed_tasks[2] == taskC &&
142                 changed_tasks[3] == NULL,
143                 "Unexpected simulation results");
144     
145     xbt_free(changed_tasks);
146   }
147   DEBUG0("Destroying tasks...");
148
149   SD_task_destroy(taskA);
150   SD_task_destroy(taskB);
151   SD_task_destroy(taskC);
152   SD_task_destroy(taskD);
153
154   DEBUG0("Tasks destroyed. Exiting SimDag...");
155
156   SD_exit();
157   return 0;
158 }
159