Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove a huge memory leak
[simgrid.git] / examples / simdag / sd_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "simdag/simdag.h"
4 #include "xbt/ex.h"
5 #include "xbt/log.h"
6
7 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test,
8                              "Logging specific to this SimDag example");
9
10 int main(int argc, char **argv) {
11   int i;
12   
13   /* initialisation of SD */
14   SD_init(&argc, argv);
15
16 /*   xbt_log_control_set("sd_test.thres=debug"); */
17 /*   xbt_log_control_set("sd.thres=debug"); */
18   
19   if (argc < 2) {
20      INFO1("Usage: %s platform_file", argv[0]);
21      INFO1("example: %s sd_platform.xml", argv[0]);
22      exit(1);
23   }
24
25   /* creation of the environment */
26   char * platform_file = argv[1];
27   SD_create_environment(platform_file);
28
29   /* test the estimation functions (use small_platform.xml) */
30   SD_workstation_t w1 = SD_workstation_get_by_name("Jupiter");
31   SD_workstation_t w2 = SD_workstation_get_by_name("Ginette");
32   const char *name1 = SD_workstation_get_name(w1);
33   const char *name2 = SD_workstation_get_name(w2);
34   const double computation_amount1 = 2000000;
35   const double computation_amount2 = 1000000;
36   const double communication_amount12 = 2000000;
37   const double communication_amount21 = 3000000;
38   INFO3("Computation time for %f flops on %s: %f", computation_amount1, name1,
39         SD_workstation_get_computation_time(w1, computation_amount1));
40   INFO3("Computation time for %f flops on %s: %f", computation_amount2, name2,
41         SD_workstation_get_computation_time(w2, computation_amount2));
42
43   INFO2("Route between %s and %s:", name1, name2);
44   SD_link_t *route = SD_workstation_route_get_list(w1, w2);
45   int route_size = SD_workstation_route_get_size(w1, w2);
46   for (i = 0; i < route_size; i++) {
47     INFO3("\tLink %s: latency = %f, bandwidth = %f", SD_link_get_name(route[i]),
48           SD_link_get_current_latency(route[i]), SD_link_get_current_bandwidth(route[i]));
49   }
50   INFO2("Route latency = %f, route bandwidth = %f", SD_workstation_route_get_latency(w1, w2),
51         SD_workstation_route_get_bandwidth(w1, w2));
52   INFO4("Communication time for %f bytes between %s and %s: %f", communication_amount12, name1, name2,
53         SD_workstation_route_get_communication_time(w1, w2, communication_amount12));
54   INFO4("Communication time for %f bytes between %s and %s: %f", communication_amount21, name2, name1,
55         SD_workstation_route_get_communication_time(w2, w1, communication_amount21));
56   xbt_free(route);
57
58   /* creation of the tasks and their dependencies */
59   SD_task_t taskA = SD_task_create("Task A", NULL, 10.0);
60   SD_task_t taskB = SD_task_create("Task B", NULL, 40.0);
61   SD_task_t taskC = SD_task_create("Task C", NULL, 30.0);
62   SD_task_t taskD = SD_task_create("Task D", NULL, 60.0);
63   
64   SD_task_dependency_add(NULL, NULL, taskB, taskA);
65   SD_task_dependency_add(NULL, NULL, taskC, taskA);
66   SD_task_dependency_add(NULL, NULL, taskD, taskB);
67   SD_task_dependency_add(NULL, NULL, taskD, taskC);
68   /*  SD_task_dependency_add(NULL, NULL, taskA, taskD); /\* deadlock *\/ */
69
70   /* scheduling parameters */
71
72   const int workstation_number = 2;
73   /*  const SD_workstation_t *workstation_list = SD_workstation_get_list();*/
74   const SD_workstation_t workstation_list[] = {w1, w2};
75   double computation_amount[] = {computation_amount1, computation_amount2};
76   double communication_amount[] =
77     {
78       0, communication_amount12,
79       communication_amount21, 0
80     };
81   double rate = -1.0;
82
83   /* estimated time */
84   SD_task_t task = taskD;
85   INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
86         SD_task_get_execution_time(task, workstation_number, workstation_list,
87                                    computation_amount, communication_amount, rate));
88
89   /* let's launch the simulation! */
90
91   SD_task_schedule(taskA, workstation_number, workstation_list,
92                    computation_amount, communication_amount, rate);
93   SD_task_schedule(taskB, workstation_number, workstation_list,
94                    computation_amount, communication_amount, rate);
95   SD_task_schedule(taskC, workstation_number, workstation_list,
96                    computation_amount, communication_amount, rate);
97   SD_task_schedule(taskD, workstation_number, workstation_list,
98                    computation_amount, communication_amount, rate);
99
100   SD_task_watch(taskC, SD_DONE);
101
102   SD_task_t *changed_tasks;
103
104   changed_tasks = SD_simulate(0.001);
105   
106   while (changed_tasks[0] != NULL) {
107     INFO0("Tasks whose state has changed:");
108     i = 0;
109     while(changed_tasks[i] != NULL) {
110       switch (SD_task_get_state(changed_tasks[i])) {
111       case SD_SCHEDULED:
112         INFO1("%s is scheduled.", SD_task_get_name(changed_tasks[i]));
113         break;
114       case SD_READY:
115         INFO1("%s is ready.", SD_task_get_name(changed_tasks[i]));
116         break;
117       case SD_RUNNING:
118         INFO1("%s is running.", SD_task_get_name(changed_tasks[i]));
119         break;
120       case SD_DONE:
121         INFO1("%s is done.", SD_task_get_name(changed_tasks[i]));
122         break;
123       case SD_FAILED:
124         INFO1("%s is failed.", SD_task_get_name(changed_tasks[i]));
125         break;
126       default:
127         INFO1("Unknown status for %s", SD_task_get_name(changed_tasks[i]));
128         break;
129       }
130       i++;
131     }
132     xbt_free(changed_tasks);
133     changed_tasks = SD_simulate(100);
134   }
135
136   xbt_free(changed_tasks);
137
138   DEBUG0("Destroying tasks...");
139
140   SD_task_destroy(taskA);
141   SD_task_destroy(taskB);
142   SD_task_destroy(taskC);
143   SD_task_destroy(taskD);
144
145   DEBUG0("Tasks destroyed. Exiting SimDag...");
146
147   SD_exit();
148   return 0;
149 }