Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Huge set of modifications to restore more uniform APIs when dealing with src and...
[simgrid.git] / testsuite / simdag / sd_test.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "simdag/simdag.h"
10 #include "xbt/ex.h"
11 #include "xbt/log.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test,
14                              "Logging specific to this SimDag example");
15
16 int main(int argc, char **argv)
17 {
18   int i;
19   const char *platform_file;
20
21   const SD_workstation_t *workstations;
22   SD_workstation_t w1;
23   SD_workstation_t w2;
24   const char *name1 = NULL;
25   const char *name2 = NULL;
26   const double computation_amount1 = 2000000;
27   const double computation_amount2 = 1000000;
28   const double communication_amount12 = 2000000;
29   const double communication_amount21 = 3000000;
30   const SD_link_t *route;
31   int route_size;
32   SD_task_t taskA;
33   SD_task_t taskB, checkB;
34   SD_task_t taskC, checkC;
35   SD_task_t taskD, checkD;
36   xbt_ex_t ex;
37
38   /* initialisation of SD */
39   SD_init(&argc, argv);
40
41   if (argc < 2) {
42     INFO1("Usage: %s platform_file", argv[0]);
43     INFO1("example: %s sd_platform.xml", argv[0]);
44     exit(1);
45   }
46
47   /* creation of the environment */
48
49   platform_file = argv[1];
50
51   SD_create_environment(platform_file);
52
53   /* test the estimation functions (use small_platform.xml) */
54   workstations = SD_workstation_get_list();
55
56
57   for(i=0 ; i < SD_workstation_get_number() ; i++)
58   {
59           INFO1("name : %s",SD_workstation_get_name(workstations[i]) );
60   }
61
62   w1 = workstations[0];
63   w2 = workstations[1];
64   name1 = SD_workstation_get_name(w1);
65   name2 = SD_workstation_get_name(w2);
66
67   route = SD_route_get_list(w1, w2);
68
69   route_size = SD_route_get_size(w1, w2);
70
71   taskA = SD_task_create("Task A", NULL, 10.0);
72   taskB = SD_task_create("Task B", NULL, 40.0);
73   taskC = SD_task_create("Task C", NULL, 30.0);
74   taskD = SD_task_create("Task D", NULL, 60.0);
75
76   INFO3("Computation time for %f flops on %s: %f", computation_amount1, name1,
77         SD_workstation_get_computation_time(w1, computation_amount1));
78   INFO3("Computation time for %f flops on %s: %f", computation_amount2, name2,
79         SD_workstation_get_computation_time(w2, computation_amount2));
80
81   INFO2("Route between %s and %s:", name1, name2);
82   for (i = 0; i < route_size; i++) {
83     INFO3("\tLink %s: latency = %f, bandwidth = %f",
84           SD_link_get_name(route[i]), SD_link_get_current_latency(route[i]),
85           SD_link_get_current_bandwidth(route[i]));
86   }
87   INFO2("Route latency = %f, route bandwidth = %f",
88         SD_route_get_current_latency(w1, w2),
89         SD_route_get_current_bandwidth(w1, w2));
90   INFO4("Communication time for %f bytes between %s and %s: %f",
91         communication_amount12, name1, name2,
92         SD_route_get_communication_time(w1, w2, communication_amount12));
93   INFO4("Communication time for %f bytes between %s and %s: %f",
94         communication_amount21, name2, name1,
95         SD_route_get_communication_time(w2, w1, communication_amount21));
96
97   /* creation of the tasks and their dependencies */
98
99   SD_task_dependency_add(NULL, NULL, taskB, taskA);
100   SD_task_dependency_add(NULL, NULL, taskC, taskA);
101   SD_task_dependency_add(NULL, NULL, taskD, taskB);
102   SD_task_dependency_add(NULL, NULL, taskD, taskC);
103   /*  SD_task_dependency_add(NULL, NULL, taskA, taskD); /\* deadlock */
104
105   TRY {
106     SD_task_dependency_add(NULL, NULL, taskA, taskA);   /* shouldn't work and must raise an exception */
107     xbt_assert0(0, "Hey, I can add a dependency between Task A and Task A!");
108   }
109   CATCH(ex) {
110   }
111
112   TRY {
113     SD_task_dependency_add(NULL, NULL, taskA, taskB);   /* shouldn't work and must raise an exception */
114     xbt_assert0(0, "Oh oh, I can add an already existing dependency!");
115   }
116   CATCH(ex) {
117   }
118
119   SD_task_dependency_remove(taskA, taskB);
120
121   TRY {
122     SD_task_dependency_remove(taskC, taskA);    /* shouldn't work and must raise an exception */
123     xbt_assert0(0, "Dude, I can remove an unknown dependency!");
124   }
125   CATCH(ex) {
126   }
127
128   TRY {
129     SD_task_dependency_remove(taskC, taskC);    /* shouldn't work and must raise an exception */
130     xbt_assert0(0,
131                 "Wow, I can remove a dependency between Task C and itself!");
132   }
133   CATCH(ex) {
134   }
135
136
137   /* if everything is ok, no exception is forwarded or rethrown by main() */
138
139   /* watch points */
140   SD_task_watch(taskD, SD_DONE);
141   SD_task_watch(taskB, SD_DONE);
142   SD_task_unwatch(taskD, SD_DONE);
143
144
145   /* scheduling parameters */
146   {
147     const int workstation_number = 2;
148     const SD_workstation_t workstation_list[] = { w1, w2 };
149     double computation_amount[] =
150       { computation_amount1, computation_amount2 };
151     double communication_amount[] = {
152       0, communication_amount12,
153       communication_amount21, 0
154     };
155     xbt_dynar_t changed_tasks;
156     double rate = -1.0;
157
158     /* estimated time */
159     SD_task_t task = taskD;
160     INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
161           SD_task_get_execution_time(task, workstation_number,
162                                      workstation_list, computation_amount,
163                                      communication_amount));
164
165     /* let's launch the simulation! */
166
167     SD_task_schedule(taskA, workstation_number, workstation_list,
168                      computation_amount, communication_amount, rate);
169     SD_task_schedule(taskB, workstation_number, workstation_list,
170                      computation_amount, communication_amount, rate);
171     SD_task_schedule(taskC, workstation_number, workstation_list,
172                      computation_amount, communication_amount, rate);
173     SD_task_schedule(taskD, workstation_number, workstation_list,
174                      computation_amount, communication_amount, rate);
175
176     changed_tasks = SD_simulate(-1.0);
177
178     xbt_dynar_get_cpy(changed_tasks, 0, &checkD);
179     xbt_dynar_get_cpy(changed_tasks, 1, &checkC);
180     xbt_dynar_get_cpy(changed_tasks, 2, &checkB);
181  
182     xbt_assert0(checkD == taskD &&
183                 checkC == taskC &&
184                 checkB == taskB, "Unexpected simulation results");
185
186     xbt_dynar_free_container(&changed_tasks);
187   }
188   DEBUG0("Destroying tasks...");
189
190   SD_task_destroy(taskA);
191   SD_task_destroy(taskB);
192   SD_task_destroy(taskC);
193   SD_task_destroy(taskD);
194
195   DEBUG0("Tasks destroyed. Exiting SimDag...");
196
197   SD_exit();
198   return 0;
199 }