ViennaCL - The Vienna Computing Library  1.6.2
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-2014, 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 PDF 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:
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 
51  {
52  if (diag_) return (i == j) ? value_ : off_diag_;
53  return value_;
54  }
55 protected:
58  NumericT value_;
59  bool diag_;
60  NumericT off_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 
111  typedef NumericT cpu_value_type;
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  matrix_base(const self_type & other);
161  self_type & operator=(const self_type & other);
164  template<typename LHS, typename RHS, typename OP>
165  self_type & operator=(const matrix_expression<const LHS, const RHS, OP> & proxy);
166  // A = trans(B). Currently achieved in CPU memory
168  template<typename LHS, typename RHS, typename OP>
169  self_type & operator+=(const matrix_expression<const LHS, const RHS, OP> & proxy);
170  template<typename LHS, typename RHS, typename OP>
171  self_type & operator-=(const matrix_expression<const LHS, const RHS, OP> & proxy);
173  self_type & operator = (identity_matrix<NumericT> const & m);
175  self_type & operator = (zero_matrix<NumericT> const & m);
177  self_type & operator = (scalar_matrix<NumericT> const & m);
178  //read-write access to an element of the matrix/matrix_range/matrix_slice
180  entry_proxy<NumericT> operator()(size_type row_index, size_type col_index);
182  const_entry_proxy<NumericT> operator()(size_type row_index, size_type col_index) const;
183  self_type & operator += (const self_type & other);
184  self_type & operator -= (const self_type & other);
185 
187  self_type & operator *= (char val);
189  self_type & operator *= (short val);
191  self_type & operator *= (int val);
193  self_type & operator *= (long val);
195  self_type & operator *= (float val);
197  self_type & operator *= (double val);
198 
200  self_type & operator /= (char val);
202  self_type & operator /= (short val);
204  self_type & operator /= (int val);
206  self_type & operator /= (long val);
208  self_type & operator /= (float val);
210  self_type & operator /= (double val);
211 
215  size_type size1() const { return size1_;}
217  size_type size2() const { return size2_; }
219  size_type start1() const { return start1_;}
221  size_type start2() const { return start2_; }
223  size_type stride1() const { return stride1_;}
225  size_type stride2() const { return stride2_; }
227  void clear();
229  size_type internal_size1() const { return internal_size1_; }
231  size_type internal_size2() const { return internal_size2_; }
235  handle_type & handle() { return elements_; }
237  const handle_type & handle() const { return elements_; }
239  bool row_major() const { return row_major_; }
240 protected:
243  void resize(size_type rows, size_type columns, bool preserve = true);
244 private:
245  size_type size1_;
246  size_type size2_;
247  size_type start1_;
248  size_type start2_;
249  size_type stride1_;
250  size_type stride2_;
251  size_type internal_size1_;
252  size_type internal_size2_;
253  bool row_major_fixed_; //helper flag to make layout of matrix<T, row_major> A; persistent
254  bool row_major_;
255  handle_type elements_;
256 }; //matrix
257 
258 }
259 
260 #endif
const handle_type & handle() const
Returns the OpenCL handle, const-version.
Definition: matrix_def.hpp:237
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:226
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:235
size_type internal_size() const
Returns the total amount of allocated memory in multiples of sizeof(NumericT)
Definition: matrix_def.hpp:233
self_type & operator=(const self_type &other)
Definition: matrix.hpp:243
Helper class for checking whether a matrix has a row-major layout.
Definition: forwards.h:483
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:591
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:340
size_type stride2() const
Returns the number of columns.
Definition: matrix_def.hpp:225
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:440
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:238
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
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)
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:597
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:223
NumericT const & const_reference
Definition: matrix_def.hpp:85
std::size_t vcl_size_t
Definition: forwards.h:74
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:217
handle_type & handle()
Returns the OpenCL handle, non-const-version.
Definition: matrix_def.hpp:235
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:215
A dense matrix class.
Definition: forwards.h:368
bool row_major() const
Definition: matrix_def.hpp:239
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:221
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:231
self_type & operator*=(char val)
Scales the matrix by a char (8-bit integer)
Definition: matrix.hpp:480
size_type internal_size1() const
Returns the internal number of rows. Usually required for launching OpenCL kernels only...
Definition: matrix_def.hpp:229
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:536
void resize(size_type rows, size_type columns, bool preserve=true)
Definition: matrix.hpp:601
memory_types
Definition: forwards.h:344
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:232
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:219
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