-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Kokkos Function System #31653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Kokkos Function System #31653
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7302a2b
Make FunctionBase class #30655
NamjaeChoi b1c9f5a
Kokkos functor and function base classes #30655
NamjaeChoi 052abc0
Refactor PiecewiseTabularBase #30655
NamjaeChoi 20807f6
Add Kokkos functions to the problem #30655
NamjaeChoi 645e5c9
Kokkos function examples #30655
NamjaeChoi 31ae89b
Update documentation #30655
NamjaeChoi b6f88ea
Add tests #30655
NamjaeChoi 33fca55
Temporarily disable retrieving abstract Kokkos function for GPU #30655
NamjaeChoi e961bd0
Add a device linking step to be prepared for RDC #30655
NamjaeChoi 44ef5bf
Add a unit test that shows virtual function call works on GPU #30655
NamjaeChoi 51802dc
Address comments #30655
NamjaeChoi 13b9bb2
Address comments 2 #30655
NamjaeChoi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
framework/doc/content/source/kokkos/actions/AddKokkosFunctionAction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # AddKokkosFunctionAction | ||
|
|
||
| !if! function=hasCapability('kokkos') | ||
|
|
||
| !syntax description /KokkosFunctions/AddKokkosFunctionAction | ||
|
|
||
| Kokkos `Functions` are specified as an object inside the `[KokkosFunctions]` block. This action adds them to the [Problem](syntax/Problem/index.md). | ||
|
|
||
| More information about Kokkos `Functions` can be found on the [Kokkos Functions syntax documentation](syntax/KokkosFunctions/index.md). | ||
|
|
||
| !syntax parameters /KokkosFunctions/AddKokkosFunctionAction | ||
|
|
||
| !if-end! | ||
|
|
||
| !else | ||
| !include kokkos/kokkos_warning.md |
2 changes: 1 addition & 1 deletion
2
framework/doc/content/source/kokkos/actions/AddKokkosKernelAction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # AddKokkosKokkosKernelAction | ||
| # AddKokkosKernelAction | ||
|
|
||
| !if! function=hasCapability('kokkos') | ||
|
|
||
|
|
||
18 changes: 18 additions & 0 deletions
18
framework/doc/content/source/kokkos/auxkernels/KokkosFunctionAux.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # KokkosFunctionAux | ||
|
|
||
| !if! function=hasCapability('kokkos') | ||
|
|
||
| This is the Kokkos version of [FunctionAux](FunctionAux.md). See the original document for details. | ||
|
|
||
| ## Example Syntax | ||
|
|
||
| !listing test/tests/kokkos/functions/piecewise_constant/kokkos_piecewise_constant.i block=KokkosAuxKernels | ||
|
|
||
| !syntax parameters /KokkosAuxKernels/KokkosFunctionAux | ||
|
|
||
| !syntax inputs /KokkosAuxKernels/KokkosFunctionAux | ||
|
|
||
| !if-end! | ||
|
|
||
| !else | ||
| !include kokkos/kokkos_warning.md |
18 changes: 18 additions & 0 deletions
18
framework/doc/content/source/kokkos/functions/KokkosConstantFunction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # KokkosConstantFunction | ||
|
|
||
| !if! function=hasCapability('kokkos') | ||
|
|
||
| This is the Kokkos version of [ConstantFunction](ConstantFunction.md). See the original document for details. | ||
|
|
||
| ## Example Syntax | ||
|
|
||
| !listing test/tests/kokkos/functions/constant_function/kokkos_constant_function.i block=KokkosFunctions | ||
|
|
||
| !syntax parameters /KokkosFunctions/KokkosConstantFunction | ||
|
|
||
| !syntax inputs /KokkosFunctions/KokkosConstantFunction | ||
|
|
||
| !if-end! | ||
|
|
||
| !else | ||
| !include kokkos/kokkos_warning.md |
18 changes: 18 additions & 0 deletions
18
framework/doc/content/source/kokkos/functions/KokkosPiecewiseConstant.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # KokkosPiecewiseConstant | ||
|
|
||
| !if! function=hasCapability('kokkos') | ||
|
|
||
| This is the Kokkos version of [PiecewiseConstant](PiecewiseConstant.md). See the original document for details. | ||
|
|
||
| ## Example Syntax | ||
|
|
||
| !listing test/tests/kokkos/functions/piecewise_constant/kokkos_piecewise_constant.i block=KokkosFunctions | ||
|
|
||
| !syntax parameters /KokkosFunctions/KokkosPiecewiseConstant | ||
|
|
||
| !syntax inputs /KokkosFunctions/KokkosPiecewiseConstant | ||
|
|
||
| !if-end! | ||
|
|
||
| !else | ||
| !include kokkos/kokkos_warning.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Kokkos Functions System | ||
|
|
||
| !if! function=hasCapability('kokkos') | ||
|
|
||
| Before reading this documentation, consider reading the following materials first for a better understanding of this documentation: | ||
|
|
||
| - [Functions System](syntax/Functions/index.md) to understand the MOOSE function system, | ||
| - [Getting Started with Kokkos-MOOSE](syntax/Kokkos/index.md) to understand the programming practices for Kokkos-MOOSE, | ||
| - [Kokkos Kernels System](syntax/KokkosKernels/index.md) to understand the common design pattern of objects in Kokkos-MOOSE. | ||
|
|
||
| !alert note | ||
| Kokkos-MOOSE functions do not support automatic differention yet. | ||
|
|
||
| A Kokkos-MOOSE function can be created by subclassing `Moose::Kokkos::FunctionBase` (not `Moose::Kokkos::Function`), which should be registered with `registerKokkosFunction()` instead of `registerMooseObject()`. | ||
| The signatures of the hook methods are defined as follows: | ||
|
|
||
| ```cpp | ||
| KOKKOS_FUNCTION Real value(Real t, Real3 p) const; | ||
| KOKKOS_FUNCTION Real3 vectorValue(Real t, Real3 p) const; | ||
| KOKKOS_FUNCTION Real3 gradient(Real t, Real3 p) const; | ||
| KOKKOS_FUNCTION Real3 curl(Real t, Real3 p) const; | ||
| KOKKOS_FUNCTION Real div(Real t, Real3 p) const; | ||
| KOKKOS_FUNCTION Real timeDerivative(Real t, Real3 p) const; | ||
| KOKKOS_FUNCTION Real timeIntegral(Real t1, Real t2, Real3 p) const; | ||
| KOKKOS_FUNCTION Real integral() const; | ||
| KOKKOS_FUNCTION Real average() const; | ||
| ``` | ||
| As in other Kokkos-MOOSE objects, they should be defined in the derived class as +*inlined public*+ methods instead of virtual override. | ||
| It is not mandatory to define each hook method in the derived class, but any hook method that was not defined in the derived class should not be called. | ||
| See the following source codes of `KokkosPiecewiseConstant` for an example of a function: | ||
| !listing framework/include/kokkos/functions/KokkosPiecewiseConstant.h id=kokkos-piecewise-constant-header | ||
| caption=The `KokkosPiecewiseConstant` header file. | ||
| !listing framework/src/kokkos/functions/KokkosPiecewiseConstant.K id=kokkos-piecewise-constant-source language=cpp | ||
| caption=The `KokkosPiecewiseConstant` source file. | ||
| Functions can be acquired in your object by calling `getKokkosFunction<T>()`, where `T` should be your function type. | ||
| Namely, the actual type of the function should be known in advance. | ||
| This is the limitation of the current Kokkos-MOOSE function implementation which is being addressed, and more discussions can be found below. | ||
| It is recommended to store the acquired function in a `ReferenceWrapper` instance. | ||
| Otherwise, it should be stored as a copy, and it becomes your responsibility to maintain synchronization between the original function and the copy. | ||
| ## Use of Dynamic Polymorphism | ||
| Unlike most of other Kokkos-MOOSE objects, functions are pluggable objects that are to be used in other objects. | ||
| Therefore, it is desired to use functions type-agnostically in your object so that any type of function can be plugged in. | ||
| Such design requires dynamic polymorphism, which is represented by virtual functions. | ||
| Although using virtual functions on GPU can sacrifice performance due to the inability of code inlining and vtable lookup and is still advised to avoid if possible, it can be useful for avoiding code duplications and improving code maintainability. | ||
| In fact, Kokkos-MOOSE already has the code path for acquiring functions as a type-agnostic abstract type and using virtual dispatch to evaluate actual functions. | ||
| There is another non-template API `getKokkosFunction()` which returns an abstract type `Moose::Kokkos::Function`. | ||
| It is not the base class of your function but a wrapper class that contains your function and provides shims to call the function methods. | ||
| However, that code path is currently blocked for GPU backends, and you will hit a runtime error when you attempt to use it. | ||
| While our target GPU backends (CUDA, HIP, and Intel SYCL) support virtual functions on GPU, it requires the relocatable device code (RDC) option to be enabled during compilation, which the current MOOSE build system is lacking. | ||
| Kokkos imposes a restriction to have a consistent RDC configuration across the whole software stack, but PETSc, from which MOOSE is currently acquring Kokkos, requires significant changes in its build system to enable RDC. | ||
| The non-templated `getKokkosFunction()` code path is hypothetically functional under the assumption that the MOOSE software stack can be built with the RDC option, but this hypothetical will only become a reality after a PETSc build system rework. | ||
| The PETSc and MOOSE team are looking into this issue, and it is expected to be resolved in the foreseeable future. | ||
| !syntax list /KokkosFunctions objects=True actions=False subsystems=False | ||
| !if-end! | ||
| !else | ||
| !include kokkos/kokkos_warning.md | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //* This file is part of the MOOSE framework | ||
| //* https://mooseframework.inl.gov | ||
| //* | ||
| //* All rights reserved, see COPYRIGHT for full restrictions | ||
| //* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
| //* | ||
| //* Licensed under LGPL 2.1, please see LICENSE for details | ||
| //* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "MooseObject.h" | ||
| #include "SetupInterface.h" | ||
| #include "TransientInterface.h" | ||
| #include "PostprocessorInterface.h" | ||
| #include "UserObjectInterface.h" | ||
| #include "Restartable.h" | ||
| #include "MeshChangedInterface.h" | ||
| #include "ScalarCoupleable.h" | ||
|
|
||
| namespace Moose | ||
| { | ||
|
|
||
| class FunctionBase : public MooseObject, | ||
| public SetupInterface, | ||
| public TransientInterface, | ||
| public PostprocessorInterface, | ||
| public UserObjectInterface, | ||
| public Restartable, | ||
| public MeshChangedInterface, | ||
| public ScalarCoupleable | ||
| { | ||
| public: | ||
| static InputParameters validParams(); | ||
|
|
||
| FunctionBase(const InputParameters & parameters); | ||
|
|
||
| #ifdef MOOSE_KOKKOS_ENABLED | ||
| /** | ||
| * Special constructor used for Kokkos functor copy during parallel dispatch | ||
| */ | ||
| FunctionBase(const FunctionBase & object, const Moose::Kokkos::FunctorCopy & key); | ||
| #endif | ||
|
|
||
| virtual ~FunctionBase(); | ||
| }; | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.