mgcpp
A C++ Math Library Based on CUDA
device_matrix.hpp
Go to the documentation of this file.
1 
2 // Copyright RedPortal 2017 - 2017.
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_MATRIX_DEVICE_MATRIX_HPP_
8 #define _MGCPP_MATRIX_DEVICE_MATRIX_HPP_
9 
14 #include <mgcpp/global/shape.hpp>
18 #include <mgcpp/system/concept.hpp>
21 
22 #include <cstdlib>
23 #include <initializer_list>
24 #include <type_traits>
25 
26 namespace mgcpp {
27 template <typename Type, typename Alloc = mgcpp::allocator<Type>>
28 class device_matrix : public dense_matrix<device_matrix<Type, Alloc>, Type> {
29  static_assert(is_supported_type<Type>::value, "Element type not supported.");
30 
31  public:
33  using value_type = Type;
34  using pointer = value_type*;
35  using const_pointer = value_type const*;
40  template <typename T>
42  using allocator_type = Alloc;
44 
45  private:
46  thread_context* _context;
47  shape_type _shape;
48  Alloc _allocator;
49  device_pointer _data;
50  size_t _capacity;
51 
52  static inline size_t determine_ndim(
53  std::initializer_list<std::initializer_list<value_type>> const&
54  list) noexcept;
55 
56  public:
57  inline device_matrix() noexcept;
58 
59  inline ~device_matrix() noexcept;
60 
61  inline explicit device_matrix(Alloc const& alloc);
62 
63  inline explicit device_matrix(shape_type shape, Alloc const& alloc = Alloc());
64 
65  inline explicit device_matrix(shape_type shape,
67  Alloc const& alloc = Alloc());
68 
69  inline explicit device_matrix(shape_type shape,
71  Alloc const& alloc = Alloc());
72 
73  static inline device_matrix from_list(
74  std::initializer_list<std::initializer_list<value_type>> const& array,
75  Alloc const& alloc = Alloc());
76 
77  template <size_t S1, size_t S2>
78  static inline device_matrix from_c_array(Type (&arr)[S1][S2],
79  Alloc const& alloc = Alloc());
80 
81  template <typename HostMat, MGCPP_CONCEPT(adapter<HostMat>::value)>
82  inline explicit device_matrix(HostMat const& host_mat,
83  Alloc const& alloc = Alloc());
84 
85  inline device_matrix(device_matrix<Type, Alloc> const& other);
86 
87  template <typename DenseMatrix>
88  inline explicit device_matrix(dense_matrix<DenseMatrix, Type> const& other);
89 
90  inline device_matrix(device_matrix<Type, Alloc>&& other) noexcept;
91 
92  inline device_matrix<Type, Alloc>& operator=(
93  device_matrix<Type, Alloc> const& other);
94 
95  template <typename DenseMatrix>
96  inline device_matrix<Type, Alloc>& operator=(
97  dense_matrix<DenseMatrix, Type> const& other);
98 
99  inline device_matrix<Type, Alloc>& operator=(
100  device_matrix<Type, Alloc>&& other) noexcept;
101 
102  inline device_matrix<Type, Alloc>& zero();
103 
104  inline device_matrix<Type, Alloc>& resize(shape_type new_shape);
105 
106  inline device_matrix<Type, Alloc>& resize(shape_type new_shape,
107  value_type init);
108 
109  inline column_view<this_type, Type> column(size_t i) noexcept;
110 
111  // inline row_view<this_type, Type>
112  // row(size_t i) noexcept;
113 
114  inline void copy_to_host(pointer host_p) const;
115 
116  inline value_type check_value(size_t i, size_t j) const;
117 
118  inline void set_value(size_t i, size_t j, value_type value);
119 
120  inline const_device_pointer data() const noexcept;
121 
122  inline device_pointer data_mutable() noexcept;
123 
124  inline size_t capacity() const noexcept;
125 
126  inline thread_context* context() const noexcept;
127 
128  inline device_pointer release_data() noexcept;
129 
130  inline Alloc allocator() const noexcept;
131 
132  inline size_t device_id() const noexcept;
133 
134  inline shape_type const& shape() const noexcept;
135 };
136 } // namespace mgcpp
137 
138 #include <mgcpp/matrix/device_matrix.tpp>
139 #endif
thread_context * context() const noexcept
Definition: adapter_base.hpp:14
Definition: adapter_base.hpp:12
Type value_type
Definition: device_matrix.hpp:33
Definition: thread_context.hpp:20
void set_value(size_t i, size_t j, value_type value)
value_type check_value(size_t i, size_t j) const
void copy_to_host(pointer host_p) const
Definition: shape.hpp:33
typename device_value_type< Type >::type device_value_type
Definition: device_matrix.hpp:36
device_matrix< Type, Alloc > this_type
Definition: device_matrix.hpp:32
Definition: column_view.hpp:19
device_value_type * device_pointer
Definition: device_matrix.hpp:37
value_type * pointer
Definition: device_matrix.hpp:34
static device_matrix from_list(std::initializer_list< std::initializer_list< value_type >> const &array, Alloc const &alloc=Alloc())
device_value_type const * const_device_pointer
Definition: device_matrix.hpp:38
Definition: dmat_expr.hpp:19
const_device_pointer data() const noexcept
device_matrix() noexcept
device_matrix< Type, Alloc > & zero()
Definition: is_supported_type.hpp:16
device_pointer data_mutable() noexcept
Definition: dense_matrix.hpp:15
size_t device_id() const noexcept
#define MGCPP_CONCEPT(...)
Definition: concept.hpp:10
Alloc allocator_type
Definition: device_matrix.hpp:42
Definition: allocator.hpp:12
size_t capacity() const noexcept
void init(bool print_system_info=true)
column_view< this_type, Type > column(size_t i) noexcept
device_matrix< Type, Alloc > & resize(shape_type new_shape)
value_type const * const_pointer
Definition: device_matrix.hpp:35
device_pointer release_data() noexcept
Definition: device_matrix.hpp:28
static device_matrix from_c_array(Type(&arr)[S1][S2], Alloc const &alloc=Alloc())
Type type
Definition: device_value_type.hpp:20