MGIS 3.1.0
Loading...
Searching...
No Matches
Algorithms.ixx File Reference

Implementation of the CUDA assign algorithm declared in MGIS/Function/CUDA/Algorithms.hxx, including the generic CUDA kernel and the helper which launches it. More...

Namespaces

namespace  mgis
 the namespace mgis encloses all the MGIS project.

Functions

template<typename CallableType, typename SpaceSizeType>
__global__ void mgis::function::cuda::apply_kernel (CallableType f, const SpaceSizeType s)
 CUDA kernel calling a callable object on every element of a space.
template<typename CallableType, typename SpaceSizeType>
bool mgis::function::cuda::call_kernel (AbstractErrorHandler &ctx, const CUDAExecutionConfiguration &c, CallableType f, const SpaceSizeType s)
 launch apply_kernel on the space and wait for its completion
template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>) && std::same_as<function_space<FunctionType>, evaluator_space<EvaluatorType>>)
bool mgis::function::assign (AbstractErrorHandler &, const CUDAExecutionConfiguration &, FunctionType &, const EvaluatorType)
 assign the evaluator to a function

Detailed Description

Implementation of the CUDA assign algorithm declared in MGIS/Function/CUDA/Algorithms.hxx, including the generic CUDA kernel and the helper which launches it.

Author
Thomas Helfer
Date
28/06/2026

Function Documentation

◆ apply_kernel()

template<typename CallableType, typename SpaceSizeType>
__global__ void mgis::function::cuda::apply_kernel ( CallableType f,
const SpaceSizeType s )

CUDA kernel calling a callable object on every element of a space.

Each thread computes its own linear index from the block and thread indices and calls f with this index. Threads whose index is greater than or equal to the size of the space return immediately, which allows the grid to oversubscribe the space.

Template Parameters
CallableTypetype of the callable object, which must be callable from the device
SpaceSizeTypeintegral type used to index the space
Parameters
[in]fcallable object applied to every element
[in]ssize of the space

◆ assign()

template<typename FunctionType, EvaluatorConcept EvaluatorType>
requires ( (LinearElementSpaceConcept<evaluator_space<EvaluatorType>>) && std::same_as<function_space<FunctionType>, evaluator_space<EvaluatorType>>)
bool mgis::function::assign ( AbstractErrorHandler & ctx,
const CUDAExecutionConfiguration & c,
FunctionType & f,
const EvaluatorType e )
nodiscard

assign the evaluator to a function

implementation of the CUDA assign algorithm

This is the CUDA counterpart of the assign algorithm declared in MGIS/Function/Algorithms.hxx: the values are computed on the graphic card by a CUDA kernel instead of being computed on the host. The evaluator and the function must therefore be callable from the device, which means in particular that they must be built on data accessible from the device, e.g. thrust::universal_vectors, and that the lambda expressions they rely on must be MGIS_HOST_DEVICE lambda expressions.

The spaces of the function and of the evaluator and their number of components are compared and the evaluator is checked before the kernel is launched; the launch strategy itself is described in MGIS/Function/CUDA/Algorithms.ixx.

Only the function arguments which can be captured by a kernel are supported: if the function is not a lightweight view, a view of it is computed on the host and captured by the kernel.

Template Parameters
FunctionTypetype of the function the values are assigned to, which must be defined on the same space as the evaluator
EvaluatorTypetype of the evaluator, which must satisfy the EvaluatorConcept concept and be defined on a linear element space
Parameters
[in]ctxexecution context, used to report the errors
[in]cCUDA execution configuration
[in]fleft hand side, i.e. the function the values are assigned to
[in]eright hand side, i.e. the evaluator computing the values
Returns
true if the assignment succeeded

After checking that the spaces of the function and of the evaluator are equivalent, that their numbers of components match and that the evaluator is valid, a device callable lambda expression is built to assign, for a given index, the value computed by the evaluator to that of the function and is applied to every element of the space by cuda::call_kernel.

Two orthogonal choices are made at compile time:

  • if the function is a lightweight view, it is captured directly by the lambda expression, otherwise a view of it is computed on the host and captured,
  • if the value returned by the function can be assigned from that of the evaluator using operator=, a direct assignment is used, otherwise the internals::assign_value helper is used, which handles the cases where the two values have different, but compatible, types.
Note
the requires-clause restricts the algorithm to evaluators defined on a linear element space and to functions defined on the very same space so that a single linear index can address both the function and the evaluator.

◆ call_kernel()

template<typename CallableType, typename SpaceSizeType>
bool mgis::function::cuda::call_kernel ( AbstractErrorHandler & ctx,
const CUDAExecutionConfiguration & c,
CallableType f,
const SpaceSizeType s )
nodiscard

launch apply_kernel on the space and wait for its completion

The number of threads per block is taken from the configuration and the number of blocks is computed from the size of the space so that every index is covered by at least one thread. Both the launch and the execution of the kernel are checked: the corresponding CUDA error message is registered through the error handler when an error occurs.

Template Parameters
CallableTypetype of the callable object, which must be callable from the device
SpaceSizeTypeintegral type used to index the space
Parameters
[in]ctxerror handler used to report the errors
[in]cCUDA execution configuration
[in]fcallable object applied to every element of the space
[in]ssize of the space
Returns
true if the kernel was launched and executed without error