Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_DenseVector.hpp
1
23#ifndef MORPHEUS_DENSEVECTOR_HPP
24#define MORPHEUS_DENSEVECTOR_HPP
25
26#include <Morpheus_FormatTraits.hpp>
27#include <Morpheus_FormatTags.hpp>
28#include <Morpheus_ContainerTraits.hpp>
29
30#include <impl/Morpheus_Functors.hpp>
31
32#include <Kokkos_Core.hpp>
33#include <Kokkos_Random.hpp>
34
35namespace Morpheus {
36
81template <class ValueType, class... Properties>
83 : public ContainerTraits<DenseVector, ValueType, Properties...> {
84 public:
86 using traits = ContainerTraits<DenseVector, ValueType, Properties...>;
88 using type = typename traits::type;
91
96 using size_type = typename traits::size_type;
101
102 using array_layout = typename traits::array_layout;
103 using backend = typename traits::backend;
104 using memory_space = typename traits::memory_space;
105 using execution_space = typename traits::execution_space;
106 using device_type = typename traits::device_type;
107 using memory_traits = typename traits::memory_traits;
108 using HostMirror = typename traits::HostMirror;
109
110 using pointer = typename traits::pointer;
111 using const_pointer = typename traits::const_pointer;
112 using reference = typename traits::reference;
113 using const_reference = typename traits::const_reference;
114
117 Kokkos::View<value_type*, array_layout, execution_space, memory_traits>;
118 using value_array_pointer = typename value_array_type::pointer_type;
119 using value_array_reference = typename value_array_type::reference_type;
120
124 ~DenseVector() = default;
129 DenseVector(const DenseVector&) = default;
145
149 inline DenseVector() : _size(0), _values() {}
150
158 inline DenseVector(const size_type n, value_type val = 0)
159 : _size(n), _values("vector", n) {
160 assign(n, val);
161 }
162
171 template <typename ValuePtr>
172 DenseVector(const size_type n, ValuePtr ptr,
173 typename std::enable_if<std::is_pointer<ValuePtr>::value>::type* =
174 nullptr)
175 : _size(n), _values(ptr, n) {
176 static_assert(std::is_same<value_array_pointer, ValuePtr>::value,
177 "Constructing DenseVector to wrap user memory must supply "
178 "matching pointer type");
179 }
180
191 template <typename Generator>
192 inline DenseVector(const size_type n, Generator rand_pool,
193 const value_type range_low, const value_type range_high)
194 : _size(n), _values("vector", n) {
195 Kokkos::fill_random(_values, rand_pool, range_low, range_high);
196 }
197
207 template <class VR, class... PR>
209 const DenseVector<VR, PR...>& src,
210 typename std::enable_if_t<
212 : _size(src.size()), _values(src.const_view()) {}
213
223 template <class VR, class... PR>
224 typename std::enable_if_t<
225 is_compatible<DenseVector, DenseVector<VR, PR...>>::value, DenseVector&>
227 _size = src.size();
228 _values = src.const_view();
229 return *this;
230 }
231
240 template <class VR, class... PR>
242 _size = src.size();
243 this->resize(src.size());
244 return *this;
245 }
246
253 inline void assign(const size_type n, const value_type val) {
254 using range_policy = Kokkos::RangePolicy<size_type, execution_space>;
255
256 range_policy policy(0, n);
258 Kokkos::parallel_for("Morpheus::DenseVector::assign", policy, f);
259 }
260
271 template <typename Generator>
272 inline void assign(const size_type n, Generator rand_pool,
273 const value_type range_low, const value_type range_high) {
274 auto vals = Kokkos::subview(_values, std::make_pair(size_type(0), n));
275 Kokkos::fill_random(vals, rand_pool, range_low, range_high);
276 }
277
284 MORPHEUS_FORCEINLINE_FUNCTION value_array_reference
285 operator()(const size_type i) const {
286 return _values(i);
287 }
288
295 MORPHEUS_FORCEINLINE_FUNCTION value_array_reference
296 operator[](const size_type i) const {
297 return _values(i);
298 }
299
305 inline size_type size() const { return _size; }
306
312 inline value_array_pointer data() const { return _values.data(); }
313
319 inline value_array_type& view() { return _values; }
320
327 inline const value_array_type& const_view() const { return _values; }
328
334 inline void resize(const size_type n) {
335 Kokkos::resize(_values, n);
336 _size = n;
337 }
338
346 inline void resize(const size_type n, const value_type val) {
347 resize(n);
348 assign(n, val);
349 }
350
359 template <class VR, class... PR>
360 inline void resize(const DenseVector<VR, PR...>& src) {
361 resize(src.size());
362 }
363
364 private:
365 size_type _size;
366 value_array_type _values;
367};
368
371} // namespace Morpheus
372
373#endif // MORPHEUS_DENSEVECTOR_HPP
The DenseVector container is a one-dimensional container that contains contiguous elements....
Definition: Morpheus_DenseVector.hpp:83
value_array_type & view()
Returns a reference to the beginning of the view that holds the data.
Definition: Morpheus_DenseVector.hpp:319
void assign(const size_type n, Generator rand_pool, const value_type range_low, const value_type range_high)
Assigns n elements of values between range_low and range_high to the DenseVector.
Definition: Morpheus_DenseVector.hpp:272
DenseVector(const DenseVector &)=default
The default copy contructor (shallow copy) of a DenseVector container from another DenseVector contai...
typename traits::index_type index_type
Definition: Morpheus_DenseVector.hpp:98
typename traits::type type
Definition: Morpheus_DenseVector.hpp:88
DenseVector(DenseVector &&)=default
The default move contructor (shallow copy) of a DenseVector container from another DenseVector contai...
void assign(const size_type n, const value_type val)
Assigns n elements of value val to the DenseVector.
Definition: Morpheus_DenseVector.hpp:253
void resize(const size_type n)
Resizes DenseVector with size of n and sets values to zero.
Definition: Morpheus_DenseVector.hpp:334
DenseVector(const size_type n, Generator rand_pool, const value_type range_low, const value_type range_high)
Construct a DenseVector object with values from range_low to range_high.
Definition: Morpheus_DenseVector.hpp:192
Kokkos::View< value_type *, array_layout, execution_space, memory_traits > value_array_type
Definition: Morpheus_DenseVector.hpp:117
void resize(const size_type n, const value_type val)
Resizes DenseVector with size of n and sets values to val. Note that compared to assign() member func...
Definition: Morpheus_DenseVector.hpp:346
DenseVector(const size_type n, value_type val=0)
Construct a DenseVector object with size n and values set to val.
Definition: Morpheus_DenseVector.hpp:158
typename traits::non_const_index_type non_const_index_type
Definition: Morpheus_DenseVector.hpp:100
const value_array_type & const_view() const
Returns a constant reference to the beginning of the view that holds the data.
Definition: Morpheus_DenseVector.hpp:327
size_type size() const
Returns the size of the container.
Definition: Morpheus_DenseVector.hpp:305
typename traits::non_const_value_type non_const_value_type
Definition: Morpheus_DenseVector.hpp:95
DenseVector & operator=(const DenseVector &)=default
The default copy assignment (shallow copy) of a DenseVector container from another DenseVector contai...
DenseVector & operator=(DenseVector &&)=default
The default move assignment (shallow copy) of a DenseVector container from another DenseVector contai...
std::enable_if_t< is_compatible< DenseVector, DenseVector< VR, PR... > >::value, DenseVector & > operator=(const DenseVector< VR, PR... > &src)
Shallow Copy Assignment from another DenseVector container with different properties....
Definition: Morpheus_DenseVector.hpp:226
typename VectorFormatTag< Morpheus::DenseVectorFormatTag >::tag tag
Definition: Morpheus_DenseVector.hpp:90
typename traits::value_type value_type
Definition: Morpheus_DenseVector.hpp:93
MORPHEUS_FORCEINLINE_FUNCTION value_array_reference operator[](const size_type i) const
Returns a reference to the element with index i.
Definition: Morpheus_DenseVector.hpp:296
DenseVector(const size_type n, ValuePtr ptr, typename std::enable_if< std::is_pointer< ValuePtr >::value >::type *=nullptr)
Construct a DenseVector object from a raw pointer. This is only enabled if the DenseVector is an unma...
Definition: Morpheus_DenseVector.hpp:172
~DenseVector()=default
The default destructor.
void resize(const DenseVector< VR, PR... > &src)
Resizes DenseVector with the shape another DenseVector with different parameters.
Definition: Morpheus_DenseVector.hpp:360
DenseVector()
Construct an empty DenseVector object.
Definition: Morpheus_DenseVector.hpp:149
DenseVector(const DenseVector< VR, PR... > &src, typename std::enable_if_t< is_compatible< DenseVector, DenseVector< VR, PR... > >::value > *=nullptr)
Shallow Copy contrustor from another DenseVector container with different properties....
Definition: Morpheus_DenseVector.hpp:208
MORPHEUS_FORCEINLINE_FUNCTION value_array_reference operator()(const size_type i) const
Returns a reference to the element with index i.
Definition: Morpheus_DenseVector.hpp:285
value_array_pointer data() const
Returns a pointer to the data at the beginning of the container.
Definition: Morpheus_DenseVector.hpp:312
DenseVector & allocate(const DenseVector< VR, PR... > &src)
Allocates memory from another DenseVector container with different properties.
Definition: Morpheus_DenseVector.hpp:241
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
Generic Morpheus interfaces.
Definition: dummy.cpp:24
Traits class for accessing attributes of a Container (Matrix or Vector)
Definition: Morpheus_ContainerTraits.hpp:54
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
DenseVector< 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
DenseVector< 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 Vector Containers (Dense) with Dense Format.
Definition: Morpheus_FormatTags.hpp:77
Definition: Morpheus_Functors.hpp:32
A wrapper that checks if the provided type is a scalar type.
Definition: Morpheus_TypeTraits.hpp:85