Advanced Multi-Physics (AMP)
On-Line Documentation
Util.h
Go to the documentation of this file.
1#ifndef included_AMP_AMG_UTIL
2#define included_AMP_AMG_UTIL
3
4#include <limits>
5#include <map>
6#include <memory>
7#include <vector>
8
9#include "AMP/matrices/CSRMatrix.h"
10#include "AMP/matrices/data/CSRMatrixData.h"
11#include "AMP/operators/LinearOperator.h"
12#include "AMP/operators/OperatorParameters.h"
13
14namespace AMP::Solver::AMG {
15
16constexpr inline std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
17template<std::size_t E>
19 explicit extent_storage( std::size_t ) {}
20 [[nodiscard]] constexpr std::size_t value() const { return E; }
21};
22template<>
24 [[nodiscard]] constexpr std::size_t value() const { return e; }
25 std::size_t e;
26};
27
28
29template<class T, std::size_t Extent = dynamic_extent>
30struct span {
31 using element_type = T;
32 using value_type = typename std::remove_cv_t<T>;
33 using size_type = std::size_t;
34 using difference_type = std::ptrdiff_t;
35 using pointer = T *;
36 using const_pointer = const T *;
37 using reference = T &;
38 using const_reference = const T &;
39 using iterator = T *;
40 using reverse_iterator = std::reverse_iterator<iterator>;
41
42 static constexpr std::size_t extent = Extent;
43
44
45 template<std::size_t E = Extent,
46 class = typename std::enable_if_t<E == dynamic_extent || E == 0>>
47 span() : b( nullptr ), ext{ 0 }
48 {
49 }
50
51 constexpr span( pointer p, size_type s ) : b( p ), ext{ s } {}
52
53 constexpr iterator begin() const noexcept { return b; }
54
55 constexpr iterator end() const noexcept { return b + size(); }
56
57 constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator( end() ); }
58
59 constexpr reverse_iterator rend() const noexcept { return reverse_iterator( begin() ); }
60
61 constexpr reference front() const { return *b; }
62
63 constexpr reference back() const { return *( b + ( size() - 1 ) ); }
64
65 constexpr reference operator[]( size_type idx ) const { return begin()[idx]; }
66
67 constexpr pointer data() const noexcept { return b; }
68
69 [[nodiscard]] constexpr size_type size() const noexcept { return ext.value(); }
70
71 [[nodiscard]] constexpr size_type size_bytes() const noexcept { return sizeof( T ) * size(); }
72
73 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
74
75 template<size_type Count>
76 constexpr span<T, Count> first() const noexcept
77 {
78 return { b, Count };
79 }
80
81 constexpr span<T, dynamic_extent> first( size_type count ) const noexcept
82 {
83 return { data(), count };
84 }
85
86 template<size_type Count>
87 constexpr span<T, Count> last() const noexcept
88 {
89 return { data() + ( size() - Count ), Count };
90 }
91
92 constexpr span<T, dynamic_extent> last( size_type count ) const noexcept
93 {
94 return { data() + ( size() - count ), count };
95 }
96
97 constexpr span<T, dynamic_extent> subspan( size_t offset,
98 size_t count = dynamic_extent ) const noexcept
99 {
100 if ( offset >= size() )
101 return { nullptr, 0 };
102 if ( count == dynamic_extent )
103 count = size() - offset;
104 AMP_INSIST( offset + count <= size(), "span: invalid subset" );
105 return { data() + offset, count };
106 }
107
108protected:
111};
112
113template<class A, class T>
114using rebind_alloc = typename std::allocator_traits<A>::template rebind_alloc<T>;
115
116template<class Config, class ColID>
117struct seq_csr {
118 using allocator_type = typename Config::allocator_type;
119 using csr_policy = Config;
120 using col_idx_t = ColID;
121 using lidx_t = typename Config::lidx_t;
122 using scalar_t = typename Config::scalar_t;
123 template<class T>
124 using vector_type = std::vector<T, rebind_alloc<allocator_type, T>>;
125
129};
130
131template<class Config, class ColID = typename Config::lidx_t>
132struct par_csr {
133 using allocator_type = typename Config::allocator_type;
134 using csr_policy = Config;
135 using lidx_t = typename Config::lidx_t;
136 using scalar_t = typename Config::scalar_t;
138
139 par_csr() : d_diag{ std::make_shared<seq_type>() }, d_offd{ std::make_shared<seq_type>() } {}
140
141 seq_type &diag() { return *d_diag; }
142 seq_type &offd() { return *d_offd; }
143
144 const seq_type &diag() const { return *d_diag; }
145 const seq_type &offd() const { return *d_offd; }
146
147 bool has_offd() const { return offd().rowptr.size() > 0; }
148
149protected:
150 std::shared_ptr<seq_type> d_diag, d_offd;
151};
152
153
154template<class Config>
156 using gidx_t = typename Config::gidx_t;
158
160 std::array<gidx_t, 2> diag_extents;
161 std::shared_ptr<LinearAlgebra::Variable> right_var, left_var;
163};
164
165template<class C>
171
172template<class Config>
175 explicit coarse_operator( std::shared_ptr<AMP::Operator::OperatorParameters> params )
176 : AMP::Operator::LinearOperator( params )
177 {
178 auto cop = std::dynamic_pointer_cast<coarse_operator_parameters<Config>>( params );
180 storage = cop->matrix.store;
181 setMatrix( create_matrix( cop->matrix.comm,
182 cop->matrix.diag_extents,
183 cop->matrix.left_var,
184 cop->matrix.right_var ) );
185 setVariables( cop->matrix.left_var, cop->matrix.right_var );
186 }
187
188 std::shared_ptr<LinearAlgebra::CSRMatrix<Config>>
189 create_matrix( const AMP_MPI &comm,
190 const std::array<typename Config::gidx_t, 2> diag_extents,
191 std::shared_ptr<LinearAlgebra::Variable> left_var,
192 std::shared_ptr<LinearAlgebra::Variable> right_var )
193 {
194 using seq_type = typename parcsr_t::seq_type;
195 auto make_params = []( seq_type &in ) {
197 params;
198 params.d_row_starts = in.rowptr.data();
199 params.d_cols = in.colind.data();
200 params.d_coeffs = in.values.data();
201 return params;
202 };
203
204 auto [diag_params, offd_params] = [&]( auto &...smats ) {
205 return std::tuple( make_params( smats )... );
206 }( storage.diag(), storage.offd() );
207
208 auto params =
209 std::make_shared<LinearAlgebra::RawCSRMatrixParameters<Config>>( diag_extents[0],
210 diag_extents[1],
211 diag_extents[0],
212 diag_extents[1],
215 comm,
216 left_var,
217 right_var );
218
219 return std::make_shared<LinearAlgebra::CSRMatrix<Config>>( params );
220 }
221
222protected:
224};
225
226
227template<class Mat>
228struct csr_view {
229};
230
231template<class Config>
232struct csr_view<LinearAlgebra::CSRMatrix<Config>> {
233 using allocator_type = typename Config::allocator_type;
234 using csr_policy = Config;
237 using reference = const value_type &;
238 using pointer = const value_type *;
239
241 using lidx_t = typename csr_policy::lidx_t;
242 using gidx_t = typename csr_policy::gidx_t;
243 using scalar_t = typename csr_policy::scalar_t;
244
246 typename std::tuple<span<const lidx_t>, span<const lidx_t>, span<const scalar_t>>;
247
248 explicit csr_view( reference p ) : ptr( &p ) {}
249
250 auto diag() const { return csr_ptrs( *( data().getDiagMatrix() ) ); }
251
252 auto offd() const { return csr_ptrs( *( data().getOffdMatrix() ) ); }
253
254 [[nodiscard]] bool has_offd() const { return data().hasOffDiag(); }
255
256 [[nodiscard]] size_t numLocalRows() const { return ptr->numLocalRows(); }
257
258 [[nodiscard]] size_t numGlobalRows() const { return ptr->numGlobalRows(); }
259
260 [[nodiscard]] size_t numGhosts() const { return data().getOffdMatrix()->numUniqueColumns(); }
261
262 void getGhostValues( const LinearAlgebra::Vector &vec, scalar_t *dst ) const
263 {
264 auto &offd_mat = *( data().getOffdMatrix() );
265 if constexpr ( std::is_same_v<size_t, gidx_t> ) {
266 size_t *colmap = offd_mat.getColumnMap();
267 vec.getGhostValuesByGlobalID( numGhosts(), colmap, dst );
268 } else {
269 std::vector<size_t> colmap;
270 offd_mat.getColumnMap( colmap );
271 vec.getGhostValuesByGlobalID( numGhosts(), colmap.data(), dst );
272 }
273 }
274
275 const auto &data() const
276 {
277 auto data = std::dynamic_pointer_cast<const csr_data_type>( ptr->getMatrixData() );
278 AMP_DEBUG_ASSERT( data );
279 return *data;
280 }
281
282protected:
283 template<class T>
284 csr_ptrs_t csr_ptrs( const T &local_data ) const
285 {
286 auto nrows = local_data.numLocalRows();
287 auto nnz = local_data.numberOfNonZeros();
288 auto [rowptr, ignore, colind, values] = local_data.getDataFields();
289 return std::make_tuple( span<const lidx_t>{ rowptr, static_cast<size_t>( nrows + 1 ) },
290 span<const lidx_t>{ colind, static_cast<size_t>( nnz ) },
291 span<const scalar_t>{ values, static_cast<size_t>( nnz ) } );
292 }
293
295};
296template<class Config>
298
299
300template<class Config, class ColID>
301struct csr_view<par_csr<Config, ColID>> {
302 using allocator_type = typename Config::allocator_type;
303 using csr_policy = Config;
306 using lidx_t = typename Config::lidx_t;
307 using scalar_t = typename Config::scalar_t;
310 typename std::tuple<span<const lidx_t>, span<const lidx_t>, span<const scalar_t>>;
311
312 csr_view( value_type val ) : data{ val } {}
313
314 auto diag() const { return csr_ptrs( data.diag() ); }
315
316 auto offd() const { return csr_ptrs( data.offd() ); }
317
318 [[nodiscard]] bool has_offd() const { return data.has_offd(); }
319
320 [[nodiscard]] size_t numLocalRows() const { return data.diag().rowptr.size() - 1; }
321
322private:
323 csr_ptrs_t csr_ptrs( const typename value_type::seq_type &v ) const
324 {
325 return std::make_tuple( span<const lidx_t>( v.rowptr.data(), v.rowptr.size() ),
326 span<const ColID>( v.colind.data(), v.colind.size() ),
327 span<const scalar_t>( v.values.data(), v.values.size() ) );
328 }
329
331};
332template<class Config, class ColID>
334
335} // namespace AMP::Solver::AMG
336
337#endif
Provides C++ wrapper around MPI routines.
Definition AMP_MPI.h:63
typename localmatrixdata_t::mask_t mask_t
An concrete class for dealing with dense serial matrices.
Definition CSRMatrix.h:26
Abstraction of a discrete Vector in a linear simulation.
Definition Vector.h:54
void getGhostValuesByGlobalID(int num, const size_t *indices, TYPE *vals) const
Definition Vector.h:748
virtual void setMatrix(std::shared_ptr< AMP::LinearAlgebra::Matrix > in_mat)
LinearOperator()
Empty constructor.
virtual void setVariables(std::shared_ptr< AMP::LinearAlgebra::Variable > in, std::shared_ptr< AMP::LinearAlgebra::Variable > out)
OperatorParameters(std::shared_ptr< AMP::Database > db, std::shared_ptr< AMP::Mesh::Mesh > mesh=nullptr)
Operator(void)
Default constructor.
std::shared_ptr< AMP::Operator::Operator > shared_ptr
Definition Operator.h:29
std::shared_ptr< ParameterBase > shared_ptr
#define AMP_DEBUG_ASSERT(EXP)
Assert error (debug only)
#define AMP_INSIST(EXP, MSG)
Insist error.
constexpr std::size_t dynamic_extent
Definition Util.h:16
typename std::allocator_traits< A >::template rebind_alloc< T > rebind_alloc
Definition Util.h:114
typename Config::gidx_t gidx_t
Definition Util.h:156
std::shared_ptr< LinearAlgebra::Variable > right_var
Definition Util.h:161
std::shared_ptr< LinearAlgebra::Variable > left_var
Definition Util.h:161
std::array< gidx_t, 2 > diag_extents
Definition Util.h:160
coarse_operator(std::shared_ptr< AMP::Operator::OperatorParameters > params)
Definition Util.h:175
std::shared_ptr< LinearAlgebra::CSRMatrix< Config > > create_matrix(const AMP_MPI &comm, const std::array< typename Config::gidx_t, 2 > diag_extents, std::shared_ptr< LinearAlgebra::Variable > left_var, std::shared_ptr< LinearAlgebra::Variable > right_var)
Definition Util.h:189
void getGhostValues(const LinearAlgebra::Vector &vec, scalar_t *dst) const
Definition Util.h:262
typename std::tuple< span< const lidx_t >, span< const lidx_t >, span< const scalar_t > > csr_ptrs_t
Definition Util.h:246
csr_ptrs_t csr_ptrs(const T &local_data) const
Definition Util.h:284
typename std::tuple< span< const lidx_t >, span< const lidx_t >, span< const scalar_t > > csr_ptrs_t
Definition Util.h:310
typename csr_data_type::mask_t mask_t
Definition Util.h:305
typename Config::allocator_type allocator_type
Definition Util.h:302
csr_ptrs_t csr_ptrs(const typename value_type::seq_type &v) const
Definition Util.h:323
constexpr std::size_t value() const
Definition Util.h:24
extent_storage(std::size_t)
Definition Util.h:19
constexpr std::size_t value() const
Definition Util.h:20
typename Config::allocator_type allocator_type
Definition Util.h:133
bool has_offd() const
Definition Util.h:147
const seq_type & diag() const
Definition Util.h:144
std::shared_ptr< seq_type > d_diag
Definition Util.h:150
std::shared_ptr< seq_type > d_offd
Definition Util.h:150
const seq_type & offd() const
Definition Util.h:145
seq_csr< Config, typename Config::gidx_t > seq_type
Definition Util.h:137
typename Config::lidx_t lidx_t
Definition Util.h:135
seq_type & offd()
Definition Util.h:142
typename Config::scalar_t scalar_t
Definition Util.h:136
seq_type & diag()
Definition Util.h:141
typename Config::allocator_type allocator_type
Definition Util.h:118
vector_type< scalar_t > values
Definition Util.h:128
typename Config::lidx_t lidx_t
Definition Util.h:121
typename Config::scalar_t scalar_t
Definition Util.h:122
std::vector< T, rebind_alloc< allocator_type, T > > vector_type
Definition Util.h:124
vector_type< col_idx_t > colind
Definition Util.h:127
vector_type< lidx_t > rowptr
Definition Util.h:126
constexpr reference back() const
Definition Util.h:63
constexpr pointer data() const noexcept
Definition Util.h:67
constexpr iterator begin() const noexcept
Definition Util.h:53
constexpr iterator end() const noexcept
Definition Util.h:55
extent_storage< Extent > ext
Definition Util.h:110
constexpr bool empty() const noexcept
Definition Util.h:73
constexpr size_type size() const noexcept
Definition Util.h:69
const T * const_pointer
Definition Util.h:36
constexpr span< T, dynamic_extent > subspan(size_t offset, size_t count=dynamic_extent) const noexcept
Definition Util.h:97
constexpr span< T, dynamic_extent > last(size_type count) const noexcept
Definition Util.h:92
constexpr reference operator[](size_type idx) const
Definition Util.h:65
constexpr size_type size_bytes() const noexcept
Definition Util.h:71
typename std::remove_cv_t< T > value_type
Definition Util.h:32
constexpr reverse_iterator rend() const noexcept
Definition Util.h:59
const T & const_reference
Definition Util.h:38
std::reverse_iterator< iterator > reverse_iterator
Definition Util.h:40
std::size_t size_type
Definition Util.h:33
std::ptrdiff_t difference_type
Definition Util.h:34
static constexpr std::size_t extent
Definition Util.h:42
constexpr reference front() const
Definition Util.h:61
constexpr span< T, dynamic_extent > first(size_type count) const noexcept
Definition Util.h:81
constexpr span< T, Count > last() const noexcept
Definition Util.h:87
constexpr reverse_iterator rbegin() const noexcept
Definition Util.h:57
constexpr span< T, Count > first() const noexcept
Definition Util.h:76
constexpr span(pointer p, size_type s)
Definition Util.h:51



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page