Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_CsrMatrix.hpp
1
24#ifndef MORPHEUS_CSRMATRIX_HPP
25#define MORPHEUS_CSRMATRIX_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_MatrixBase.hpp>
32
33namespace Morpheus {
34
90template <class ValueType, class... Properties>
91class CsrMatrix : public MatrixBase<CsrMatrix, ValueType, Properties...> {
92 public:
94 using traits = ContainerTraits<CsrMatrix, ValueType, Properties...>;
96 using type = typename traits::type;
97 using base = MatrixBase<CsrMatrix, ValueType, Properties...>;
100
105 using size_type = typename traits::size_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>;
129 using index_array_pointer = typename index_array_type::value_array_pointer;
130 using index_array_reference =
131 typename index_array_type::value_array_reference;
132 using const_index_array_reference = const index_array_reference;
133
136 Morpheus::DenseVector<value_type, size_type, array_layout, backend,
137 memory_traits>;
139 using value_array_pointer = typename value_array_type::value_array_pointer;
140 using value_array_reference =
141 typename value_array_type::value_array_reference;
142 using const_value_array_reference = const value_array_reference;
143
147 ~CsrMatrix() = default;
152 CsrMatrix(const CsrMatrix &) = default;
157 CsrMatrix(CsrMatrix &&) = default;
162 CsrMatrix &operator=(const CsrMatrix &) = default;
168
172 inline CsrMatrix() : base(), _row_offsets(), _column_indices(), _values() {}
173
182 inline CsrMatrix(const size_type num_rows, const size_type num_cols,
183 const size_type num_entries)
184 : base(num_rows, num_cols, num_entries),
185 _row_offsets(num_rows + 1),
186 _column_indices(num_entries),
187 _values(num_entries) {}
188
189 // Construct from raw pointers
190 template <typename ValuePtr, typename IndexPtr>
191 explicit inline CsrMatrix(
192 const size_type num_rows, const size_type num_cols,
193 const size_type num_entries, IndexPtr roff_ptr, IndexPtr cind_ptr,
194 ValuePtr vals_ptr,
195 typename std::enable_if<
196 (std::is_pointer<ValuePtr>::value &&
198 memory_traits::is_unmanaged) &&
199 (std::is_pointer<IndexPtr>::value &&
201 memory_traits::is_unmanaged)>::type * = nullptr)
202 : base(num_rows, num_cols, num_entries),
203 _row_offsets(num_rows + 1, roff_ptr),
204 _column_indices(num_entries, cind_ptr),
205 _values(num_entries, vals_ptr) {}
206
222 template <typename ValueArray, typename IndexArray>
223 explicit inline CsrMatrix(
224 const size_type num_rows, const size_type num_cols,
225 const size_type num_entries, IndexArray roff, IndexArray cind,
226 ValueArray vals,
227 typename std::enable_if<
231 ValueArray>::value &&
233 IndexArray>::value &&
234 !ValueArray::memory_traits::is_unmanaged &&
235 !IndexArray::memory_traits::is_unmanaged>::type * = nullptr)
236 : base(num_rows, num_cols, num_entries),
237 _row_offsets(roff),
238 _column_indices(cind),
239 _values(vals) {}
240
251 template <class VR, class... PR>
253 typename std::enable_if<is_format_compatible<
254 CsrMatrix, CsrMatrix<VR, PR...>>::value>::type * = nullptr)
255 : base(src.nrows(), src.ncols(), src.nnnz()),
256 _row_offsets(src.crow_offsets()),
257 _column_indices(src.ccolumn_indices()),
258 _values(src.cvalues()) {}
259
271 template <class VR, class... PR>
272 typename std::enable_if<
273 is_format_compatible<CsrMatrix, CsrMatrix<VR, PR...>>::value,
276 this->set_nrows(src.nrows());
277 this->set_ncols(src.ncols());
278 this->set_nnnz(src.nnnz());
279
280 _row_offsets = src.crow_offsets();
281 _column_indices = src.ccolumn_indices();
282 _values = src.cvalues();
283
284 return *this;
285 }
286
300 template <class VR, class... PR>
302 typename std::enable_if<is_dynamically_compatible<
303 CsrMatrix, DynamicMatrix<VR, PR...>>::value>::type * = nullptr)
304 : base(src.nrows(), src.ncols(), src.nnnz()) {
305 auto f = std::bind(Impl::any_type_assign(), std::placeholders::_1,
306 std::ref(*this));
307
308 std::visit(f, src.const_formats());
309 }
310
324 template <class VR, class... PR>
325 typename std::enable_if<
329 auto f = std::bind(Impl::any_type_assign(), std::placeholders::_1,
330 std::ref(*this));
331
332 std::visit(f, src.const_formats());
333
334 return *this;
335 }
336
345 template <typename MatrixType>
346 CsrMatrix(const MatrixType &src) = delete;
347
356 template <typename MatrixType>
357 reference operator=(const MatrixType &src) = delete;
358
367 inline void resize(const size_type num_rows, const size_type num_cols,
368 const size_type num_entries) {
369 base::resize(num_rows, num_cols, num_entries);
370 _row_offsets.resize(num_rows + 1);
371 _column_indices.resize(num_entries);
372 _values.resize(num_entries);
373 }
374
383 template <class VR, class... PR>
384 inline void resize(const CsrMatrix<VR, PR...> &src) {
385 resize(src.nrows(), src.ncols(), src.nnnz());
386 }
387
396 template <class VR, class... PR>
398 resize(src.nrows(), src.ncols(), src.nnnz());
399 return *this;
400 }
401
407 formats_e format_enum() const { return _id; }
408
415 int format_index() const { return static_cast<int>(_id); }
416
423 MORPHEUS_FORCEINLINE_FUNCTION index_array_reference row_offsets(size_type n) {
424 return _row_offsets(n);
425 }
426
434 MORPHEUS_FORCEINLINE_FUNCTION index_array_reference
435 column_indices(size_type n) {
436 return _column_indices(n);
437 }
438
445 MORPHEUS_FORCEINLINE_FUNCTION value_array_reference values(size_type n) {
446 return _values(n);
447 }
448
456 MORPHEUS_FORCEINLINE_FUNCTION const_index_array_reference
457 crow_offsets(size_type n) const {
458 return _row_offsets(n);
459 }
460
468 MORPHEUS_FORCEINLINE_FUNCTION const_index_array_reference
469 ccolumn_indices(size_type n) const {
470 return _column_indices(n);
471 }
472
479 MORPHEUS_FORCEINLINE_FUNCTION const_value_array_reference
480 cvalues(size_type n) const {
481 return _values(n);
482 }
483
489 MORPHEUS_FORCEINLINE_FUNCTION index_array_type &row_offsets() {
490 return _row_offsets;
491 }
492
498 MORPHEUS_FORCEINLINE_FUNCTION index_array_type &column_indices() {
499 return _column_indices;
500 }
501
507 MORPHEUS_FORCEINLINE_FUNCTION value_array_type &values() { return _values; }
508
514 MORPHEUS_FORCEINLINE_FUNCTION const_index_array_type &crow_offsets() const {
515 return _row_offsets;
516 }
517
523 MORPHEUS_FORCEINLINE_FUNCTION const_index_array_type &ccolumn_indices()
524 const {
525 return _column_indices;
526 }
527
533 MORPHEUS_FORCEINLINE_FUNCTION const_value_array_type &cvalues() const {
534 return _values;
535 }
536
537 private:
538 index_array_type _row_offsets, _column_indices;
539 value_array_type _values;
540 static constexpr formats_e _id = Morpheus::CSR_FORMAT;
541};
544} // namespace Morpheus
545
546#endif // MORPHEUS_CSRMATRIX_HPP
Implementation of the Compressed-Sparse Row (CSR) Sparse Matrix Format Representation.
Definition: Morpheus_CsrMatrix.hpp:91
MORPHEUS_FORCEINLINE_FUNCTION const_index_array_type & ccolumn_indices() const
Returns a const-reference to the column indices of the matrix.
Definition: Morpheus_CsrMatrix.hpp:523
MORPHEUS_FORCEINLINE_FUNCTION index_array_type & column_indices()
Returns a reference to the column indices of the matrix.
Definition: Morpheus_CsrMatrix.hpp:498
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_CsrMatrix.hpp:469
CsrMatrix()
Construct an empty CsrMatrix object.
Definition: Morpheus_CsrMatrix.hpp:172
MORPHEUS_FORCEINLINE_FUNCTION const_value_array_type & cvalues() const
Returns a reference to the values of the matrix.
Definition: Morpheus_CsrMatrix.hpp:533
std::enable_if< is_format_compatible< CsrMatrix, CsrMatrix< VR, PR... > >::value, CsrMatrix & >::type operator=(const CsrMatrix< VR, PR... > &src)
Assigns a CsrMatrix from another compatible CsrMatrix.
Definition: Morpheus_CsrMatrix.hpp:275
CsrMatrix(const CsrMatrix< VR, PR... > &src, typename std::enable_if< is_format_compatible< CsrMatrix, CsrMatrix< VR, PR... > >::value >::type *=nullptr)
Constructs a CsrMatrix from another compatible CsrMatrix.
Definition: Morpheus_CsrMatrix.hpp:252
MORPHEUS_FORCEINLINE_FUNCTION const_index_array_type & crow_offsets() const
Returns a const-reference to the row offsets of the matrix.
Definition: Morpheus_CsrMatrix.hpp:514
typename traits::type type
Definition: Morpheus_CsrMatrix.hpp:96
~CsrMatrix()=default
The default destructor.
typename traits::value_type value_type
Definition: Morpheus_CsrMatrix.hpp:102
MORPHEUS_FORCEINLINE_FUNCTION value_array_type & values()
Returns a reference to the values of the matrix.
Definition: Morpheus_CsrMatrix.hpp:507
typename traits::index_type index_type
Definition: Morpheus_CsrMatrix.hpp:107
CsrMatrix & operator=(CsrMatrix &&)=default
The default move assignment (shallow copy) of a CsrMatrix container from another CsrMatrix container ...
CsrMatrix & operator=(const CsrMatrix &)=default
The default copy assignment (shallow copy) of a CsrMatrix container from another CsrMatrix container ...
int format_index() const
Returns the equivalent index to the format enum assigned to the CsrMatrix container.
Definition: Morpheus_CsrMatrix.hpp:415
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_CsrMatrix.hpp:480
CsrMatrix(const MatrixType &src)=delete
Construct a CsrMatrix object from another storage format. This functionality is disabled to avoid imp...
MORPHEUS_FORCEINLINE_FUNCTION const_index_array_reference crow_offsets(size_type n) const
Returns a const-reference to the row offset of the matrix with index n.
Definition: Morpheus_CsrMatrix.hpp:457
typename traits::non_const_value_type non_const_value_type
Definition: Morpheus_CsrMatrix.hpp:104
MORPHEUS_FORCEINLINE_FUNCTION value_array_reference values(size_type n)
Returns a reference to the value of the matrix with index n.
Definition: Morpheus_CsrMatrix.hpp:445
CsrMatrix(const size_type num_rows, const size_type num_cols, const size_type num_entries)
Construct a CsrMatrix object with shape (num_rows, num_cols) and number of non-zeros equal to num_ent...
Definition: Morpheus_CsrMatrix.hpp:182
CsrMatrix(const CsrMatrix &)=default
The default copy contructor (shallow copy) of a CsrMatrix container from another CsrMatrix container ...
MORPHEUS_FORCEINLINE_FUNCTION index_array_reference row_offsets(size_type n)
Returns a reference to the row offset of the matrix with index n.
Definition: Morpheus_CsrMatrix.hpp:423
std::enable_if< is_dynamically_compatible< CsrMatrix, DynamicMatrix< VR, PR... > >::value, CsrMatrix & >::type operator=(const DynamicMatrix< VR, PR... > &src)
Assigns a CsrMatrix from a compatible DynamicMatrix.
Definition: Morpheus_CsrMatrix.hpp:328
typename traits::non_const_index_type non_const_index_type
Definition: Morpheus_CsrMatrix.hpp:109
formats_e format_enum() const
Returns the format enum assigned to the CsrMatrix container.
Definition: Morpheus_CsrMatrix.hpp:407
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_CsrMatrix.hpp:435
Morpheus::DenseVector< index_type, size_type, array_layout, backend, memory_traits > index_array_type
Definition: Morpheus_CsrMatrix.hpp:127
typename MatrixFormatTag< Morpheus::CsrFormatTag >::tag tag
Definition: Morpheus_CsrMatrix.hpp:99
MORPHEUS_FORCEINLINE_FUNCTION index_array_type & row_offsets()
Returns a reference to the row offsets of the matrix.
Definition: Morpheus_CsrMatrix.hpp:489
CsrMatrix & allocate(const CsrMatrix< VR, PR... > &src)
Allocates memory from another CsrMatrix container with different properties.
Definition: Morpheus_CsrMatrix.hpp:397
reference operator=(const MatrixType &src)=delete
Assigns to CsrMatrix object from another storage format. This functionality is disabled to avoid impl...
CsrMatrix(const size_type num_rows, const size_type num_cols, const size_type num_entries, IndexArray roff, 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 CsrMatrix::value_array_type, ValueArray >::value &&is_compatible< typename CsrMatrix::index_array_type, IndexArray >::value &&!ValueArray::memory_traits::is_unmanaged &&!IndexArray::memory_traits::is_unmanaged >::type *=nullptr)
Construct a CsrMatrix object with shape (num_rows, num_cols) and number of non-zeros equal to num_ent...
Definition: Morpheus_CsrMatrix.hpp:223
Morpheus::DenseVector< value_type, size_type, array_layout, backend, memory_traits > value_array_type
Definition: Morpheus_CsrMatrix.hpp:137
void resize(const CsrMatrix< VR, PR... > &src)
Resizes CsrMatrix with the shape and number of non-zero entries of another CsrMatrix with different p...
Definition: Morpheus_CsrMatrix.hpp:384
CsrMatrix(const DynamicMatrix< VR, PR... > &src, typename std::enable_if< is_dynamically_compatible< CsrMatrix, DynamicMatrix< VR, PR... > >::value >::type *=nullptr)
Constructs a CsrMatrix from a compatible DynamicMatrix.
Definition: Morpheus_CsrMatrix.hpp:301
void resize(const size_type num_rows, const size_type num_cols, const size_type num_entries)
Resizes CsrMatrix with shape of (num_rows, num_cols) and sets number of non-zero entries to num_entri...
Definition: Morpheus_CsrMatrix.hpp:367
CsrMatrix(CsrMatrix &&)=default
The default move contructor (shallow copy) of a CsrMatrix container from another CsrMatrix container ...
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
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
CsrMatrix< 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
CsrMatrix< 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