libMesh
Loading...
Searching...
No Matches
kokkos_storage_policy.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18#ifndef LIBMESH_KOKKOS_STORAGE_POLICY_H
19#define LIBMESH_KOKKOS_STORAGE_POLICY_H
20
21// libMesh Kokkos compile-time storage policies for fixed-dimension linalg data.
22//
23// These policies keep storage selection separate from the linalg algorithms:
24// kernels operate on refs/materialized values, while the backend policy chooses
25// the underlying Kokkos view layout. Code written against a policy's
26// vector_view/tensor_view typedefs can be instantiated with different memory
27// layouts (or, in the future, different scalar types) without changing the
28// math it performs.
29
30#include "libmesh/libmesh_common.h"
31
32#define PETSC_SKIP_CXX_COMPLEX_FIX 1
33#include "libmesh/ignore_warnings.h"
34#include <Kokkos_Core.hpp>
35#include "libmesh/restore_warnings.h"
36#undef __CUDACC_VER__
37
38#include <string>
39#include <type_traits>
40#include <vector>
41
42namespace libMesh::Kokkos
43{
44
54template <typename Scalar, typename Layout>
56{
57 using scalar_type = Scalar;
58 using layout_type = Layout;
59
64 using vector_view = ::Kokkos::View<scalar_type * [LIBMESH_DIM], layout_type>;
65
71 using tensor_view = ::Kokkos::View<scalar_type * [LIBMESH_DIM][LIBMESH_DIM], layout_type>;
72
77 static constexpr const char *
79 {
80 return std::is_same<layout_type, ::Kokkos::LayoutLeft>::value ? "layoutleft" :
81 std::is_same<layout_type, ::Kokkos::LayoutRight>::value ? "layoutright" :
82 "layoutcustom";
83 }
84};
85
101
108template <typename StoragePolicy>
109constexpr const char *
111{
112 return StoragePolicy::name();
113}
114
120template <typename StoragePolicy>
121inline typename StoragePolicy::vector_view
122make_vector_storage(const char * label, const std::size_t n)
123{
124 return typename StoragePolicy::vector_view(std::string(label), n);
125}
126
132make_vector_storage(const char * label, const std::size_t n)
133{
134 return make_vector_storage<default_storage_policy>(label, n);
135}
136
142template <typename StoragePolicy>
143inline typename StoragePolicy::tensor_view
144make_tensor_storage(const char * label, const std::size_t n)
145{
146 return typename StoragePolicy::tensor_view(std::string(label), n);
147}
148
154make_tensor_storage(const char * label, const std::size_t n)
155{
156 return make_tensor_storage<default_storage_policy>(label, n);
157}
158
171template <typename StoragePolicy, typename VectorType>
172inline typename StoragePolicy::vector_view
173upload_vector_storage(const std::vector<VectorType> & values, const char * label)
174{
175 auto d = make_vector_storage<StoragePolicy>(label, values.size());
176 auto h = ::Kokkos::create_mirror_view(d);
177
178 for (std::size_t i = 0; i < values.size(); ++i)
179 for (unsigned int component = 0; component < LIBMESH_DIM; ++component)
180 h(i, component) = values[i](component);
181
182 ::Kokkos::deep_copy(d, h);
183 return d;
184}
185
190template <typename VectorType>
192upload_vector_storage(const std::vector<VectorType> & values, const char * label)
193{
194 return upload_vector_storage<default_storage_policy>(values, label);
195}
196
209template <typename StoragePolicy, typename TensorType>
210inline typename StoragePolicy::tensor_view
211upload_tensor_storage(const std::vector<TensorType> & values, const char * label)
212{
213 auto d = make_tensor_storage<StoragePolicy>(label, values.size());
214 auto h = ::Kokkos::create_mirror_view(d);
215
216 for (std::size_t i = 0; i < values.size(); ++i)
217 for (unsigned int row = 0; row < LIBMESH_DIM; ++row)
218 for (unsigned int col = 0; col < LIBMESH_DIM; ++col)
219 h(i, row, col) = values[i](row, col);
220
221 ::Kokkos::deep_copy(d, h);
222 return d;
223}
224
229template <typename TensorType>
231upload_tensor_storage(const std::vector<TensorType> & values, const char * label)
232{
233 return upload_tensor_storage<default_storage_policy>(values, label);
234}
235
236} // namespace libMesh::Kokkos
237
238#endif // LIBMESH_KOKKOS_STORAGE_POLICY_H
constexpr const char * storage_policy_name()
StoragePolicy::tensor_view upload_tensor_storage(const std::vector< TensorType > &values, const char *label)
Allocates a device view sized for values, copies each tensor's components into a host mirror,...
StoragePolicy::tensor_view make_tensor_storage(const char *label, const std::size_t n)
LIBMESH_DEVICE_INLINE auto row(const TensorLike &T_in, const unsigned int i) -> std::enable_if_t< is_tensor_like_v< TensorLike >, std::conditional_t< std::is_void< ResultVector >::value, libMesh::TypeVector< typename TensorLike::value_type >, ResultVector > >
StoragePolicy::vector_view upload_vector_storage(const std::vector< VectorType > &values, const char *label)
Allocates a device view sized for values, copies each vector's components into a host mirror,...
static_dim_storage_policy< libMesh::Real, ::Kokkos::LayoutRight > layout_right_storage_policy
StoragePolicy::vector_view make_vector_storage(const char *label, const std::size_t n)
Storage policy describing Kokkos views that hold runtime-many vectors or tensors whose per-entry dime...
::Kokkos::View< scalar_type *[LIBMESH_DIM][LIBMESH_DIM], layout_type > tensor_view
View type holding n tensors: the runtime extent indexes the entry, the static LIBMESH_DIM x LIBMESH_D...
::Kokkos::View< scalar_type *[LIBMESH_DIM], layout_type > vector_view
View type holding n vectors: the runtime extent indexes the entry, the static LIBMESH_DIM extent inde...