Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[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   MSG_task_destroy(*task);
59
60   return 0;
61 }
62
63 #define DOUBLE_MAX 1e11
64
65 static void test_dynamic_change(void)
66 {
67   msg_host_t pm0 = MSG_host_by_name("Fafard");
68
69   msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
70   msg_host_t vm1 = MSG_vm_create_core(pm0, "VM1");
71   MSG_vm_start(vm0);
72   MSG_vm_start(vm1);
73
74   msg_task_t task0 = MSG_task_create("Task0", DOUBLE_MAX, 0, NULL);
75   msg_task_t task1 = MSG_task_create("Task1", DOUBLE_MAX, 0, NULL);
76   MSG_process_create("worker0", worker_busy_loop_main, &task0, vm0);
77   MSG_process_create("worker1", worker_busy_loop_main, &task1, vm1);
78
79   double task0_remain_prev = MSG_task_get_flops_amount(task0);
80   double task1_remain_prev = MSG_task_get_flops_amount(task1);
81
82   const double cpu_speed = MSG_host_get_speed(pm0);
83   for (int i = 0; i < 10; i++) {
84     double new_bound = (cpu_speed / 10) * i;
85     XBT_INFO("set bound of VM1 to %f", new_bound);
86     MSG_vm_set_bound(vm1, new_bound);
87     MSG_process_sleep(100);
88
89     double task0_remain_now = MSG_task_get_flops_amount(task0);
90     double task1_remain_now = MSG_task_get_flops_amount(task1);
91
92     double task0_flops_per_sec = task0_remain_prev - task0_remain_now;
93     double task1_flops_per_sec = task1_remain_prev - task1_remain_now;
94
95     XBT_INFO("Task0@VM0: %f flops/s", task0_flops_per_sec / 100);
96     XBT_INFO("Task1@VM1: %f flops/s", task1_flops_per_sec / 100);
97
98     task0_remain_prev = task0_remain_now;
99     task1_remain_prev = task1_remain_now;
100   }
101
102   MSG_process_sleep(2000); // let the tasks end
103
104   MSG_vm_destroy(vm0);
105   MSG_vm_destroy(vm1);
106 }
107
108 static void test_one_task(msg_host_t hostA)
109 {
110   const double cpu_speed = MSG_host_get_speed(hostA);
111   const double computation_amount = cpu_speed * 10;
112   const char *hostA_name = MSG_host_get_name(hostA);
113
114   XBT_INFO("### Test: with/without MSG_task_set_bound");
115
116   XBT_INFO("### Test: no bound for Task1@%s", hostA_name);
117   launch_worker(hostA, "worker0", computation_amount, 0, 0);
118
119   MSG_process_sleep(1000);
120
121   XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
122   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
123
124   MSG_process_sleep(1000);
125
126   XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
127   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
128
129   MSG_process_sleep(1000);
130
131   XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
132   launch_worker(hostA, "worker0", computation_amount, 1, 0);
133
134   MSG_process_sleep(1000);
135
136   XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", hostA_name);
137   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 2);
138
139   MSG_process_sleep(1000);
140 }
141
142 static void test_two_tasks(msg_host_t hostA, msg_host_t hostB)
143 {
144   const double cpu_speed = MSG_host_get_speed(hostA);
145   xbt_assert(cpu_speed == MSG_host_get_speed(hostB));
146   const double computation_amount = cpu_speed * 10;
147   const char *hostA_name = MSG_host_get_name(hostA);
148   const char *hostB_name = MSG_host_get_name(hostB);
149
150   XBT_INFO("### Test: no bound for Task1@%s, no bound for Task2@%s", hostA_name, hostB_name);
151   launch_worker(hostA, "worker0", computation_amount, 0, 0);
152   launch_worker(hostB, "worker1", computation_amount, 0, 0);
153
154   MSG_process_sleep(1000);
155
156   XBT_INFO("### Test: 0 for Task1@%s, 0 for Task2@%s (i.e., unlimited)", hostA_name, hostB_name);
157   launch_worker(hostA, "worker0", computation_amount, 1, 0);
158   launch_worker(hostB, "worker1", computation_amount, 1, 0);
159
160   MSG_process_sleep(1000);
161
162   XBT_INFO("### Test: 50%% for Task1@%s, 50%% for Task2@%s", hostA_name, hostB_name);
163   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
164   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 2);
165
166   MSG_process_sleep(1000);
167
168   XBT_INFO("### Test: 25%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
169   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 4);
170   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
171
172   MSG_process_sleep(1000);
173
174   XBT_INFO("### Test: 75%% for Task1@%s, 100%% for Task2@%s", hostA_name, hostB_name);
175   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
176   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed);
177
178   MSG_process_sleep(1000);
179
180   XBT_INFO("### Test: no bound for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
181   launch_worker(hostA, "worker0", computation_amount, 0, 0);
182   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
183
184   MSG_process_sleep(1000);
185
186   XBT_INFO("### Test: 75%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
187   launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
188   launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
189
190   MSG_process_sleep(1000);
191 }
192
193 static int master_main(int argc, char *argv[])
194 {
195   msg_host_t pm0 = MSG_host_by_name("Fafard");
196   msg_host_t pm1 = MSG_host_by_name("Fafard");
197
198   XBT_INFO("# 1. Put a single task on a PM. ");
199   test_one_task(pm0);
200   XBT_INFO(" ");
201
202   XBT_INFO("# 2. Put two tasks on a PM.");
203   test_two_tasks(pm0, pm0);
204   XBT_INFO(" ");
205
206   msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
207   MSG_vm_start(vm0);
208
209   XBT_INFO("# 3. Put a single task on a VM. ");
210   test_one_task(vm0);
211   XBT_INFO(" ");
212
213   XBT_INFO("# 4. Put two tasks on a VM.");
214   test_two_tasks(vm0, vm0);
215   XBT_INFO(" ");
216
217   MSG_vm_destroy(vm0);
218
219   vm0 = MSG_vm_create_core(pm0, "VM0");
220   MSG_vm_start(vm0);
221
222   XBT_INFO("# 6. Put a task on a PM and a task on a VM.");
223   test_two_tasks(pm0, vm0);
224   XBT_INFO(" ");
225
226   MSG_vm_destroy(vm0);
227
228   vm0 = MSG_vm_create_core(pm0, "VM0");
229   double cpu_speed = MSG_host_get_speed(pm0);
230   MSG_vm_set_bound(vm0, cpu_speed / 10);
231   MSG_vm_start(vm0);
232
233   XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
234   test_one_task(vm0);
235   XBT_INFO(" ");
236
237   XBT_INFO("# 8. Put two tasks on the VM capped by 10%%.");
238   test_two_tasks(vm0, vm0);
239   XBT_INFO(" ");
240
241   XBT_INFO("# 9. Put a task on a PM and a task on the VM capped by 10%%.");
242   test_two_tasks(pm0, vm0);
243   XBT_INFO(" ");
244
245   MSG_vm_destroy(vm0);
246
247   vm0 = MSG_vm_create_core(pm0, "VM0");
248
249   s_vm_params_t params;
250   memset(&params, 0, sizeof(params));
251   params.ramsize = 1L * 1000 * 1000 * 1000; // 1Gbytes
252   MSG_vm_set_params(vm0, &params);
253   MSG_vm_start(vm0);
254
255   cpu_speed = MSG_host_get_speed(pm0);
256   MSG_vm_start(vm0);
257
258   XBT_INFO("# 10. Test migration");
259   const double computation_amount = cpu_speed * 10;
260
261   XBT_INFO("# 10. (a) Put a task on a VM without any bound.");
262   launch_worker(vm0, "worker0", computation_amount, 0, 0);
263   MSG_process_sleep(1000);
264   XBT_INFO(" ");
265
266   XBT_INFO("# 10. (b) set 10%% bound to the VM, and then put a task on the VM.");
267   MSG_vm_set_bound(vm0, cpu_speed / 10);
268   launch_worker(vm0, "worker0", computation_amount, 0, 0);
269   MSG_process_sleep(1000);
270   XBT_INFO(" ");
271
272   XBT_INFO("# 10. (c) migrate");
273   MSG_vm_migrate(vm0, pm1);
274   XBT_INFO(" ");
275
276   XBT_INFO("# 10. (d) Put a task again on the VM.");
277   launch_worker(vm0, "worker0", computation_amount, 0, 0);
278   MSG_process_sleep(1000);
279   XBT_INFO(" ");
280
281   MSG_vm_destroy(vm0);
282
283   XBT_INFO("# 11. Change a bound dynamically.");
284   test_dynamic_change();
285
286   return 0;
287 }
288
289 static void launch_master(msg_host_t host)
290 {
291   const char *pr_name = "master_";
292   char **argv = xbt_new(char *, 2);
293   argv[0] = xbt_strdup(pr_name);
294   argv[1] = NULL;
295
296   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
297 }
298
299 int main(int argc, char *argv[])
300 {
301   /* Get the arguments */
302   MSG_init(&argc, argv);
303
304   /* load the platform file */
305   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
306
307   MSG_create_environment(argv[1]);
308
309   msg_host_t pm0 = MSG_host_by_name("Fafard");
310   launch_master(pm0);
311
312   int res = MSG_main();
313   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
314
315   return !(res == MSG_OK);
316 }