https://mooseframework.inl.gov
Public Member Functions | Private Attributes | List of all members
Moose::Kokkos::FunctionWrapperHost< Object > Class Template Reference

Host function wrapper class that allocates a function on device and creates its device wrapper. More...

#include <KokkosFunctionWrapper.h>

Inheritance diagram for Moose::Kokkos::FunctionWrapperHost< Object >:
[legend]

Public Member Functions

 FunctionWrapperHost (const void *function)
 Constructor. More...
 
 ~FunctionWrapperHost ()
 Destructor. More...
 
FunctionWrapperDeviceBaseallocate () override final
 Allocate device function and wrapper. More...
 
void copyFunction () override final
 Copy function to device. More...
 
void freeFunction () override final
 Free host and device copies of function. More...
 

Private Attributes

const Object & _function_host
 Reference of the function on host. More...
 
std::unique_ptr< Object > _function_copy
 Copy of the function on host. More...
 
Object * _function_device = nullptr
 Copy of the function on device. More...
 

Detailed Description

template<typename Object>
class Moose::Kokkos::FunctionWrapperHost< Object >

Host function wrapper class that allocates a function on device and creates its device wrapper.

This class holds the actual device instance of the function and manages its allocation and deallocation, and the device wrapper simply keeps a pointer to it.

Template Parameters
ObjectThe function class type

Definition at line 20 of file KokkosFunctionWrapper.h.

Constructor & Destructor Documentation

◆ FunctionWrapperHost()

template<typename Object >
Moose::Kokkos::FunctionWrapperHost< Object >::FunctionWrapperHost ( const void function)
inline

Constructor.

Parameters
functionPointer to the function

Definition at line 142 of file KokkosFunctionWrapper.h.

143  : _function_host(*static_cast<const Object *>(function))
144  {
145  }
const Object & _function_host
Reference of the function on host.

◆ ~FunctionWrapperHost()

template<typename Object >
Moose::Kokkos::FunctionWrapperHost< Object >::~FunctionWrapperHost ( )

Destructor.

Definition at line 213 of file KokkosFunctionWrapper.h.

214 {
215  ::Kokkos::kokkos_free<ExecSpace::memory_space>(_function_device);
216 }
Object * _function_device
Copy of the function on device.

Member Function Documentation

◆ allocate()

template<typename Object >
FunctionWrapperDeviceBase * Moose::Kokkos::FunctionWrapperHost< Object >::allocate ( )
finaloverridevirtual

Allocate device function and wrapper.

Returns
The pointer to the device wrapper

Implements Moose::Kokkos::FunctionWrapperHostBase.

Definition at line 172 of file KokkosFunctionWrapper.h.

173 {
174  // Allocate storage for device wrapper on device
175  auto wrapper_device = static_cast<FunctionWrapperDevice<Object> *>(
176  ::Kokkos::kokkos_malloc<ExecSpace::memory_space>(sizeof(FunctionWrapperDevice<Object>)));
177 
178  // Allocate device wrapper on device using placement new to populate vtable with device pointers
179  ::Kokkos::parallel_for(
180  1, KOKKOS_LAMBDA(const int) { new (wrapper_device) FunctionWrapperDevice<Object>(); });
181 
182  // Allocate storage for function on device
184  static_cast<Object *>(::Kokkos::kokkos_malloc<ExecSpace::memory_space>(sizeof(Object)));
185 
186  // Let device wrapper point to the copy
187  ::Kokkos::Impl::DeepCopy<MemSpace, ::Kokkos::HostSpace>(
188  &(wrapper_device->_function), &_function_device, sizeof(Object *));
189 
190  return wrapper_device;
191 }
Object * _function_device
Copy of the function on device.

◆ copyFunction()

template<typename Object >
void Moose::Kokkos::FunctionWrapperHost< Object >::copyFunction ( )
finaloverridevirtual

Copy function to device.

Implements Moose::Kokkos::FunctionWrapperHostBase.

Definition at line 195 of file KokkosFunctionWrapper.h.

196 {
197  // Make a copy of function on host to trigger copy constructor
198  _function_copy = std::make_unique<Object>(_function_host);
199 
200  // Copy function to device
201  ::Kokkos::Impl::DeepCopy<MemSpace, ::Kokkos::HostSpace>(
202  _function_device, _function_copy.get(), sizeof(Object));
203 }
const Object & _function_host
Reference of the function on host.
std::unique_ptr< Object > _function_copy
Copy of the function on host.
Object * _function_device
Copy of the function on device.

◆ freeFunction()

template<typename Object >
void Moose::Kokkos::FunctionWrapperHost< Object >::freeFunction ( )
finaloverridevirtual

Free host and device copies of function.

Implements Moose::Kokkos::FunctionWrapperHostBase.

Definition at line 207 of file KokkosFunctionWrapper.h.

208 {
209  _function_copy.reset();
210 }
std::unique_ptr< Object > _function_copy
Copy of the function on host.

Member Data Documentation

◆ _function_copy

template<typename Object >
std::unique_ptr<Object> Moose::Kokkos::FunctionWrapperHost< Object >::_function_copy
private

◆ _function_device

template<typename Object >
Object* Moose::Kokkos::FunctionWrapperHost< Object >::_function_device = nullptr
private

◆ _function_host

template<typename Object >
const Object& Moose::Kokkos::FunctionWrapperHost< Object >::_function_host
private

Reference of the function on host.

Definition at line 159 of file KokkosFunctionWrapper.h.

Referenced by Moose::Kokkos::FunctionWrapperHost< Object >::copyFunction().


The documentation for this class was generated from the following file: