tfel
Loading...
Searching...
No Matches
GPU.hxx File Reference

This file declares the primitive of the cuda and hip backends of the generic-parallel interfaces. This file must be compiled by a CUDA or a HIP compiler. The HIP runtime is used if the hip/hip_runtime.h header has been included before this file or if the HIP language is enabled, the CUDA runtime otherwise. Including the HIP runtime header first is required to use HIP on NVIDIA GPUs, since hipcc then calls nvcc. More...

Namespaces

namespace  mfront
 This namespace contains material used to build the MFront code generator.
namespace  mfront::generic_parallel

Macros

#define MFRONT_GENERIC_PARALLEL_GPU_GRID_CONSTANT
 qualifier of the function object passed by value to the kernels.

Typedefs

using mfront::generic_parallel::gpu::internals::ExecutionPolicy = CUDABackendExecutionPolicy
 execution policy of the backend

Functions

template<typename OutputStatus>
bool mfront::generic_parallel::gpu::internals::failed (OutputStatus &s, const Error e) noexcept
 report an error of the runtime. Only the first error is reported.
template<auto kernel, typename OutputStatus>
bool mfront::generic_parallel::gpu::internals::ensureStackSize (OutputStatus &s, const bool preserve)
 increase the stack size of the threads if the given kernel requires it (see mfront::generic_parallel::gpu::ensureStackSize).
template<typename T>
__device__ T mfront::generic_parallel::gpu::internals::warpSum (T v)
 sum of the values of a warp, in the first thread of the warp
__device__ bool mfront::generic_parallel::gpu::internals::warpAny (const bool p)
__device__ atomic_size_type mfront::generic_parallel::gpu::internals::warpMin (atomic_size_type v)
 minimum of the values of a warp, in the first thread of the warp
template<typename Statistics, typename PointFunction>
__global__ void mfront::generic_parallel::gpu::internals::apply (const MFRONT_GENERIC_PARALLEL_GPU_GRID_CONSTANT PointFunction f, const size_type n, Statistics *const s, const atomic_size_type rdt0)
 apply a function at the points of a batch, one thread per point, and update the statistics of the batch. The number of threads per block must be a multiple of the warp size.
template<typename Statistics, typename PointFunction>
auto & mfront::generic_parallel::gpu::internals::getStatisticsBlocks () noexcept
template<typename StatisticsHandler, typename PointFunction>
int mfront::generic_parallel::gpu::apply (typename StatisticsHandler::OutputStatus &s, const size_type n, const BatchExecution< typename StatisticsHandler::Statistics > &e, const StatisticsHandler &h, const PointFunction &f, const unsigned int threads_per_block)
 apply a function at the points of a batch, one thread per point, and report the statistics of the batch.
auto mfront::generic_parallel::gpu::makeApply (const unsigned int threads_per_block)

Variables

constexpr auto mfront::generic_parallel::gpu::internals::warp_size_attribute = cudaDevAttrWarpSize
 device attribute giving the warp size

Detailed Description

This file declares the primitive of the cuda and hip backends of the generic-parallel interfaces. This file must be compiled by a CUDA or a HIP compiler. The HIP runtime is used if the hip/hip_runtime.h header has been included before this file or if the HIP language is enabled, the CUDA runtime otherwise. Including the HIP runtime header first is required to use HIP on NVIDIA GPUs, since hipcc then calls nvcc.

Date
05/10/2026

Macro Definition Documentation

◆ MFRONT_GENERIC_PARALLEL_GPU_GRID_CONSTANT

#define MFRONT_GENERIC_PARALLEL_GPU_GRID_CONSTANT

qualifier of the function object passed by value to the kernels.

The member functions of this object take its address. nvcc then copies it in the stack of each thread (local memory), unless the parameter is declared __grid_constant__: the threads then read it in place. This qualifier is defined by the headers of CUDA 11.7 or later and requires a compute capability of 7.0 or later. clang reads this parameter in place without it, as do the HIP compilers for AMD GPUs.

Function Documentation

◆ apply() [1/2]

template<typename StatisticsHandler, typename PointFunction>
int mfront::generic_parallel::gpu::apply ( typename StatisticsHandler::OutputStatus & s,
const size_type n,
const BatchExecution< typename StatisticsHandler::Statistics > & e,
const StatisticsHandler & h,
const PointFunction & f,
const unsigned int threads_per_block )

apply a function at the points of a batch, one thread per point, and report the statistics of the batch.

The kernel is launched on the stream given by the execution policy. If the computations are asynchronous, this function returns once the kernel is launched. Otherwise, it waits for its completion.

Without statistics given by the calling code, the statistics are held by a block of device memory of a pool of blocks (see StatusBlockPool): no memory is allocated per call. Local statistics are initialized on the host, copied to the block just before the launch and copied back once the computations are completed: the host never accesses the block, which the device may not share with the host while kernels run (devices without concurrent access to managed memory). The device only accesses the block if an event occurs at a point.

If the function never reports an event (see the hasEvents method of the handler), the statistics are neither combined nor reported, and no block of the pool is used.

Template Parameters
StatisticsHandlerhandler of the statistics (see MFront/GenericParallel/Batch.hxx)
PointFunctionfunction object taking the index of a point and returning the statistics associated with this point
Parameters
[in,out]soutput status, which must have been initialized
[in]nnumber of points
[in]eexecution of the batch. The policy is a CUDABackendExecutionPolicy (HIPBackendExecutionPolicy with HIP), or null for the default stream.
[in]hhandler of the statistics
[in]ffunction
[in]threads_per_blocknumber of threads per block, rounded up to a multiple of the warp size
Returns
the status

◆ apply() [2/2]

template<typename Statistics, typename PointFunction>
__global__ void mfront::generic_parallel::gpu::internals::apply ( const MFRONT_GENERIC_PARALLEL_GPU_GRID_CONSTANT PointFunction f,
const size_type n,
Statistics *const s,
const atomic_size_type rdt0 )

apply a function at the points of a batch, one thread per point, and update the statistics of the batch. The number of threads per block must be a multiple of the warp size.

Parameters
[in]ffunction
[in]nnumber of points
[out]sstatistics, or null if the function never reports an event (see the hasEvents method of the handlers of the statistics)
[in]rdt0ordered key of the initial time step scaling factor

◆ ensureStackSize()

template<auto kernel, typename OutputStatus>
bool mfront::generic_parallel::gpu::internals::ensureStackSize ( OutputStatus & s,
const bool preserve )

increase the stack size of the threads if the given kernel requires it (see mfront::generic_parallel::gpu::ensureStackSize).

Returns
true on success
Template Parameters
kernelkernel
Parameters
[in,out]soutput status
[in]preserveif true, the stack size is not modified and an error is reported if it is too small

◆ failed()

template<typename OutputStatus>
bool mfront::generic_parallel::gpu::internals::failed ( OutputStatus & s,
const Error e )
noexcept

report an error of the runtime. Only the first error is reported.

Returns
true if the given code denotes an error
Parameters
[in,out]soutput status
[in]eerror code

◆ getStatisticsBlocks()

template<typename Statistics, typename PointFunction>
auto & mfront::generic_parallel::gpu::internals::getStatisticsBlocks ( )
noexcept
Returns
the pool of blocks of device memory holding the statistics of the synchronous computations of a function of the generated library
Template Parameters
Statisticsstatistics updated by the devices
PointFunctionfunction applied at the points, which is specific to a generated function

The pool depends on the function applied at the points, whose type is declared in an anonymous namespace by the generated code: each generated function, and thus each generated library, has its own pool. A pool shared by several libraries through the dynamic linker would be destroyed when the library which initialized it is unloaded, even if another library still uses it.

◆ makeApply()

auto mfront::generic_parallel::gpu::makeApply ( const unsigned int threads_per_block)
inline
Returns
the primitive of the backend, called by the functions of the interfaces (see for instance mfront::gpmp::evaluateBatch)
Parameters
[in]threads_per_blocknumber of threads per block

◆ warpAny()

__device__ bool mfront::generic_parallel::gpu::internals::warpAny ( const bool p)
inline
Returns
true if the predicate is true for a thread of the warp