Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_DenseMatrix.hpp
1
24#ifndef MORPHEUS_DENSEMATRIX_HPP
25#define MORPHEUS_DENSEMATRIX_HPP
26
27#include <Morpheus_Exceptions.hpp>
28#include <Morpheus_FormatTags.hpp>
29#include <Morpheus_MatrixBase.hpp>
30
31#include <impl/Morpheus_Functors.hpp>
32
33#include <Kokkos_Core.hpp>
34
35namespace Morpheus {
36
73template <class ValueType, class... Properties>
74class DenseMatrix : public MatrixBase<DenseMatrix, ValueType, Properties...> {
75 public:
77 using traits = ContainerTraits<DenseMatrix, ValueType, Properties...>;
79 using type = typename traits::type;
80 using base = MatrixBase<DenseMatrix, ValueType, Properties...>;
83
88 using size_type = typename traits::size_type;
93
94 using array_layout = typename traits::array_layout;
95 using backend = typename traits::backend;
96 using memory_space = typename traits::memory_space;
97 using execution_space = typename traits::execution_space;
98 using device_type = typename traits::device_type;
99 using memory_traits = typename traits::memory_traits;
100 using HostMirror = typename traits::HostMirror;
101
102 using pointer = typename traits::pointer;
103 using const_pointer = typename traits::const_pointer;
104 using reference = typename traits::reference;
105 using const_reference = typename traits::const_reference;
106
109 Kokkos::View<value_type **, array_layout, execution_space, memory_traits>;
110 using value_array_pointer = typename value_array_type::pointer_type;
111 using value_array_reference = typename value_array_type::reference_type;
112
116 ~DenseMatrix() = default;
121 DenseMatrix(const DenseMatrix &) = default;
131 DenseMatrix &operator=(const DenseMatrix &) = default;
137
141 inline DenseMatrix() : base(), _values() {}
142
151 inline DenseMatrix(const size_type num_rows, const size_type num_cols,
152 const value_type val = 0)
153 : base(num_rows, num_cols, num_rows * num_cols),
154 _values("matrix", size_t(num_rows), size_t(num_cols)) {
155 assign(num_rows, num_cols, val);
156 }
157
167 template <typename ValuePtr>
168 explicit DenseMatrix(
169 const size_type num_rows, const size_type num_cols, ValuePtr ptr,
170 typename std::enable_if<std::is_pointer<ValuePtr>::value &&
172 memory_traits::is_unmanaged>::type * = nullptr)
173 : base(num_rows, num_cols, num_rows * num_cols),
174 _values(ptr, size_t(num_rows), size_t(num_cols)) {
175 static_assert(std::is_same<value_array_pointer, ValuePtr>::value,
176 "Constructing DenseMatrix to wrap user memory must supply "
177 "matching pointer type");
178 }
179
189 template <class VR, class... PR>
191 const DenseMatrix<VR, PR...> &src,
192 typename std::enable_if<is_format_compatible<
193 DenseMatrix, DenseMatrix<VR, PR...>>::value>::type * = nullptr)
194 : base(src.nrows(), src.ncols(), src.nrows() * src.ncols()),
195 _values(src.const_view()) {}
196
206 template <class VR, class... PR>
207 typename std::enable_if<
208 is_format_compatible<DenseMatrix, DenseMatrix<VR, PR...>>::value,
211 this->set_nrows(src.nrows());
212 this->set_ncols(src.ncols());
213 this->set_nnnz(src.nnnz());
214 _values = src.const_view();
215 return *this;
216 }
217
226 template <class MatrixType>
227 DenseMatrix(const MatrixType &src) = delete;
228
237 template <class MatrixType>
238 reference operator=(const MatrixType &src) = delete;
239
248 inline void assign(size_type num_rows, size_type num_cols,
249 const value_type val) {
250 using range_policy = Kokkos::RangePolicy<size_type, execution_space>;
251
252 range_policy policy(0, num_rows);
254 num_cols);
255 Kokkos::parallel_for("Morpheus::DenseMatrix::assign", policy, f);
256 }
257
265 inline void resize(size_type num_rows, size_type num_cols) {
266 base::resize(num_rows, num_cols, num_rows * num_cols);
267 Kokkos::resize(_values, size_t(num_rows), size_t(num_cols));
268 }
269
278 template <class VR, class... PR>
279 inline void resize(const DenseMatrix<VR, PR...> &src) {
280 resize(src.nrows(), src.ncols());
281 }
282
291 template <class VR, class... PR>
293 resize(src.nrows(), src.ncols());
294 return *this;
295 }
296
304 MORPHEUS_FORCEINLINE_FUNCTION value_array_reference
305 operator()(size_type i, size_type j) const {
306 return _values(i, j);
307 }
308
314 inline value_array_pointer data() const { return _values.data(); }
315
321 inline value_array_type &view() { return _values; }
322
329 inline const value_array_type &const_view() const { return _values; }
330
331 private:
332 value_array_type _values;
333};
334
337} // namespace Morpheus
338
339#endif // MORPHEUS_DENSEMATRIX_HPP
The DenseMatrix container is a two-dimensional dense container that contains contiguous elements....
Definition: Morpheus_DenseMatrix.hpp:74
void assign(size_type num_rows, size_type num_cols, const value_type val)
Assigns (num_rows * num_cols) elements of value val to the DenseMatrix. The container will always res...
Definition: Morpheus_DenseMatrix.hpp:248
void resize(const DenseMatrix< VR, PR... > &src)
Resizes DenseVector with the shape another DenseMatrix with different parameters.
Definition: Morpheus_DenseMatrix.hpp:279
value_array_pointer data() const
Returns a pointer to the data at the beginning of the container.
Definition: Morpheus_DenseMatrix.hpp:314
typename MatrixFormatTag< Morpheus::DenseMatrixFormatTag >::tag tag
Definition: Morpheus_DenseMatrix.hpp:82
DenseMatrix(const DenseMatrix &)=default
The default copy contructor (shallow copy) of a DenseMatrix container from another DenseMatrix contai...
DenseMatrix & operator=(DenseMatrix &&)=default
The default move assignment (shallow copy) of a DenseMatrix container from another DenseMatrix contai...
MORPHEUS_FORCEINLINE_FUNCTION value_array_reference operator()(size_type i, size_type j) const
Returns a reference to the element with index (i,j)
Definition: Morpheus_DenseMatrix.hpp:305
typename traits::type type
Definition: Morpheus_DenseMatrix.hpp:79
value_array_type & view()
Returns a reference to the beginning of the view that holds the data.
Definition: Morpheus_DenseMatrix.hpp:321
Kokkos::View< value_type **, array_layout, execution_space, memory_traits > value_array_type
Definition: Morpheus_DenseMatrix.hpp:109
DenseMatrix()
Construct an empty DenseVector object.
Definition: Morpheus_DenseMatrix.hpp:141
typename traits::index_type index_type
Definition: Morpheus_DenseMatrix.hpp:90
typename traits::non_const_index_type non_const_index_type
Definition: Morpheus_DenseMatrix.hpp:92
DenseMatrix(const size_type num_rows, const size_type num_cols, ValuePtr ptr, typename std::enable_if< std::is_pointer< ValuePtr >::value &&is_same_value_type< value_type, ValuePtr >::value &&memory_traits::is_unmanaged >::type *=nullptr)
Construct a DenseMatrix object from a raw pointer. This is only enabled if the DenseMatrix is an unma...
Definition: Morpheus_DenseMatrix.hpp:168
typename traits::value_type value_type
Definition: Morpheus_DenseMatrix.hpp:85
const value_array_type & const_view() const
Returns a constant reference to the beginning of the view that holds the data.
Definition: Morpheus_DenseMatrix.hpp:329
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
reference operator=(const MatrixType &src)=delete
Assign to DenseMatrix object from another storage format. This functionality is disabled to avoid imp...
DenseMatrix(DenseMatrix &&)=default
The default move contructor (shallow copy) of a DenseMatrix container from another DenseMatrix contai...
DenseMatrix(const MatrixType &src)=delete
Construct a DenserMatrix object from another storage format. This functionality is disabled to avoid ...
typename traits::non_const_value_type non_const_value_type
Definition: Morpheus_DenseMatrix.hpp:87
~DenseMatrix()=default
Default destructor.
DenseMatrix & operator=(const DenseMatrix &)=default
The default copy assignment (shallow copy) of a DenseMatrix container from another DenseMatrix contai...
DenseMatrix(const DenseMatrix< VR, PR... > &src, typename std::enable_if< is_format_compatible< DenseMatrix, DenseMatrix< VR, PR... > >::value >::type *=nullptr)
Shallow Copy contrustor from another DenseMatrix container with different properties....
Definition: Morpheus_DenseMatrix.hpp:190
DenseMatrix(const size_type num_rows, const size_type num_cols, const value_type val=0)
Construct a DenseMatrix object with shape (num_rows, num_cols) and values set to val.
Definition: Morpheus_DenseMatrix.hpp:151
DenseMatrix & allocate(const DenseMatrix< VR, PR... > &src)
Allocates memory from another DenseMatrix container with different properties.
Definition: Morpheus_DenseMatrix.hpp:292
std::enable_if< is_format_compatible< DenseMatrix, DenseMatrix< VR, PR... > >::value, DenseMatrix & >::type operator=(const DenseMatrix< VR, PR... > &src)
Shallow Copy Assignment from another DenseMatrix container with different properties....
Definition: Morpheus_DenseMatrix.hpp:210
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 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
DenseMatrix< 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
DenseMatrix< 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_Functors.hpp:32
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