ViennaCL - The Vienna Computing Library: viennacl/detail/matrix_def.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
matrix_def.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_DETAIL_MATRIX_DEF_HPP_
2 #define VIENNACL_DETAIL_MATRIX_DEF_HPP_
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
25 #include "viennacl/forwards.h"
26 #include "viennacl/tools/entry_proxy.hpp"
27
28 namespace viennacl
29 {
34 template<typename NumericT>
35 class implicit_matrix_base
36 {
37 protected:
38 typedef vcl_size_t size_type;
39 implicit_matrix_base(size_type size1, size_type size2, NumericT value, bool diag, viennacl::context ctx) : size1_(size1), size2_(size2), value_(value), diag_(diag), off_diag_(0), ctx_(ctx){ }
40 public:
41 typedef NumericT const & const_reference;
42 typedef NumericT cpu_value_type;
43
44 size_type size1() const { return size1_; }
45 size_type size2() const { return size2_; }
46 viennacl::context context() const { return ctx_; }
47 NumericT value() const { return value_; }
48 bool diag() const { return diag_; }
49
50 const_reference operator()(size_type i, size_type j) const
51 {
52 if (diag_) return (i == j) ? value_ : off_diag_;
53 return value_;
54 }
55 protected:
56 size_type size1_;
57 size_type size2_;
58 NumericT value_;
59 bool diag_;
60 NumericT off_diag_;
61 viennacl::context ctx_;
62 };
63
64 //
65 // Initializer types
66 //
68 template<typename NumericT>
69 class identity_matrix : public implicit_matrix_base<NumericT>
70 {
71 public:
72 typedef vcl_size_t size_type;
73 typedef NumericT const & const_reference;
74
75 identity_matrix(size_type s, viennacl::context ctx = viennacl::context()) : implicit_matrix_base<NumericT>(s, s, 1, true, ctx){}
76 };
77
78
80 template<typename NumericT>
81 class zero_matrix : public implicit_matrix_base<NumericT>
82 {
83 public:
84 typedef vcl_size_t size_type;
85 typedef NumericT const & const_reference;
86
87 zero_matrix(size_type s1, size_type s2, viennacl::context ctx = viennacl::context()) : implicit_matrix_base<NumericT>(s1, s2, 0, false, ctx){}
88 };
89
90
92 template<typename NumericT>
93 class scalar_matrix : public implicit_matrix_base<NumericT>
94 {
95 public:
96 typedef vcl_size_t size_type;
97 typedef NumericT const & const_reference;
98
99 scalar_matrix(size_type s1, size_type s2, const_reference val, viennacl::context ctx = viennacl::context()) : implicit_matrix_base<NumericT>(s1, s2, val, false, ctx) {}
100 };
101
102 template<class NumericT, typename SizeT, typename DistanceT>
103 class matrix_base
104 {
105 typedef matrix_base<NumericT, SizeT, DistanceT> self_type;
106 public:
107
108 typedef matrix_iterator<row_iteration, self_type > iterator1;
109 typedef matrix_iterator<col_iteration, self_type > iterator2;
110 typedef scalar<NumericT> value_type;
111 typedef NumericT cpu_value_type;
112 typedef SizeT size_type;
113 typedef DistanceT difference_type;
114 typedef viennacl::backend::mem_handle handle_type;
115
117 explicit matrix_base(): size1_(0), size2_(0), start1_(0), start2_(0), stride1_(1), stride2_(1), internal_size1_(0), internal_size2_(0), row_major_fixed_(false), row_major_(true) {}
118
120 explicit matrix_base(bool is_row_major) : size1_(0), size2_(0), start1_(0), start2_(0), stride1_(1), stride2_(1), internal_size1_(0), internal_size2_(0), row_major_fixed_(true), row_major_(is_row_major) {}
121
129 explicit matrix_base(size_type rows, size_type columns, bool is_row_major, viennacl::context ctx = viennacl::context());
130
132 explicit matrix_base(viennacl::backend::mem_handle & h,
133 size_type mat_size1, size_type mat_start1, size_type mat_stride1, size_type mat_internal_size1,
134 size_type mat_size2, size_type mat_start2, size_type mat_stride2, size_type mat_internal_size2,
135 bool is_row_major): size1_(mat_size1), size2_(mat_size2),
136 start1_(mat_start1), start2_(mat_start2),
137 stride1_(mat_stride1), stride2_(mat_stride2),
138 internal_size1_(mat_internal_size1), internal_size2_(mat_internal_size2),
139 row_major_fixed_(true), row_major_(is_row_major),
140 elements_(h) {}
141
142
143 template<typename LHS, typename RHS, typename OP>
144 explicit matrix_base(matrix_expression<const LHS, const RHS, OP> const & proxy);
145
146 // CUDA or host memory:
147 explicit matrix_base(NumericT * ptr_to_mem, viennacl::memory_types mem_type,
148 size_type mat_size1, size_type mat_start1, size_type mat_stride1, size_type mat_internal_size1,
149 size_type mat_size2, size_type mat_start2, size_type mat_stride2, size_type mat_internal_size2,
150 bool is_row_major);
151
152 #ifdef VIENNACL_WITH_OPENCL
153 explicit matrix_base(cl_mem mem, size_type rows, size_type columns, bool is_row_major, viennacl::context ctx = viennacl::context());
154 explicit matrix_base(cl_mem mem, viennacl::context ctx,
155 size_type mat_size1, size_type mat_start1, size_type mat_stride1, size_type mat_internal_size1,
156 size_type mat_size2, size_type mat_start2, size_type mat_stride2, size_type mat_internal_size2,
157 bool is_row_major);
158 #endif
159
160 /* Copy CTOR */
161 matrix_base(const self_type & other);
162
163 /* Conversion CTOR */
164 template<typename OtherNumericT>
165 matrix_base(const matrix_base<OtherNumericT, SizeT, DistanceT> & other);
166
167 self_type & operator=(const self_type & other);
168 template<typename OtherNumericT>
169 self_type & operator=(const matrix_base<OtherNumericT, SizeT, DistanceT> & other);
170
173 template<typename LHS, typename RHS, typename OP>
174 self_type & operator=(const matrix_expression<const LHS, const RHS, OP> & proxy);
175 // A = trans(B). Currently achieved in CPU memory
176 self_type & operator=(const matrix_expression< const self_type, const self_type, op_trans> & proxy);
177 template<typename LHS, typename RHS, typename OP>
178 self_type & operator+=(const matrix_expression<const LHS, const RHS, OP> & proxy);
179 template<typename LHS, typename RHS, typename OP>
180 self_type & operator-=(const matrix_expression<const LHS, const RHS, OP> & proxy);
182 self_type & operator = (identity_matrix<NumericT> const & m);
184 self_type & operator = (zero_matrix<NumericT> const & m);
186 self_type & operator = (scalar_matrix<NumericT> const & m);
187 //read-write access to an element of the matrix/matrix_range/matrix_slice
189 entry_proxy<NumericT> operator()(size_type row_index, size_type col_index);
191 const_entry_proxy<NumericT> operator()(size_type row_index, size_type col_index) const;
192 self_type & operator += (const self_type & other);
193 self_type & operator -= (const self_type & other);
194
196 self_type & operator *= (char val);
198 self_type & operator *= (short val);
200 self_type & operator *= (int val);
202 self_type & operator *= (long val);
204 self_type & operator *= (float val);
206 self_type & operator *= (double val);
207
209 self_type & operator /= (char val);
211 self_type & operator /= (short val);
213 self_type & operator /= (int val);
215 self_type & operator /= (long val);
217 self_type & operator /= (float val);
219 self_type & operator /= (double val);
220
222 matrix_expression<const self_type, const NumericT, op_mult> operator-() const;
224 size_type size1() const { return size1_;}
226 size_type size2() const { return size2_; }
228 size_type start1() const { return start1_;}
230 size_type start2() const { return start2_; }
232 size_type stride1() const { return stride1_;}
234 size_type stride2() const { return stride2_; }
236 void clear();
238 size_type internal_size1() const { return internal_size1_; }
240 size_type internal_size2() const { return internal_size2_; }
242 size_type internal_size() const { return internal_size1() * internal_size2(); }
244 handle_type & handle() { return elements_; }
246 const handle_type & handle() const { return elements_; }
247 viennacl::memory_types memory_domain() const { return elements_.get_active_handle_id(); }
248 bool row_major() const { return row_major_; }
249 void switch_memory_context(viennacl::context new_ctx) { viennacl::backend::switch_memory_context<NumericT>(elements_, new_ctx); }
250
251 protected:
252 void set_handle(viennacl::backend::mem_handle const & h);
253 void resize(size_type rows, size_type columns, bool preserve = true);
254 private:
255 size_type size1_;
256 size_type size2_;
257 size_type start1_;
258 size_type start2_;
259 size_type stride1_;
260 size_type stride2_;
261 size_type internal_size1_;
262 size_type internal_size2_;
263 bool row_major_fixed_; //helper flag to make layout of matrix<T, row_major> A; persistent
264 bool row_major_;
265 handle_type elements_;
266 }; //matrix
267
268 }
269
270 #endif
viennacl::matrix_base::handle
const handle_type & handle() const
Returns the OpenCL handle, const-version.
Definition: matrix_def.hpp:246
viennacl::matrix_base::handle_type
viennacl::backend::mem_handle handle_type
Definition: matrix_def.hpp:114
viennacl::implicit_matrix_base::value_
NumericT value_
Definition: matrix_def.hpp:58
viennacl::scalar
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
viennacl::matrix_base::size_type
SizeT size_type
Definition: matrix_def.hpp:112
viennacl::const_entry_proxy
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:236
viennacl::implicit_matrix_base::value
NumericT value() const
Definition: matrix_def.hpp:47
viennacl::matrix_base::internal_size
size_type internal_size() const
Returns the total amount of allocated memory in multiples of sizeof(NumericT)
Definition: matrix_def.hpp:242
viennacl::matrix_base::operator=
self_type & operator=(const self_type &other)
Definition: matrix.hpp:262
viennacl::is_row_major
Helper class for checking whether a matrix has a row-major layout.
Definition: forwards.h:484
viennacl::implicit_matrix_base::operator()
const_reference operator()(size_type i, size_type j) const
Definition: matrix_def.hpp:50
viennacl::matrix_base::operator-
matrix_expression< const self_type, const NumericT, op_mult > operator-() const
Sign flip for the matrix. Emulated to be equivalent to -1.0 * matrix.
Definition: matrix.hpp:628
viennacl::scalar_matrix::scalar_matrix
scalar_matrix(size_type s1, size_type s2, const_reference val, viennacl::context ctx=viennacl::context())
Definition: matrix_def.hpp:99
viennacl::identity_matrix::size_type
vcl_size_t size_type
Definition: matrix_def.hpp:72
entry_proxy.hpp
A proxy class for entries in a vector.
viennacl::implicit_matrix_base::size1_
size_type size1_
Definition: matrix_def.hpp:56
viennacl::implicit_matrix_base::context
viennacl::context context() const
Definition: matrix_def.hpp:46
viennacl::matrix_base
Definition: matrix_def.hpp:103
viennacl::matrix_base::difference_type
DistanceT difference_type
Definition: matrix_def.hpp:113
viennacl::matrix_expression
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:341
viennacl::implicit_matrix_base::off_diag_
NumericT off_diag_
Definition: matrix_def.hpp:60
viennacl::matrix_base::stride2
size_type stride2() const
Returns the number of columns.
Definition: matrix_def.hpp:234
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::matrix_base::operator()
entry_proxy< NumericT > operator()(size_type row_index, size_type col_index)
Read-write access to a single element of the matrix/matrix_range/matrix_slice.
Definition: matrix.hpp:477
viennacl::identity_matrix
Represents a vector consisting of 1 at a given index and zeros otherwise. To be used as an initialize...
Definition: matrix_def.hpp:69
s2
viennacl::scalar< int > s2
Definition: global_variables.cpp:58
s1
viennacl::scalar< float > s1
Definition: global_variables.cpp:57
viennacl::matrix_base::memory_domain
viennacl::memory_types memory_domain() const
Definition: matrix_def.hpp:247
viennacl::identity_matrix::const_reference
NumericT const & const_reference
Definition: matrix_def.hpp:73
viennacl::scalar_matrix::const_reference
NumericT const & const_reference
Definition: matrix_def.hpp:97
viennacl::zero_matrix::zero_matrix
zero_matrix(size_type s1, size_type s2, viennacl::context ctx=viennacl::context())
Definition: matrix_def.hpp:87
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::context
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::implicit_matrix_base::size2
size_type size2() const
Definition: matrix_def.hpp:45
viennacl::matrix_base::matrix_base
matrix_base(bool is_row_major)
The layout constructor. Does not allocate any memory.
Definition: matrix_def.hpp:120
viennacl::matrix_base::switch_memory_context
void switch_memory_context(viennacl::context new_ctx)
Definition: matrix_def.hpp:249
viennacl::implicit_matrix_base::cpu_value_type
NumericT cpu_value_type
Definition: matrix_def.hpp:42
viennacl::implicit_matrix_base::diag_
bool diag_
Definition: matrix_def.hpp:59
viennacl::implicit_matrix_base
Base class for representing matrices where the individual entries are not all stored explicitly...
Definition: matrix_def.hpp:35
viennacl::implicit_matrix_base::const_reference
NumericT const & const_reference
Definition: matrix_def.hpp:41
viennacl::matrix_base::matrix_base
matrix_base()
The default constructor. Does not allocate any memory.
Definition: matrix_def.hpp:117
viennacl::matrix_base::iterator2
matrix_iterator< col_iteration, self_type > iterator2
Definition: matrix_def.hpp:109
viennacl::implicit_matrix_base::size1
size_type size1() const
Definition: matrix_def.hpp:44
viennacl::implicit_matrix_base::ctx_
viennacl::context ctx_
Definition: matrix_def.hpp:61
viennacl::matrix_base::clear
void clear()
Resets all entries to zero.
Definition: matrix.hpp:634
viennacl::scalar_matrix::size_type
vcl_size_t size_type
Definition: matrix_def.hpp:96
viennacl::scalar_matrix
Represents a vector consisting of scalars 's' only, i.e. v[i] = s for all i. To be used as an initial...
Definition: matrix_def.hpp:93
viennacl::matrix_base::stride1
size_type stride1() const
Returns the number of rows.
Definition: matrix_def.hpp:232
viennacl::zero_matrix::const_reference
NumericT const & const_reference
Definition: matrix_def.hpp:85
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::matrix_base::size2
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
viennacl::matrix_base::handle
handle_type & handle()
Returns the OpenCL handle, non-const-version.
Definition: matrix_def.hpp:244
viennacl::zero_matrix
Represents a vector consisting of zeros only. To be used as an initializer for viennacl::vector, vector_range, or vector_slize only.
Definition: matrix_def.hpp:81
viennacl::matrix_base::size1
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
viennacl::zero_matrix::size_type
vcl_size_t size_type
Definition: matrix_def.hpp:84
viennacl::matrix_iterator
A dense matrix class.
Definition: forwards.h:369
viennacl::matrix_base::row_major
bool row_major() const
Definition: matrix_def.hpp:248
viennacl::matrix_base::set_handle
void set_handle(viennacl::backend::mem_handle const &h)
viennacl::implicit_matrix_base::size_type
vcl_size_t size_type
Definition: matrix_def.hpp:38
viennacl::matrix_base::value_type
scalar< NumericT > value_type
Definition: matrix_def.hpp:110
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::matrix_base::iterator1
matrix_iterator< row_iteration, self_type > iterator1
Definition: matrix_def.hpp:108
viennacl::matrix_base::start2
size_type start2() const
Returns the number of columns.
Definition: matrix_def.hpp:230
viennacl::identity_matrix::identity_matrix
identity_matrix(size_type s, viennacl::context ctx=viennacl::context())
Definition: matrix_def.hpp:75
viennacl::implicit_matrix_base::size2_
size_type size2_
Definition: matrix_def.hpp:57
viennacl::matrix_base::internal_size2
size_type internal_size2() const
Returns the internal number of columns. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:240
viennacl::matrix_base::operator*=
self_type & operator*=(char val)
Scales the matrix by a char (8-bit integer)
Definition: matrix.hpp:517
viennacl::matrix_base::internal_size1
size_type internal_size1() const
Returns the internal number of rows. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:238
viennacl::matrix_base::operator+=
self_type & operator+=(const matrix_expression< const LHS, const RHS, OP > &proxy)
viennacl::matrix_base::cpu_value_type
NumericT cpu_value_type
Definition: matrix_def.hpp:111
viennacl::matrix_base::operator/=
self_type & operator/=(char val)
Scales the matrix by a char (8-bit integer)
Definition: matrix.hpp:573
viennacl::matrix_base::resize
void resize(size_type rows, size_type columns, bool preserve=true)
Definition: matrix.hpp:638
viennacl::memory_types
memory_types
Definition: forwards.h:345
viennacl::entry_proxy
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:233
viennacl::matrix_base::matrix_base
matrix_base(viennacl::backend::mem_handle &h, size_type mat_size1, size_type mat_start1, size_type mat_stride1, size_type mat_internal_size1, size_type mat_size2, size_type mat_start2, size_type mat_stride2, size_type mat_internal_size2, bool is_row_major)
Constructor for creating a matrix_range or matrix_stride from some other matrix/matrix_range/matrix_s...
Definition: matrix_def.hpp:132
viennacl::implicit_matrix_base::diag
bool diag() const
Definition: matrix_def.hpp:48
viennacl::implicit_matrix_base::implicit_matrix_base
implicit_matrix_base(size_type size1, size_type size2, NumericT value, bool diag, viennacl::context ctx)
Definition: matrix_def.hpp:39
viennacl::matrix_base::start1
size_type start1() const
Returns the number of rows.
Definition: matrix_def.hpp:228
viennacl::matrix_base::operator-=
self_type & operator-=(const matrix_expression< const LHS, const RHS, OP > &proxy)
viennacl::backend::mem_handle::get_active_handle_id
memory_types get_active_handle_id() const
Returns an ID for the currently active memory buffer. Other memory buffers might contain old or no da...
Definition: mem_handle.hpp:118