17#ifndef EBGEOMETRY_GPUALGORITHMS_HPP
18#define EBGEOMETRY_GPUALGORITHMS_HPP
77namespace AlgorithmsDetail {
107template <
class T,
class Source>
135 for (uint32_t i = begin; i < end; i++) {
138 finite = finite && std::isfinite(p[0]) && std::isfinite(p[1]) && std::isfinite(p[2]);
178 for (uint32_t c = 1; c <
m_chunks; c++) {
193template <
class Key,
class Less>
216 const auto t =
static_cast<uint32_t
>(a_t);
217 const uint32_t i = (t /
m_j) * 2 *
m_j + t %
m_j;
218 const uint32_t l = i +
m_j;
223 const bool ascending = (i &
m_k) == 0;
256 const uint32_t begin =
static_cast<uint32_t
>(a_c) *
ScanChunk;
261 for (uint32_t i = begin; i < end; i++) {
287 uint32_t running = 0;
289 for (uint32_t c = 0; c <
m_chunks; c++) {
290 const uint32_t sum =
m_sums[c];
322 const uint32_t begin =
static_cast<uint32_t
>(a_c) *
ScanChunk;
327 for (uint32_t i = begin; i < end; i++) {
353 operator()(
const Key& a_lhs,
const Key& a_rhs)
const noexcept
355 return a_lhs < a_rhs;
373template <
class T,
class Source>
378 static_assert(std::is_trivially_copyable_v<Source>,
"GPU::boundingBoxOf: the source must be trivially copyable");
391#if defined(EBGEOMETRY_CUDA) || defined(EBGEOMETRY_HIP)
394 corners[0] = a_scratch[0];
395 corners[1] = a_scratch[1];
429template <
class Key,
class Less = Ascending>
432bitonicSort(Key* a_keys,
const uint32_t a_count,
const Less& a_less = Less())
434 static_assert(std::is_trivially_copyable_v<Key>,
"GPU::bitonicSort: the key type must be trivially copyable");
437 (a_count & (a_count - 1)) == 0,
"GPU::bitonicSort: the key count must be a power of two (%u)",
unsigned(a_count));
439 for (uint32_t k = 2; k <= a_count && k != 0; k *= 2) {
440 for (uint32_t j = k / 2; j > 0; j /= 2) {
459exclusiveScan(uint32_t* a_values,
const uint32_t a_count, uint32_t* a_scratch)
473#if defined(EBGEOMETRY_CUDA) || defined(EBGEOMETRY_HIP)
476 total = a_scratch[chunks];
Declarations of bounding volume types used in bounding volume hierarchies.
Backend-neutral GPU runtime API layer (CUDA or HIP) plus the EBGEOMETRY_GPU_CHECK macro.
#define EBGEOMETRY_GPU_CHECK(x)
Always-on GPU runtime API error check: prints the failing call and aborts.
Definition EBGeometry_GPURuntime.hpp:527
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_HOST_DEVICE
Mark a function as callable from both host and device code (no-op on host compilers).
Definition EBGeometry_GPU.hpp:80
#define EBGEOMETRY_HOST
Mark a function as callable from host code only (no-op on host compilers).
Definition EBGeometry_GPU.hpp:83
Utility macros for EBGeometry.
#define EBGEOMETRY_REQUIRE(cond,...)
Always-on check of user input and one-time host setup.
Definition EBGeometry_Macros.hpp:84
Scalar helpers and numeric limits that compile for both host and device.
Declaration of 2D and 3D point/vector classes with templated precision, and free functions on them.
Axis-aligned bounding box (AABB) enclosing a set of 3D points.
Definition EBGeometry_BoundingVolumes.hpp:37
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
static constexpr Vec3T< T > max() noexcept
Return the most-positive representable vector.
void parallelFor(std::size_t a_n, const F &a_f)
Run a functor on every index: a_f(i) for i = 0, ..., a_n - 1, in no particular order.
BoundingVolumes::AABBT< T > boundingBox(const Vec3T< T > *a_points, const uint32_t a_count, Vec3T< T > *a_scratch)
The smallest box holding an array of points; see boundingBoxOf().
Definition EBGeometry_GPUAlgorithms.hpp:412
BoundingVolumes::AABBT< T > boundingBoxOf(const Source &a_points, const uint32_t a_count, Vec3T< T > *a_scratch)
The smallest box holding every point, from a source of points: one box per chunk of BoundingBoxChunk ...
Definition EBGeometry_GPUAlgorithms.hpp:376
Error memcpy(void *a_dst, const void *a_src, size_t a_numBytes, MemcpyKind a_kind) noexcept
Copy memory between host and device (cudaMemcpy / hipMemcpy). Synchronous.
uint32_t exclusiveScan(uint32_t *a_values, const uint32_t a_count, uint32_t *a_scratch)
Replace a_count values by their exclusive prefix sums, in place, and return their total: one sum per ...
Definition EBGeometry_GPUAlgorithms.hpp:459
constexpr uint32_t ScanChunk
Values per chunk in exclusiveScan()'s passes.
Definition EBGeometry_GPUAlgorithms.hpp:42
void bitonicSort(Key *a_keys, const uint32_t a_count, const Less &a_less=Less())
Sort a_count keys in place with a bitonic network: log2(n) (log2(n) + 1) / 2 passes of n / 2 compare-...
Definition EBGeometry_GPUAlgorithms.hpp:432
constexpr uint32_t BoundingBoxChunk
Points per chunk in boundingBox()'s first pass.
Definition EBGeometry_GPUAlgorithms.hpp:59
uint32_t boundingBoxScratchSize(const uint32_t a_count) noexcept
Scratch elements boundingBox() needs for a_count points: two corners per chunk.
Definition EBGeometry_GPUAlgorithms.hpp:68
constexpr MemcpyKind MemcpyDeviceToHost
Device-to-host copy direction (cudaMemcpyDeviceToHost / hipMemcpyDeviceToHost).
Definition EBGeometry_GPURuntime.hpp:81
uint32_t exclusiveScanScratchSize(const uint32_t a_count) noexcept
Scratch elements exclusiveScan() needs for a_count values: one per chunk, and one for the total.
Definition EBGeometry_GPUAlgorithms.hpp:51
constexpr T max(const T &a_a, const T &a_b) noexcept
The larger of two values.
Definition EBGeometry_Math.hpp:61
constexpr T min(const T &a_a, const T &a_b) noexcept
The smaller of two values.
Definition EBGeometry_Math.hpp:44
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
Vec2T< T > min(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Component-wise minimum of two vectors.
Vec2T< T > max(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Component-wise maximum of two vectors.
bitonicSort(): one compare-exchange pass, one thread per pair.
Definition EBGeometry_GPUAlgorithms.hpp:195
Key * m_keys
The keys.
Definition EBGeometry_GPUAlgorithms.hpp:197
void operator()(const std::size_t a_t) const noexcept
Pair a_t.
Definition EBGeometry_GPUAlgorithms.hpp:214
uint32_t m_k
Size of the bitonic sequences being merged.
Definition EBGeometry_GPUAlgorithms.hpp:203
Less m_less
The order.
Definition EBGeometry_GPUAlgorithms.hpp:200
uint32_t m_j
Distance between the two keys of a pair.
Definition EBGeometry_GPUAlgorithms.hpp:206
boundingBox(), pass 1: the box of each chunk of points.
Definition EBGeometry_GPUAlgorithms.hpp:109
void operator()(const std::size_t a_c) const noexcept
Box of chunk a_c.
Definition EBGeometry_GPUAlgorithms.hpp:125
uint32_t m_count
Number of points.
Definition EBGeometry_GPUAlgorithms.hpp:117
Vec3T< T > * m_corners
Chunk corners: lower corner of chunk c at 2c, upper at 2c + 1.
Definition EBGeometry_GPUAlgorithms.hpp:114
Source m_points
The points.
Definition EBGeometry_GPUAlgorithms.hpp:111
exclusiveScan(), pass 1: the sum of each chunk of values.
Definition EBGeometry_GPUAlgorithms.hpp:238
uint32_t * m_sums
Chunk sums, one per chunk.
Definition EBGeometry_GPUAlgorithms.hpp:243
void operator()(const std::size_t a_c) const noexcept
Sum of chunk a_c.
Definition EBGeometry_GPUAlgorithms.hpp:254
uint32_t m_count
Number of values.
Definition EBGeometry_GPUAlgorithms.hpp:246
const uint32_t * m_values
The values.
Definition EBGeometry_GPUAlgorithms.hpp:240
boundingBox()'s source for an array of points.
Definition EBGeometry_GPUAlgorithms.hpp:85
const Vec3T< T > * m_points
The points.
Definition EBGeometry_GPUAlgorithms.hpp:87
Vec3T< T > operator()(const uint32_t a_i) const noexcept
Point a_i.
Definition EBGeometry_GPUAlgorithms.hpp:96
boundingBox(), pass 2: one thread merges the chunk boxes into chunk 0's. One thread is enough at the ...
Definition EBGeometry_GPUAlgorithms.hpp:161
void operator()(std::size_t) const noexcept
Merge every chunk's box.
Definition EBGeometry_GPUAlgorithms.hpp:173
Vec3T< T > * m_corners
Chunk corners, as ChunkBox wrote them.
Definition EBGeometry_GPUAlgorithms.hpp:163
uint32_t m_chunks
Number of chunks.
Definition EBGeometry_GPUAlgorithms.hpp:166
exclusiveScan(), pass 3: each chunk's values replaced by their exclusive prefix sums.
Definition EBGeometry_GPUAlgorithms.hpp:304
uint32_t * m_values
The values, in place.
Definition EBGeometry_GPUAlgorithms.hpp:306
const uint32_t * m_offsets
Each chunk's offset.
Definition EBGeometry_GPUAlgorithms.hpp:309
void operator()(const std::size_t a_c) const noexcept
Scan chunk a_c.
Definition EBGeometry_GPUAlgorithms.hpp:320
uint32_t m_count
Number of values.
Definition EBGeometry_GPUAlgorithms.hpp:312
exclusiveScan(), pass 2: one thread turns the chunk sums into each chunk's offset,...
Definition EBGeometry_GPUAlgorithms.hpp:273
uint32_t * m_sums
Chunk sums in, chunk offsets out; one more entry receives the total.
Definition EBGeometry_GPUAlgorithms.hpp:275
uint32_t m_chunks
Number of chunks.
Definition EBGeometry_GPUAlgorithms.hpp:278
void operator()(std::size_t) const noexcept
Scan the chunk sums.
Definition EBGeometry_GPUAlgorithms.hpp:285
The order operator< gives, as a trivially copyable functor callable on a device.
Definition EBGeometry_GPUAlgorithms.hpp:342
bool operator()(const Key &a_lhs, const Key &a_rhs) const noexcept
Whether a_lhs sorts before a_rhs.
Definition EBGeometry_GPUAlgorithms.hpp:353
Numeric limits, with the meanings of the std::numeric_limits members of the same names.
Definition EBGeometry_Math.hpp:91