Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / exec-ptask-multicore / s4u-exec-ptask-multicore.cpp
1 /* Copyright (c) 2017-2022. 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
10 namespace sg4 = simgrid::s4u;
11
12 static void runner()
13 {
14   auto e = sg4::Engine::get_instance();
15   std::vector<double> comp(2, 1e9);
16   std::vector<double> comm(4, 0.0);
17   // Different hosts.
18   std::vector<sg4::Host*> hosts_diff = {e->host_by_name("MyHost2"), e->host_by_name("MyHost3")};
19   double start_time                  = sg4::Engine::get_clock();
20   sg4::this_actor::parallel_execute(hosts_diff, comp, comm);
21   XBT_INFO("Computed 2-core activity on two different hosts. Took %g s", e->get_clock() - start_time);
22
23   // Same host, monocore.
24   std::vector<sg4::Host*> monocore_hosts = {e->host_by_name("MyHost2"), e->host_by_name("MyHost2")};
25   start_time                             = sg4::Engine::get_clock();
26   sg4::this_actor::parallel_execute(monocore_hosts, comp, comm);
27   XBT_INFO("Computed 2-core activity a 1-core host. Took %g s", e->get_clock() - start_time);
28
29   // Same host, multicore.
30   std::vector<sg4::Host*> multicore_host = {e->host_by_name("MyHost1"), e->host_by_name("MyHost1")};
31   start_time                             = sg4::Engine::get_clock();
32   sg4::this_actor::parallel_execute(multicore_host, comp, comm);
33   XBT_INFO("Computed 2-core activity on a 4-core host. Took %g s", e->get_clock() - start_time);
34
35   // Same host, using too many cores
36   std::vector<double> comp6(6, 1e9);
37   std::vector<double> comm6(36, 0.0);
38   std::vector<sg4::Host*> multicore_overload(6, e->host_by_name("MyHost1"));
39   start_time = sg4::Engine::get_clock();
40   sg4::this_actor::parallel_execute(multicore_overload, comp6, comm6);
41   XBT_INFO("Computed 6-core activity on a 4-core host. Took %g s", e->get_clock() - start_time);
42
43   // Same host, adding some communication
44   std::vector<double> comm2 = {0, 1E7, 1E7, 0};
45   start_time                = sg4::Engine::get_clock();
46   sg4::this_actor::parallel_execute(multicore_host, comp, comm2);
47   XBT_INFO("Computed 2-core activity on a 4-core host with some communication. Took %g s", e->get_clock() - start_time);
48
49   // See if the multicore execution continues to work after changing pstate
50   XBT_INFO("Switching machine multicore to pstate 1.");
51   e->host_by_name("MyHost1")->set_pstate(1);
52   XBT_INFO("Switching back to pstate 0.");
53   e->host_by_name("MyHost1")->set_pstate(0);
54
55   start_time = sg4::Engine::get_clock();
56   sg4::this_actor::parallel_execute(multicore_host, comp, comm);
57   XBT_INFO("Computed 2-core activity on a 4-core host. Took %g s", e->get_clock() - start_time);
58
59   start_time = sg4::Engine::get_clock();
60   sg4::this_actor::parallel_execute(hosts_diff, comp, comm);
61   XBT_INFO("Computed 2-core activity on two different hosts. Took %g s", e->get_clock() - start_time);
62
63   // Add a background task and change ptask on the fly
64   auto MyHost1                          = e->host_by_name("MyHost1");
65   simgrid::s4u::ExecPtr background_task = MyHost1->exec_async(5e9);
66   XBT_INFO("Start a 1-core background task on the 4-core host.");
67
68   start_time = sg4::Engine::get_clock();
69   sg4::this_actor::parallel_execute(multicore_host, comp, comm);
70   XBT_INFO("Computed 2-core activity on the 4-core host. Took %g s", e->get_clock() - start_time);
71   XBT_INFO("Remaining amount of work for the background task: %.0f%%",
72            100 * background_task->get_remaining_ratio());
73
74   XBT_INFO("Switching to pstate 1 while background task is still running.");
75   MyHost1->set_pstate(1);
76   start_time = sg4::Engine::get_clock();
77   sg4::this_actor::parallel_execute(multicore_host, comp, comm);
78   XBT_INFO("Computed again the same 2-core activity on it. Took %g s", e->get_clock() - start_time);
79
80   background_task->wait();
81   XBT_INFO("The background task has ended.");
82 }
83
84 int main(int argc, char* argv[])
85 {
86   sg4::Engine e(&argc, argv);
87
88   xbt_assert(argc == 2, "Usage: %s <platform file>", argv[0]);
89
90   e.load_platform(argv[1]);
91   sg4::Actor::create("test", e.host_by_name("MyHost1"), runner);
92
93   e.run();
94   XBT_INFO("Simulation done.");
95   return 0;
96 }