https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KokkosFunctionWrapper.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#include "KokkosTypes.h"
13
14namespace Moose::Kokkos
15{
16
17template <typename Object>
18class FunctionWrapperHost;
19
24{
25public:
29 KOKKOS_FUNCTION FunctionWrapperDeviceBase() {}
33 KOKKOS_FUNCTION KOKKOS_VIRTUAL ~FunctionWrapperDeviceBase() {}
34
39 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real value(Real /* t */, Real3 /* p */) const
40 {
41 KOKKOS_ASSERT(false);
42 return 0;
43 }
44 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real3 vectorValue(Real /* t */, Real3 /* p */) const
45 {
46 KOKKOS_ASSERT(false);
47 return Real3(0);
48 }
49 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real3 gradient(Real /* t */, Real3 /* p */) const
50 {
51 KOKKOS_ASSERT(false);
52 return Real3(0);
53 }
54 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real3 curl(Real /* t */, Real3 /* p */) const
55 {
56 KOKKOS_ASSERT(false);
57 return Real3(0);
58 }
59 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real div(Real /* t */, Real3 /* p */) const
60 {
61 KOKKOS_ASSERT(false);
62 return 0;
63 }
64 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real timeDerivative(Real /* t */, Real3 /* p */) const
65 {
66 KOKKOS_ASSERT(false);
67 return 0;
68 }
69 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real timeIntegral(Real /* t1 */,
70 Real /* t2 */,
71 Real3 /* p */) const
72 {
73 KOKKOS_ASSERT(false);
74 return 0;
75 }
76 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real integral() const
77 {
78 KOKKOS_ASSERT(false);
79 return 0;
80 }
81 KOKKOS_FUNCTION KOKKOS_VIRTUAL Real average() const
82 {
83 KOKKOS_ASSERT(false);
84 return 0;
85 }
87};
88
95template <typename Object>
97{
98 friend class FunctionWrapperHost<Object>;
99
100public:
104 KOKKOS_FUNCTION FunctionWrapperDevice() {}
105
106 KOKKOS_FUNCTION Real value(Real t, Real3 p) const KOKKOS_OVERRIDE
107 {
108 return _function->value(t, p);
109 }
110 KOKKOS_FUNCTION Real3 vectorValue(Real t, Real3 p) const KOKKOS_OVERRIDE
111 {
112 return _function->vectorValue(t, p);
113 }
114 KOKKOS_FUNCTION Real3 gradient(Real t, Real3 p) const KOKKOS_OVERRIDE
115 {
116 return _function->gradient(t, p);
117 }
118 KOKKOS_FUNCTION Real3 curl(Real t, Real3 p) const KOKKOS_OVERRIDE
119 {
120 return _function->curl(t, p);
121 }
122 KOKKOS_FUNCTION Real div(Real t, Real3 p) const KOKKOS_OVERRIDE { return _function->div(t, p); }
123 KOKKOS_FUNCTION Real timeDerivative(Real t, Real3 p) const KOKKOS_OVERRIDE
124 {
125 return _function->timeDerivative(t, p);
126 }
127 KOKKOS_FUNCTION Real timeIntegral(Real t1, Real t2, Real3 p) const KOKKOS_OVERRIDE
128 {
129 return _function->timeIntegral(t1, t2, p);
130 }
131 KOKKOS_FUNCTION Real integral() const KOKKOS_OVERRIDE { return _function->integral(); }
132 KOKKOS_FUNCTION Real average() const KOKKOS_OVERRIDE { return _function->average(); }
133
134protected:
138 Object * _function = nullptr;
139};
140
145{
146public:
151
160 virtual void copyFunction() = 0;
164 virtual void freeFunction() = 0;
165};
166
173template <typename Object>
175{
176public:
181 FunctionWrapperHost(const void * function)
182 : _function_host(*static_cast<const Object *>(function))
183 {
184 }
189
190 FunctionWrapperDeviceBase * allocate() override final;
191 void copyFunction() override final;
192 void freeFunction() override final;
193
194private:
198 const Object & _function_host;
202 std::unique_ptr<Object> _function_copy;
206 Object * _function_device = nullptr;
207};
208
209template <typename Object>
212{
213 // Allocate storage for device wrapper on device
214 auto wrapper_device = static_cast<FunctionWrapperDevice<Object> *>(
215 ::Kokkos::kokkos_malloc<ExecSpace::memory_space>(sizeof(FunctionWrapperDevice<Object>)));
216
217 // Allocate device wrapper on device using placement new to populate vtable with device pointers
218 ::Kokkos::parallel_for(
219 1, KOKKOS_LAMBDA(const int) { new (wrapper_device) FunctionWrapperDevice<Object>(); });
220
221 // Allocate storage for function on device
223 static_cast<Object *>(::Kokkos::kokkos_malloc<ExecSpace::memory_space>(sizeof(Object)));
224
225 // Let device wrapper point to the copy
226 ::Kokkos::Impl::DeepCopy<MemSpace, ::Kokkos::HostSpace>(
227 &(wrapper_device->_function), &_function_device, sizeof(Object *));
228
229 return wrapper_device;
230}
231
232template <typename Object>
233void
235{
236 // Make a copy of function on host to trigger copy constructor
237 _function_copy = std::make_unique<Object>(_function_host);
238
239 // Copy function to device
240 ::Kokkos::Impl::DeepCopy<MemSpace, ::Kokkos::HostSpace>(
241 _function_device, _function_copy.get(), sizeof(Object));
242}
243
244template <typename Object>
245void
250
251template <typename Object>
253{
254 ::Kokkos::kokkos_free<ExecSpace::memory_space>(_function_device);
255}
256
257} // namespace Moose::Kokkos
Base class for device function wrapper.
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real3 gradient(Real, Real3) const
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real value(Real, Real3) const
Virtual shims that calls the corresponding methods of the actual stored function.
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real average() const
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real timeIntegral(Real, Real, Real3) const
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real3 vectorValue(Real, Real3) const
KOKKOS_FUNCTION KOKKOS_VIRTUAL ~FunctionWrapperDeviceBase()
Virtual destructor.
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real timeDerivative(Real, Real3) const
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real div(Real, Real3) const
KOKKOS_FUNCTION FunctionWrapperDeviceBase()
Constructor.
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real3 curl(Real, Real3) const
KOKKOS_FUNCTION KOKKOS_VIRTUAL Real integral() const
Device function wrapper class that provides polymorphic interfaces for a function.
KOKKOS_FUNCTION Real3 gradient(Real t, Real3 p) const KOKKOS_OVERRIDE
KOKKOS_FUNCTION Real3 vectorValue(Real t, Real3 p) const KOKKOS_OVERRIDE
KOKKOS_FUNCTION Real average() const KOKKOS_OVERRIDE
KOKKOS_FUNCTION Real div(Real t, Real3 p) const KOKKOS_OVERRIDE
KOKKOS_FUNCTION Real3 curl(Real t, Real3 p) const KOKKOS_OVERRIDE
KOKKOS_FUNCTION FunctionWrapperDevice()
Constructor.
KOKKOS_FUNCTION Real value(Real t, Real3 p) const KOKKOS_OVERRIDE
Object * _function
Pointer to the function on device.
KOKKOS_FUNCTION Real timeDerivative(Real t, Real3 p) const KOKKOS_OVERRIDE
KOKKOS_FUNCTION Real timeIntegral(Real t1, Real t2, Real3 p) const KOKKOS_OVERRIDE
KOKKOS_FUNCTION Real integral() const KOKKOS_OVERRIDE
Base class for host function wrapper.
virtual ~FunctionWrapperHostBase()
Virtual destructor.
virtual void freeFunction()=0
Free host and device copies of function.
virtual FunctionWrapperDeviceBase * allocate()=0
Allocate device function and wrapper.
virtual void copyFunction()=0
Copy function to device.
Host function wrapper class that allocates a function on device and creates its device wrapper.
FunctionWrapperDeviceBase * allocate() override final
Allocate device function and wrapper.
FunctionWrapperHost(const void *function)
Constructor.
const Object & _function_host
Reference of the function on host.
void freeFunction() override final
Free host and device copies of function.
std::unique_ptr< Object > _function_copy
Copy of the function on host.
void copyFunction() override final
Copy function to device.
Object * _function_device
Copy of the function on device.
Vector3< Real > Real3
Definition KokkosTypes.h:32