Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_CooMatrix.hpp
1
24#ifndef MORPHEUS_COOMATRIX_HPP
25#define MORPHEUS_COOMATRIX_HPP
26
27#include <Morpheus_Exceptions.hpp>
28#include <Morpheus_FormatTags.hpp>
29#include <Morpheus_DenseVector.hpp>
30#include <Morpheus_DynamicMatrix.hpp>
31#include <Morpheus_Sort.hpp>
32#include <Morpheus_MatrixBase.hpp>
33
34namespace Morpheus {
35
90template <class ValueType, class... Properties>
91class CooMatrix : public MatrixBase<CooMatrix, ValueType, Properties...> {
92 public:
94 using traits = ContainerTraits<CooMatrix, ValueType, Properties...>;
96 using type = typename traits::type;
97 using base = MatrixBase<CooMatrix, ValueType, Properties...>;
100
105 using size_type = typename traits::size_type;
107 using index_type = typename traits::index_type;
110
111 using array_layout = typename traits::array_layout;
112 using backend = typename traits::backend;
113 using memory_space = typename traits::memory_space;
114 using execution_space = typename traits::execution_space;
115 using device_type = typename traits::device_type;
116 using memory_traits = typename traits::memory_traits;
117 using HostMirror = typename traits::HostMirror;
118
119 using pointer = typename traits::pointer;
120 using const_pointer = typename traits::const_pointer;
121 using reference = typename traits::reference;
122 using const_reference = typename traits::const_reference;
123
126 Morpheus::DenseVector<index_type, size_type, array_layout, backend,
127 memory_traits>;
128 using index_array_pointer = typename index_array_type::value_array_pointer;
129 using index_array_reference =
130 typename index_array_type::value_array_reference;
131
134 Morpheus::DenseVector<value_type, size_type, array_layout, backend,
135 memory_traits>;
136 using value_array_pointer = typename value_array_type::value_array_pointer;
137 using value_array_reference =
138 typename value_array_type::value_array_reference;
139
143 ~CooMatrix() = default;
148 CooMatrix(const CooMatrix &) = default;
153 CooMatrix(CooMatrix &&) = default;
158 CooMatrix &operator=(const CooMatrix &) = default;
164
168 inline CooMatrix() : base(), _row_indices(), _column_indices(), _values() {}
169
178 inline CooMatrix(const size_type num_rows, const size_type num_cols,
179 const size_type num_entries)
180 : base(num_rows, num_cols, num_entries),
181 _row_indices(num_entries),
182 _column_indices(num_entries),
183 _values(num_entries) {}
184
185 // Construct from raw pointers
186 template <typename ValuePtr, typename IndexPtr>
187 explicit inline CooMatrix(
188 const size_type num_rows, const size_type num_cols,
189 const size_type num_entries, IndexPtr rind_ptr, IndexPtr cind_ptr,
190 ValuePtr vals_ptr,
191 typename std::enable_if<
192 (std::is_pointer<ValuePtr>::value &&
194 memory_traits::is_unmanaged) &&
195 (std::is_pointer<IndexPtr>::value &&
197 memory_traits::is_unmanaged)>::type * = nullptr)
198 : base(num_rows, num_cols, num_entries),
199 _row_indices(size_t(num_entries), rind_ptr),
200 _column_indices(size_t(num_entries), cind_ptr),
201 _values(size_t(num_entries), vals_ptr) {}
202
218 template <typename ValueArray, typename IndexArray>
219 explicit inline CooMatrix(
220 const size_type num_rows, const size_type num_cols,
221 const size_type num_entries, IndexArray rind, IndexArray cind,
222 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 _row_indices(rind),
234 _column_indices(cind),
235 _values(vals) {}
236
247 template <class VR, class... PR>
249 typename std::enable_if<is_format_compatible<
250 CooMatrix, CooMatrix<VR, PR...>>::value>::type * = nullptr)
251 : base(src.nrows(), src.ncols(), src.nnnz()),
252 _row_indices(src.crow_indices()),
253 _column_indices(src.ccolumn_indices()),
254 _values(src.cvalues()) {}
255
267 template <class VR, class... PR>
268 typename std::enable_if<
269 is_format_compatible<CooMatrix, CooMatrix<VR, PR...>>::value,
270 CooMatrix &>::type
272 this->set_nrows(src.nrows());
273 this->set_ncols(src.ncols());
274 this->set_nnnz(src.nnnz());
275
276 _row_indices = src.crow_indices();
277 _column_indices = src.ccolumn_indices();
278 _values = src.cvalues();
279
280 return *this;
281 }
282
296 template <class VR, class... PR>
298 typename std::enable_if<is_dynamically_compatible<
299 CooMatrix, DynamicMatrix<VR, PR...>>::value>::type * = nullptr)
300 : base(src.nrows(), src.ncols(), src.nnnz()) {
301 auto f = std::bind(Impl::any_type_assign(), std::placeholders::_1,
302 std::ref(*this));
303
304 std::visit(f, src.const_formats());
305 }
306
320 template <class VR, class... PR>
321 typename std::enable_if<
323 CooMatrix &>::type
325 auto f = std::bind(Impl::any_type_assign(), std::placeholders::_1,
326 std::ref(*this));
327
328 std::visit(f, src.const_formats());
329
330 return *this;
331 }
332
341 template <typename MatrixType>
342 CooMatrix(const MatrixType &src) = delete;
343
352 template <typename MatrixType>
353 reference operator=(const MatrixType &src) = delete;
354
363 inline void resize(const size_type num_rows, const size_type num_cols,
364 const size_type num_entries) {
365 base::resize(num_rows, num_cols, num_entries);
366 _row_indices.resize(num_entries);
367 _column_indices.resize(num_entries);
368 _values.resize(num_entries);
369 }
370
379 template <class VR, class... PR>
380 inline void resize(const CooMatrix<VR, PR...> &src) {
381 resize(src.nrows(), src.ncols(), src.nnnz());
382 }
383
392 template <class VR, class... PR>
394 resize(src.nrows(), src.ncols(), src.nnnz());
395 return *this;
396 }
397
402 void sort_by_row(void) {
403 throw Morpheus::NotImplementedException("CooMatrix.sort_by_row()");
404 }
405
410 void sort(void) { Morpheus::sort_by_row_and_column<backend>(*this); }
411
416 bool is_sorted_by_row(void) {
417 throw Morpheus::NotImplementedException("CooMatrix.is_sorted_by_row()");
418 return true;
419 }
420
426 bool is_sorted(void) { return Morpheus::is_sorted<backend>(*this); }
427
433 formats_e format_enum() const { return _id; }
434
441 int format_index() const { return static_cast<int>(_id); }
442
449 MORPHEUS_FORCEINLINE_FUNCTION index_array_reference row_indices(size_type n) {
450 return _row_indices(n);
451 }
452
460 MORPHEUS_FORCEINLINE_FUNCTION index_array_reference
462 return _column_indices(n);
463 }
464
471 MORPHEUS_FORCEINLINE_FUNCTION value_array_reference values(size_type n) {
472 return _values(n);
473 }
474
482 MORPHEUS_FORCEINLINE_FUNCTION const index_array_reference
484 return _row_indices(n);
485 }
486
494 MORPHEUS_FORCEINLINE_FUNCTION const index_array_reference
496 return _column_indices(n);
497 }
498
505 MORPHEUS_FORCEINLINE_FUNCTION const value_array_reference
507 return _values(n);
508 }
509
515 MORPHEUS_FORCEINLINE_FUNCTION index_array_type &row_indices() {
516 return _row_indices;
517 }
518
524 MORPHEUS_FORCEINLINE_FUNCTION index_array_type &column_indices() {
525 return _column_indices;
526 }
527
533 MORPHEUS_FORCEINLINE_FUNCTION value_array_type &values() { return _values; }
534
540 MORPHEUS_FORCEINLINE_FUNCTION const index_array_type &crow_indices() const {
541 return _row_indices;
542 }
543
549 MORPHEUS_FORCEINLINE_FUNCTION const index_array_type &ccolumn_indices()
550 const {
551 return _column_indices;
552 }
553
559 MORPHEUS_FORCEINLINE_FUNCTION const value_array_type &cvalues() const {
560 return _values;
561 }
562
563 private:
564 index_array_type _row_indices, _column_indices;
565 value_array_type _values;
566 static constexpr formats_e _id = Morpheus::COO_FORMAT;
567};
570} // namespace Morpheus
571
572#endif // MORPHEUS_COOMATRIX_HPP
Implementation of the Coordinate (COO) Sparse Matrix Format Representation.
Definition: Morpheus_CooMatrix.hpp:91
MORPHEUS_FORCEINLINE_FUNCTION index_array_type & column_indices()
Returns a reference to the column indices of the matrix.
Definition: Morpheus_CooMatrix.hpp:524
CooMatrix(const DynamicMatrix< VR, PR... > &src, typename std::enable_if< is_dynamically_compatible< CooMatrix, DynamicMatrix< VR, PR... > >::value >::type *=nullptr)
Constructs a CooMatrix from a compatible DynamicMatrix.
Definition: Morpheus_CooMatrix.hpp:297
std::enable_if< is_format_compatible< CooMatrix, CooMatrix< VR, PR... > >::value, CooMatrix & >::type operator=(const CooMatrix< VR, PR... > &src)
Assigns a CooMatrix from another compatible CooMatrix.
Definition: Morpheus_CooMatrix.hpp:271
typename traits::value_type value_type
Definition: Morpheus_CooMatrix.hpp:102
CooMatrix & operator=(const CooMatrix &)=default
The default copy assignment (shallow copy) of a CooMatrix container from another CooMatrix container ...
CooMatrix(const MatrixType &src)=delete
Construct a CooMatrix object from another storage format. This functionality is disabled to avoid imp...
formats_e format_enum() const
Returns the format enum assigned to the CooMatrix container.
Definition: Morpheus_CooMatrix.hpp:433
MORPHEUS_FORCEINLINE_FUNCTION const value_array_type & cvalues() const
Returns a reference to the values of the matrix.
Definition: Morpheus_CooMatrix.hpp:559
~CooMatrix()=default
The default destructor.
CooMatrix(const size_type num_rows, const size_type num_cols, const size_type num_entries)
Construct a CooMatrix object with shape (num_rows, num_cols) and number of non-zeros equal to num_ent...
Definition: Morpheus_CooMatrix.hpp:178
MORPHEUS_FORCEINLINE_FUNCTION const index_array_reference ccolumn_indices(size_type n) const
Returns a const-reference to the column index of the matrix with index n.
Definition: Morpheus_CooMatrix.hpp:495
void resize(const CooMatrix< VR, PR... > &src)
Resizes CooMatrix with the shape and number of non-zero entries of another CooMatrix with different p...
Definition: Morpheus_CooMatrix.hpp:380
MORPHEUS_FORCEINLINE_FUNCTION index_array_reference column_indices(size_type n)
Returns a reference to the column index of the matrix with index n.
Definition: Morpheus_CooMatrix.hpp:461
MORPHEUS_FORCEINLINE_FUNCTION const value_array_reference cvalues(size_type n) const
Returns a const-reference to the value of the matrix with index n.
Definition: Morpheus_CooMatrix.hpp:506
reference operator=(const MatrixType &src)=delete
Assign to CooMatrix object from another storage format. This functionality is disabled to avoid impli...
CooMatrix & operator=(CooMatrix &&)=default
The default move assignment (shallow copy) of a CooMatrix container from another CooMatrix container ...
MORPHEUS_FORCEINLINE_FUNCTION value_array_type & values()
Returns a reference to the values of the matrix.
Definition: Morpheus_CooMatrix.hpp:533
std::enable_if< is_dynamically_compatible< CooMatrix, DynamicMatrix< VR, PR... > >::value, CooMatrix & >::type operator=(const DynamicMatrix< VR, PR... > &src)
Assigns a CooMatrix from a compatible DynamicMatrix.
Definition: Morpheus_CooMatrix.hpp:324
CooMatrix(const CooMatrix< VR, PR... > &src, typename std::enable_if< is_format_compatible< CooMatrix, CooMatrix< VR, PR... > >::value >::type *=nullptr)
Constructs a CooMatrix from another compatible CooMatrix.
Definition: Morpheus_CooMatrix.hpp:248
typename traits::non_const_index_type non_const_index_type
Definition: Morpheus_CooMatrix.hpp:109
void sort_by_row(void)
Sorts matrix elements by row index.
Definition: Morpheus_CooMatrix.hpp:402
MatrixBase< CooMatrix, ValueType, Properties... > base
The tag associated specificaly to the particular container*‍/.
Definition: Morpheus_CooMatrix.hpp:98
MORPHEUS_FORCEINLINE_FUNCTION const index_array_reference crow_indices(size_type n) const
Returns a const-reference to the row index of the matrix with index n.
Definition: Morpheus_CooMatrix.hpp:483
MORPHEUS_FORCEINLINE_FUNCTION const index_array_type & ccolumn_indices() const
Returns a const-reference to the column indices of the matrix.
Definition: Morpheus_CooMatrix.hpp:549
CooMatrix(const size_type num_rows, const size_type num_cols, const size_type num_entries, IndexArray rind, IndexArray cind, ValueArray vals, typename std::enable_if< is_dense_vector_format_container< ValueArray >::value &&is_dense_vector_format_container< IndexArray >::value &&is_compatible< typename CooMatrix::value_array_type, ValueArray >::value &&is_compatible< typename CooMatrix::index_array_type, IndexArray >::value &&!ValueArray::memory_traits::is_unmanaged &&!IndexArray::memory_traits::is_unmanaged >::type *=nullptr)
Construct a CooMatrix object with shape (num_rows, num_cols) and number of non-zeros equal to num_ent...
Definition: Morpheus_CooMatrix.hpp:219
typename traits::non_const_value_type non_const_value_type
Definition: Morpheus_CooMatrix.hpp:104
CooMatrix(const CooMatrix &)=default
The default copy contructor (shallow copy) of a CooMatrix container from another CooMatrix container ...
Morpheus::DenseVector< value_type, size_type, array_layout, backend, memory_traits > value_array_type
Definition: Morpheus_CooMatrix.hpp:135
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
MORPHEUS_FORCEINLINE_FUNCTION value_array_reference values(size_type n)
Returns a reference to the value of the matrix with index n.
Definition: Morpheus_CooMatrix.hpp:471
bool is_sorted(void)
Determines whether matrix elements are sorted by row and column index.
Definition: Morpheus_CooMatrix.hpp:426
void sort(void)
Sorts matrix elements by row index first and then by column index.
Definition: Morpheus_CooMatrix.hpp:410
int format_index() const
Returns the equivalent index to the format enum assigned to the CooMatrix container.
Definition: Morpheus_CooMatrix.hpp:441
MORPHEUS_FORCEINLINE_FUNCTION index_array_type & row_indices()
Returns a reference to the row indices of the matrix.
Definition: Morpheus_CooMatrix.hpp:515
bool is_sorted_by_row(void)
Determines whether matrix elements are sorted by row index.
Definition: Morpheus_CooMatrix.hpp:416
CooMatrix & allocate(const CooMatrix< VR, PR... > &src)
Allocates memory from another CooMatrix container with different properties.
Definition: Morpheus_CooMatrix.hpp:393
CooMatrix()
Construct an empty CooMatrix object.
Definition: Morpheus_CooMatrix.hpp:168
MORPHEUS_FORCEINLINE_FUNCTION const index_array_type & crow_indices() const
Returns a const-reference to the row indices of the matrix.
Definition: Morpheus_CooMatrix.hpp:540
Morpheus::DenseVector< index_type, size_type, array_layout, backend, memory_traits > index_array_type
Definition: Morpheus_CooMatrix.hpp:127
CooMatrix(CooMatrix &&)=default
The default move contructor (shallow copy) of a CooMatrix container from another CooMatrix container ...
typename traits::size_type size_type
The type of the indices held by the container - can be const.
Definition: Morpheus_CooMatrix.hpp:106
MORPHEUS_FORCEINLINE_FUNCTION index_array_reference row_indices(size_type n)
Returns a reference to the row index of the matrix with index n.
Definition: Morpheus_CooMatrix.hpp:449
void resize(const size_type n)
Resizes DenseVector with size of n and sets values to zero.
Definition: Morpheus_DenseVector.hpp:334
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
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
Definition: Morpheus_Exceptions.hpp:43
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 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
CooMatrix< 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
CooMatrix< 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