Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_DynamicMatrix.hpp
1
24#ifndef MORPHEUS_DYNAMICMATRIX_HPP
25#define MORPHEUS_DYNAMICMATRIX_HPP
26
27#include <Morpheus_FormatTags.hpp>
28#include <Morpheus_MatrixBase.hpp>
29
30#include <impl/Morpheus_Variant.hpp>
31#include <impl/Dynamic/Morpheus_DynamicMatrix_Impl.hpp>
32
33#include <iostream>
34#include <string>
35#include <functional>
36
37namespace Morpheus {
38
102template <class ValueType, class... Properties>
104 : public MatrixBase<DynamicMatrix, ValueType, Properties...> {
105 public:
109 using type = typename traits::type;
110 using base = MatrixBase<DynamicMatrix, ValueType, Properties...>;
113
118 using size_type = typename traits::size_type;
123
124 using array_layout = typename traits::array_layout;
125 using backend = typename traits::backend;
126 using memory_space = typename traits::memory_space;
127 using execution_space = typename traits::execution_space;
128 using device_type = typename traits::device_type;
129 using memory_traits = typename traits::memory_traits;
130 using HostMirror = typename traits::HostMirror;
131
132 using pointer = typename traits::pointer;
133 using const_pointer = typename traits::const_pointer;
134 using reference = typename traits::reference;
135 using const_reference = typename traits::const_reference;
136
139 typename MatrixFormats<ValueType, Properties...>::variant;
140
144 ~DynamicMatrix() = default;
149 DynamicMatrix(const DynamicMatrix &) = default;
165
169 inline DynamicMatrix() : _formats() {}
170
185 template <typename Matrix>
187 const Matrix &src,
188 typename std::enable_if<is_variant_member_v<Matrix, variant_type>>::type
189 * = nullptr) {
190 this->activate(src.format_enum());
191 base::resize(src.nrows(), src.ncols(), src.nnnz());
192
193 auto f = std::bind(Impl::any_type_assign(), std::cref(src),
194 std::placeholders::_1);
195 Morpheus::Impl::Variant::visit(f, _formats);
196 }
197
212 template <typename Matrix>
213 typename std::enable_if<is_variant_member_v<Matrix, variant_type>,
215 operator=(const Matrix &matrix) {
216 this->activate(matrix.format_enum());
217 base::resize(matrix.nrows(), matrix.ncols(), matrix.nnnz());
218
219 auto f = std::bind(Impl::any_type_assign(), std::cref(matrix),
220 std::placeholders::_1);
221 Morpheus::Impl::Variant::visit(f, _formats);
222 return *this;
223 }
224
238 template <class VR, class... PR>
240 const DynamicMatrix<VR, PR...> &src,
241 typename std::enable_if<is_format_compatible<
242 DynamicMatrix, DynamicMatrix<VR, PR...>>::value>::type * = nullptr) {
243 this->activate(src.active_index()); // switch to src format
244 base::resize(src.nrows(), src.ncols(), src.nnnz());
245
246 Morpheus::Impl::Variant::visit(Impl::any_type_assign(), src.const_formats(),
247 _formats);
248 }
249
263 template <class VR, class... PR>
264 typename std::enable_if<
268 this->activate(src.active_index()); // switch to src format
269 base::resize(src.nrows(), src.ncols(), src.nnnz());
270
271 Morpheus::Impl::Variant::visit(Impl::any_type_assign(), src.const_formats(),
272 _formats);
273
274 return *this;
275 }
276
277 // template <typename... Args>
278 // inline void resize(const size_type m, const size_type n,
279 // const size_type nnz, Args &&... args) {
280 // base::resize(m, n, nnz);
281 // auto f = std::bind(Impl::any_type_resize<ValueType, Properties...>(),
282 // std::placeholders::_1, m, n, nnz,
283 // std::forward<Args>(args)...);
284 // return Morpheus::Impl::Variant::visit(f, _formats);
285 // }
286
295 template <class VR, class... PR>
296 inline void resize(const DynamicMatrix<VR, PR...> &src) {
297 this->activate(src.format_enum());
298 base::resize(src.nrows(), src.ncols(), src.nnnz());
299
300 Morpheus::Impl::Variant::visit(Impl::any_type_resize_from_mat(),
301 src.const_formats(), _formats);
302 }
303
314 template <typename Matrix>
315 inline void resize(
316 const Matrix &src,
317 typename std::enable_if<is_variant_member_v<Matrix, variant_type>>::type
318 * = nullptr) {
319 this->activate(src.format_enum());
320 base::resize(src.nrows(), src.ncols(), src.nnnz());
321
322 auto f = std::bind(Impl::any_type_resize_from_mat(), std::cref(src),
323 std::placeholders::_1);
324 Morpheus::Impl::Variant::visit(f, _formats);
325 }
326
338 template <class VR, class... PR>
340 this->activate(src.active_index()); // switch to src format
341 base::resize(src.nrows(), src.ncols(), src.nnnz());
342
343 Morpheus::Impl::Variant::visit(Impl::any_type_allocate(),
344 src.const_formats(), _formats);
345 return *this;
346 }
347
354 inline int active_index() const { return _formats.index(); }
355
362 int format_index() const { return this->active_index(); }
363
370 inline formats_e active_enum() const {
371 return static_cast<formats_e>(_formats.index());
372 }
373
380 inline formats_e format_enum() const { return this->active_enum(); }
381
388 inline void activate(const formats_e index) {
389 constexpr int size = Morpheus::Impl::Variant::variant_size_v<
390 typename MatrixFormats<ValueType, Properties...>::variant>;
391 const int idx = static_cast<int>(index);
392
393 if (idx == active_index()) {
394 return;
395 } else if (idx > size) {
396 std::cout << "Warning: There are " << size
397 << " available formats to switch to. "
398 << "Selecting to switch to format with index " << idx
399 << " will default to no change." << std::endl;
400
401 return;
402 }
403
404 // Set metadata to 0
405 base::resize(0, 0, 0);
407 idx);
408 }
409
416 inline void activate(const int index) {
417 activate(static_cast<formats_e>(index));
418 }
419
426 inline const variant_type &const_formats() const { return _formats; }
427
434 inline variant_type &formats() { return _formats; }
435
436 private:
437 variant_type _formats;
438};
441} // namespace Morpheus
442
443#endif // MORPHEUS_DYNAMICMATRIX_HPP
Implementation of the Dynamic Sparse Matrix Format Representation.
Definition: Morpheus_DynamicMatrix.hpp:104
int format_index() const
Returns the format index assigned to the active type of the DynamicMatrix container.
Definition: Morpheus_DynamicMatrix.hpp:362
formats_e active_enum() const
Returns the format enum assigned to the active type of the DynamicMatrix container.
Definition: Morpheus_DynamicMatrix.hpp:370
void activate(const int index)
Changes the active type of the DynamicMatrix container to the one given by the index parameter.
Definition: Morpheus_DynamicMatrix.hpp:416
void resize(const Matrix &src, typename std::enable_if< is_variant_member_v< Matrix, variant_type > >::type *=nullptr)
Resizes DynamicMatrix with the shape and number of non-zero entries of another Matrix with possibly a...
Definition: Morpheus_DynamicMatrix.hpp:315
typename traits::type type
Definition: Morpheus_DynamicMatrix.hpp:109
int active_index() const
Returns the format index assigned to the active type of the DynamicMatrix container.
Definition: Morpheus_DynamicMatrix.hpp:354
DynamicMatrix(const DynamicMatrix &)=default
The default copy contructor (shallow copy) of a DynamicMatrix container from another DynamicMatrix co...
typename traits::value_type value_type
Definition: Morpheus_DynamicMatrix.hpp:115
~DynamicMatrix()=default
The default destructor.
std::enable_if< is_format_compatible< DynamicMatrix, DynamicMatrix< VR, PR... > >::value, DynamicMatrix & >::type operator=(const DynamicMatrix< VR, PR... > &src)
Assigns a DynamicMatrix from another compatible DynamicMatrix.
Definition: Morpheus_DynamicMatrix.hpp:267
DynamicMatrix & allocate(const DynamicMatrix< VR, PR... > &src)
Allocates DynamicMatrix with the shape and number of non-zero entries of the active type of another D...
Definition: Morpheus_DynamicMatrix.hpp:339
variant_type & formats()
Returns a reference to the variant container that holds the supported formats in the DynamicMatrix.
Definition: Morpheus_DynamicMatrix.hpp:434
DynamicMatrix(const Matrix &src, typename std::enable_if< is_variant_member_v< Matrix, variant_type > >::type *=nullptr)
Constructs a DynamicMatrix from another concrete format.
Definition: Morpheus_DynamicMatrix.hpp:186
DynamicMatrix()
Constructs an empty DynamicMatrix object.
Definition: Morpheus_DynamicMatrix.hpp:169
formats_e format_enum() const
Returns the format enum assigned to the active type of the DynamicMatrix container.
Definition: Morpheus_DynamicMatrix.hpp:380
const variant_type & const_formats() const
Returns a const-reference to the variant container that holds the supported formats in the DynamicMat...
Definition: Morpheus_DynamicMatrix.hpp:426
typename MatrixFormats< ValueType, Properties... >::variant variant_type
Definition: Morpheus_DynamicMatrix.hpp:139
typename traits::non_const_value_type non_const_value_type
Definition: Morpheus_DynamicMatrix.hpp:117
typename traits::index_type index_type
Definition: Morpheus_DynamicMatrix.hpp:120
DynamicMatrix & operator=(const DynamicMatrix &)=default
The default copy assignment (shallow copy) of a DynamicMatrix container from another DynamicMatrix co...
DynamicMatrix(DynamicMatrix &&)=default
The default move contructor (shallow copy) of a DynamicMatrix container from another DynamicMatrix co...
DynamicMatrix(const DynamicMatrix< VR, PR... > &src, typename std::enable_if< is_format_compatible< DynamicMatrix, DynamicMatrix< VR, PR... > >::value >::type *=nullptr)
Constructs a DynamicMatrix from another compatible DynamicMatrix.
Definition: Morpheus_DynamicMatrix.hpp:239
typename traits::non_const_index_type non_const_index_type
Definition: Morpheus_DynamicMatrix.hpp:122
std::enable_if< is_variant_member_v< Matrix, variant_type >, DynamicMatrix & >::type operator=(const Matrix &matrix)
Assigns a DynamicMatrix from another concrete format.
Definition: Morpheus_DynamicMatrix.hpp:215
typename MatrixFormatTag< Morpheus::DynamicMatrixFormatTag >::tag tag
Definition: Morpheus_DynamicMatrix.hpp:112
void activate(const formats_e index)
Changes the active type of the DynamicMatrix container to the one given by the enum index parameter.
Definition: Morpheus_DynamicMatrix.hpp:388
DynamicMatrix & operator=(DynamicMatrix &&)=default
The default move assignment (shallow copy) of a DynamicMatrix container from another DynamicMatrix co...
void resize(const DynamicMatrix< VR, PR... > &src)
Resizes DynamicMatrix with the shape and number of non-zero entries of the active type of another Dyn...
Definition: Morpheus_DynamicMatrix.hpp:296
Base class used to derive new matrices.
Definition: Morpheus_MatrixBase.hpp:70
void resize(size_type rows, size_type cols, size_type entries)
Resizes MatrixBase with shape of (num_rows, num_cols) and sets number of non-zero entries to num_entr...
Definition: Morpheus_MatrixBase.hpp:108
size_type nnnz() const
Number of non-zeros of the matrix.
Definition: Morpheus_MatrixBase.hpp:133
size_type nrows() const
Number of rows of the matrix.
Definition: Morpheus_MatrixBase.hpp:119
size_type ncols() const
Number of columns of the matrix.
Definition: Morpheus_MatrixBase.hpp:126
Generic Morpheus interfaces.
Definition: dummy.cpp:24
typename std::add_lvalue_reference< typename std::add_const< type >::type >::type const_reference
The const reference type of the container.
Definition: Morpheus_ContainerTraits.hpp:158
MemorySpace memory_space
The space in which data will be stored in.
Definition: Morpheus_ContainerTraits.hpp:119
MemoryTraits memory_traits
Represents the user's intended access behaviour.
Definition: Morpheus_ContainerTraits.hpp:125
ArrayLayout array_layout
The storage layout of data held by the container.
Definition: Morpheus_ContainerTraits.hpp:110
size_t size_type
The size type of the container.
Definition: Morpheus_ContainerTraits.hpp:103
IndexType index_type
The type of indices held by the container.
Definition: Morpheus_ContainerTraits.hpp:100
typename std::remove_const< ValueType >::type non_const_value_type
The non-const type of values held by the container.
Definition: Morpheus_ContainerTraits.hpp:97
typename std::add_pointer< typename std::add_const< type >::type >::type const_pointer
The const pointer type of the container.
Definition: Morpheus_ContainerTraits.hpp:151
ValueType value_type
The type of values held by the container.
Definition: Morpheus_ContainerTraits.hpp:91
typename std::add_pointer< type >::type pointer
The pointer type of the container.
Definition: Morpheus_ContainerTraits.hpp:147
Backend backend
The backend out of which algorithms will be dispatched from.
Definition: Morpheus_ContainerTraits.hpp:113
DynamicMatrix< value_type, index_type, array_layout, backend, memory_traits > type
The complete type of the container.
Definition: Morpheus_ContainerTraits.hpp:133
ExecutionSpace execution_space
The space in which member functions will be executed in.
Definition: Morpheus_ContainerTraits.hpp:116
typename std::add_lvalue_reference< type >::type reference
The reference type of the container.
Definition: Morpheus_ContainerTraits.hpp:154
DynamicMatrix< non_const_value_type, non_const_index_type, array_layout, Morpheus::Device< typename host_mirror_backend::execution_space, typename host_mirror_backend::memory_space, typename host_mirror_backend::backend >, typename Kokkos::MemoryManaged > HostMirror
The host mirror equivalent for the container.
Definition: Morpheus_ContainerTraits.hpp:143
typename std::remove_const< IndexType >::type non_const_index_type
The non-const type of indices held by the container.
Definition: Morpheus_ContainerTraits.hpp:106
Morpheus::Device< execution_space, memory_space, backend > device_type
A device aware of the execution, memory spaces and backend.
Definition: Morpheus_ContainerTraits.hpp:122
Tag used to mark containers as Matrix containers (Sparse) with Coordinate (COO) Storage Format.
Definition: Morpheus_FormatTags.hpp:46
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_FormatsRegistry.hpp:38
A wrapper that checks if the provided type is a scalar type.
Definition: Morpheus_TypeTraits.hpp:85
Checks if the two types are format compatible containers i.e are compatible containers and have the s...
Definition: Morpheus_FormatTraits.hpp:362