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"
27 
28 namespace viennacl
29 {
34 template<typename NumericT>
36 {
37 protected:
40 public:
41  typedef NumericT const & const_reference;
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 
51  {
52  if (diag_) return (i == j) ? value_ : off_diag_;
53  return value_;
54  }
55 protected:
59  bool diag_;
62 };
63 
64 //
65 // Initializer types
66 //
68 template<typename NumericT>
69 class identity_matrix : public implicit_matrix_base<NumericT>
70 {
71 public:
73  typedef NumericT const & const_reference;
74 
76 };
77 
78 
80 template<typename NumericT>
81 class zero_matrix : public implicit_matrix_base<NumericT>
82 {
83 public:
85  typedef NumericT const & const_reference;
86 
88 };
89 
90 
92 template<typename NumericT>
93 class scalar_matrix : public implicit_matrix_base<NumericT>
94 {
95 public:
97  typedef NumericT const & const_reference;
98 
100 };
101 
102 template<class NumericT, typename SizeT, typename DistanceT>
104 {
106 public:
107 
112  typedef SizeT size_type;
113  typedef DistanceT difference_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 
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>
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>
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
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 
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_; }
244  handle_type & handle() { return elements_; }
246  const handle_type & handle() const { return elements_; }
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:
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
const handle_type & handle() const
Returns the OpenCL handle, const-version.
Definition: matrix_def.hpp:246
viennacl::backend::mem_handle handle_type
Definition: matrix_def.hpp:114
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
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
size_type internal_size() const
Returns the total amount of allocated memory in multiples of sizeof(NumericT)
Definition: matrix_def.hpp:242
self_type & operator=(const self_type &other)
Definition: matrix.hpp:262
Helper class for checking whether a matrix has a row-major layout.
Definition: forwards.h:484
const_reference operator()(size_type i, size_type j) const
Definition: matrix_def.hpp:50
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
scalar_matrix(size_type s1, size_type s2, const_reference val, viennacl::context ctx=viennacl::context())
Definition: matrix_def.hpp:99
A proxy class for entries in a vector.
viennacl::context context() const
Definition: matrix_def.hpp:46
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:341
size_type stride2() const
Returns the number of columns.
Definition: matrix_def.hpp:234
This file provides the forward declarations for the main types used within ViennaCL.
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
Represents a vector consisting of 1 at a given index and zeros otherwise. To be used as an initialize...
Definition: matrix_def.hpp:69
viennacl::scalar< int > s2
viennacl::scalar< float > s1
viennacl::memory_types memory_domain() const
Definition: matrix_def.hpp:247
NumericT const & const_reference
Definition: matrix_def.hpp:73
NumericT const & const_reference
Definition: matrix_def.hpp:97
zero_matrix(size_type s1, size_type s2, viennacl::context ctx=viennacl::context())
Definition: matrix_def.hpp:87
float NumericT
Definition: bisect.cpp:40
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
size_type size2() const
Definition: matrix_def.hpp:45
matrix_base(bool is_row_major)
The layout constructor. Does not allocate any memory.
Definition: matrix_def.hpp:120
void switch_memory_context(viennacl::context new_ctx)
Definition: matrix_def.hpp:249
Base class for representing matrices where the individual entries are not all stored explicitly...
Definition: matrix_def.hpp:35
NumericT const & const_reference
Definition: matrix_def.hpp:41
matrix_base()
The default constructor. Does not allocate any memory.
Definition: matrix_def.hpp:117
matrix_iterator< col_iteration, self_type > iterator2
Definition: matrix_def.hpp:109
size_type size1() const
Definition: matrix_def.hpp:44
void clear()
Resets all entries to zero.
Definition: matrix.hpp:634
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
size_type stride1() const
Returns the number of rows.
Definition: matrix_def.hpp:232
NumericT const & const_reference
Definition: matrix_def.hpp:85
std::size_t vcl_size_t
Definition: forwards.h:75
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
handle_type & handle()
Returns the OpenCL handle, non-const-version.
Definition: matrix_def.hpp:244
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
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
A dense matrix class.
Definition: forwards.h:369
bool row_major() const
Definition: matrix_def.hpp:248
void set_handle(viennacl::backend::mem_handle const &h)
scalar< NumericT > value_type
Definition: matrix_def.hpp:110
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
matrix_iterator< row_iteration, self_type > iterator1
Definition: matrix_def.hpp:108
size_type start2() const
Returns the number of columns.
Definition: matrix_def.hpp:230
identity_matrix(size_type s, viennacl::context ctx=viennacl::context())
Definition: matrix_def.hpp:75
size_type internal_size2() const
Returns the internal number of columns. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:240
self_type & operator*=(char val)
Scales the matrix by a char (8-bit integer)
Definition: matrix.hpp:517
size_type internal_size1() const
Returns the internal number of rows. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:238
self_type & operator+=(const matrix_expression< const LHS, const RHS, OP > &proxy)
self_type & operator/=(char val)
Scales the matrix by a char (8-bit integer)
Definition: matrix.hpp:573
void resize(size_type rows, size_type columns, bool preserve=true)
Definition: matrix.hpp:638
memory_types
Definition: forwards.h:345
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
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
implicit_matrix_base(size_type size1, size_type size2, NumericT value, bool diag, viennacl::context ctx)
Definition: matrix_def.hpp:39
size_type start1() const
Returns the number of rows.
Definition: matrix_def.hpp:228
self_type & operator-=(const matrix_expression< const LHS, const RHS, OP > &proxy)
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