Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in the coordinate-based platform files
[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 "simgrid/simdag.h"
8 #include "xbt/ex.h"
9 #include "xbt/log.h"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test, "Logging specific to this SimDag example");
12
13 int main(int argc, char **argv)
14 {
15   int i;
16   unsigned int ctr;
17   const sg_host_t *hosts;
18   const char *name1, *name2;
19   double comp_amount1, comp_amount2;
20   double comm_amount12, comm_amount21;
21   const SD_link_t *route;
22   int route_size;
23   SD_task_t task, taskA, taskB, taskC, taskD, checkB, checkD;
24   xbt_dynar_t changed_tasks;
25   xbt_ex_t ex;
26   const int host_count = 2;
27   sg_host_t host_list[2];
28   double computation_amount[2];
29   double communication_amount[4] = { 0 };
30   double rate = -1.0;
31   sg_host_t h1, h2;
32
33   /* initialization of SD */
34   SD_init(&argc, argv);
35
36   xbt_assert(argc > 1, "Usage: %s platform_file\n\nExample: %s two_clusters.xml", argv[0], argv[0]);
37   SD_create_environment(argv[1]);
38
39   /* test the estimation functions */
40   hosts = sg_host_list();
41   h1 = hosts[0];
42   h2 = hosts[1];
43   name1 = sg_host_get_name(h1);
44   name2 = sg_host_get_name(h2);
45   comp_amount1 = 2000000;
46   comp_amount2 = 1000000;
47   comm_amount12 = 2000000;
48   comm_amount21 = 3000000;
49   XBT_INFO("Computation time for %f flops on %s: %f", comp_amount1, name1, comp_amount1/sg_host_speed(h1));
50   XBT_INFO("Computation time for %f flops on %s: %f", comp_amount2, name2, comp_amount2/sg_host_speed(h2));
51
52   XBT_INFO("Route between %s and %s:", name1, name2);
53   route = SD_route_get_list(h1, h2);
54   route_size = SD_route_get_size(h1, h2);
55   for (i = 0; i < route_size; i++) {
56     XBT_INFO("   Link %s: latency = %f, bandwidth = %f", sg_link_name(route[i]), sg_link_latency(route[i]),
57              sg_link_bandwidth(route[i]));
58   }
59   XBT_INFO("Route latency = %f, route bandwidth = %f", SD_route_get_latency(h1, h2), SD_route_get_bandwidth(h1, h2));
60   XBT_INFO("Communication time for %f bytes between %s and %s: %f", comm_amount12, name1, name2,
61         SD_route_get_latency(h1, h2) + comm_amount12 / SD_route_get_bandwidth(h1, h2));
62   XBT_INFO("Communication time for %f bytes between %s and %s: %f", comm_amount21, name2, name1,
63         SD_route_get_latency(h2, h1) + comm_amount21 / SD_route_get_bandwidth(h2, h1));
64
65   /* creation of the tasks and their dependencies */
66   taskA = SD_task_create("Task A", NULL, 10.0);
67   taskB = SD_task_create("Task B", NULL, 40.0);
68   taskC = SD_task_create("Task C", NULL, 30.0);
69   taskD = SD_task_create("Task D", NULL, 60.0);
70
71   /* try to attach and retrieve user data to a task */
72   SD_task_set_data(taskA, (void*) &comp_amount1);
73   if (comp_amount1 != (*((double*) SD_task_get_data(taskA))))
74       XBT_ERROR("User data was corrupted by a simple set/get");
75
76   SD_task_dependency_add(NULL, NULL, taskB, taskA);
77   SD_task_dependency_add(NULL, NULL, taskC, taskA);
78   SD_task_dependency_add(NULL, NULL, taskD, taskB);
79   SD_task_dependency_add(NULL, NULL, taskD, taskC);
80   SD_task_dependency_add(NULL, NULL, taskB, taskC);
81
82   TRY {
83     SD_task_dependency_add(NULL, NULL, taskA, taskA);   /* shouldn't work and must raise an exception */
84     xbt_die("Hey, I can add a dependency between Task A and Task A!");
85   }
86   CATCH(ex) {
87     if (ex.category != arg_error)
88       RETHROW;                  /* this is a serious error */
89     xbt_ex_free(ex);
90   }
91
92   TRY {
93     SD_task_dependency_add(NULL, NULL, taskB, taskA);   /* shouldn't work and must raise an exception */
94     xbt_die("Oh oh, I can add an already existing dependency!");
95   }
96   CATCH(ex) {
97     if (ex.category != arg_error)
98       RETHROW;
99     xbt_ex_free(ex);
100   }
101
102   TRY {
103     SD_task_dependency_remove(taskA, taskC);    /* shouldn't work and must raise an exception */
104     xbt_die("Dude, I can remove an unknown dependency!");
105   }
106   CATCH(ex) {
107     if (ex.category != arg_error)
108       RETHROW;
109     xbt_ex_free(ex);
110   }
111
112   TRY {
113     SD_task_dependency_remove(taskC, taskC);    /* shouldn't work and must raise an exception */
114     xbt_die("Wow, I can remove a dependency between Task C and itself!");
115   }
116   CATCH(ex) {
117     if (ex.category != arg_error)
118       RETHROW;
119     xbt_ex_free(ex);
120   }
121
122   /* if everything is ok, no exception is forwarded or rethrown by main() */
123
124   /* watch points */
125   SD_task_watch(taskD, SD_DONE);
126   SD_task_watch(taskB, SD_DONE);
127   SD_task_unwatch(taskD, SD_DONE);
128
129   /* scheduling parameters */
130   host_list[0] = h1;
131   host_list[1] = h2;
132   computation_amount[0] = comp_amount1;
133   computation_amount[1] = comp_amount2;
134
135   communication_amount[1] = comm_amount12;
136   communication_amount[2] = comm_amount21;
137
138   /* estimated time */
139   task = taskD;
140   XBT_INFO("Estimated time for '%s': %f", SD_task_get_name(task), SD_task_get_execution_time(task, host_count,
141            host_list, computation_amount, communication_amount));
142
143   SD_task_schedule(taskA, host_count, host_list, computation_amount, communication_amount, rate);
144   SD_task_schedule(taskB, host_count, host_list, computation_amount, communication_amount, rate);
145   SD_task_schedule(taskC, host_count, host_list, computation_amount, communication_amount, rate);
146   SD_task_schedule(taskD, host_count, host_list, computation_amount, communication_amount, rate);
147
148   changed_tasks = SD_simulate(-1.0);
149   xbt_dynar_foreach(changed_tasks, ctr, task) {
150     XBT_INFO("Task '%s' start time: %f, finish time: %f", SD_task_get_name(task),
151           SD_task_get_start_time(task), SD_task_get_finish_time(task));
152   }
153
154   xbt_dynar_get_cpy(changed_tasks, 0, &checkD);
155   xbt_dynar_get_cpy(changed_tasks, 1, &checkB);
156
157   xbt_assert(checkD == taskD && checkB == taskB, "Unexpected simulation results");
158
159   XBT_DEBUG("Destroying tasks...");
160   SD_task_destroy(taskA);
161   SD_task_destroy(taskB);
162   SD_task_destroy(taskC);
163   SD_task_destroy(taskD);
164
165   XBT_DEBUG("Tasks destroyed. Exiting SimDag...");
166   SD_exit();
167   return 0;
168 }