Loading...
Searching...
No Matches
local_matrix_storage.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <Kokkos_UnorderedMap.hpp>
5
7#include "dense/vec.hpp"
11#include "grid/grid_types.hpp"
13#include "linalg/operator.hpp"
14#include "linalg/vector.hpp"
15#include "linalg/vector_q1.hpp"
16
19
20namespace terra::linalg::solvers {
21
26
27///
28template < typename ScalarT, int LocalMatrixDim >
30{
31 public:
32 using ScalarType = ScalarT;
33
35
36 private:
37 // coarsest grid boolean field for elements on which a GCA hierarchy has to be built
38 int level_range_;
41 Kokkos::View< dense::Mat< ScalarType, LocalMatrixDim, LocalMatrixDim >*, terra::grid::Layout >
42 local_matrices_selective_;
44 linalg::OperatorStoredMatrixMode operator_stored_matrix_mode_;
45 Kokkos::View< int > nMatrices_;
46 int capacity_;
47
48 public:
49 // default initialize storage without memory in GCAElements_ or local_matrices_
50 // necessary as optional stuff is not available or complicated on GPU
51 // (e.g. mem allocation for pointers, std::optional)
53
54 // ctor to be called in operators
56 // domain required for full storage size
58 // mode required to determine which storage type to read/write/allocate
59 linalg::OperatorStoredMatrixMode operator_stored_matrix_mode,
60
61 // only required for selective storage: level range for mapping from marked coarse elements
62 // to domain at hand
63 int level_range,
64 // marked coarse elements
66 : operator_stored_matrix_mode_( operator_stored_matrix_mode )
67 {
68 if ( operator_stored_matrix_mode == linalg::OperatorStoredMatrixMode::Selective )
69 {
70 level_range_ = level_range;
71 GCAElements_ = GCAElements;
72
73 // compute required capacity of map/selective storage
74 int nGCAElements = kernels::common::dot_product( GCAElements_, GCAElements_ );
75 capacity_ = 2 * nGCAElements * Kokkos::pow( 2, ( 3 * level_range_ ) );
76 std::cout << "Number of GCA coarse elements: " << nGCAElements << "/"
77 << GCAElements_.extent( 0 ) * ( GCAElements_.extent( 1 ) - 1 ) *
78 ( GCAElements_.extent( 2 ) - 1 ) * ( GCAElements_.extent( 2 ) - 1 )
79 << ", capacity: " << capacity_ << std::endl;
80 local_matrices_selective_ =
81 Kokkos::View< dense::Mat< ScalarType, LocalMatrixDim, LocalMatrixDim >*, terra::grid::Layout >(
82 "local_matrices_selective_", capacity_ );
83
84 // initialize indexing field
86 "indices_",
87 domain.subdomains().size(),
91 2 );
92
93 kernels::common::set_constant( indices_, -1 );
94
95 // init matrix counter
96 nMatrices_ = Kokkos::View< int >( "nMatrices_" );
97 Kokkos::deep_copy( nMatrices_, 0 );
98
99 //local_matrices_selective_ = Kokkos::UnorderedMap< SelectiveStorageKey, LocalMatrixType >( 10 * capacity );
100 }
101 else if ( operator_stored_matrix_mode == linalg::OperatorStoredMatrixMode::Full )
102 {
104 "local_matrices_full",
105 domain.subdomains().size(),
109 }
110 else
111 {
112 Kokkos::abort( "LocalMatrixStorage() not implemented." );
113 }
114 }
115
116 /// @brief Set the local matrix stored in the operator
117 KOKKOS_INLINE_FUNCTION
119 const int local_subdomain_id,
120 const int x_cell,
121 const int y_cell,
122 const int r_cell,
123 const int wedge,
125 {
126 if ( operator_stored_matrix_mode_ == linalg::OperatorStoredMatrixMode::Full )
127 {
128 for ( int i = 0; i < LocalMatrixDim; ++i )
129 {
130 for ( int j = 0; j < LocalMatrixDim; ++j )
131 {
132 local_matrices_full_( local_subdomain_id, x_cell, y_cell, r_cell, wedge )( i, j ) = mat( i, j );
133 }
134 }
135 }
136 else if ( operator_stored_matrix_mode_ == linalg::OperatorStoredMatrixMode::Selective )
137 {
138 // access map
139 if ( indices_( local_subdomain_id, x_cell, y_cell, r_cell, wedge ) == -1 )
140 {
141 // matrix at that spatial index not written:
142 // assign current linear index + increment linear index atomically
143 indices_( local_subdomain_id, x_cell, y_cell, r_cell, wedge ) =
144 Kokkos::atomic_fetch_add( &nMatrices_(), 1 );
145 }
146 else
147 {
148 Kokkos::abort( "Trying to write matrix that is already present." );
149 }
150 if ( nMatrices_() >= capacity_ + 1 )
151 {
152 Kokkos::abort( "Too many matrices to store." );
153 }
154
155 for ( int i = 0; i < LocalMatrixDim; ++i )
156 {
157 for ( int j = 0; j < LocalMatrixDim; ++j )
158 {
159 // write matrix to linearized position in local_matrices_selective_
160 local_matrices_selective_( indices_( local_subdomain_id, x_cell, y_cell, r_cell, wedge ) )( i, j ) =
161 mat( i, j );
162 }
163 }
164 }
165 else
166 {
167 Kokkos::abort( "set_matrix() not implemented." );
168 }
169 }
170
171 /// @brief Retrives the local matrix
172 /// if there is stored local matrices, the desired local matrix is loaded and returned
173 /// if not, the local matrix is assembled on-the-fly
174 KOKKOS_INLINE_FUNCTION
176 const int local_subdomain_id,
177 const int x_cell,
178 const int y_cell,
179 const int r_cell,
180 const int wedge ) const
181 {
183 if ( operator_stored_matrix_mode_ == linalg::OperatorStoredMatrixMode::Full )
184 {
185 for ( int i = 0; i < LocalMatrixDim; ++i )
186 {
187 for ( int j = 0; j < LocalMatrixDim; ++j )
188 {
189 ijslice( i, j ) = local_matrices_full_( local_subdomain_id, x_cell, y_cell, r_cell, wedge )( i, j );
190 }
191 }
192 }
193 else if ( operator_stored_matrix_mode_ == linalg::OperatorStoredMatrixMode::Selective )
194 {
195 // access map
196 if ( indices_( local_subdomain_id, x_cell, y_cell, r_cell, wedge ) == -1 )
197 {
198 Kokkos::abort( "Matrix not found." );
199 }
200 if ( indices_( local_subdomain_id, x_cell, y_cell, r_cell, wedge ) >= capacity_ )
201 {
202 Kokkos::abort( "Too many matrices." );
203 }
204
205 for ( int i = 0; i < LocalMatrixDim; ++i )
206 {
207 for ( int j = 0; j < LocalMatrixDim; ++j )
208 {
209 ijslice( i, j ) = local_matrices_selective_(
210 indices_( local_subdomain_id, x_cell, y_cell, r_cell, wedge ) )( i, j );
211 }
212 }
213 }
214 else
215 {
216 Kokkos::abort( "get_matrix() not implemented." );
217 }
218 return ijslice;
219 }
220
221 /// @brief Checks for presence of a local matrix for a certain element
222 KOKKOS_INLINE_FUNCTION
224 const int local_subdomain_id,
225 const int x_cell,
226 const int y_cell,
227 const int r_cell,
228 const int wedge ) const
229 {
230 if ( operator_stored_matrix_mode_ == linalg::OperatorStoredMatrixMode::Full )
231 {
232 return true;
233 }
234 else if ( operator_stored_matrix_mode_ == linalg::OperatorStoredMatrixMode::Selective )
235 {
236 return indices_( local_subdomain_id, x_cell, y_cell, r_cell, wedge ) != -1;
237 }
238 else
239 {
240 Kokkos::abort( "This should not happen." );
241 }
242 }
243};
244} // namespace terra::linalg::solvers
Parallel data structure organizing the thick spherical shell metadata for distributed (MPI parallel) ...
Definition spherical_shell.hpp:2498
const std::map< SubdomainInfo, std::tuple< LocalSubdomainIdx, SubdomainNeighborhood > > & subdomains() const
Definition spherical_shell.hpp:2580
const DomainInfo & domain_info() const
Returns a const reference.
Definition spherical_shell.hpp:2577
int subdomain_num_nodes_radially() const
Equivalent to calling subdomain_num_nodes_radially( subdomain_refinement_level() )
Definition spherical_shell.hpp:861
int subdomain_num_nodes_per_side_laterally() const
Equivalent to calling subdomain_num_nodes_per_side_laterally( subdomain_refinement_level() )
Definition spherical_shell.hpp:852
Definition local_matrix_storage.hpp:30
bool has_matrix(const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell, const int wedge) const
Checks for presence of a local matrix for a certain element.
Definition local_matrix_storage.hpp:223
ScalarT ScalarType
Definition local_matrix_storage.hpp:32
LocalMatrixStorage(grid::shell::DistributedDomain domain, linalg::OperatorStoredMatrixMode operator_stored_matrix_mode, int level_range, grid::Grid4DDataScalar< ScalarType > GCAElements)
Definition local_matrix_storage.hpp:55
dense::Mat< ScalarT, LocalMatrixDim, LocalMatrixDim > get_matrix(const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell, const int wedge) const
Retrives the local matrix if there is stored local matrices, the desired local matrix is loaded and r...
Definition local_matrix_storage.hpp:175
LocalMatrixStorage()
Definition local_matrix_storage.hpp:52
void set_matrix(const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell, const int wedge, dense::Mat< ScalarT, LocalMatrixDim, LocalMatrixDim > mat) const
Set the local matrix stored in the operator.
Definition local_matrix_storage.hpp:118
constexpr int num_wedges_per_hex_cell
Definition kernel_helpers.hpp:5
constexpr int num_nodes_per_wedge
Definition kernel_helpers.hpp:7
Kokkos::View< ScalarType *****, Layout > Grid5DDataScalar
Definition grid_types.hpp:28
Kokkos::View< dense::Mat< ScalarType, Rows, Cols > ****[NumMatrices], Layout > Grid4DDataMatrices
Definition grid_types.hpp:46
Kokkos::LayoutRight Layout
Definition grid_types.hpp:10
Kokkos::View< ScalarType ****, Layout > Grid4DDataScalar
Definition grid_types.hpp:25
ScalarType dot_product(const grid::Grid4DDataScalar< ScalarType > &x, const grid::Grid4DDataScalar< ScalarType > &y)
Definition grid_operations.hpp:668
void set_constant(const grid::Grid2DDataScalar< ScalarType > &x, ScalarType value)
Definition grid_operations.hpp:12
Definition block_preconditioner_2x2.hpp:7
OperatorStoredMatrixMode
Modes for applying stored matrices.
Definition operator.hpp:47
@ Selective
Use stored matrices on selected, marked elements only, assemble on all others.
@ Full
Use stored matrices on all elements.
Definition mat.hpp:10
Definition local_matrix_storage.hpp:23
int local_subdomain_id
Definition local_matrix_storage.hpp:24
int wedge
Definition local_matrix_storage.hpp:24
int y_cell
Definition local_matrix_storage.hpp:24
int r_cell
Definition local_matrix_storage.hpp:24
int x_cell
Definition local_matrix_storage.hpp:24