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 18 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 140 of file KokkosFunctionWrapper.h.

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

◆ ~FunctionWrapperHost()

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

Destructor.

Definition at line 211 of file KokkosFunctionWrapper.h.

212 {
213  ::Kokkos::kokkos_free<ExecSpace::memory_space>(_function_device);
214 }
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 170 of file KokkosFunctionWrapper.h.

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

194 {
195  // Make a copy of function on host to trigger copy constructor
196  _function_copy = std::make_unique<Object>(_function_host);
197 
198  // Copy function to device
199  ::Kokkos::Impl::DeepCopy<MemSpace, ::Kokkos::HostSpace>(
200  _function_device, _function_copy.get(), sizeof(Object));
201 }
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 205 of file KokkosFunctionWrapper.h.

206 {
207  _function_copy.reset();
208 }
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 157 of file KokkosFunctionWrapper.h.

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


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