Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4100574b449735b6ddfa17165a5faa6545281695
[simgrid.git] / examples / c / plugin-host-load / plugin-host-load.c
1 /* Copyright (c) 2007-2021. 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/actor.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10 #include "simgrid/plugins/load.h"
11
12 #include "xbt/asserts.h"
13 #include "xbt/log.h"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(hostload_test, "Messages specific for this example");
16
17 static void execute_load_test(int argc, char* argv[])
18 {
19   sg_host_t host = sg_host_by_name("MyHost1");
20
21   XBT_INFO("Initial peak speed: %.0E flop/s; number of flops computed so far: %.0E (should be 0) and current average "
22            "load: %.5f (should be 0)",
23            sg_host_get_speed(host), sg_host_get_computed_flops(host), sg_host_get_avg_load(host));
24
25   double start = simgrid_get_clock();
26   XBT_INFO("Sleep for 10 seconds");
27   sg_actor_sleep_for(10);
28
29   double speed = sg_host_get_speed(host);
30   XBT_INFO("Done sleeping %.2fs; peak speed: %.0E flop/s; number of flops computed so far: %.0E (nothing should have "
31            "changed)",
32            simgrid_get_clock() - start, sg_host_get_speed(host), sg_host_get_computed_flops(host));
33
34   // Run a task
35   start = simgrid_get_clock();
36   XBT_INFO("Run a task of %.0E flops at current speed of %.0E flop/s", 200e6, sg_host_get_speed(host));
37   sg_actor_execute(200e6);
38
39   XBT_INFO("Done working on my task; this took %.2fs; current peak speed: %.0E flop/s (when I started the computation, "
40            "the speed was set to %.0E flop/s); number of flops computed so "
41            "far: %.2E, average load as reported by the HostLoad plugin: %.5f (should be %.5f)",
42            simgrid_get_clock() - start, sg_host_get_speed(host), speed, sg_host_get_computed_flops(host),
43            sg_host_get_avg_load(host),
44            200E6 / (10.5 * speed * sg_host_core_count(host) +
45                     (simgrid_get_clock() - start - 0.5) * sg_host_get_speed(host) * sg_host_core_count(host)));
46
47   // ========= Change power peak =========
48   int pstate = 1;
49   sg_host_set_pstate(host, pstate);
50   XBT_INFO(
51       "========= Requesting pstate %d (speed should be of %.0E flop/s and is of %.0E flop/s, average load is %.5f)",
52       pstate, sg_host_get_pstate_speed(host, pstate), sg_host_get_speed(host), sg_host_get_avg_load(host));
53
54   // Run a second task
55   start = simgrid_get_clock();
56   XBT_INFO("Run a task of %.0E flops", 100e6);
57   sg_actor_execute(100e6);
58   XBT_INFO("Done working on my task; this took %.2fs; current peak speed: %.0E flop/s; number of flops computed so "
59            "far: %.2E",
60            simgrid_get_clock() - start, sg_host_get_speed(host), sg_host_get_computed_flops(host));
61
62   start = simgrid_get_clock();
63   XBT_INFO("========= Requesting a reset of the computation and load counters");
64   sg_host_load_reset(host);
65   XBT_INFO("After reset: %.0E flops computed; load is %.5f", sg_host_get_computed_flops(host),
66            sg_host_get_avg_load(host));
67   XBT_INFO("Sleep for 4 seconds");
68   sg_actor_sleep_for(4);
69   XBT_INFO("Done sleeping %.2f s; peak speed: %.0E flop/s; number of flops computed so far: %.0E",
70            simgrid_get_clock() - start, sg_host_get_speed(host), sg_host_get_computed_flops(host));
71
72   // =========== Turn the other host off ==========
73   XBT_INFO("Turning MyHost2 off, and sleeping another 10 seconds. MyHost2 computed %.0f flops so far and has an "
74            "average load of %.5f.",
75            sg_host_get_computed_flops(sg_host_by_name("MyHost2")), sg_host_get_avg_load(sg_host_by_name("MyHost2")));
76   sg_host_turn_off(sg_host_by_name("MyHost2"));
77   start = simgrid_get_clock();
78   sg_actor_sleep_for(10);
79   XBT_INFO("Done sleeping %.2f s; peak speed: %.0E flop/s; number of flops computed so far: %.0E",
80            simgrid_get_clock() - start, sg_host_get_speed(host), sg_host_get_computed_flops(host));
81 }
82
83 static void change_speed(int argc, char* argv[])
84 {
85   sg_host_t host = sg_host_by_name("MyHost1");
86   sg_actor_sleep_for(10.5);
87   XBT_INFO("I slept until now, but now I'll change the speed of this host "
88            "while the other actor is still computing! This should slow the computation down.");
89   sg_host_set_pstate(host, 2);
90 }
91
92 int main(int argc, char* argv[])
93 {
94   sg_host_load_plugin_init();
95   simgrid_init(&argc, argv);
96
97   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s platform.xml\n", argv[0], argv[0]);
98
99   simgrid_load_platform(argv[1]);
100   sg_actor_create("load_test", sg_host_by_name("MyHost1"), execute_load_test, 0, NULL);
101
102   sg_actor_create("change_speed", sg_host_by_name("MyHost1"), change_speed, 0, NULL);
103
104   simgrid_run();
105
106   XBT_INFO("Total simulation time: %.2f", simgrid_get_clock());
107
108   return 0;
109 }