1 /* Copyright (c) 2007-2013. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
8 #include "xbt/sysdep.h" /* calloc, printf */
10 /* Create a log channel to have nice outputs. */
12 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14 "Messages specific for this msg example");
16 /** @addtogroup MSG_examples
18 * - <b>priority/priority.c</b>: Demonstrates the use of @ref
19 * MSG_task_set_bound to change the computation priority of a
24 static int worker_main(int argc, char *argv[])
26 double computation_amount = atof(argv[1]);
27 int use_bound = atoi(argv[2]);
28 double bound = atof(argv[3]);
31 double clock_sta = MSG_get_clock();
33 msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
35 MSG_task_set_bound(task, bound);
36 MSG_task_execute(task);
37 MSG_task_destroy(task);
39 double clock_end = MSG_get_clock();
40 double duration = clock_end - clock_sta;
41 double flops_per_sec = computation_amount / duration;
44 XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
46 XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
52 static void launch_worker(msg_host_t host, const char *pr_name, double computation_amount, int use_bound, double bound)
54 char **argv = xbt_new(char *, 5);
55 argv[0] = xbt_strdup(pr_name);
56 argv[1] = bprintf("%lf", computation_amount);
57 argv[2] = bprintf("%d", use_bound);
58 argv[3] = bprintf("%lf", bound);
61 MSG_process_create_with_arguments(pr_name, worker_main, NULL, host, 4, argv);
66 static int worker_busy_loop_main(int argc, char *argv[])
68 msg_task_t *task = MSG_process_get_data(MSG_process_self());
70 MSG_task_execute(*task);
75 #define DOUBLE_MAX 100000000000L
77 static void test_dynamic_change(void)
79 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
80 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
82 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
83 msg_host_t vm1 = MSG_vm_create_core(pm0, "VM1");
87 msg_task_t task0 = MSG_task_create("Task0", DOUBLE_MAX, 0, NULL);
88 msg_task_t task1 = MSG_task_create("Task1", DOUBLE_MAX, 0, NULL);
89 msg_process_t pr0 = MSG_process_create("worker0", worker_busy_loop_main, &task0, vm0);
90 msg_process_t pr1 = MSG_process_create("worker1", worker_busy_loop_main, &task1, vm1);
93 double task0_remain_prev = MSG_task_get_remaining_computation(task0);
94 double task1_remain_prev = MSG_task_get_remaining_computation(task1);
97 const double cpu_speed = MSG_get_host_speed(pm0);
99 for (i = 0; i < 10; i++) {
100 double new_bound = (cpu_speed / 10) * i;
101 XBT_INFO("set bound of VM1 to %f", new_bound);
102 MSG_vm_set_bound(vm1, new_bound);
103 MSG_process_sleep(100);
105 double task0_remain_now = MSG_task_get_remaining_computation(task0);
106 double task1_remain_now = MSG_task_get_remaining_computation(task1);
108 double task0_flops_per_sec = task0_remain_prev - task0_remain_now;
109 double task1_flops_per_sec = task1_remain_prev - task1_remain_now;
111 XBT_INFO("VM0: %f flops/s", task0_flops_per_sec / 100);
112 XBT_INFO("VM1: %f flops/s", task1_flops_per_sec / 100);
114 task0_remain_prev = task0_remain_now;
115 task1_remain_prev = task1_remain_now;
119 MSG_process_kill(pr0);
120 MSG_process_kill(pr1);
128 static void test_one_task(msg_host_t hostA)
130 const double cpu_speed = MSG_get_host_speed(hostA);
131 const double computation_amount = cpu_speed * 10;
132 const char *hostA_name = MSG_host_get_name(hostA);
134 XBT_INFO("### Test: with/without MSG_task_set_bound");
137 /* Easy-to-understand code (without calling MSG_task_set_bound) */
139 double clock_sta = MSG_get_clock();
141 msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
142 MSG_task_execute(task);
143 MSG_task_destroy(task);
145 double clock_end = MSG_get_clock();
146 double duration = clock_end - clock_sta;
147 double flops_per_sec = computation_amount / duration;
149 XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);
152 /* Easy-to-understand code (with calling MSG_task_set_bound) */
154 double clock_sta = MSG_get_clock();
156 msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
157 MSG_task_set_bound(task, cpu_speed / 2);
158 MSG_task_execute(task);
159 MSG_task_destroy(task);
161 double clock_end = MSG_get_clock();
162 double duration = clock_end - clock_sta;
163 double flops_per_sec = computation_amount / duration;
165 XBT_INFO("bound to 0.5 => duration %f (%f flops/s)", duration, flops_per_sec);
170 XBT_INFO("### Test: no bound for Task1@%s", hostA_name);
171 launch_worker(hostA, "worker0", computation_amount, 0, 0);
174 MSG_process_sleep(1000);
177 XBT_INFO("### Test: 50%% for Task1@%s", hostA_name);
178 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
181 MSG_process_sleep(1000);
184 XBT_INFO("### Test: 33%% for Task1@%s", hostA_name);
185 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 3);
188 MSG_process_sleep(1000);
191 XBT_INFO("### Test: zero for Task1@%s (i.e., unlimited)", hostA_name);
192 launch_worker(hostA, "worker0", computation_amount, 1, 0);
195 MSG_process_sleep(1000);
198 XBT_INFO("### Test: 200%% for Task1@%s (i.e., meaningless)", hostA_name);
199 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 2);
202 MSG_process_sleep(1000);
206 static void test_two_tasks(msg_host_t hostA, msg_host_t hostB)
208 const double cpu_speed = MSG_get_host_speed(hostA);
209 xbt_assert(cpu_speed == MSG_get_host_speed(hostB));
210 const double computation_amount = cpu_speed * 10;
211 const char *hostA_name = MSG_host_get_name(hostA);
212 const char *hostB_name = MSG_host_get_name(hostB);
215 XBT_INFO("### Test: no bound for Task1@%s, no bound for Task2@%s", hostA_name, hostB_name);
216 launch_worker(hostA, "worker0", computation_amount, 0, 0);
217 launch_worker(hostB, "worker1", computation_amount, 0, 0);
220 MSG_process_sleep(1000);
223 XBT_INFO("### Test: 0 for Task1@%s, 0 for Task2@%s (i.e., unlimited)", hostA_name, hostB_name);
224 launch_worker(hostA, "worker0", computation_amount, 1, 0);
225 launch_worker(hostB, "worker1", computation_amount, 1, 0);
228 MSG_process_sleep(1000);
231 XBT_INFO("### Test: 50%% for Task1@%s, 50%% for Task2@%s", hostA_name, hostB_name);
232 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 2);
233 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 2);
236 MSG_process_sleep(1000);
239 XBT_INFO("### Test: 25%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
240 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed / 4);
241 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
244 MSG_process_sleep(1000);
247 XBT_INFO("### Test: 75%% for Task1@%s, 100%% for Task2@%s", hostA_name, hostB_name);
248 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
249 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed);
252 MSG_process_sleep(1000);
255 XBT_INFO("### Test: no bound for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
256 launch_worker(hostA, "worker0", computation_amount, 0, 0);
257 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
260 MSG_process_sleep(1000);
263 XBT_INFO("### Test: 75%% for Task1@%s, 25%% for Task2@%s", hostA_name, hostB_name);
264 launch_worker(hostA, "worker0", computation_amount, 1, cpu_speed * 0.75);
265 launch_worker(hostB, "worker1", computation_amount, 1, cpu_speed / 4);
268 MSG_process_sleep(1000);
271 static int master_main(int argc, char *argv[])
273 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
274 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
278 XBT_INFO("# 1. Put a single task on a PM. ");
283 XBT_INFO("# 2. Put two tasks on a PM.");
284 test_two_tasks(pm0, pm0);
290 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
293 XBT_INFO("# 3. Put a single task on a VM. ");
297 XBT_INFO("# 4. Put two tasks on a VM.");
298 test_two_tasks(vm0, vm0);
307 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
310 XBT_INFO("# 6. Put a task on a PM and a task on a VM.");
311 test_two_tasks(pm0, vm0);
320 msg_host_t vm0 = MSG_vm_create_core(pm0, "VM0");
321 const double cpu_speed = MSG_get_host_speed(pm0);
322 MSG_vm_set_bound(vm0, cpu_speed / 10);
325 XBT_INFO("# 7. Put a single task on the VM capped by 10%%.");
329 XBT_INFO("# 8. Put two tasks on the VM capped by 10%%.");
330 test_two_tasks(vm0, vm0);
333 XBT_INFO("# 9. Put a task on a PM and a task on the VM capped by 10%%.");
334 test_two_tasks(pm0, vm0);
341 XBT_INFO("# 10. Change a bound dynamically");
342 test_dynamic_change();
347 static void launch_master(msg_host_t host)
349 const char *pr_name = "master_";
350 char **argv = xbt_new(char *, 2);
351 argv[0] = xbt_strdup(pr_name);
354 MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
357 int main(int argc, char *argv[])
359 /* Get the arguments */
360 MSG_init(&argc, argv);
362 /* load the platform file */
363 xbt_assert(argc == 2);
364 MSG_create_environment(argv[1]);
366 xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
367 msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
370 int res = MSG_main();
371 XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
374 return !(res == MSG_OK);