Advanced Multi-Physics (AMP)
On-Line Documentation
CSRConfig.h
Go to the documentation of this file.
1#ifndef included_AMP_CSRConfig
2#define included_AMP_CSRConfig
3
4#include "AMP/AMP_TPLs.h"
5#include "AMP/IO/PIO.h"
6#include "AMP/utils/Memory.h"
7
8#ifdef AMP_USE_HYPRE
9 #include "HYPRE_utilities.h"
10#endif
11
12#include <cstdint>
13#include <limits>
14#include <tuple>
15
16
17namespace AMP::LinearAlgebra {
18
19// labels for components of a CSR configuration
20enum class alloc : uint8_t { host, device, managed };
21enum class index : uint8_t { i32, i64, ill };
22enum class scalar : uint8_t { f32, f64, fld };
23
24// Helpers to recover types and other info from these labels
25template<alloc>
27template<>
30 static constexpr alloc id = alloc::host;
32 static constexpr bool host_accessible = true;
33 static constexpr bool device_accessible = false;
34 static const char *name() { return "host"; }
35};
36#ifdef AMP_USE_DEVICE
37template<>
38struct alloc_info<alloc::device> {
39 using type = DeviceAllocator<void>;
40 static constexpr alloc id = alloc::device;
42 static constexpr bool host_accessible = false;
43 static constexpr bool device_accessible = true;
44 static const char *name() { return "device"; }
45};
46template<>
47struct alloc_info<alloc::managed> {
48 using type = ManagedAllocator<void>;
49 static constexpr alloc id = alloc::managed;
51 static constexpr bool host_accessible = true;
52 static constexpr bool device_accessible = true;
53 static const char *name() { return "managed"; }
54};
55#endif
56
57template<index>
59template<>
61 using type = int;
62 static constexpr index id = index::i32;
63 static const char *name() { return "int"; }
64};
65template<>
67 using type = size_t;
68 static constexpr index id = index::i64;
69 static const char *name() { return "size_t"; }
70};
71
72template<>
74 using type = long long int;
75 static constexpr index id = index::ill;
76 static const char *name() { return "long long"; }
77};
78
79template<scalar>
81template<>
83 using type = float;
84 static constexpr scalar id = scalar::f32;
85 static const char *name() { return "single precision"; }
86};
87template<>
89 using type = double;
90 static constexpr scalar id = scalar::f64;
91 static const char *name() { return "double precision"; }
92};
93
94template<>
96 using type = long double;
97 static constexpr scalar id = scalar::fld;
98 static const char *name() { return "long double"; }
99};
100
101/*
102 Describes the layout of a CSR mode (uint16_t).
103
104 Component types define shift and mask which indicate the bits
105 of the overall CSR mode integer given to each component.
106 */
108 using I = std::uint16_t;
109 struct alloc {
110 static constexpr I shift = 0U;
111 static constexpr I mask = 0xfU;
113 };
114 struct lidx {
115 static constexpr I shift = alloc::shift + 4;
116 static constexpr I mask = 0xfU << shift;
117 using type = index;
118 };
119 struct gidx {
120 static constexpr I shift = lidx::shift + 4;
121 static constexpr I mask = 0xfU << shift;
122 using type = index;
123 };
124 struct scalar {
125 static constexpr I shift = gidx::shift + 4;
126 static constexpr I mask = 0xfU << shift;
128 };
129 using components = std::tuple<alloc, lidx, gidx, scalar>;
130 template<I index>
131 using element = std::tuple_element_t<index, components>;
132};
133
134// Helper type to assemble the overall CSR mode integer from component enums.
135template<alloc Alloc, index local, index global, scalar Scalar>
136struct make_mode {
137 using I = std::uint16_t;
138 template<auto... vs>
139 struct assemble {
140 template<I... Is>
141 static constexpr I get( std::integer_sequence<I, Is...> )
142 {
143 return ( ( static_cast<I>( vs ) << csr_mode_layout::element<Is>::shift ) | ... );
144 }
145 static constexpr auto value = get( std::make_integer_sequence<I, sizeof...( vs )>{} );
146 };
148};
149template<alloc a, index l, index g, scalar s>
151
152#define AMP_GEN_MODE( i1_name, i1_id, i2_name, i2_id ) \
153 h##i1_name##i2_name##f = make_mode_v<alloc::host, i1_id, i2_id, scalar::f32>, \
154 h##i1_name##i2_name##d = make_mode_v<alloc::host, i1_id, i2_id, scalar::f64>, \
155 d##i1_name##i2_name##f = make_mode_v<alloc::device, i1_id, i2_id, scalar::f32>, \
156 d##i1_name##i2_name##d = make_mode_v<alloc::device, i1_id, i2_id, scalar::f64>, \
157 m##i1_name##i2_name##f = make_mode_v<alloc::managed, i1_id, i2_id, scalar::f32>, \
158 m##i1_name##i2_name##d = make_mode_v<alloc::managed, i1_id, i2_id, scalar::f64>
159// CSR mode labels for a CSR configurations
160enum class csr_mode : std::uint16_t {
165 other = std::numeric_limits<std::uint16_t>::max()
166};
167
168#define EMPTY()
169#define DEFER( X ) X EMPTY()
170#define EXPAND( X ) X
171
172#if defined( AMP_USE_HYPRE )
173 #include "CSRConfigHypre.h"
174#else
175 #define CSR_CONFIG_FORALL_HYPRE_HOST( INST )
176 #ifdef AMP_USE_DEVICE
177 #define CSR_CONFIG_FORALL_HYPRE_DEVICE( INST )
178 #endif
179 #define CSR_CONFIG_CC_FORALL_HYPRE( INST )
180 #define CSR_CONFIG_CC_FORALL0_HYPRE( F, G )
181#endif
182
183#define CSR_CONFIG_FORALL_HOST( INST ) \
184 INST( csr_mode::hiIf ) \
185 INST( csr_mode::hiId ) \
186 CSR_CONFIG_FORALL_HYPRE_HOST( INST )
187#define CSR_CONFIG_CC_FORALL_HOST( F, G ) \
188 F( G, csr_mode::hiIf ) \
189 F( G, csr_mode::hiId )
190
191#ifdef AMP_USE_DEVICE
192 #define CSR_CONFIG_FORALL_DEVICE( INST ) \
193 INST( csr_mode::diIf ) \
194 INST( csr_mode::diId ) \
195 INST( csr_mode::miIf ) \
196 INST( csr_mode::miId ) \
197 CSR_CONFIG_FORALL_HYPRE_DEVICE( INST )
198
199 #define CSR_CONFIG_CC_FORALL_DEVICE( F, G ) \
200 F( G, csr_mode::diIf ) \
201 F( G, csr_mode::diId ) \
202 F( G, csr_mode::miIf ) \
203 F( G, csr_mode::miId )
204#else
205 #define CSR_CONFIG_FORALL_DEVICE( INST )
206 #define CSR_CONFIG_CC_FORALL_DEVICE( F, G )
207#endif
208
209#define CSR_CONFIG_FORALL( INST ) \
210 CSR_CONFIG_FORALL_HOST( INST ) \
211 CSR_CONFIG_FORALL_DEVICE( INST )
212
213#define CSR_CONFIG_CC_FORALL0( F, G ) \
214 CSR_CONFIG_CC_FORALL_HOST( F, G ) \
215 CSR_CONFIG_CC_FORALL_DEVICE( F, G )
216#define CSR_CONFIG_CC_FORALL1() CSR_CONFIG_CC_FORALL0
217#define CSR_CONFIG_CC_FORALL2( F, G ) DEFER( CSR_CONFIG_CC_FORALL1 )()( F, G )
218#define CSR_CONFIG_CC_FORALL( INST ) \
219 EXPAND( CSR_CONFIG_CC_FORALL0( CSR_CONFIG_CC_FORALL2, INST ) ) \
220 CSR_CONFIG_CC_FORALL_HYPRE( INST )
221
222
223/******************************************************************************
224 * Helpers for defining the instantiations for the rutines supporting migration
225 * of csr matrices between host / managed / device memories, between global
226 * indexing using ill / i64, and between float and double scalar types.
227 *
228 * There are two main macros supposed to be used externally:
229 * \brief instantiate migrate routines templated againt both in and out
230 * csr_config's
231 * \param[in] INST macro with config_in and config_out arguments
232 * \detail As oposed to CSR_CONFIG_CC_FORALL, the permutations include hypre
233 * and AMP configurations
234 * CSR_INOUT_CONFIG_MIGRATE ( INST )
235 *
236 * There are several inner helper macros with the following naming convention:
237 * 1.- OUTLIST_(HOST/DEVICE/HYPRE_HOST/HYPRE_DEVICE)**
238 * Perform a list of calls to the subsequent macros for
239 * host/device/hypre_host/hypre_device apropriate csr configurations.
240 * 2.- CSR_INOUT_CONFIG_MIGRATE_LOOP
241 * Given an in csr_config and the INST macro it calls through the list of
242 * of host/device/hypre_host/hypre_device CSR_CONFIG_CC* macros which
243 * define the out csr_config.
244 *
245 * These instantiations add the possibility to migrate between different
246 * global index types, not needed (for the moment) in CSR_CONFIG_CC_FORALL
247 ******************************************************************************/
248
249#define CSR_INOUT_CONFIG_MIGRATE_LOOP( mode_in, INST ) \
250 CSR_CONFIG_CC_FORALL0( INST, mode_in ) \
251 CSR_CONFIG_CC_FORALL0_HYPRE( INST, mode_in )
252
253#define CSR_INOUT_CONFIG_MIGRATE_OUTLIST_HOST( INST ) \
254 CSR_INOUT_CONFIG_MIGRATE_LOOP( csr_mode::hiIf, INST ) \
255 CSR_INOUT_CONFIG_MIGRATE_LOOP( csr_mode::hiId, INST )
256
257#if defined( AMP_USE_DEVICE )
258 #define CSR_INOUT_CONFIG_MIGRATE_OUTLIST_DEVICE( INST ) \
259 CSR_INOUT_CONFIG_MIGRATE_LOOP( csr_mode::miIf, INST ) \
260 CSR_INOUT_CONFIG_MIGRATE_LOOP( csr_mode::miId, INST ) \
261 CSR_INOUT_CONFIG_MIGRATE_LOOP( csr_mode::diIf, INST ) \
262 CSR_INOUT_CONFIG_MIGRATE_LOOP( csr_mode::diId, INST )
263#else
264 #define CSR_INOUT_CONFIG_MIGRATE_OUTLIST_DEVICE( INST )
265#endif
266
267#ifndef AMP_USE_HYPRE
268 #define CSR_INOUT_CONFIG_MIGRATE_OUTLIST_HYPRE_HOST( INST )
269 #define CSR_INOUT_CONFIG_MIGRATE_OUTLIST_HYPRE_DEVICE( INST )
270#endif
271
272#define CSR_INOUT_CONFIG_MIGRATE( INST ) \
273 CSR_INOUT_CONFIG_MIGRATE_OUTLIST_HOST( INST ) \
274 CSR_INOUT_CONFIG_MIGRATE_OUTLIST_DEVICE( INST ) \
275 CSR_INOUT_CONFIG_MIGRATE_OUTLIST_HYPRE_HOST( INST ) \
276 CSR_INOUT_CONFIG_MIGRATE_OUTLIST_HYPRE_DEVICE( INST )
277
278
279template<alloc Alloc, index LocalInd, index GlobalInd, scalar Scalar>
280struct CSRConfig {
281 static constexpr alloc allocator = Alloc;
282 static constexpr index lidx = LocalInd;
283 static constexpr index gidx = GlobalInd;
284 static constexpr scalar scalar_id = Scalar;
285
286 static constexpr csr_mode mode =
287 static_cast<csr_mode>( make_mode_v<allocator, lidx, gidx, scalar_id> );
288
293
294 template<alloc new_alloc>
298 template<alloc new_alloc>
300
301 template<scalar new_scalar>
305 template<scalar new_scalar>
307
308 template<index new_lidx>
310
311 static void print()
312 {
313 AMP::pout << "Local Index: " << index_info<lidx>::name() << '\n'
314 << "Global Index: " << index_info<gidx>::name() << '\n'
315 << "Scalar: " << scalar_info<scalar_id>::name() << std::endl;
316 }
317};
318
319// Helpers to statically determine a component mode from a csr_mode
320template<csr_mode mode, class L>
322 static constexpr auto value = static_cast<typename L::type>(
323 ( static_cast<std::uint16_t>( mode ) & L::mask ) >> L::shift );
324};
325
326template<csr_mode mode>
328
329template<csr_mode mode>
331
332template<csr_mode mode>
334
335template<csr_mode mode>
337
338// Helpers to dynamically determine a component mode from a csr_mode
339template<class L>
341{
342 return static_cast<typename L::type>( ( static_cast<std::uint16_t>( mode ) & L::mask ) >>
343 L::shift );
344}
345inline auto get_alloc( csr_mode mode ) { return get_from_mode<csr_mode_layout::alloc>( mode ); }
346inline auto get_lidx( csr_mode mode ) { return get_from_mode<csr_mode_layout::lidx>( mode ); }
347inline auto get_gidx( csr_mode mode ) { return get_from_mode<csr_mode_layout::gidx>( mode ); }
348inline auto get_scalar( csr_mode mode ) { return get_from_mode<csr_mode_layout::scalar>( mode ); }
349
350template<csr_mode mode>
352 using type =
353 CSRConfig<get_alloc_v<mode>, get_lidx_v<mode>, get_gidx_v<mode>, get_scalar_v<mode>>;
354};
355template<csr_mode mode>
357
358namespace detail {
359template<class... Cs>
361};
363#define X( C ) , config_mode_t<C>
365#undef X
366 >;
367
368template<class T>
370template<class Ignore, class... Cs>
371struct ignore_first<config_list<Ignore, Cs...>> {
372 using type = config_list<Cs...>;
373};
375} // namespace detail
377
378namespace detail {
379template<class C, class L>
380struct contains;
381template<class C, class... Cs>
382struct contains<C, config_list<Cs...>> {
383 static constexpr bool value = ( std::is_same_v<C, Cs> || ... );
384};
385} // namespace detail
386
387template<class C>
389
390inline bool is_built( csr_mode mode )
391{
392 bool built = false;
393 switch ( mode ) {
394#define X( MODE ) \
395case MODE: \
396 built = true; \
397 break;
399#undef X
400 default:
401 built = false;
402 }
403 return built;
404}
405
406#if defined( AMP_USE_HYPRE )
407template<alloc Alloc>
409using DefaultHostCSRConfig = DefaultCSRConfig<alloc::host>;
410#else
411template<alloc Alloc>
414#endif
415
416} // namespace AMP::LinearAlgebra
417#endif
#define CSR_CONFIG_FORALL(INST)
Definition CSRConfig.h:209
#define AMP_GEN_MODE(i1_name, i1_id, i2_name, i2_id)
Definition CSRConfig.h:152
#define X(C)
Scalar is a class used to store a scalar variable that may be different types/precision.
Definition Scalar.h:21
typename ignore_first< built_configs_extra_first >::type built_configs
Definition CSRConfig.h:374
constexpr bool is_config_built
Definition CSRConfig.h:388
constexpr index get_lidx_v
Definition CSRConfig.h:330
constexpr scalar get_scalar_v
Definition CSRConfig.h:336
auto get_gidx(csr_mode mode)
Definition CSRConfig.h:347
detail::built_configs built_configs
Definition CSRConfig.h:376
bool is_built(csr_mode mode)
Definition CSRConfig.h:390
constexpr index get_gidx_v
Definition CSRConfig.h:333
DefaultCSRConfig< alloc::host > DefaultHostCSRConfig
Definition CSRConfig.h:413
constexpr alloc get_alloc_v
Definition CSRConfig.h:327
auto get_lidx(csr_mode mode)
Definition CSRConfig.h:346
CSRConfig< Alloc, index::i32, index::i64, scalar::f64 > DefaultCSRConfig
Definition CSRConfig.h:412
constexpr std::uint16_t make_mode_v
Definition CSRConfig.h:150
auto get_from_mode(csr_mode mode)
Definition CSRConfig.h:340
auto get_alloc(csr_mode mode)
Definition CSRConfig.h:345
auto get_scalar(csr_mode mode)
Definition CSRConfig.h:348
typename config_mode< mode >::type config_mode_t
Definition CSRConfig.h:356
MemoryType
Enum to store pointer type.
Definition Memory.h:21
std::allocator< TYPE > HostAllocator
Definition Memory.h:69
std::ostream pout
typename set_scalar< new_scalar >::type set_scalar_t
Definition CSRConfig.h:306
typename alloc_info< Alloc >::type allocator_type
Definition CSRConfig.h:292
typename scalar_info< scalar_id >::type scalar_t
Definition CSRConfig.h:291
static constexpr scalar scalar_id
Definition CSRConfig.h:284
static constexpr index gidx
Definition CSRConfig.h:283
static constexpr alloc allocator
Definition CSRConfig.h:281
static constexpr index lidx
Definition CSRConfig.h:282
typename index_info< gidx >::type gidx_t
Definition CSRConfig.h:290
typename index_info< lidx >::type lidx_t
Definition CSRConfig.h:289
static constexpr csr_mode mode
Definition CSRConfig.h:286
typename set_alloc< new_alloc >::type set_alloc_t
Definition CSRConfig.h:299
std::tuple_element_t< index, components > element
Definition CSRConfig.h:131
std::tuple< alloc, lidx, gidx, scalar > components
Definition CSRConfig.h:129
static constexpr auto value
Definition CSRConfig.h:322
static constexpr I get(std::integer_sequence< I, Is... >)
Definition CSRConfig.h:141
static constexpr I value
Definition CSRConfig.h:147



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:40.
Comments on this page