Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2f236a65d2513d216a35a63085f3c795ee4e0a56
[simgrid.git] / examples / simdag / sd_test.c
1 /* Copyright (c) 2006-2015. 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 "simgrid/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   unsigned int ctr;
20   const char *platform_file;
21   const sg_host_t *workstations;
22   const char *name1;
23   const char *name2;
24   double computation_amount1;
25   double computation_amount2;
26   double communication_amount12;
27   double communication_amount21;
28   const SD_link_t *route;
29   int route_size;
30   SD_task_t task, taskA, taskB, taskC, taskD, checkB, checkD;
31   xbt_dynar_t changed_tasks;
32   xbt_ex_t ex;
33   const int workstation_number = 2;
34   sg_host_t workstation_list[2];
35   double computation_amount[2];
36   double communication_amount[4] = { 0 };
37   double rate = -1.0;
38   sg_host_t w1, w2;
39
40   /* initialization of SD */
41   SD_init(&argc, argv);
42
43   /*  xbt_log_control_set("sd.thres=debug"); */
44
45   xbt_assert(argc > 1, "Usage: %s platform_file\n"
46              "\nExample: %s two_clusters.xml", argv[0], argv[0]);
47
48   /* creation of the environment */
49   platform_file = argv[1];
50   SD_create_environment(platform_file);
51
52   /* test the estimation functions */
53   workstations = sg_host_list();
54   w1 = workstations[0];
55   w2 = workstations[1];
56   name1 = sg_host_get_name(w1);
57   name2 = sg_host_get_name(w2);
58   computation_amount1 = 2000000;
59   computation_amount2 = 1000000;
60   communication_amount12 = 2000000;
61   communication_amount21 = 3000000;
62   XBT_INFO("Computation time for %f flops on %s: %f", computation_amount1,
63         name1, computation_amount1/sg_host_speed(w1));
64   XBT_INFO("Computation time for %f flops on %s: %f", computation_amount2,
65         name2, computation_amount2/sg_host_speed(w2));
66
67   XBT_INFO("Route between %s and %s:", name1, name2);
68   route = SD_route_get_list(w1, w2);
69   route_size = SD_route_get_size(w1, w2);
70   for (i = 0; i < route_size; i++) {
71     XBT_INFO("   Link %s: latency = %f, bandwidth = %f",
72           sg_link_name(route[i]),
73           sg_link_latency(route[i]),
74           sg_link_bandwidth(route[i]));
75   }
76   XBT_INFO("Route latency = %f, route bandwidth = %f",
77         SD_route_get_latency(w1, w2),
78         SD_route_get_bandwidth(w1, w2));
79   XBT_INFO("Communication time for %f bytes between %s and %s: %f",
80         communication_amount12, name1, name2,
81         SD_route_get_latency(w1, w2) +
82         communication_amount12 / SD_route_get_bandwidth(w1, w2));
83   XBT_INFO("Communication time for %f bytes between %s and %s: %f",
84         communication_amount21, name2, name1,
85         SD_route_get_latency(w2, w1) +
86         communication_amount21 / SD_route_get_bandwidth(w2, w1));
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   /* try to attach and retrieve user data to a task */
95   SD_task_set_data(taskA, (void*) &computation_amount1);
96   if (computation_amount1 != (*((double*) SD_task_get_data(taskA))))
97       XBT_ERROR("User data was corrupted by a simple set/get");
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, taskB, taskC);
104
105
106
107   TRY {
108     SD_task_dependency_add(NULL, NULL, taskA, taskA);   /* shouldn't work and must raise an exception */
109     xbt_die("Hey, I can add a dependency between Task A and Task A!");
110   }
111   CATCH(ex) {
112     if (ex.category != arg_error)
113       RETHROW;                  /* this is a serious error */
114     xbt_ex_free(ex);
115   }
116
117   TRY {
118     SD_task_dependency_add(NULL, NULL, taskB, taskA);   /* shouldn't work and must raise an exception */
119     xbt_die("Oh oh, I can add an already existing dependency!");
120   }
121   CATCH(ex) {
122     if (ex.category != arg_error)
123       RETHROW;
124     xbt_ex_free(ex);
125   }
126
127   TRY {
128     SD_task_dependency_remove(taskA, taskC);    /* shouldn't work and must raise an exception */
129     xbt_die("Dude, I can remove an unknown dependency!");
130   }
131   CATCH(ex) {
132     if (ex.category != arg_error)
133       RETHROW;
134     xbt_ex_free(ex);
135   }
136
137   TRY {
138     SD_task_dependency_remove(taskC, taskC);    /* shouldn't work and must raise an exception */
139     xbt_die("Wow, I can remove a dependency between Task C and itself!");
140   }
141   CATCH(ex) {
142     if (ex.category != arg_error)
143       RETHROW;
144     xbt_ex_free(ex);
145   }
146
147
148   /* if everything is ok, no exception is forwarded or rethrown by main() */
149
150   /* watch points */
151   SD_task_watch(taskD, SD_DONE);
152   SD_task_watch(taskB, SD_DONE);
153   SD_task_unwatch(taskD, SD_DONE);
154
155
156   /* scheduling parameters */
157   workstation_list[0] = w1;
158   workstation_list[1] = w2;
159   computation_amount[0] = computation_amount1;
160   computation_amount[1] = computation_amount2;
161
162   communication_amount[1] = communication_amount12;
163   communication_amount[2] = communication_amount21;
164
165   /* estimated time */
166   task = taskD;
167   XBT_INFO("Estimated time for '%s': %f", SD_task_get_name(task),
168         SD_task_get_execution_time(task, workstation_number,
169                                    workstation_list, computation_amount,
170                                    communication_amount));
171
172   /* let's launch the simulation! */
173
174   SD_task_schedule(taskA, workstation_number, workstation_list,
175                    computation_amount, communication_amount, rate);
176   SD_task_schedule(taskB, workstation_number, workstation_list,
177                    computation_amount, communication_amount, rate);
178   SD_task_schedule(taskC, workstation_number, workstation_list,
179                    computation_amount, communication_amount, rate);
180   SD_task_schedule(taskD, workstation_number, workstation_list,
181                    computation_amount, communication_amount, rate);
182
183   changed_tasks = SD_simulate(-1.0);
184   xbt_dynar_foreach(changed_tasks, ctr, task) {
185     XBT_INFO("Task '%s' start time: %f, finish time: %f",
186           SD_task_get_name(task),
187           SD_task_get_start_time(task), SD_task_get_finish_time(task));
188   }
189
190   xbt_dynar_get_cpy(changed_tasks, 0, &checkD);
191   xbt_dynar_get_cpy(changed_tasks, 1, &checkB);
192
193   xbt_assert(checkD == taskD &&
194               checkB == taskB, "Unexpected simulation results");
195
196   XBT_DEBUG("Destroying tasks...");
197
198   SD_task_destroy(taskA);
199   SD_task_destroy(taskB);
200   SD_task_destroy(taskC);
201   SD_task_destroy(taskD);
202
203   XBT_DEBUG("Tasks destroyed. Exiting SimDag...");
204
205   SD_exit();
206   return 0;
207 }