Logo AND Algorithmique Numérique Distribuée

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