/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/physics/integration/map_scheduler.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/physics/integration/map_scheduler.hh Source File
DiFfRG
Discretization Framework for functional Renormalization Group flows
map_scheduler.hh
Go to the documentation of this file.
1#pragma once
2
3// DiFfRG
6
7// std
8#include <array>
9#include <cstddef>
10#include <cstdint>
11#include <type_traits>
12#include <vector>
13
14namespace DiFfRG
15{
16 namespace internal
17 {
35
38 inline constexpr double host_grain = 1024.;
39
50 inline constexpr double launch_threshold = 1024.;
51 } // namespace internal
52
62 enum class MapResource : int { device = 0, host = 1 };
63 inline constexpr int n_map_resources = 2;
64
65 inline const char *to_string(const MapResource r) { return r == MapResource::host ? "host" : "device"; }
66
72
80 template <typename ExecutionSpace> inline MapTarget map_target()
81 {
82 if constexpr (std::is_same_v<typename ExecutionSpace::memory_space, CPU_memory>)
84 else
86 }
87
89 template <typename ExecutionSpace> inline double map_fill_threshold()
90 {
92 }
93
97 struct MapSlice {
99 size_t offset = 0;
101 size_t count = 0;
102
103 bool owns_all(const size_t grid_size) const { return offset == 0 && count == grid_size; }
104 };
105
195 {
196 public:
199 NoMapsHere(const NoMapsHere &) = delete;
200 NoMapsHere &operator=(const NoMapsHere &) = delete;
201
203 static bool active();
204 };
205
207 {
208 public:
210
213 bool active() const { return m_n_ranks > 1; }
214
215 uint rank() const { return m_rank; }
216 uint n_ranks() const { return m_n_ranks; }
217 MPI_Comm comm() const { return m_comm; }
218
240 MapSlice schedule(size_t integrator_id, void *dest, size_t elem_size, size_t grid_size, size_t quadrature_volume,
241 bool splittable, MapTarget target);
242
245 bool plan_contains(size_t integrator_id) const;
246
247 bool has_open_plan() const { return !m_plan.empty(); }
248
255 void complete();
256
259 void poison(const char *reason);
260
272 void set_batched(const bool batched) { m_batched = batched; }
273
276 void set_quantum(double quantum);
278 double quantum() const { return m_quantum_override; }
279 void set_verbose(bool verbose) { m_verbose = verbose; }
280
281 protected:
283
292
298
299 // Grow-only scratch, so a flush does not allocate. Protected rather than private so the
300 // multi-rank simulation in the tests can play the part of the network.
301 std::vector<char> m_send;
302 std::vector<char> m_recv;
303 std::vector<int> m_counts;
304 std::vector<int> m_displs;
305
306 private:
307 struct Entry {
309 void *dest;
310 size_t elem_size;
311 size_t grid_size;
314 std::vector<uint> owners;
315 };
316
318 static size_t part(size_t grid_size, size_t r, size_t j) { return (j * grid_size) / r; }
319
320 std::vector<uint> least_loaded(size_t r, MapResource resource) const;
323 uint64_t plan_hash() const;
324 void log_plan() const;
325
326 MPI_Comm m_comm;
329
332 bool m_quantum_pinned = false;
334 bool m_batched = false;
337 size_t m_batch_index = 0;
340 size_t m_verify_every = 64;
342 static constexpr size_t verify_head = 8;
343 bool m_verbose = false;
344 bool m_logged = false;
345 const char *m_poisoned = nullptr;
346
347 std::vector<Entry> m_plan;
350 std::array<std::vector<double>, n_map_resources> m_load;
352 bool m_simulated = false;
353 };
354} // namespace DiFfRG
Definition map_scheduler.hh:207
bool m_batched
Whether a DeferredMaps scope is open; see set_batched().
Definition map_scheduler.hh:334
double quantum() const
The explicit override, or 0 when the automatic defaults are in force.
Definition map_scheduler.hh:278
bool m_quantum_pinned
Definition map_scheduler.hh:332
uint m_n_ranks
Definition map_scheduler.hh:328
std::vector< int > m_displs
Definition map_scheduler.hh:304
static size_t part(size_t grid_size, size_t r, size_t j)
Canonical partition: slice j of r covers [part(G, r, j), part(G, r, j + 1)).
Definition map_scheduler.hh:318
const char * m_poisoned
Definition map_scheduler.hh:345
void set_quantum(double quantum)
uint rank() const
Definition map_scheduler.hh:215
static constexpr size_t verify_head
Batches at the start of a run that are always verified, whatever m_verify_every says.
Definition map_scheduler.hh:342
std::vector< int > m_counts
Definition map_scheduler.hh:303
std::vector< uint > least_loaded(size_t r, MapResource resource) const
MapSlice schedule(size_t integrator_id, void *dest, size_t elem_size, size_t grid_size, size_t quadrature_volume, bool splittable, MapTarget target)
Register one map() call and return this rank's slice of it.
std::vector< char > m_send
Definition map_scheduler.hh:301
MapScheduler(uint rank, uint n_ranks)
Bind to an explicit rank and rank count instead of MPI_COMM_WORLD.
void finish_batch()
Land every other rank's slices out of the receive buffer, then close the batch.
bool has_open_plan() const
Definition map_scheduler.hh:247
void reset_load()
Zero every resource budget, sized to the rank count.
void set_batched(const bool batched)
Tell the scheduler whether the caller has a deferral scope open.
Definition map_scheduler.hh:272
size_t m_verify_every
Definition map_scheduler.hh:340
MPI_Comm m_comm
Definition map_scheduler.hh:326
std::vector< char > m_recv
Definition map_scheduler.hh:302
void log_plan() const
uint n_ranks() const
Definition map_scheduler.hh:216
MPI_Comm comm() const
Definition map_scheduler.hh:217
bool m_simulated
Simulated instances (see the rank/n_ranks constructor) never communicate.
Definition map_scheduler.hh:352
void set_verbose(bool verbose)
Definition map_scheduler.hh:279
bool active() const
Definition map_scheduler.hh:213
double m_quantum_override
0 == no override, use the hardware-derived per-space thresholds.
Definition map_scheduler.hh:331
bool plan_contains(size_t integrator_id) const
size_t m_batch_index
Definition map_scheduler.hh:337
static MapScheduler & instance()
bool m_verbose
Definition map_scheduler.hh:343
uint64_t plan_hash() const
uint m_rank
Definition map_scheduler.hh:327
void poison(const char *reason)
std::vector< Entry > m_plan
Definition map_scheduler.hh:347
std::array< std::vector< double >, n_map_resources > m_load
Definition map_scheduler.hh:350
bool m_logged
Definition map_scheduler.hh:344
void complete()
Exchange every slice registered since the last completion.
Decides, without any user input, which rank computes which part of each map().
Definition map_scheduler.hh:195
NoMapsHere(const NoMapsHere &)=delete
static bool active()
Whether any NoMapsHere scope is currently open.
NoMapsHere & operator=(const NoMapsHere &)=delete
constexpr double launch_threshold
Evaluations below which a slice cannot pay for the launch that computes it.
Definition map_scheduler.hh:50
constexpr double host_grain
Definition map_scheduler.hh:38
double host_fill_threshold()
double device_fill_threshold()
Kernel evaluations needed to saturate one rank's compute resource.
Definition complex_math.hh:10
const char * to_string(const ThreadSource source)
The name of a thread-count source, as it appears in the precedence warning.
double map_fill_threshold()
The fill threshold alone, for callers that do not need the resource class.
Definition map_scheduler.hh:89
MapResource
The physical resource a map() competes for.
Definition map_scheduler.hh:62
constexpr int n_map_resources
Definition map_scheduler.hh:63
MapTarget map_target()
The scheduling target of an execution space, selected at compile time.
Definition map_scheduler.hh:80
unsigned int uint
Definition utils.hh:24
Definition map_scheduler.hh:307
std::vector< uint > owners
The r owning ranks, ascending. Slice j of the grid belongs to owners[j].
Definition map_scheduler.hh:314
size_t elem_size
Definition map_scheduler.hh:310
MapResource resource
Definition map_scheduler.hh:312
void * dest
Definition map_scheduler.hh:309
size_t integrator_id
Definition map_scheduler.hh:308
size_t grid_size
Definition map_scheduler.hh:311
This rank's window into the external grid of one QuadratureIntegrator::map() call.
Definition map_scheduler.hh:97
bool owns_all(const size_t grid_size) const
Definition map_scheduler.hh:103
size_t count
Number of grid points; 0 means this rank does not participate in this map().
Definition map_scheduler.hh:101
size_t offset
First external grid point this rank computes.
Definition map_scheduler.hh:99
Which resource a map() runs on and how many evaluations saturate one rank's share of it.
Definition map_scheduler.hh:68
double fill_threshold
Definition map_scheduler.hh:70
MapResource resource
Definition map_scheduler.hh:69