Skip to content

Commit

Permalink
add trait IsKernelArgumentTriviallyCopyable
Browse files Browse the repository at this point in the history
fix #2195

Provide an alpaka specific trait to validate kernel argument conditions which can be specialized by the user
for their own types and at their own risk.
  • Loading branch information
psychocoderHPC authored and bernhardmgruber committed Dec 4, 2023
1 parent f82033f commit 188361b
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions include/alpaka/kernel/Traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,29 @@ namespace alpaka
"-Wdocumentation" // clang does not support the syntax for variadic template arguments "args,..."
#endif


//! Check if a type used as kernel argument is trivially copyable
//!
//! \attention In case this trait is specialized for a user type the user should be sure that the result of calling
//! the copy constructor is equal to use memcpy to duplicate the object. An existing destructor should be free
//! of side effects.
//!
//! It's implementation defined whether the closure type of a lambda is trivially copyable.
//! Therefor the default implementation is true for trivially copyable or empty (stateless) types.
//!
//! @tparam T type to check
//! @{
template<typename T, typename = void>
struct IsKernelArgumentTriviallyCopyable
: std::bool_constant<std::is_empty_v<T> || std::is_trivially_copyable_v<T>>
{
};

template<typename T>
inline constexpr bool isKernelArgumentTriviallyCopyable = IsKernelArgumentTriviallyCopyable<T>::value;

//! @}

namespace detail
{
//! Check that the return of TKernelFnObj is void
Expand All @@ -221,13 +244,8 @@ namespace alpaka
template<typename T>
inline void assertKernelArgIsTriviallyCopyable()
{
static_assert(
// it's implementation defined whether the closure type of a lambda is trivially copyable. But they
// should be at least empty then (stateless).
std::is_empty_v<T> || std::is_trivially_copyable_v<T>,
"The kernel argument T must be trivially copyable!");
static_assert(isKernelArgumentTriviallyCopyable<T>, "The kernel argument T must be trivially copyable!");
}

} // namespace detail

//! Creates a kernel execution task.
Expand Down

0 comments on commit 188361b

Please sign in to comment.