Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
less indirect calls to get_instance in examples (exec to trace)
[simgrid.git] / examples / cpp / exec-ptask / s4u-exec-ptask.cpp
1 /* Copyright (c) 2017-2021. 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 /* Parallel activities are convenient abstractions of parallel computational kernels that span over several machines.
7  * To create a new one, you have to provide several things:
8  *   - a vector of hosts on which the activity will execute
9  *   - a vector of values, the amount of computation for each of the hosts (in flops)
10  *   - a matrix of values, the amount of communication between each pair of hosts (in bytes)
11  *
12  * Each of these operation will be processed at the same relative speed.
13  * This means that at some point in time, all sub-executions and all sub-communications will be at 20% of completion.
14  * Also, they will all complete at the exact same time.
15  *
16  * This is obviously a simplistic abstraction, but this is very handful in a large amount of situations.
17  *
18  * Please note that you must have the LV07 platform model enabled to use such constructs.
19  */
20
21 #include <simgrid/s4u.hpp>
22
23 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_ptask, "Messages specific for this s4u example");
24
25 static void runner()
26 {
27   /* Retrieve the list of all hosts as an array of hosts */
28   auto hosts         = simgrid::s4u::Engine::get_instance()->get_all_hosts();
29   size_t hosts_count = hosts.size();
30
31   std::vector<double> computation_amounts;
32   std::vector<double> communication_amounts;
33
34   /* ------[ test 1 ]----------------- */
35   XBT_INFO("First, build a classical parallel activity, with 1 Gflop to execute on each node, "
36            "and 10MB to exchange between each pair");
37
38   computation_amounts.assign(hosts_count, 1e9 /*1Gflop*/);
39   communication_amounts.assign(hosts_count * hosts_count, 0);
40   for (size_t i = 0; i < hosts_count; i++)
41     for (size_t j = i + 1; j < hosts_count; j++)
42       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
43
44   simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
45
46   /* ------[ test 2 ]----------------- */
47   XBT_INFO("We can do the same with a timeout of 10 seconds enabled.");
48   computation_amounts.assign(hosts_count, 1e9 /*1Gflop*/);
49   communication_amounts.assign(hosts_count * hosts_count, 0);
50   for (size_t i = 0; i < hosts_count; i++)
51     for (size_t j = i + 1; j < hosts_count; j++)
52       communication_amounts[i * hosts_count + j] = 1e7; // 10 MB
53
54   simgrid::s4u::ExecPtr activity =
55       simgrid::s4u::this_actor::exec_init(hosts, computation_amounts, communication_amounts);
56   try {
57     activity->wait_for(10.0 /* timeout (in seconds)*/);
58     xbt_die("Woops, this did not timeout as expected... Please report that bug.");
59   } catch (const simgrid::TimeoutException&) {
60     XBT_INFO("Caught the expected timeout exception.");
61     activity->cancel();
62   }
63
64   /* ------[ test 3 ]----------------- */
65   XBT_INFO("Then, build a parallel activity involving only computations (of different amounts) and no communication");
66   computation_amounts = {3e8, 6e8, 1e9}; // 300Mflop, 600Mflop, 1Gflop
67   communication_amounts.clear();         // no comm
68   simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
69
70   /* ------[ test 4 ]----------------- */
71   XBT_INFO("Then, build a parallel activity with no computation nor communication (synchro only)");
72   computation_amounts.clear();
73   communication_amounts.clear();
74   simgrid::s4u::this_actor::parallel_execute(hosts, computation_amounts, communication_amounts);
75
76   /* ------[ test 5 ]----------------- */
77   XBT_INFO("Then, Monitor the execution of a parallel activity");
78   computation_amounts.assign(hosts_count, 1e6 /*1Mflop*/);
79   communication_amounts = {0, 1e6, 0, 0, 0, 1e6, 1e6, 0, 0};
80   activity              = simgrid::s4u::this_actor::exec_init(hosts, computation_amounts, communication_amounts);
81   activity->start();
82
83   while (not activity->test()) {
84     XBT_INFO("Remaining flop ratio: %.0f%%", 100 * activity->get_remaining_ratio());
85     simgrid::s4u::this_actor::sleep_for(5);
86   }
87   activity->wait();
88
89   /* ------[ test 6 ]----------------- */
90   XBT_INFO("Finally, simulate a malleable task (a parallel execution that gets reconfigured after its start).");
91   XBT_INFO("  - Start a regular parallel execution, with both comm and computation");
92   computation_amounts.assign(hosts_count, 1e6 /*1Mflop*/);
93   communication_amounts = {0, 1e6, 0, 0, 1e6, 0, 1e6, 0, 0};
94   activity              = simgrid::s4u::this_actor::exec_init(hosts, computation_amounts, communication_amounts);
95   activity->start();
96
97   simgrid::s4u::this_actor::sleep_for(10);
98   double remaining_ratio = activity->get_remaining_ratio();
99   XBT_INFO("  - After 10 seconds, %.2f%% remains to be done. Change it from 3 hosts to 2 hosts only.",
100            remaining_ratio * 100);
101   XBT_INFO("    Let's first suspend the task.");
102   activity->suspend();
103
104   XBT_INFO("  - Now, simulate the reconfiguration (modeled as a comm from the removed host to the remaining ones).");
105   std::vector<double> rescheduling_comp{0, 0, 0};
106   std::vector<double> rescheduling_comm{0, 0, 0, 0, 0, 0, 25000, 25000, 0};
107   simgrid::s4u::this_actor::parallel_execute(hosts, rescheduling_comp, rescheduling_comm);
108
109   XBT_INFO("  - Now, let's cancel the old task and create a new task with modified comm and computation vectors:");
110   XBT_INFO("    What was already done is removed, and the load of the removed host is shared between remaining ones.");
111   for (int i = 0; i < 2; i++) {
112     // remove what we've done so far, for both comm and compute load
113     computation_amounts[i]   *= remaining_ratio;
114     communication_amounts[i] *= remaining_ratio;
115     // The work from 1 must be shared between 2 remaining ones. 1/2=50% of extra work for each
116     computation_amounts[i]   *= 1.5;
117     communication_amounts[i] *= 1.5;
118   }
119   hosts.resize(2);
120   computation_amounts.resize(2);
121   double remaining_comm = communication_amounts[1];
122   communication_amounts = {0, remaining_comm, remaining_comm, 0}; // Resizing a linearized matrix is hairly
123
124   activity->cancel();
125   activity = simgrid::s4u::this_actor::exec_init(hosts, computation_amounts, communication_amounts);
126
127   XBT_INFO("  - Done, let's wait for the task completion");
128   activity->wait();
129
130   XBT_INFO("Goodbye now!");
131 }
132
133 int main(int argc, char* argv[])
134 {
135   simgrid::s4u::Engine e(&argc, argv);
136
137   xbt_assert(argc == 2, "Usage: %s <platform file>", argv[0]);
138
139   e.load_platform(argv[1]);
140   simgrid::s4u::Actor::create("test", e.host_by_name("MyHost1"), runner);
141
142   e.run();
143   XBT_INFO("Simulation done.");
144   return 0;
145 }