mgcpp
A C++ Math Library Based on CUDA
device_vector.hpp
Go to the documentation of this file.
1 
2 // Copyright RedPortal, mujjingun 2017 - 2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef _MGCPP_VECTOR_DEVICE_VECTOR_HPP_
8 #define _MGCPP_VECTOR_DEVICE_VECTOR_HPP_
9 
13 #include <mgcpp/global/shape.hpp>
14 #include <mgcpp/system/concept.hpp>
18 
19 #include <cstdlib>
20 #include <initializer_list>
21 #include <vector>
22 
23 namespace mgcpp {
24 template <typename Type, typename Alloc = mgcpp::allocator<Type>>
25 class device_vector : public dense_vector<device_vector<Type, Alloc>, Type> {
26  static_assert(is_supported_type<Type>::value, "Element type not supported.");
27 
28  public:
30  using value_type = Type;
31  using pointer = value_type*;
32  using const_pointer = value_type const*;
37  template <typename T>
39  using allocator_type = Alloc;
42 
43  private:
44  thread_context* _context;
45  size_t _shape;
46  Alloc _allocator;
47  device_pointer _data;
48  size_t _capacity;
49 
50  public:
51  inline device_vector() noexcept;
52 
53  inline ~device_vector() noexcept;
54 
55  inline explicit device_vector(Alloc const& alloc) noexcept;
56 
57  inline explicit device_vector(size_t size, Alloc const& alloc = Alloc());
58 
59  inline explicit device_vector(size_t size,
61  Alloc const& alloc = Alloc());
62 
63  inline explicit device_vector(size_t size,
65  Alloc const& alloc = Alloc());
66 
67  inline explicit device_vector(std::initializer_list<value_type> const& array,
68  Alloc const& alloc = Alloc());
69 
70  inline explicit device_vector(std::vector<value_type> const& vec,
71  Alloc const& alloc = Alloc());
72 
73  template <typename HostVec, MGCPP_CONCEPT(adapter<HostVec>::value)>
74  inline explicit device_vector(HostVec const& host_mat,
75  Alloc const& alloc = Alloc());
76 
77  inline device_vector(device_vector<Type, Alloc> const& other);
78 
79  template <typename DenseVec>
80  inline explicit device_vector(dense_vector<DenseVec, Type> const& other);
81 
82  inline device_vector(device_vector<Type, Alloc>&& other) noexcept;
83 
84  template <size_t S>
85  static inline device_vector from_c_array(Type (&arr)[S],
86  Alloc const& alloc = Alloc());
87 
88  template <typename DenseVec>
89  inline device_vector<Type, Alloc>& operator=(
90  dense_vector<DenseVec, Type> const& other);
91 
92  inline device_vector<Type, Alloc>& operator=(
93  device_vector<Type, Alloc> const& other);
94 
95  inline device_vector<Type, Alloc>& operator=(
96  device_vector<Type, Alloc>&& other) noexcept;
97 
98  inline device_vector<Type, Alloc>& zero();
99 
100  inline device_vector<Type, Alloc>& resize(
101  size_t size,
102  value_type pad_value = value_type{});
103 
104  inline void copy_to_host(pointer host_p) const;
105 
106  inline value_type check_value(size_t i) const;
107 
108  inline void set_value(size_t i, value_type value);
109 
110  inline const_device_pointer data() const noexcept;
111 
112  inline device_pointer data_mutable() noexcept;
113 
114  inline size_t capacity() const noexcept;
115 
116  inline device_pointer release_data() noexcept;
117 
118  inline thread_context* context() const noexcept;
119 
120  inline shape_type shape() const noexcept;
121 
122  inline Alloc allocator() const noexcept;
123 
124  inline size_t device_id() const noexcept;
125 
126  inline size_t size() const noexcept;
127 };
128 } // namespace mgcpp
129 
131 #endif
size_t capacity() const noexcept
Definition: adapter_base.hpp:14
Definition: adapter_base.hpp:12
Definition: thread_context.hpp:20
device_vector< Type, Alloc > & resize(size_t size, value_type pad_value=value_type{})
size_t size() const noexcept
device_vector< Type, Alloc > & zero()
value_type check_value(size_t i) const
Definition: shape.hpp:33
device_pointer data_mutable() noexcept
Definition: dvec_expr.hpp:20
Type value_type
Definition: device_vector.hpp:30
Definition: dense_vector.hpp:15
Definition: shape.hpp:10
device_vector() noexcept
Alloc allocator_type
Definition: device_vector.hpp:39
typename device_value_type< Type >::type device_value_type
Definition: device_vector.hpp:33
Definition: is_supported_type.hpp:16
value_type const * const_pointer
Definition: device_vector.hpp:32
Definition: device_vector.hpp:25
size_t device_id() const noexcept
thread_context * context() const noexcept
const_device_pointer data() const noexcept
void copy_to_host(pointer host_p) const
#define MGCPP_CONCEPT(...)
Definition: concept.hpp:10
device_value_type const * const_device_pointer
Definition: device_vector.hpp:35
device_vector< Type, Alloc > this_type
Definition: device_vector.hpp:29
value_type * pointer
Definition: device_vector.hpp:31
void init(bool print_system_info=true)
Alloc allocator() const noexcept
device_value_type * device_pointer
Definition: device_vector.hpp:34
Type type
Definition: device_value_type.hpp:20
shape_type shape() const noexcept
void set_value(size_t i, value_type value)
static device_vector from_c_array(Type(&arr)[S], Alloc const &alloc=Alloc())
device_pointer release_data() noexcept