Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some MSG examples mainly useful to test the model-checker
[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;
25   const char *name2;
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;
34   SD_task_t taskC;
35   SD_task_t taskD;
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   w1 = workstations[0];
56   w2 = workstations[1];
57   name1 = SD_workstation_get_name(w1);
58   name2 = SD_workstation_get_name(w2);
59   route = SD_route_get_list(w1, w2);
60   route_size = SD_route_get_size(w1, w2);
61   taskA = SD_task_create("Task A", NULL, 10.0);
62   taskB = SD_task_create("Task B", NULL, 40.0);
63   taskC = SD_task_create("Task C", NULL, 30.0);
64   taskD = SD_task_create("Task D", NULL, 60.0);
65
66   INFO3("Computation time for %f flops on %s: %f", computation_amount1, name1,
67         SD_workstation_get_computation_time(w1, computation_amount1));
68   INFO3("Computation time for %f flops on %s: %f", computation_amount2, name2,
69         SD_workstation_get_computation_time(w2, computation_amount2));
70
71   INFO2("Route between %s and %s:", name1, name2);
72   for (i = 0; i < route_size; i++) {
73     INFO3("\tLink %s: latency = %f, bandwidth = %f",
74           SD_link_get_name(route[i]), SD_link_get_current_latency(route[i]),
75           SD_link_get_current_bandwidth(route[i]));
76   }
77   INFO2("Route latency = %f, route bandwidth = %f",
78         SD_route_get_current_latency(w1, w2),
79         SD_route_get_current_bandwidth(w1, w2));
80   INFO4("Communication time for %f bytes between %s and %s: %f",
81         communication_amount12, name1, name2,
82         SD_route_get_communication_time(w1, w2, communication_amount12));
83   INFO4("Communication time for %f bytes between %s and %s: %f",
84         communication_amount21, name2, name1,
85         SD_route_get_communication_time(w2, w1, communication_amount21));
86
87   /* creation of the tasks and their dependencies */
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   TRY {
96     SD_task_dependency_add(NULL, NULL, taskA, taskA);   /* shouldn't work and must raise an exception */
97     xbt_assert0(0, "Hey, I can add a dependency between Task A and Task A!");
98   }
99   CATCH(ex) {
100   }
101
102   TRY {
103     SD_task_dependency_add(NULL, NULL, taskA, taskB);   /* shouldn't work and must raise an exception */
104     xbt_assert0(0, "Oh oh, I can add an already existing dependency!");
105   }
106   CATCH(ex) {
107   }
108
109   SD_task_dependency_remove(taskA, taskB);
110
111   TRY {
112     SD_task_dependency_remove(taskC, taskA);    /* shouldn't work and must raise an exception */
113     xbt_assert0(0, "Dude, I can remove an unknown dependency!");
114   }
115   CATCH(ex) {
116   }
117
118   TRY {
119     SD_task_dependency_remove(taskC, taskC);    /* shouldn't work and must raise an exception */
120     xbt_assert0(0,
121                 "Wow, I can remove a dependency between Task C and itself!");
122   }
123   CATCH(ex) {
124   }
125
126
127   /* if everything is ok, no exception is forwarded or rethrown by main() */
128
129   /* watch points */
130   SD_task_watch(taskD, SD_DONE);
131   SD_task_watch(taskB, SD_DONE);
132   SD_task_unwatch(taskD, SD_DONE);
133
134
135   /* scheduling parameters */
136   {
137     const int workstation_number = 2;
138     const SD_workstation_t workstation_list[] = { w1, w2 };
139     double computation_amount[] =
140       { computation_amount1, computation_amount2 };
141     double communication_amount[] = {
142       0, communication_amount12,
143       communication_amount21, 0
144     };
145     SD_task_t *changed_tasks;
146     double rate = -1.0;
147
148     /* estimated time */
149     SD_task_t task = taskD;
150     INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
151           SD_task_get_execution_time(task, workstation_number,
152                                      workstation_list, computation_amount,
153                                      communication_amount));
154
155     /* let's launch the simulation! */
156
157     SD_task_schedule(taskA, workstation_number, workstation_list,
158                      computation_amount, communication_amount, rate);
159     SD_task_schedule(taskB, workstation_number, workstation_list,
160                      computation_amount, communication_amount, rate);
161     SD_task_schedule(taskC, workstation_number, workstation_list,
162                      computation_amount, communication_amount, rate);
163     SD_task_schedule(taskD, workstation_number, workstation_list,
164                      computation_amount, communication_amount, rate);
165
166     changed_tasks = SD_simulate(-1.0);
167     xbt_assert0(changed_tasks[0] == taskD &&
168                 changed_tasks[1] == taskC &&
169                 changed_tasks[2] == taskB &&
170                 changed_tasks[3] == NULL, "Unexpected simulation results");
171
172     xbt_free(changed_tasks);
173   }
174   DEBUG0("Destroying tasks...");
175
176   SD_task_destroy(taskA);
177   SD_task_destroy(taskB);
178   SD_task_destroy(taskC);
179   SD_task_destroy(taskD);
180
181   DEBUG0("Tasks destroyed. Exiting SimDag...");
182
183   SD_exit();
184   return 0;
185 }