Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / cpp / exec-ptask-multicore / s4u-exec-ptask-multicore.cpp
1 /* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/s4u.hpp>
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_ptask_multicore, "Messages specific for this s4u example");
9 namespace sg4 = simgrid::s4u;
10
11 static void runner()
12 {
13   auto e = sg4::Engine::get_instance();
14   std::vector<double> comp(2, 1e9);
15   std::vector<double> comm(4, 0.0);
16   // Different hosts.
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);
21
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);
27
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);
33
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);
41
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);
47
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);
53
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);
57
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);
61
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.");
66
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());
72
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);
78
79   background_task->wait();
80   XBT_INFO("The background task has ended.");
81 }
82
83 int main(int argc, char* argv[])
84 {
85   sg4::Engine e(&argc, argv);
86
87   xbt_assert(argc == 2, "Usage: %s <platform file>", argv[0]);
88
89   e.load_platform(argv[1]);
90   sg4::Actor::create("test", e.host_by_name("MyHost1"), runner);
91
92   e.run();
93   XBT_INFO("Simulation done.");
94   return 0;
95 }