libMesh
Loading...
Searching...
No Matches
kokkos_storage.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_H
19#define LIBMESH_KOKKOS_STORAGE_H
20
21// libMesh Kokkos storage helpers for dimension-aware vector/tensor views.
22//
23// The functions here convert between the two representations used by the
24// Kokkos numerics layer:
25//
26// - "storage": a Kokkos View holding many vectors or tensors, with the
27// leading (runtime) extent indexing the entry and the trailing
28// LIBMESH_DIM extents indexing components, and
29// - "values": individual owning objects (TypeVector, TypeTensor, Point,
30// ...) that kernels compute with in registers.
31//
32// load_*() reads one entry out of a view into an owning value;
33// store_*() writes an owning value back into one entry of a view. Both
34// are usable inside device kernels.
35
36#include "libmesh/kokkos_linalg_base.h"
37
38#include "libmesh/libmesh_common.h"
39#include "libmesh/libmesh_device.h"
40#include "libmesh/type_tensor.h"
41#include "libmesh/type_vector.h"
42
43namespace libMesh::Kokkos
44{
45
55template <typename VectorType, typename ViewType>
56LIBMESH_DEVICE_INLINE
57VectorType load_vector(const ViewType & view, const unsigned int i)
58{
59 return materialize_vector<VectorType>(make_vector_ref(view, i));
60}
61
66template <typename ViewType, typename VectorType>
67LIBMESH_DEVICE_INLINE
68void store_vector(const ViewType & view, const unsigned int i, const VectorType & v)
69{
70 auto out = make_vector_ref(view, i);
71
72 for (unsigned int d = 0; d < LIBMESH_DIM; ++d)
73 out(d) = v(d);
74}
75
85template <typename TensorType, typename ViewType>
86LIBMESH_DEVICE_INLINE
87TensorType load_tensor(const ViewType & view, const unsigned int i)
88{
89 return materialize_tensor<TensorType>(make_tensor_ref(view, i));
90}
91
96template <typename ViewType, typename TensorType>
97LIBMESH_DEVICE_INLINE
98void store_tensor(const ViewType & view, const unsigned int i, const TensorType & T)
99{
100 auto out = make_tensor_ref(view, i);
101
102 for (unsigned int row = 0; row < LIBMESH_DIM; ++row)
103 for (unsigned int col = 0; col < LIBMESH_DIM; ++col)
104 out(row, col) = T(row, col);
105}
106
107} // namespace libMesh::Kokkos
108
109#endif // LIBMESH_KOKKOS_STORAGE_H
LIBMESH_DEVICE_INLINE vector_ref< typename std::remove_reference< ViewType >::type > make_vector_ref(ViewType &&view, const unsigned int index)
LIBMESH_DEVICE_INLINE void store_vector(const ViewType &view, const unsigned int i, const VectorType &v)
Copies the components of the vector v into entry i of the vector view view, overwriting whatever that...
LIBMESH_DEVICE_INLINE VectorType load_vector(const ViewType &view, const unsigned int i)
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 > >
LIBMESH_DEVICE_INLINE tensor_ref< typename std::remove_reference< ViewType >::type > make_tensor_ref(ViewType &&view, const unsigned int index)
LIBMESH_DEVICE_INLINE TensorType load_tensor(const ViewType &view, const unsigned int i)
LIBMESH_DEVICE_INLINE void store_tensor(const ViewType &view, const unsigned int i, const TensorType &T)
Copies the components of the tensor T into entry i of the tensor view view, overwriting whatever that...
OStreamProxy out