Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aef36265ba0695f753bac3e01e0b0e17336bf4c5
[simgrid.git] / examples / msg / cloud-capping / cloud-capping.c
1 /* Copyright (c) 2007-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/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  *
13  * - <b>cloud/bound.c</b>: Demonstrates the use of @ref MSG_task_set_bound
14  */
15
16 static int worker_main(int argc, char *argv[])
17 {
18   double computation_amount = xbt_str_parse_double(argv[1], "Invalid computation amount: %s");
19   int use_bound = xbt_str_parse_int(argv[2], "Second parameter (use_bound) should be 0 or 1 but is: %s");
20   double bound = xbt_str_parse_double(argv[3], "Invalid bound: %s");
21
22   double clock_sta = MSG_get_clock();
23
24   msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
25   if (use_bound)
26      MSG_task_set_bound(task, bound);
27   MSG_task_execute(task);
28   MSG_task_destroy(task);
29
30   double clock_end = MSG_get_clock();
31   double duration = clock_end - clock_sta;
32   double flops_per_sec = computation_amount / duration;
33
34   if (use_bound)
35     XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
36   else
37     XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
38
39   return 0;
40 }
41
42 static void launch_worker(msg_host_t host, const char *pr_name, double computation_amount, int use_bound, double bound)
43 {
44   char **argv = xbt_new(char *, 5);
45   argv[0] = xbt_strdup(pr_name);
46   argv[1] = bprintf("%f", computation_amount);
47   argv[2] = bprintf("%d", use_bound);
48   argv[3] = bprintf("%f", bound);
49   argv[4] = NULL;
50
51   MSG_process_create_with_arguments(pr_name, worker_main, NULL, host, 4, argv);
52 }
53
54 static int worker_busy_loop_main(int argc, char *argv[])
55 {
56   msg_task_t *task = MSG_process_get_data(MSG_process_self());
57   MSG_task_execute(*task);
58
59   return 0;
60 }
61
62 /* FIXME: */
63 #define DOUBLE_MAX 1e11
64
65 static void test_dynamic_change(void)
66 {
67   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
68   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
69
70   msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
71   msg_host_t vm1 = MSG_vm_create_core(pm0, "VM1");
72   MSG_vm_start(vm0);
73   MSG_vm_start(vm1);
74
75   msg_task_t task0 = MSG_task_create("Task0", DOUBLE_MAX, 0, NULL);
76   msg_task_t task1 = MSG_task_create("Task1", DOUBLE_MAX, 0, NULL);
77   MSG_process_create("worker0", worker_busy_loop_main, &task0, vm0);
78   MSG_process_create("worker1", worker_busy_loop_main, &task1, vm1);
79
80   double task0_remain_prev = MSG_task_get_flops_amount(task0);
81   double task1_remain_prev = MSG_task_get_flops_amount(task1);
82
83   {
84     const double cpu_speed = MSG_host_get_speed(pm0);
85     int i = 0;
86     for (i = 0; i < 10; i++) {
87       double new_bound = (cpu_speed / 10) * i;
88       XBT_INFO("set bound of VM1 to %f", new_bound);
89       MSG_vm_set_bound(vm1, new_bound);
90       MSG_process_sleep(100);
91
92       double task0_remain_now = MSG_task_get_flops_amount(task0);
93       double task1_remain_now = MSG_task_get_flops_amount(task1);
94
95       double task0_flops_per_sec = task0_remain_prev - task0_remain_now;
96       double task1_flops_per_sec = task1_remain_prev - task1_remain_now;
97
98       XBT_INFO("Task0@VM0: %f flops/s", task0_flops_per_sec / 100);
99       XBT_INFO("Task1@VM1: %f flops/s", task1_flops_per_sec / 100);
100
101       task0_remain_prev = task0_remain_now;
102       task1_remain_prev = task1_remain_now;
103     }
104   }
105   MSG_process_sleep(2000); // let the tasks end
106
107   MSG_vm_destroy(vm0);
108   MSG_vm_destroy(vm1);
109 }
110
111 static void test_one_task(msg_host_t hostA)
112 {
113   const double cpu_speed = MSG_host_get_speed(hostA);
114   const double computation_amount = cpu_speed * 10;
115   const char *hostA_name = MSG_host_get_name(hostA);
116
117   XBT_INFO("### Test: with/without MSG_task_set_bound");
118
119   XBT_INFO("### Test: no bound for Task1@%s", hostA_name);
120   launch_worker(hostA, "worker0", computation_amount, 0, 0);
121
122   MSG_process_sleep(1000);
123
124   XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
125   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
126
127   MSG_process_sleep(1000);
128
129   XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
130   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
131
132   MSG_process_sleep(1000);
133
134   XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
135   launch_worker(hostA, "worker0", computation_amount, 1, 0);
136
137   MSG_process_sleep(1000);
138
139   XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", hostA_name);
140   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 2);
141
142   MSG_process_sleep(1000);
143 }
144
145 static void test_two_tasks(msg_host_t hostA, msg_host_t hostB)
146 {
147   const double cpu_speed = MSG_host_get_speed(hostA);
148   xbt_assert(cpu_speed == MSG_host_get_speed(hostB));
149   const double computation_amount = cpu_speed * 10;
150   const char *hostA_name = MSG_host_get_name(hostA);
151   const char *hostB_name = MSG_host_get_name(hostB);
152
153   XBT_INFO("### Test: no bound for Task1@%s, no bound for Task2@%s", hostA_name, hostB_name);
154   launch_worker(hostA, "worker0", computation_amount, 0, 0);
155   launch_worker(hostB, "worker1", computation_amount, 0, 0);
156
157   MSG_process_sleep(1000);
158
159   XBT_INFO("### Test: 0 for Task1@%s, 0 for Task2@%s (i.e., unlimited)", hostA_name, hostB_name);
160   launch_worker(hostA, "worker0", computation_amount, 1, 0);
161   launch_worker(hostB, "worker1", computation_amount, 1, 0);
162
163   MSG_process_sleep(1000);
164
165   XBT_INFO("### Test: 50%% for Task1@%s, 50%% for Task2@%s", hostA_name, hostB_name);
166   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
167   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 2);
168
169   MSG_process_sleep(1000);
170
171   XBT_INFO("### Test: 25%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
172   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 4);
173   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
174
175   MSG_process_sleep(1000);
176
177   XBT_INFO("### Test: 75%% for Task1@%s, 100%% for Task2@%s", hostA_name, hostB_name);
178   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
179   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed);
180
181   MSG_process_sleep(1000);
182
183   XBT_INFO("### Test: no bound for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
184   launch_worker(hostA, "worker0", computation_amount, 0, 0);
185   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
186
187   MSG_process_sleep(1000);
188
189   XBT_INFO("### Test: 75%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
190   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
191   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
192
193   MSG_process_sleep(1000);
194 }
195
196 static int master_main(int argc, char *argv[])
197 {
198   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
199   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
200   msg_host_t pm1 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
201
202   XBT_INFO("# 1. Put a single task on a PM. ");
203   test_one_task(pm0);
204   XBT_INFO(" ");
205
206   XBT_INFO("# 2. Put two tasks on a PM.");
207   test_two_tasks(pm0, pm0);
208   XBT_INFO(" ");
209
210   msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
211   MSG_vm_start(vm0);
212
213   XBT_INFO("# 3. Put a single task on a VM. ");
214   test_one_task(vm0);
215   XBT_INFO(" ");
216
217   XBT_INFO("# 4. Put two tasks on a VM.");
218   test_two_tasks(vm0, vm0);
219   XBT_INFO(" ");
220
221   MSG_vm_destroy(vm0);
222
223   vm0 = MSG_vm_create_core(pm0, "VM0");
224   MSG_vm_start(vm0);
225
226   XBT_INFO("# 6. Put a task on a PM and a task on a VM.");
227   test_two_tasks(pm0, vm0);
228   XBT_INFO(" ");
229
230   MSG_vm_destroy(vm0);
231
232   vm0 = MSG_vm_create_core(pm0, "VM0");
233   double cpu_speed = MSG_host_get_speed(pm0);
234   MSG_vm_set_bound(vm0, cpu_speed / 10);
235   MSG_vm_start(vm0);
236
237   XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
238   test_one_task(vm0);
239   XBT_INFO(" ");
240
241   XBT_INFO("# 8. Put two tasks on the VM capped by 10%%.");
242   test_two_tasks(vm0, vm0);
243   XBT_INFO(" ");
244
245   XBT_INFO("# 9. Put a task on a PM and a task on the VM capped by 10%%.");
246   test_two_tasks(pm0, vm0);
247   XBT_INFO(" ");
248
249   MSG_vm_destroy(vm0);
250
251   vm0 = MSG_vm_create_core(pm0, "VM0");
252
253   s_vm_params_t params;
254   memset(&params, 0, sizeof(params));
255   params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
256   MSG_host_set_params(vm0, &params);
257   MSG_vm_start(vm0);
258
259   cpu_speed = MSG_host_get_speed(pm0);
260   MSG_vm_start(vm0);
261
262   XBT_INFO("# 10. Test migration");
263   const double computation_amount = cpu_speed * 10;
264
265   XBT_INFO("# 10. (a) Put a task on a VM without any bound.");
266   launch_worker(vm0, "worker0", computation_amount, 0, 0);
267   MSG_process_sleep(1000);
268   XBT_INFO(" ");
269
270   XBT_INFO("# 10. (b) set 10%% bound to the VM, and then put a task on the VM.");
271   MSG_vm_set_bound(vm0, cpu_speed / 10);
272   launch_worker(vm0, "worker0", computation_amount, 0, 0);
273   MSG_process_sleep(1000);
274   XBT_INFO(" ");
275
276   XBT_INFO("# 10. (c) migrate");
277   MSG_vm_migrate(vm0, pm1);
278   XBT_INFO(" ");
279
280   XBT_INFO("# 10. (d) Put a task again on the VM.");
281   launch_worker(vm0, "worker0", computation_amount, 0, 0);
282   MSG_process_sleep(1000);
283   XBT_INFO(" ");
284
285   MSG_vm_destroy(vm0);
286
287   XBT_INFO("# 11. Change a bound dynamically.");
288   test_dynamic_change();
289
290   return 0;
291 }
292
293 static void launch_master(msg_host_t host)
294 {
295   const char *pr_name = "master_";
296   char **argv = xbt_new(char *, 2);
297   argv[0] = xbt_strdup(pr_name);
298   argv[1] = NULL;
299
300   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
301 }
302
303 int main(int argc, char *argv[])
304 {
305   /* Get the arguments */
306   MSG_init(&argc, argv);
307
308   /* load the platform file */
309   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
310
311   MSG_create_environment(argv[1]);
312
313   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
314   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
315   launch_master(pm0);
316
317   int res = MSG_main();
318   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
319
320   return !(res == MSG_OK);
321 }