Orrery
A GPU-accelerated N-body gravitational simulator
Loading...
Searching...
No Matches
partition.hpp
Go to the documentation of this file.
1#pragma once
2
20
21#include <cstddef>
22
24#include "orrery/core/types.hpp"
25
26namespace orrery::backend {
27
34inline constexpr core::Index kPartitionGrain = core::kCacheLineBytes / sizeof(core::Real);
35
37struct IndexRange {
38 core::Index begin{};
39 core::Index end{};
40
41 [[nodiscard]] constexpr bool empty() const noexcept { return begin >= end; }
42
43 [[nodiscard]] constexpr core::Index size() const noexcept { return empty() ? 0 : end - begin; }
44};
45
57[[nodiscard]] constexpr IndexRange equal_share(core::Index count, unsigned workers,
58 unsigned worker) noexcept {
59 if (workers == 0 || worker >= workers) {
60 return IndexRange{count, count};
61 }
62
63 const core::Index lines = (count + kPartitionGrain - 1) / kPartitionGrain;
64 const core::Index share = lines / workers;
65 const core::Index remainder = lines % workers;
66 const core::Index index = worker;
67
68 const core::Index first_line = (index * share) + (index < remainder ? index : remainder);
69 const core::Index line_count = share + (index < remainder ? 1 : 0);
70
71 const core::Index begin = first_line * kPartitionGrain;
72 const core::Index end = (first_line + line_count) * kPartitionGrain;
73
74 // The last worker's range runs off the end of the array, because the count
75 // is not generally a whole number of cache lines. Clamping here rather than
76 // in every caller keeps the boundary arithmetic above in one piece.
77 return IndexRange{begin < count ? begin : count, end < count ? end : count};
78}
79
80} // namespace orrery::backend
An allocator that starts every allocation on a cache line.
constexpr std::size_t kCacheLineBytes
The cache line length on the target machine, in bytes.
Definition aligned_allocator.hpp:41
constexpr core::Index kPartitionGrain
How many scalars fit in a cache line, and so the granularity every partition boundary is a multiple o...
Definition partition.hpp:34
constexpr IndexRange equal_share(core::Index count, unsigned workers, unsigned worker) noexcept
The share of [0, count) belonging to worker out of workers.
Definition partition.hpp:57
A half-open range of indices.
Definition partition.hpp:37
The scalar and index types that every layer of the project agrees on.
std::size_t Index
The type of a particle index and of any count of particles.
Definition types.hpp:45