1 /* Copyright (c) 2017-2023. 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. */
6 #include <simgrid/s4u.hpp>
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_ptask_multicore, "Messages specific for this s4u example");
9 namespace sg4 = simgrid::s4u;
13 const auto* e = sg4::Engine::get_instance();
14 std::vector<double> comp(2, 1e9);
15 std::vector<double> comm(4, 0.0);
17 std::vector<sg4::Host*> hosts_diff = {e->host_by_name("MyHost2"), e->host_by_name("MyHost3")};
18 double start_time = sg4::Engine::get_clock();
19 sg4::this_actor::parallel_execute(hosts_diff, comp, comm);
20 XBT_INFO("Computed 2-core activity on two different hosts. Took %g s", e->get_clock() - start_time);
22 // Same host, monocore.
23 std::vector<sg4::Host*> monocore_hosts = {e->host_by_name("MyHost2"), e->host_by_name("MyHost2")};
24 start_time = sg4::Engine::get_clock();
25 sg4::this_actor::parallel_execute(monocore_hosts, comp, comm);
26 XBT_INFO("Computed 2-core activity a 1-core host. Took %g s", e->get_clock() - start_time);
28 // Same host, multicore.
29 std::vector<sg4::Host*> multicore_host = {e->host_by_name("MyHost1"), e->host_by_name("MyHost1")};
30 start_time = sg4::Engine::get_clock();
31 sg4::this_actor::parallel_execute(multicore_host, comp, comm);
32 XBT_INFO("Computed 2-core activity on a 4-core host. Took %g s", e->get_clock() - start_time);
34 // Same host, using too many cores
35 std::vector<double> comp6(6, 1e9);
36 std::vector<double> comm6(36, 0.0);
37 std::vector<sg4::Host*> multicore_overload(6, e->host_by_name("MyHost1"));
38 start_time = sg4::Engine::get_clock();
39 sg4::this_actor::parallel_execute(multicore_overload, comp6, comm6);
40 XBT_INFO("Computed 6-core activity on a 4-core host. Took %g s", e->get_clock() - start_time);
42 // Same host, adding some communication
43 std::vector<double> comm2 = {0, 1E7, 1E7, 0};
44 start_time = sg4::Engine::get_clock();
45 sg4::this_actor::parallel_execute(multicore_host, comp, comm2);
46 XBT_INFO("Computed 2-core activity on a 4-core host with some communication. Took %g s", e->get_clock() - start_time);
48 // See if the multicore execution continues to work after changing pstate
49 XBT_INFO("Switching machine multicore to pstate 1.");
50 e->host_by_name("MyHost1")->set_pstate(1);
51 XBT_INFO("Switching back to pstate 0.");
52 e->host_by_name("MyHost1")->set_pstate(0);
54 start_time = sg4::Engine::get_clock();
55 sg4::this_actor::parallel_execute(multicore_host, comp, comm);
56 XBT_INFO("Computed 2-core activity on a 4-core host. Took %g s", e->get_clock() - start_time);
58 start_time = sg4::Engine::get_clock();
59 sg4::this_actor::parallel_execute(hosts_diff, comp, comm);
60 XBT_INFO("Computed 2-core activity on two different hosts. Took %g s", e->get_clock() - start_time);
62 // Add a background task and change ptask on the fly
63 auto* MyHost1 = e->host_by_name("MyHost1");
64 sg4::ExecPtr background_task = MyHost1->exec_async(5e9);
65 XBT_INFO("Start a 1-core background task on the 4-core host.");
67 start_time = sg4::Engine::get_clock();
68 sg4::this_actor::parallel_execute(multicore_host, comp, comm);
69 XBT_INFO("Computed 2-core activity on the 4-core host. Took %g s", e->get_clock() - start_time);
70 XBT_INFO("Remaining amount of work for the background task: %.0f%%",
71 100 * background_task->get_remaining_ratio());
73 XBT_INFO("Switching to pstate 1 while background task is still running.");
74 MyHost1->set_pstate(1);
75 start_time = sg4::Engine::get_clock();
76 sg4::this_actor::parallel_execute(multicore_host, comp, comm);
77 XBT_INFO("Computed again the same 2-core activity on it. Took %g s", e->get_clock() - start_time);
79 background_task->wait();
80 XBT_INFO("The background task has ended.");
83 int main(int argc, char* argv[])
85 sg4::Engine e(&argc, argv);
87 xbt_assert(argc == 2, "Usage: %s <platform file>", argv[0]);
89 e.load_platform(argv[1]);
90 sg4::Actor::create("test", e.host_by_name("MyHost1"), runner);
93 XBT_INFO("Simulation done.");