EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
Namespaces | Classes | Typedefs | Functions | Variables
EBGeometry::GPU Namespace Reference

Backend-neutral GPU runtime API surface (CUDA or HIP). More...

Namespaces

namespace  AlgorithmsDetail
 The functors behind boundingBox() and bitonicSort().
 

Classes

struct  Ascending
 The order operator< gives, as a trivially copyable functor callable on a device. More...
 

Typedefs

using Error = int
 Backend runtime error code (cudaError_t under CUDA, hipError_t under HIP).
 
using MemcpyKind = int
 Memory-copy direction (cudaMemcpyKind under CUDA, hipMemcpyKind under HIP).
 
using Stream = void *
 A stream of device work (cudaStream_t / hipStream_t).
 
template<class Query , class In >
using TransformResult = std::decay_t< std::invoke_result_t< const Query &, const In & > >
 The result type of a query on an input.
 

Functions

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.
 
uint32_t boundingBoxScratchSize (const uint32_t a_count) noexcept
 Scratch elements boundingBox() needs for a_count points: two corners per chunk.
 
template<class T , class Source >
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 points, then one thread merging them.
 
template<class T >
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().
 
template<class Key , class Less = Ascending>
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-exchanges each.
 
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 chunk of ScanChunk values, one thread scanning the sums, then each chunk scanned from its offset.
 
template<class Source >
void mortonOrder (MemoryResource &a_where, const Source &a_source, PODSpan< uint32_t > a_order)
 Write into a_order the order to run queries at a source's points in: the permutation of 0, ..., n - 1 (n = a_order.size()) that sorts the points along a Morton curve over their bounding box, ties broken by index. Runs on the device, with a bitonic sort, when a_where's memory is device-accessible in a CUDA or HIP build; on the host, with a sort, otherwise. Both give the same order, element for element.
 
template<class Source >
void mortonOrder (const Pool &a_pool, const Source &a_source, PODVector< uint32_t > &a_order)
 Write into a_order, an array in a_pool, the order to run queries at a source's points in; the span overload on a_pool's resource, for an order reserved in a pool.
 
Error memAlloc (void **a_devicePtr, size_t a_numBytes) noexcept
 Allocate device memory (cudaMalloc / hipMalloc).
 
Error memFree (void *a_devicePtr) noexcept
 Free device memory allocated with memAlloc (cudaFree / hipFree).
 
Error memAllocManaged (void **a_ptr, size_t a_numBytes) noexcept
 Allocate managed (unified) memory accessible from host and device (cudaMallocManaged / hipMallocManaged).
 
Error memAllocHost (void **a_ptr, size_t a_numBytes) noexcept
 Allocate page-locked (pinned) host memory (cudaHostAlloc / hipHostMalloc).
 
Error memAllocHostMapped (void **a_ptr, size_t a_numBytes) noexcept
 Allocate page-locked (pinned) host memory that is additionally mapped into the device address space for zero-copy access (cudaHostAlloc with cudaHostAllocMapped / hipHostMalloc with hipHostMallocMapped). Under unified virtual addressing the returned host pointer is directly valid on the device. Free with memFreeHost.
 
Error memFreeHost (void *a_ptr) noexcept
 Free page-locked host memory allocated with memAllocHost (cudaFreeHost / hipHostFree).
 
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.
 
Error getDeviceCount (int *a_count) noexcept
 Get the number of available GPU devices (cudaGetDeviceCount / hipGetDeviceCount).
 
Error streamCreateNonBlocking (Stream *a_stream) noexcept
 Create a stream that runs alongside the default stream rather than waiting for its work (cudaStreamCreateWithFlags / hipStreamCreateWithFlags with the non-blocking flag), for a copy made while a kernel on the default stream runs. Destroy it with streamDestroy.
 
Error streamDestroy (Stream a_stream) noexcept
 Destroy a stream (cudaStreamDestroy / hipStreamDestroy).
 
Error streamSynchronize (Stream a_stream) noexcept
 Wait until a stream's work is done (cudaStreamSynchronize / hipStreamSynchronize).
 
Error memcpyAsync (void *a_dst, const void *a_src, size_t a_numBytes, MemcpyKind a_kind, Stream a_stream) noexcept
 Copy memory between host and device on a stream, returning before the copy is done (cudaMemcpyAsync / hipMemcpyAsync); from pinned host memory, it does not wait for the host either.
 
Error getClockRate (int *a_kiloHertz) noexcept
 Get the current device's clock rate, the rate at which clock64() counts in a kernel (cudaDeviceGetAttribute with cudaDevAttrClockRate / hipDeviceGetAttribute with hipDeviceAttributeClockRate).
 
Error getLastError () noexcept
 Get and clear the last runtime error (cudaGetLastError / hipGetLastError).
 
Error deviceSynchronize () noexcept
 Block until the device has finished all preceding work (cudaDeviceSynchronize / hipDeviceSynchronize).
 
const char * getErrorString (Error a_error) noexcept
 Get the human-readable description of an error code (cudaGetErrorString / hipGetErrorString).
 
bool isShutdownError (Error a_error) noexcept
 True for the errors a runtime call returns once the runtime has shut down at program exit (cudaErrorCudartUnloading or cudaErrorContextIsDestroyed under CUDA, hipErrorDeinitialized or hipErrorContextIsDestroyed under HIP).
 
const char * getErrorHint (Error a_error) noexcept
 What to do about an error that means the binary has no code this GPU can run: the binary was built for another architecture, or carries only PTX that the driver, older than the toolkit, cannot compile (cudaErrorUnsupportedPtxVersion or cudaErrorNoKernelImageForDevice under CUDA, hipErrorNoBinaryForGpu under HIP).
 
bool deviceAvailable () noexcept
 Whether a GPU is visible to the runtime.
 
MemoryResource & memoryResource () noexcept
 The memory a query's data must live in for parallelFor() and transform() to read it.
 
template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class F >
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.
 
template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void transform (const Query &a_query, PODSpan< In > a_in, PODSpan< Out > a_out)
 Run a query on every input in place: a_out[i] = a_query(a_in[i]), with no allocation and no copies.
 
template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void transform (const Query &a_query, const Pool &a_pool, const PODVector< In > &a_in, const PODVector< Out > &a_out)
 Run a query on every input of a pool-resident array, writing the results into another array in the same pool: out[i] = a_query(in[i]), with no allocation and no copies.
 
template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void transform (const Query &a_query, const std::vector< In > &a_in, std::vector< Out > &a_out)
 Run a query on every input, from host memory to host memory: a_out[i] = a_query(a_in[i]).
 
template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class F >
void parallelFor (uint32_t a_n, const F &a_f, PODSpan< const uint32_t > a_order)
 Run a functor on every index, in a given order: a_f(a_order[i]) for i = 0, ..., a_n - 1.
 
template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void transform (const Query &a_query, PODSpan< In > a_in, PODSpan< Out > a_out, PODSpan< const uint32_t > a_order)
 Run a query on every input in a given order: a_out[o] = a_query(a_in[o]) for o = a_order[i], i = 0, 1, ...
 
template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void transform (const Query &a_query, const Pool &a_pool, const PODVector< In > &a_in, const PODVector< Out > &a_out, const PODVector< uint32_t > &a_order)
 Run a query on every input of a pool-resident array in a given order, writing the results into another array in the same pool: out[o] = a_query(in[o]) for o = order[i].
 
template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void transform (const Query &a_query, const std::vector< In > &a_in, std::vector< Out > &a_out, const std::vector< uint32_t > &a_order)
 Run a query on every input in a given order, from host memory to host memory: a_out[o] = a_query(a_in[o]) for o = a_order[i].
 

Variables

constexpr uint32_t ScanChunk = 256
 Values per chunk in exclusiveScan()'s passes.
 
constexpr uint32_t BoundingBoxChunk = 256
 Points per chunk in boundingBox()'s first pass.
 
constexpr Error Success = 0
 Success value of Error (cudaSuccess / hipSuccess).
 
constexpr MemcpyKind MemcpyHostToDevice = 1
 Host-to-device copy direction (cudaMemcpyHostToDevice / hipMemcpyHostToDevice).
 
constexpr MemcpyKind MemcpyDeviceToHost = 2
 Device-to-host copy direction (cudaMemcpyDeviceToHost / hipMemcpyDeviceToHost).
 
constexpr MemcpyKind MemcpyDeviceToDevice = 3
 Device-to-device copy direction (cudaMemcpyDeviceToDevice / hipMemcpyDeviceToDevice).
 
constexpr bool enabled = false
 Whether this translation unit is compiled as CUDA or HIP, so that parallelFor() and transform() run a kernel.
 
constexpr const char * backendName = "host"
 Where parallelFor() and transform() run in this translation unit: "HIP", "CUDA" or "host".
 
constexpr unsigned int DefaultBlockSize = 128
 The number of threads per block a launch uses unless it is given another.
 
constexpr unsigned int DefaultMinResidentThreads = 384
 The threads a launch asks the CUDA compiler to keep resident on each multiprocessor, at least, unless it is given another: 384, three blocks of 128, which holds a thread to at most 168 registers.
 

Detailed Description

Backend-neutral GPU runtime API surface (CUDA or HIP).

Thin aliases and inline forwarders over the backend runtime (cuda* under CUDA, hip* under HIP) covering exactly the surface EBGeometry uses: memory allocation/copy/free, device enumeration, error retrieval, and synchronization. The two runtimes mirror each other's signatures 1:1 for this surface, so every wrapper is a one-line forwarder.

Typedef Documentation

◆ TransformResult

template<class Query , class In >
using EBGeometry::GPU::TransformResult = typedef std::decay_t<std::invoke_result_t<const Query&, const In&> >

The result type of a query on an input.

Template Parameters
QueryQuery functor type.
InInput type.

Function Documentation

◆ bitonicSort()

template<class Key , class Less = Ascending>
void EBGeometry::GPU::bitonicSort ( Key *  a_keys,
const uint32_t  a_count,
const Less &  a_less = Less() 
)
inline

Sort a_count keys in place with a bitonic network: log2(n) (log2(n) + 1) / 2 passes of n / 2 compare-exchanges each.

Not stable, so for a reproducible result a_less must order every pair of distinct keys. Pad an arbitrary count up to a power of two with keys that sort last. Aborts, in every build, if a_count is not a power of two.

Template Parameters
KeyTrivially copyable key type.
LessTrivially copyable strict order with an EBGEOMETRY_HOST_DEVICE call operator.
Parameters
[in,out]a_keysThe keys.
[in]a_countNumber of keys: a power of two, or zero.
[in]a_lessThe order.

◆ boundingBox()

template<class T >
BoundingVolumes::AABBT< T > EBGeometry::GPU::boundingBox ( const Vec3T< T > *  a_points,
const uint32_t  a_count,
Vec3T< T > *  a_scratch 
)
inline

The smallest box holding an array of points; see boundingBoxOf().

Template Parameters
TPrecision.
Parameters
[in]a_pointsThe points, which the launches can read.
[in]a_countNumber of points.
[out]a_scratchboundingBoxScratchSize(a_count) elements of scratch, which the launches can write.
Returns
The box; the empty box for no points.

◆ boundingBoxOf()

template<class T , class Source >
BoundingVolumes::AABBT< T > EBGeometry::GPU::boundingBoxOf ( const Source &  a_points,
const uint32_t  a_count,
Vec3T< T > *  a_scratch 
)
inline

The smallest box holding every point, from a source of points: one box per chunk of BoundingBoxChunk points, then one thread merging them.

The minimum and maximum are exact, so the box is the one a host loop over the points finds. The source is any trivially copyable functor whose const call operator returns point i, callable where the launches run. The result is copied back from a_scratch to the host (from device memory in a CUDA or HIP build). If a point has a non-finite coordinate, a corner of the box is not finite: check it, since the launches check nothing else.

Template Parameters
TPrecision.
SourceSource type.
Parameters
[in]a_pointsThe points.
[in]a_countNumber of points.
[out]a_scratchboundingBoxScratchSize(a_count) elements of scratch, which the launches can write.
Returns
The box; the empty box for no points.

◆ boundingBoxScratchSize()

uint32_t EBGeometry::GPU::boundingBoxScratchSize ( const uint32_t  a_count)
inlinenoexcept

Scratch elements boundingBox() needs for a_count points: two corners per chunk.

Parameters
[in]a_countNumber of points.
Returns
The scratch size, in Vec3T elements.

◆ deviceAvailable()

bool EBGeometry::GPU::deviceAvailable ( )
inlinenoexcept

Whether a GPU is visible to the runtime.

Always false in a host build. In a CUDA or HIP build, false also when the runtime itself reports an error (no driver, for instance) rather than a device count.

Returns
True if at least one device is visible.

◆ deviceSynchronize()

Error EBGeometry::GPU::deviceSynchronize ( )
inlinenoexcept

Block until the device has finished all preceding work (cudaDeviceSynchronize / hipDeviceSynchronize).

Returns
Backend error code (Success on success).

◆ exclusiveScan()

uint32_t EBGeometry::GPU::exclusiveScan ( uint32_t *  a_values,
const uint32_t  a_count,
uint32_t *  a_scratch 
)
inline

Replace a_count values by their exclusive prefix sums, in place, and return their total: one sum per chunk of ScanChunk values, one thread scanning the sums, then each chunk scanned from its offset.

Integer sums in a fixed order, so the result does not depend on thread scheduling. The values and the scratch must be memory the launches can reach; the total is copied back to the host (from device memory in a CUDA or HIP build).

Parameters
[in,out]a_valuesThe values.
[in]a_countNumber of values.
[out]a_scratchexclusiveScanScratchSize(a_count) elements of scratch, which the launches can write.
Returns
The sum of the values.

◆ exclusiveScanScratchSize()

uint32_t EBGeometry::GPU::exclusiveScanScratchSize ( const uint32_t  a_count)
inlinenoexcept

Scratch elements exclusiveScan() needs for a_count values: one per chunk, and one for the total.

Parameters
[in]a_countNumber of values.
Returns
The scratch size, in uint32_t elements.

◆ getClockRate()

Error EBGeometry::GPU::getClockRate ( int *  a_kiloHertz)
inlinenoexcept

Get the current device's clock rate, the rate at which clock64() counts in a kernel (cudaDeviceGetAttribute with cudaDevAttrClockRate / hipDeviceGetAttribute with hipDeviceAttributeClockRate).

Parameters
[out]a_kiloHertzReceives the clock rate in kHz.
Returns
Backend error code (Success on success).

◆ getDeviceCount()

Error EBGeometry::GPU::getDeviceCount ( int *  a_count)
inlinenoexcept

Get the number of available GPU devices (cudaGetDeviceCount / hipGetDeviceCount).

Parameters
[out]a_countReceives the device count.
Returns
Backend error code (Success on success).

◆ getErrorHint()

const char * EBGeometry::GPU::getErrorHint ( Error  a_error)
inlinenoexcept

What to do about an error that means the binary has no code this GPU can run: the binary was built for another architecture, or carries only PTX that the driver, older than the toolkit, cannot compile (cudaErrorUnsupportedPtxVersion or cudaErrorNoKernelImageForDevice under CUDA, hipErrorNoBinaryForGpu under HIP).

Parameters
[in]a_errorBackend error code.
Returns
A hint, starting with a line break, for those errors; an empty string otherwise.

◆ getErrorString()

const char * EBGeometry::GPU::getErrorString ( Error  a_error)
inlinenoexcept

Get the human-readable description of an error code (cudaGetErrorString / hipGetErrorString).

Parameters
[in]a_errorBackend error code.
Returns
Static string describing the error.

◆ getLastError()

Error EBGeometry::GPU::getLastError ( )
inlinenoexcept

Get and clear the last runtime error (cudaGetLastError / hipGetLastError).

Returns
Backend error code (Success if no error is pending).

◆ isShutdownError()

bool EBGeometry::GPU::isShutdownError ( Error  a_error)
inlinenoexcept

True for the errors a runtime call returns once the runtime has shut down at program exit (cudaErrorCudartUnloading or cudaErrorContextIsDestroyed under CUDA, hipErrorDeinitialized or hipErrorContextIsDestroyed under HIP).

Parameters
[in]a_errorBackend error code.
Returns
True if a_error is one of those errors.

◆ memAlloc()

Error EBGeometry::GPU::memAlloc ( void **  a_devicePtr,
size_t  a_numBytes 
)
inlinenoexcept

Allocate device memory (cudaMalloc / hipMalloc).

Parameters
[out]a_devicePtrReceives the device pointer to the allocation.
[in]a_numBytesNumber of bytes to allocate.
Returns
Backend error code (Success on success).

◆ memAllocHost()

Error EBGeometry::GPU::memAllocHost ( void **  a_ptr,
size_t  a_numBytes 
)
inlinenoexcept

Allocate page-locked (pinned) host memory (cudaHostAlloc / hipHostMalloc).

Parameters
[out]a_ptrReceives the host pointer to the pinned allocation.
[in]a_numBytesNumber of bytes to allocate.
Returns
Backend error code (Success on success).

◆ memAllocHostMapped()

Error EBGeometry::GPU::memAllocHostMapped ( void **  a_ptr,
size_t  a_numBytes 
)
inlinenoexcept

Allocate page-locked (pinned) host memory that is additionally mapped into the device address space for zero-copy access (cudaHostAlloc with cudaHostAllocMapped / hipHostMalloc with hipHostMallocMapped). Under unified virtual addressing the returned host pointer is directly valid on the device. Free with memFreeHost.

Parameters
[out]a_ptrReceives the host pointer to the mapped pinned allocation.
[in]a_numBytesNumber of bytes to allocate.
Returns
Backend error code (Success on success).

◆ memAllocManaged()

Error EBGeometry::GPU::memAllocManaged ( void **  a_ptr,
size_t  a_numBytes 
)
inlinenoexcept

Allocate managed (unified) memory accessible from host and device (cudaMallocManaged / hipMallocManaged).

Parameters
[out]a_ptrReceives the managed pointer to the allocation.
[in]a_numBytesNumber of bytes to allocate.
Returns
Backend error code (Success on success).

◆ memcpy()

Error EBGeometry::GPU::memcpy ( void *  a_dst,
const void *  a_src,
size_t  a_numBytes,
MemcpyKind  a_kind 
)
inlinenoexcept

Copy memory between host and device (cudaMemcpy / hipMemcpy). Synchronous.

Parameters
[out]a_dstDestination pointer (host or device, per a_kind).
[in]a_srcSource pointer (host or device, per a_kind).
[in]a_numBytesNumber of bytes to copy.
[in]a_kindCopy direction.
Returns
Backend error code (Success on success).

◆ memcpyAsync()

Error EBGeometry::GPU::memcpyAsync ( void *  a_dst,
const void *  a_src,
size_t  a_numBytes,
MemcpyKind  a_kind,
Stream  a_stream 
)
inlinenoexcept

Copy memory between host and device on a stream, returning before the copy is done (cudaMemcpyAsync / hipMemcpyAsync); from pinned host memory, it does not wait for the host either.

Parameters
[out]a_dstDestination pointer (host or device, per a_kind).
[in]a_srcSource pointer (host or device, per a_kind).
[in]a_numBytesNumber of bytes to copy.
[in]a_kindCopy direction.
[in]a_streamThe stream.
Returns
Backend error code (Success on success).

◆ memFree()

Error EBGeometry::GPU::memFree ( void *  a_devicePtr)
inlinenoexcept

Free device memory allocated with memAlloc (cudaFree / hipFree).

Parameters
[in]a_devicePtrDevice pointer to free (null is a no-op).
Returns
Backend error code (Success on success).

◆ memFreeHost()

Error EBGeometry::GPU::memFreeHost ( void *  a_ptr)
inlinenoexcept

Free page-locked host memory allocated with memAllocHost (cudaFreeHost / hipHostFree).

Parameters
[in]a_ptrHost pointer to free (null is a no-op).
Returns
Backend error code (Success on success).

◆ memoryResource()

MemoryResource & EBGeometry::GPU::memoryResource ( )
inlinenoexcept

The memory a query's data must live in for parallelFor() and transform() to read it.

deviceMemoryResource() in a CUDA or HIP build, and emulatedDeviceMemoryResource() (host memory that reports itself device-accessible) otherwise. Mirroring a frozen pool into it and taking a rebasedView() onto the mirror is then the same code in both builds: a device copy that a kernel reads, or a host copy that the host loop reads.

Returns
The memory resource.

◆ mortonOrder() [1/2]

template<class Source >
void EBGeometry::GPU::mortonOrder ( const Pool &  a_pool,
const Source &  a_source,
PODVector< uint32_t > &  a_order 
)
inline

Write into a_order, an array in a_pool, the order to run queries at a source's points in; the span overload on a_pool's resource, for an order reserved in a pool.

See the span overload for the order, the source and where it runs. a_pool may be frozen (a mirror), since nothing is reserved in it. Aborts with a message unless a_order lies within a_pool's used bytes, and in the cases the span overload aborts in.

Template Parameters
SourceSource of the points.
Parameters
[in]a_poolThe pool holding a_order; its resource decides where the order is computed.
[in]a_sourceThe points.
[out]a_orderThe order, one entry per point, in a_pool.

◆ mortonOrder() [2/2]

template<class Source >
void EBGeometry::GPU::mortonOrder ( MemoryResource &  a_where,
const Source &  a_source,
PODSpan< uint32_t >  a_order 
)
inline

Write into a_order the order to run queries at a source's points in: the permutation of 0, ..., n - 1 (n = a_order.size()) that sorts the points along a Morton curve over their bounding box, ties broken by index. Runs on the device, with a bitonic sort, when a_where's memory is device-accessible in a CUDA or HIP build; on the host, with a sort, otherwise. Both give the same order, element for element.

Queries that arrive in no spatial order make neighboring threads walk unrelated parts of a BVH or a mesh. Run in this order (the ordered GPU::parallelFor() and GPU::transform()), nearby points run side by side, share cache lines and diverge less, while every result still lands at its own index. That pays for queries that read memory (BVHs, meshes, point clouds), not for an analytic shape, whose distance reads none. The order depends on the points only: compute it once and reuse it while they stay put. Any permutation works with the ordered launches, so a caller with its own space-filling curve or sort can pass its own order instead; this is a convenience.

The points come from a source: a trivially copyable functor whose const call operator takes an index (uint32_t) and returns that point's Vec3T (PointCloud::FromArray wraps an array), so a struct of particles needs no copy into points first. The source and a_order are used where a_where's memory is, so they must be readable (and a_order writable) there: device or managed memory for a device-accessible a_where, host memory otherwise. This is not checked.

Scratch, a 16-byte key per point (on a device rounded up to a power of two) and a few bytes for the box, is allocated on a_where and released before returning. Aborts with a message unless n is at most 2^31 and every point is finite.

order.reserveFrom(pool, n);
Pool device = Pool::mirror(pool, deviceResource);
const PODSpan<uint32_t> o = order.bind(device.base());
GPU::mortonOrder(deviceResource, Positions{particles}, o); // once, while the particles stay put
GPU::parallelFor(n, query, o); // every time the queries run
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:205
void * base() const noexcept
Base address of the block.
Definition EBGeometry_Pool.hpp:312
static Pool mirror(const Pool &a_src, MemoryResource &a_dstResource)
Mirror a frozen pool's whole block into another resource.
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.
void mortonOrder(MemoryResource &a_where, const Source &a_source, PODSpan< uint32_t > a_order)
Write into a_order the order to run queries at a source's points in: the permutation of 0,...
Raw pointer + size view into a Pool block, for hot inner loops.
Definition EBGeometry_PODVector.hpp:52
Placement-independent descriptor of a POD array inside a Pool.
Definition EBGeometry_PODVector.hpp:131
Template Parameters
SourceSource of the points.
Parameters
[in]a_whereThe resource whose memory decides where the order is computed, and which holds the scratch.
[in]a_sourceThe points.
[out]a_orderThe order, one entry per point.

◆ parallelFor() [1/2]

template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class F >
void EBGeometry::GPU::parallelFor ( std::size_t  a_n,
const F &  a_f 
)
inline

Run a functor on every index: a_f(i) for i = 0, ..., a_n - 1, in no particular order.

In a CUDA or HIP build, a kernel runs the functor once per index (one thread each, in blocks of BlockSize threads) on the current device, and the call returns once it has finished. Every runtime call is checked with EBGEOMETRY_GPU_CHECK, which aborts with the failing call on an error, and a visible device is required (see deviceAvailable()). The functor reaches the kernel by value, so anything it reads or writes must be memory the device can access.

In a host build the functor is copied once, as a kernel launch would copy it, and run in a loop. The functor must be trivially copyable in both builds, which is checked at compile time. A grid holds at most 2^31 - 1 blocks, so a_n may not exceed (2^31 - 1) * BlockSize; a larger count aborts with a message, in both builds.

Template Parameters
BlockSizeThreads per block: a multiple of 64 (an AMD wavefront, and two NVIDIA warps), at most 1024. The kernel carries it as launch bounds, so the compiler can give each thread the registers that block size allows.
MinResidentThreadsIn a CUDA build, the threads to keep resident on a multiprocessor, at least, which fixes the register budget (see DefaultMinResidentThreads); at most 2048, 0 for no minimum.
FTrivially copyable functor with an EBGEOMETRY_HOST_DEVICE call operator taking a std::size_t.
Parameters
[in]a_nNumber of indices.
[in]a_fThe functor.

◆ parallelFor() [2/2]

template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class F >
void EBGeometry::GPU::parallelFor ( uint32_t  a_n,
const F &  a_f,
PODSpan< const uint32_t >  a_order 
)
inline

Run a functor on every index, in a given order: a_f(a_order[i]) for i = 0, ..., a_n - 1.

parallelFor() with the indices taken through a permutation, so that neighboring threads (a warp on a device, consecutive iterations on the host) run indices the caller knows to be close: mortonOrder() gives one that puts queries at nearby points side by side, so that they walk the same parts of a BVH. Every index still runs once, and the functor sees the index itself, not its position in the order, so its results land where the unordered parallelFor() puts them: only the speed changes. The order must be a permutation of 0, ..., a_n - 1, in memory the launch can read; its size is checked (it aborts with a message otherwise), each entry is checked to be below a_n with assertions on, and that no entry repeats is the caller's responsibility (a repeated index would run twice, and another not at all). An order is worth computing once and reusing while the points stay put.

Template Parameters
BlockSizeThreads per block; see parallelFor().
MinResidentThreadsThreads to keep resident, in a CUDA build; see parallelFor().
FTrivially copyable functor with an EBGEOMETRY_HOST_DEVICE call operator taking a std::size_t.
Parameters
[in]a_nNumber of indices.
[in]a_fThe functor.
[in]a_orderThe order: a_n indices, a permutation of 0, ..., a_n - 1.

◆ streamCreateNonBlocking()

Error EBGeometry::GPU::streamCreateNonBlocking ( Stream *  a_stream)
inlinenoexcept

Create a stream that runs alongside the default stream rather than waiting for its work (cudaStreamCreateWithFlags / hipStreamCreateWithFlags with the non-blocking flag), for a copy made while a kernel on the default stream runs. Destroy it with streamDestroy.

Parameters
[out]a_streamReceives the stream.
Returns
Backend error code (Success on success).

◆ streamDestroy()

Error EBGeometry::GPU::streamDestroy ( Stream  a_stream)
inlinenoexcept

Destroy a stream (cudaStreamDestroy / hipStreamDestroy).

Parameters
[in]a_streamThe stream.
Returns
Backend error code (Success on success).

◆ streamSynchronize()

Error EBGeometry::GPU::streamSynchronize ( Stream  a_stream)
inlinenoexcept

Wait until a stream's work is done (cudaStreamSynchronize / hipStreamSynchronize).

Parameters
[in]a_streamThe stream.
Returns
Backend error code (Success on success).

◆ transform() [1/6]

template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void EBGeometry::GPU::transform ( const Query &  a_query,
const Pool &  a_pool,
const PODVector< In > &  a_in,
const PODVector< Out > &  a_out 
)
inline

Run a query on every input of a pool-resident array, writing the results into another array in the same pool: out[i] = a_query(in[i]), with no allocation and no copies.

The span overload, with the memory checked first. A PODVector is an offset into a pool, so this overload takes the pool both arrays live in and binds them against its base. It aborts with a message, in every build, unless the pool is frozen (as rebasedView() requires of a device-accessible pool) and its resource is device-accessible: a mirror into memoryResource(), or a pool built in managed memory. A pool in hostMemoryResource() is therefore rejected in a host build too, where memoryResource() is the emulated device resource. A PODVector holds an offset, not its pool, so membership can be checked only as far as both arrays ending inside the pool's used bytes, which also aborts otherwise; that an array really belongs to this pool (or a mirror of its pool) is the caller's responsibility. The inputs are bound read-only. Freezing forbids reservations, not writes, so the results can be written into a frozen pool, in place over the inputs or into a disjoint array as for the span overload. Arrays in two different pools, or not in a pool at all, go through the span overload.

Template Parameters
BlockSizeThreads per block; see parallelFor().
MinResidentThreadsThreads to keep resident, in a CUDA build; see parallelFor().
QueryTrivially copyable functor with an EBGEOMETRY_HOST_DEVICE call operator taking a const In&.
InInput type.
OutResult type: the query's result type.
Parameters
[in]a_queryThe query.
[in]a_poolThe frozen, device-accessible pool holding both arrays.
[in]a_inThe inputs.
[in]a_outThe results' array, as many elements as a_in; its elements are written.

◆ transform() [2/6]

template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void EBGeometry::GPU::transform ( const Query &  a_query,
const Pool &  a_pool,
const PODVector< In > &  a_in,
const PODVector< Out > &  a_out,
const PODVector< uint32_t > &  a_order 
)
inline

Run a query on every input of a pool-resident array in a given order, writing the results into another array in the same pool: out[o] = a_query(in[o]) for o = order[i].

The pool transform() with the order of the ordered span overload: the pool is checked as there, the order is a third array in the same pool, and the inputs and the results must not overlap.

Template Parameters
BlockSizeThreads per block; see parallelFor().
MinResidentThreadsThreads to keep resident, in a CUDA build; see parallelFor().
QueryTrivially copyable functor with an EBGEOMETRY_HOST_DEVICE call operator taking a const In&.
InInput type.
OutResult type: the query's result type.
Parameters
[in]a_queryThe query.
[in]a_poolThe frozen, device-accessible pool holding the three arrays.
[in]a_inThe inputs.
[in]a_outThe results' array, as many elements as a_in, not overlapping it; its elements are written.
[in]a_orderThe order: as many entries as a_in, a permutation of their indices.

◆ transform() [3/6]

template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void EBGeometry::GPU::transform ( const Query &  a_query,
const std::vector< In > &  a_in,
std::vector< Out > &  a_out 
)
inline

Run a query on every input, from host memory to host memory: a_out[i] = a_query(a_in[i]).

A convenience over the span overload. In a CUDA or HIP build it allocates device memory for the inputs and the results, copies the inputs in, runs the query in a kernel, copies the results into a_out and frees, so timing it includes two allocations and two copies; time the span or pool overload to measure the query itself. In a host build the query runs in a loop directly on the vectors. a_out is not resized: it must already hold as many elements as a_in, which aborts with a message otherwise, and can be reused across calls. At most 2^32 - 1 inputs, which also aborts with a message otherwise. Neither the input nor the result may be bool (std::vector<bool> has no contiguous storage to copy), which is checked at compile time in both builds.

Template Parameters
BlockSizeThreads per block; see parallelFor().
MinResidentThreadsThreads to keep resident, in a CUDA build; see parallelFor().
QueryTrivially copyable functor with an EBGEOMETRY_HOST_DEVICE call operator taking a const In&.
InTrivially copyable input type.
OutResult type: the query's result type.
Parameters
[in]a_queryThe query.
[in]a_inThe inputs.
[out]a_outThe results, in input order; as many elements as a_in.

◆ transform() [4/6]

template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void EBGeometry::GPU::transform ( const Query &  a_query,
const std::vector< In > &  a_in,
std::vector< Out > &  a_out,
const std::vector< uint32_t > &  a_order 
)
inline

Run a query on every input in a given order, from host memory to host memory: a_out[o] = a_query(a_in[o]) for o = a_order[i].

The std::vector transform() with the order of the ordered span overload; in a CUDA or HIP build the order is copied to the device with the inputs.

Template Parameters
BlockSizeThreads per block; see parallelFor().
MinResidentThreadsThreads to keep resident, in a CUDA build; see parallelFor().
QueryTrivially copyable functor with an EBGEOMETRY_HOST_DEVICE call operator taking a const In&.
InTrivially copyable input type.
OutResult type: the query's result type.
Parameters
[in]a_queryThe query.
[in]a_inThe inputs.
[out]a_outThe results, in input order; as many elements as a_in.
[in]a_orderThe order (mortonOrder(), say): as many entries as a_in, a permutation of their indices.

◆ transform() [5/6]

template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void EBGeometry::GPU::transform ( const Query &  a_query,
PODSpan< In >  a_in,
PODSpan< Out >  a_out 
)
inline

Run a query on every input in place: a_out[i] = a_query(a_in[i]), with no allocation and no copies.

The inputs and the results stay wherever the caller put them, so this is the call to time when a query's cost is what is being measured, and the one to use for repeated queries over the same points. A span is a pointer and a count: bind a PODVector against the base of a pool mirrored into memoryResource() (inputs) or of a pool reserved there (results), or wrap managed memory or a buffer the application already owns on the device. In a host build the spans are host memory. The results may overwrite the inputs in place (the same array, when In and Out are the same type), but the two must otherwise not overlap, which aborts with a message. The query runs through parallelFor(), with its requirements.

Template Parameters
BlockSizeThreads per block; see parallelFor().
MinResidentThreadsThreads to keep resident, in a CUDA build; see parallelFor().
QueryTrivially copyable functor with an EBGEOMETRY_HOST_DEVICE call operator taking a const In&.
InInput type, const or not.
OutResult type: the query's result type, not const.
Parameters
[in]a_queryThe query.
[in]a_inThe inputs, in memory the device can read.
[out]a_outThe results, in memory the device can write; as many as there are inputs.

◆ transform() [6/6]

template<unsigned int BlockSize = DefaultBlockSize, unsigned int MinResidentThreads = DefaultMinResidentThreads, class Query , class In , class Out >
void EBGeometry::GPU::transform ( const Query &  a_query,
PODSpan< In >  a_in,
PODSpan< Out >  a_out,
PODSpan< const uint32_t >  a_order 
)
inline

Run a query on every input in a given order: a_out[o] = a_query(a_in[o]) for o = a_order[i], i = 0, 1, ...

The span transform() run through the ordered parallelFor(), with its requirements on the order: every result lands where the unordered transform() puts it, and only the speed changes. Unlike the unordered overload, the results may not overwrite the inputs in place, since a thread could then overwrite an input that another thread has not read yet: the inputs and the results must not overlap, which aborts with a message otherwise.

Template Parameters
BlockSizeThreads per block; see parallelFor().
MinResidentThreadsThreads to keep resident, in a CUDA build; see parallelFor().
QueryTrivially copyable functor with an EBGEOMETRY_HOST_DEVICE call operator taking a const In&.
InInput type, const or not.
OutResult type: the query's result type, not const.
Parameters
[in]a_queryThe query.
[in]a_inThe inputs, in memory the device can read.
[out]a_outThe results, in memory the device can write; as many as there are inputs, not overlapping them.
[in]a_orderThe order (mortonOrder(), say): as many entries as inputs, a permutation of their indices.

Variable Documentation

◆ DefaultBlockSize

constexpr unsigned int EBGeometry::GPU::DefaultBlockSize = 128
inlineconstexpr

The number of threads per block a launch uses unless it is given another.

Not tuned: the best block size depends on the device and on the query (a BVH traversal uses many registers per thread). Pass another as the first template argument to measure it, e.g. GPU::transform<256>(query, points, results).

◆ DefaultMinResidentThreads

constexpr unsigned int EBGeometry::GPU::DefaultMinResidentThreads = 384
inlineconstexpr

The threads a launch asks the CUDA compiler to keep resident on each multiprocessor, at least, unless it is given another: 384, three blocks of 128, which holds a thread to at most 168 registers.

The kernel carries it as launch bounds (a minimum number of resident blocks, rounded up). Without a minimum, the compiler picks one register budget for the whole kernel, every function it calls included, and the pick jumps with small changes to code the kernel may not even run (a tape's ray kernel once went from 255 registers to 40, spilling twice as much); given one, it uses the budget the minimum allows. Not tuned for every query: the budget is a ceiling the compiler tends to fill, so a kernel that runs best with few registers may be slower under it (rays traced through a fused composition went from 56 registers to 161 and ran up to a third slower, and the renderer's frame of a mesh from 128 to 161, 10% slower). The minimum still pays where registers matter: on one desktop GPU, ray tracing through a tape took 2.5 times as long with no minimum as at 384, and 1.45 times as long at 768, while a mesh's signed distance took the same time at every setting; DeviceTuning's mesh-minus-ball tape distance in float was a fifth to a quarter faster at 768 than at 384, whatever the block size. 0 asks for no minimum. HIP reads a second launch bound differently, and a HIP build ignores it.