Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_DynamicMatrix_Impl.hpp
1
24#ifndef MORPHEUS_DYNAMICMATRIX_IMPL_HPP
25#define MORPHEUS_DYNAMICMATRIX_IMPL_HPP
26
27#include <string>
28
29#include <Morpheus_Exceptions.hpp>
30#include <Morpheus_FormatsRegistry.hpp>
31#include <Morpheus_ContainerTraits.hpp>
32
33namespace Morpheus {
34
35namespace Impl {
36
37template <class ValueType, class... Properties>
39 : public Morpheus::ContainerTraits<any_type_resize, ValueType,
40 Properties...> {
41 using traits =
43 using size_type = typename traits::size_type;
44 using value_type = typename traits::value_type;
45 using result_type = void;
46
47 // Specialization for Coo resize with three arguments
48 template <typename... Args>
49 result_type operator()(
50 typename CooMatrix<ValueType, Properties...>::type &mat,
51 const size_type nrows, const size_type ncols, const size_type nnnz) {
52 mat.resize(nrows, ncols, nnnz);
53 }
54
55 // Specialization for Csr resize with three arguments
56 template <typename... Args>
57 result_type operator()(
59 const size_type nrows, const size_type ncols, const size_type nnnz) {
60 mat.resize(nrows, ncols, nnnz);
61 }
62
63 // Specialization for Dia resize with five arguments
64 template <typename... Args>
65 result_type operator()(
67 const size_type nrows, const size_type ncols, const size_type nnnz,
68 const size_type ndiag, const size_type alignment = 32) {
69 mat.resize(nrows, ncols, nnnz, ndiag, alignment);
70 }
71
72 // Constrains any other overloads for supporting formats
73 // Unsupported formats won't compile
74 template <typename... Args>
75 result_type operator()(
76 typename CooMatrix<ValueType, Properties...>::type &mat, Args &&...) {
78 "Invalid use of the dynamic resize interface for current format (" +
79 std::to_string(mat.format_index()) + ").");
80 }
81
82 template <typename... Args>
83 result_type operator()(
84 typename CsrMatrix<ValueType, Properties...>::type &mat, Args &&...) {
86 "Invalid use of the dynamic resize interface for current format (" +
87 std::to_string(mat.format_index()) + ").");
88 }
89
90 template <typename... Args>
91 result_type operator()(
92 typename DiaMatrix<ValueType, Properties...>::type &mat, Args &&...) {
94 "Invalid use of the dynamic resize interface for current format (" +
95 std::to_string(mat.format_index()) + ").");
96 }
97};
98
100 using result_type = void;
101
102 template <typename T1, typename T2>
103 result_type operator()(
104 const T1 &src, T2 &dst,
105 typename std::enable_if<has_same_format<T1, T2>::value>::type * =
106 nullptr) {
107 dst.resize(src);
108 }
109
110 template <typename T1, typename T2>
111 result_type operator()(
112 const T1 &src, T2 &dst,
113 typename std::enable_if<!has_same_format<T1, T2>::value>::type * =
114 nullptr) {
116 "Invalid use of the dynamic resize interface. Src and dst tags must be "
117 "the same (" +
118 std::to_string(src.format_index()) +
119 " != " + std::to_string(dst.format_index()) + ")");
120 }
121};
122
124 using result_type = void;
125
126 template <typename T1, typename T2>
127 result_type operator()(
128 const T1 &src, T2 &dst,
129 typename std::enable_if<has_same_format<T1, T2>::value>::type * =
130 nullptr) {
131 dst = T2().allocate(src);
132 }
133
134 template <typename T1, typename T2>
135 result_type operator()(
136 const T1 &src, T2 &dst,
137 typename std::enable_if<!has_same_format<T1, T2>::value>::type * =
138 nullptr) {
140 "Invalid use of the dynamic allocate interface. Src and std tags must "
141 "be the same (" +
142 std::to_string(src.format_index()) +
143 " != " + std::to_string(dst.format_index()) + ")");
144 }
145};
146
148 using result_type = void;
149
150 template <typename T1, typename T2>
151 result_type operator()(
152 const T1 &src, T2 &dst,
153 typename std::enable_if<has_same_format<T1, T2>::value>::type * =
154 nullptr) {
155 dst = src;
156 }
157
158 template <typename T1, typename T2>
159 result_type operator()(
160 const T1 &src, T2 &dst,
161 typename std::enable_if<!has_same_format<T1, T2>::value>::type * =
162 nullptr) {
164 "Invalid use of the dynamic assign interface. Src and dst tags must be "
165 "the same (" +
166 std::to_string(src.format_index()) +
167 " != " + std::to_string(dst.format_index()) + ")");
168 }
169};
170
171template <size_t I, class ValueType, typename... Properties>
173 using variant = typename MatrixFormats<ValueType, Properties...>::variant;
174 using type_list = typename MatrixFormats<ValueType, Properties...>::type_list;
175
176 static void activate(variant &A, size_t idx) {
177 if (idx == I - 1) {
178 A = typename type_list::template type<I - 1>{};
179 } else {
181 }
182 }
183};
184
185// Base case, activate to the first type in the variant
186template <class ValueType, typename... Properties>
187struct activate_impl<0, ValueType, Properties...> {
188 using variant = typename MatrixFormats<ValueType, Properties...>::variant;
189 using type_list = typename MatrixFormats<ValueType, Properties...>::type_list;
190
191 static void activate(variant &A, size_t idx) {
192 idx = 0;
194 }
195};
196
197} // namespace Impl
198} // namespace Morpheus
199
200#endif // MORPHEUS_DYNAMICMATRIX_IMPL_HPP
void resize(const size_type num_rows, const size_type num_cols, const size_type num_entries)
Resizes CooMatrix with shape of (num_rows, num_cols) and sets number of non-zero entries to num_entri...
Definition: Morpheus_CooMatrix.hpp:363
int format_index() const
Returns the equivalent index to the format enum assigned to the CooMatrix container.
Definition: Morpheus_CooMatrix.hpp:441
typename traits::type type
Definition: Morpheus_CsrMatrix.hpp:96
typename traits::type type
Definition: Morpheus_DiaMatrix.hpp:102
Definition: Morpheus_Exceptions.hpp:57
Checks if the two types have the same format i.e both are valid containers and have the same tag memb...
Definition: Morpheus_FormatTraits.hpp:249
Generic Morpheus interfaces.
Definition: dummy.cpp:24
Traits class for accessing attributes of a Container (Matrix or Vector)
Definition: Morpheus_ContainerTraits.hpp:54
size_t size_type
The size type of the container.
Definition: Morpheus_ContainerTraits.hpp:103
ValueType value_type
The type of values held by the container.
Definition: Morpheus_ContainerTraits.hpp:91
Definition: Morpheus_DynamicMatrix_Impl.hpp:172
Definition: Morpheus_DynamicMatrix_Impl.hpp:123
Definition: Morpheus_DynamicMatrix_Impl.hpp:147
Definition: Morpheus_DynamicMatrix_Impl.hpp:99
Definition: Morpheus_DynamicMatrix_Impl.hpp:40
Definition: Morpheus_FormatsRegistry.hpp:38
A wrapper that checks if the provided type is a scalar type.
Definition: Morpheus_TypeTraits.hpp:85