Logo AND Algorithmique Numérique Distribuée

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