Logo AND Algorithmique Numérique Distribuée

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