Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_DiaMatrix.hpp
1
24#ifndef MORPHEUS_DIAMATRIX_HPP
25#define MORPHEUS_DIAMATRIX_HPP
26
27#include <Morpheus_Exceptions.hpp>
28#include <Morpheus_FormatTags.hpp>
29#include <Morpheus_DenseVector.hpp>
30#include <Morpheus_DenseMatrix.hpp>
31#include <Morpheus_DynamicMatrix.hpp>
32#include <Morpheus_MatrixBase.hpp>
33
34#include <impl/Dia/Morpheus_Utils_Impl.hpp>
35
36namespace Morpheus {
37
96template <class ValueType, class... Properties>
97class DiaMatrix : public MatrixBase<DiaMatrix, ValueType, Properties...> {
98 public:
100 using traits = ContainerTraits<DiaMatrix, ValueType, Properties...>;
102 using type = typename traits::type;
103 using base = MatrixBase<DiaMatrix, ValueType, Properties...>;
106
111 using size_type = typename traits::size_type;
116
117 using array_layout = typename traits::array_layout;
118 using backend = typename traits::backend;
119 using memory_space = typename traits::memory_space;
120 using execution_space = typename traits::execution_space;
121 using device_type = typename traits::device_type;
122 using memory_traits = typename traits::memory_traits;
123 using HostMirror = typename traits::HostMirror;
124
125 using pointer = typename traits::pointer;
126 using const_pointer = typename traits::const_pointer;
127 using reference = typename traits::reference;
128 using const_reference = typename traits::const_reference;
129
132 Morpheus::DenseVector<index_type, size_type, array_layout, backend,
133 memory_traits>;
135 using index_array_pointer = typename index_array_type::value_array_pointer;
136 using index_array_reference =
137 typename index_array_type::value_array_reference;
138 using const_index_array_reference = const index_array_reference;
139
142 Morpheus::DenseMatrix<value_type, size_type, array_layout, backend,
143 memory_traits>;
145 using value_array_pointer = typename value_array_type::value_array_pointer;
146 using value_array_reference =
147 typename value_array_type::value_array_reference;
148 using const_value_array_reference = const value_array_reference;
149
153 ~DiaMatrix() = default;
158 DiaMatrix(const DiaMatrix &) = default;
163 DiaMatrix(DiaMatrix &&) = default;
168 DiaMatrix &operator=(const DiaMatrix &) = default;
174
178 inline DiaMatrix()
179 : base(), _ndiags(0), _alignment(0), _diagonal_offsets(), _values() {}
180
192 inline DiaMatrix(const size_type num_rows, const size_type num_cols,
193 const size_type num_entries, const size_type num_diagonals,
194 const size_type alignment = 32)
195 : base(num_rows, num_cols, num_entries),
196 _ndiags(num_diagonals),
197 _alignment(alignment),
198 _diagonal_offsets(num_diagonals) {
199 _values.resize(Impl::get_pad_size<size_type>(num_rows, alignment),
200 num_diagonals);
201 }
202
218 template <typename ValueArray, typename IndexArray>
219 explicit inline DiaMatrix(
220 const size_type num_rows, const size_type num_cols,
221 const size_type num_entries, const IndexArray &diag_offsets,
222 const ValueArray &vals,
223 typename std::enable_if<
227 ValueArray>::value &&
229 IndexArray>::value &&
230 !ValueArray::memory_traits::is_unmanaged &&
231 !IndexArray::memory_traits::is_unmanaged>::type * = nullptr)
232 : base(num_rows, num_cols, num_entries),
233 _diagonal_offsets(diag_offsets),
234 _values(vals) {
235 _ndiags = _diagonal_offsets.size();
236 _alignment = _values.nrows();
237 }
238
239 // Construct from raw pointers
240 template <typename ValuePtr, typename IndexPtr>
241 explicit inline DiaMatrix(
242 const size_type num_rows, const size_type num_cols,
243 const size_type num_entries, IndexPtr diag_offsets_ptr, ValuePtr vals_ptr,
244 const size_type num_diagonals, const size_type alignment = 32,
245 typename std::enable_if<
246 (std::is_pointer<ValuePtr>::value &&
248 memory_traits::is_unmanaged) &&
249 (std::is_pointer<IndexPtr>::value &&
251 memory_traits::is_unmanaged)>::type * = nullptr)
252 : base(num_rows, num_cols, num_entries),
253 _diagonal_offsets(num_diagonals, diag_offsets_ptr),
254 _values(Impl::get_pad_size<size_type>(num_rows, alignment),
255 num_diagonals, vals_ptr) {}
256
267 template <class VR, class... PR>
269 typename std::enable_if<is_format_compatible<
270 DiaMatrix, DiaMatrix<VR, PR...>>::value>::type * = nullptr)
271 : base(src.nrows(), src.ncols(), src.nnnz()),
272 _ndiags(src.ndiags()),
273 _alignment(src.alignment()),
274 _diagonal_offsets(src.cdiagonal_offsets()),
275 _values(src.cvalues()) {}
276
288 template <class VR, class... PR>
289 typename std::enable_if<
290 is_format_compatible<DiaMatrix, DiaMatrix<VR, PR...>>::value,
293 this->set_nrows(src.nrows());
294 this->set_ncols(src.ncols());
295 this->set_nnnz(src.nnnz());
296
297 _ndiags = src.ndiags();
298 _alignment = src.alignment();
299 _diagonal_offsets = src.cdiagonal_offsets();
300 _values = src.cvalues();
301
302 return *this;
303 }
304
318 template <class VR, class... PR>
320 typename std::enable_if<is_dynamically_compatible<
321 DiaMatrix, DynamicMatrix<VR, PR...>>::value>::type * = nullptr)
322 : base(src.nrows(), src.ncols(), src.nnnz()) {
323 auto f = std::bind(Impl::any_type_assign(), std::placeholders::_1,
324 std::ref(*this));
325
326 std::visit(f, src.const_formats());
327 }
328
342 template <class VR, class... PR>
343 typename std::enable_if<
347 auto f = std::bind(Impl::any_type_assign(), std::placeholders::_1,
348 std::ref(*this));
349
350 std::visit(f, src.const_formats());
351
352 return *this;
353 }
354
363 template <typename MatrixType>
364 DiaMatrix(const MatrixType &src) = delete;
365
374 template <typename MatrixType>
375 reference operator=(const MatrixType &src) = delete;
376
388 inline void resize(const size_type num_rows, const size_type num_cols,
389 const size_type num_entries, const size_type num_diagonals,
390 const size_type alignment = 32) {
391 base::resize(num_rows, num_cols, num_entries);
392 _ndiags = num_diagonals;
393 _alignment = alignment;
394
395 if (Impl::exceeds_tolerance(num_rows, num_entries, _ndiags)) {
397 "DiaMatrix fill-in would exceed maximum tolerance");
398 }
399
400 _diagonal_offsets.resize(num_diagonals);
401 _values.resize(Impl::get_pad_size<size_type>(num_rows, alignment),
402 num_diagonals);
403 }
404
413 template <class VR, class... PR>
414 inline void resize(const DiaMatrix<VR, PR...> &src) {
415 resize(src.nrows(), src.ncols(), src.nnnz(), src.ndiags(), src.alignment());
416 }
417
426 template <class VR, class... PR>
428 resize(src.nrows(), src.ncols(), src.nnnz(), src.ndiags(), src.alignment());
429 return *this;
430 }
431
437 formats_e format_enum() const { return _id; }
438
445 int format_index() const { return static_cast<int>(_id); }
446
454 MORPHEUS_FORCEINLINE_FUNCTION index_array_reference
455 diagonal_offsets(size_type n) {
456 return _diagonal_offsets(n);
457 }
458
466 MORPHEUS_FORCEINLINE_FUNCTION value_array_reference values(size_type i,
467 size_type j) {
468 return _values(i, j);
469 }
470
478 MORPHEUS_FORCEINLINE_FUNCTION const_index_array_reference
479 cdiagonal_offsets(size_type n) const {
480 return _diagonal_offsets(n);
481 }
482
491 MORPHEUS_FORCEINLINE_FUNCTION const_value_array_reference
492 cvalues(size_type i, size_type j) const {
493 return _values(i, j);
494 }
495
501 MORPHEUS_FORCEINLINE_FUNCTION index_array_type &diagonal_offsets() {
502 return _diagonal_offsets;
503 }
504
510 MORPHEUS_FORCEINLINE_FUNCTION value_array_type &values() { return _values; }
511
517 MORPHEUS_FORCEINLINE_FUNCTION const_index_array_type &cdiagonal_offsets()
518 const {
519 return _diagonal_offsets;
520 }
521
527 MORPHEUS_FORCEINLINE_FUNCTION const_value_array_type &cvalues() const {
528 return _values;
529 }
530
536 MORPHEUS_FORCEINLINE_FUNCTION size_type ndiags() const { return _ndiags; }
537
543 MORPHEUS_FORCEINLINE_FUNCTION size_type alignment() const {
544 return _alignment;
545 }
546
552 MORPHEUS_FORCEINLINE_FUNCTION void set_ndiags(const size_type num_diagonals) {
553 _ndiags = num_diagonals;
554 }
555
561 MORPHEUS_FORCEINLINE_FUNCTION void set_alignment(const size_type alignment) {
562 _alignment = alignment;
563 }
564
565 private:
566 size_type _ndiags, _alignment;
567 index_array_type _diagonal_offsets;
568 value_array_type _values;
569 static constexpr formats_e _id = Morpheus::DIA_FORMAT;
570};
571
574} // namespace Morpheus
575
576#endif // MORPHEUS_DIAMATRIX_HPP
void resize(size_type num_rows, size_type num_cols)
Resizes DenseMatrix with shape (num_rows * num_cols). Overlapping subextents will preserve their cont...
Definition: Morpheus_DenseMatrix.hpp:265
void resize(const size_type n)
Resizes DenseVector with size of n and sets values to zero.
Definition: Morpheus_DenseVector.hpp:334
size_type size() const
Returns the size of the container.
Definition: Morpheus_DenseVector.hpp:305
Implementation of the Diagonal (DIA) Sparse Matrix Format Representation.
Definition: Morpheus_DiaMatrix.hpp:97
void resize(const DiaMatrix< VR, PR... > &src)
Resizes DiaMatrix with the shape and number of non-zero entries of another DiaMatrix with different p...
Definition: Morpheus_DiaMatrix.hpp:414
~DiaMatrix()=default
The default destructor.
reference operator=(const MatrixType &src)=delete
Assigns to DiaMatrix object from another storage format. This functionality is disabled to avoid impl...
DiaMatrix & allocate(const DiaMatrix< VR, PR... > &src)
Allocates memory from another DiaMatrix container with different properties.
Definition: Morpheus_DiaMatrix.hpp:427
DiaMatrix(DiaMatrix &&)=default
The default move contructor (shallow copy) of a DiaMatrix container from another DiaMatrix container ...
MORPHEUS_FORCEINLINE_FUNCTION const_index_array_reference cdiagonal_offsets(size_type n) const
Returns a const-reference to the diagonal offsets of the matrix with index n.
Definition: Morpheus_DiaMatrix.hpp:479
MORPHEUS_FORCEINLINE_FUNCTION size_type ndiags() const
Returns the number of occupied diagonals of the matrix.
Definition: Morpheus_DiaMatrix.hpp:536
Morpheus::DenseVector< index_type, size_type, array_layout, backend, memory_traits > index_array_type
Definition: Morpheus_DiaMatrix.hpp:133
MORPHEUS_FORCEINLINE_FUNCTION value_array_type & values()
Returns a reference to the values of the matrix.
Definition: Morpheus_DiaMatrix.hpp:510
MORPHEUS_FORCEINLINE_FUNCTION const_value_array_reference cvalues(size_type i, size_type j) const
Returns a const-reference to the value of the matrix with indexes (i, j)
Definition: Morpheus_DiaMatrix.hpp:492
void resize(const size_type num_rows, const size_type num_cols, const size_type num_entries, const size_type num_diagonals, const size_type alignment=32)
Resizes DiaMatrix with shape of (num_rows, num_cols) and sets number of non-zero entries to num_entri...
Definition: Morpheus_DiaMatrix.hpp:388
DiaMatrix(const DiaMatrix< VR, PR... > &src, typename std::enable_if< is_format_compatible< DiaMatrix, DiaMatrix< VR, PR... > >::value >::type *=nullptr)
Constructs a DiaMatrix from another compatible DiaMatrix.
Definition: Morpheus_DiaMatrix.hpp:268
MORPHEUS_FORCEINLINE_FUNCTION index_array_type & diagonal_offsets()
Returns a reference to the diagonal offsets of the matrix.
Definition: Morpheus_DiaMatrix.hpp:501
DiaMatrix(const size_type num_rows, const size_type num_cols, const size_type num_entries, const size_type num_diagonals, const size_type alignment=32)
Construct a DiaMatrix object with shape (num_rows, num_cols) and number of non-zeros equal to num_ent...
Definition: Morpheus_DiaMatrix.hpp:192
MORPHEUS_FORCEINLINE_FUNCTION void set_ndiags(const size_type num_diagonals)
Sets the number of occupied diagonals of the matrix.
Definition: Morpheus_DiaMatrix.hpp:552
int format_index() const
Returns the equivalent index to the format enum assigned to the DiaMatrix container.
Definition: Morpheus_DiaMatrix.hpp:445
DiaMatrix(const size_type num_rows, const size_type num_cols, const size_type num_entries, const IndexArray &diag_offsets, const ValueArray &vals, typename std::enable_if< is_dense_matrix_format_container< ValueArray >::value &&is_dense_vector_format_container< IndexArray >::value &&is_compatible< typename DiaMatrix::value_array_type, ValueArray >::value &&is_compatible< typename DiaMatrix::index_array_type, IndexArray >::value &&!ValueArray::memory_traits::is_unmanaged &&!IndexArray::memory_traits::is_unmanaged >::type *=nullptr)
Construct a DiaMatrix object with shape (num_rows, num_cols) and number of non-zeros equal to num_ent...
Definition: Morpheus_DiaMatrix.hpp:219
typename traits::type type
Definition: Morpheus_DiaMatrix.hpp:102
DiaMatrix(const DiaMatrix &)=default
The default copy contructor (shallow copy) of a DiaMatrix container from another DiaMatrix container ...
std::enable_if< is_dynamically_compatible< DiaMatrix, DynamicMatrix< VR, PR... > >::value, DiaMatrix & >::type operator=(const DynamicMatrix< VR, PR... > &src)
Assigns a DiarMatrix from a compatible DynamicMatrix.
Definition: Morpheus_DiaMatrix.hpp:346
MORPHEUS_FORCEINLINE_FUNCTION size_type alignment() const
Returns the amount of padding used to align the data.
Definition: Morpheus_DiaMatrix.hpp:543
std::enable_if< is_format_compatible< DiaMatrix, DiaMatrix< VR, PR... > >::value, DiaMatrix & >::type operator=(const DiaMatrix< VR, PR... > &src)
Assigns a DiaMatrix from another compatible DiaMatrix.
Definition: Morpheus_DiaMatrix.hpp:292
typename traits::value_type value_type
Definition: Morpheus_DiaMatrix.hpp:108
DiaMatrix(const DynamicMatrix< VR, PR... > &src, typename std::enable_if< is_dynamically_compatible< DiaMatrix, DynamicMatrix< VR, PR... > >::value >::type *=nullptr)
Constructs a DiaMatrix from a compatible DynamicMatrix.
Definition: Morpheus_DiaMatrix.hpp:319
formats_e format_enum() const
Returns the format enum assigned to the DiaMatrix container.
Definition: Morpheus_DiaMatrix.hpp:437
typename MatrixFormatTag< DiaFormatTag >::tag tag
Definition: Morpheus_DiaMatrix.hpp:105
typename traits::non_const_index_type non_const_index_type
Definition: Morpheus_DiaMatrix.hpp:115
typename traits::non_const_value_type non_const_value_type
Definition: Morpheus_DiaMatrix.hpp:110
DiaMatrix(const MatrixType &src)=delete
Construct a DiaMatrix object from another storage format. This functionality is disabled to avoid imp...
DiaMatrix & operator=(DiaMatrix &&)=default
The default move assignment (shallow copy) of a DiaMatrix container from another DiaMatrix container ...
DiaMatrix()
Construct an empty DiaMatrix object.
Definition: Morpheus_DiaMatrix.hpp:178
MORPHEUS_FORCEINLINE_FUNCTION index_array_reference diagonal_offsets(size_type n)
Returns a reference to the diagonal offsets of the matrix with index n.
Definition: Morpheus_DiaMatrix.hpp:455
MORPHEUS_FORCEINLINE_FUNCTION value_array_reference values(size_type i, size_type j)
Returns a reference to the value of the matrix with indexes (i, j)
Definition: Morpheus_DiaMatrix.hpp:466
MORPHEUS_FORCEINLINE_FUNCTION const_index_array_type & cdiagonal_offsets() const
Returns a const-reference to the diagonal offsets of the matrix.
Definition: Morpheus_DiaMatrix.hpp:517
MORPHEUS_FORCEINLINE_FUNCTION const_value_array_type & cvalues() const
Returns a const-reference to the values of the matrix.
Definition: Morpheus_DiaMatrix.hpp:527
MORPHEUS_FORCEINLINE_FUNCTION void set_alignment(const size_type alignment)
Sets amount of padding with which to align the data.
Definition: Morpheus_DiaMatrix.hpp:561
DiaMatrix & operator=(const DiaMatrix &)=default
The default copy assignment (shallow copy) of a DiaMatrix container from another DiaMatrix container ...
typename traits::index_type index_type
Definition: Morpheus_DiaMatrix.hpp:113
Morpheus::DenseMatrix< value_type, size_type, array_layout, backend, memory_traits > value_array_type
Definition: Morpheus_DiaMatrix.hpp:143
Implementation of the Dynamic Sparse Matrix Format Representation.
Definition: Morpheus_DynamicMatrix.hpp:104
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
Definition: Morpheus_Exceptions.hpp:69
Base class used to derive new matrices.
Definition: Morpheus_MatrixBase.hpp:70
void set_ncols(const size_type cols)
Set the number of columns of the matrix.
Definition: Morpheus_MatrixBase.hpp:147
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
void set_nrows(const size_type rows)
Set the number of rows of the matrix.
Definition: Morpheus_MatrixBase.hpp:140
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
void set_nnnz(const size_type nnz)
Set the number of non-zeros of the matrix.
Definition: Morpheus_MatrixBase.hpp:154
Checks if the two types are compatible containers i.e are in the same memory space and have the same ...
Definition: Morpheus_FormatTraits.hpp:286
Checks if the given type T is a valid Dense Matrix Format Container i.e is valid matrix container and...
Definition: Morpheus_FormatTags.hpp:237
Checks if the given type T is a valid Dense Vector Format Container i.e is valid vector container and...
Definition: Morpheus_FormatTags.hpp:273
Checks if the two types are dynamically compatible containers i.e are compatible containers and at le...
Definition: Morpheus_FormatTraits.hpp:323
Checks if the two types is of type index_type and are the same.
Definition: Morpheus_TypeTraits.hpp:514
Checks if the two types are of type value_type and the same.
Definition: Morpheus_TypeTraits.hpp:385
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
DiaMatrix< 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
DiaMatrix< 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:147
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