All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
matrix_def.hpp
Go to the documentation of this file.
#ifndef VIENNACL_DETAIL_MATRIX_DEF_HPP_
#define VIENNACL_DETAIL_MATRIX_DEF_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include "viennacl/forwards.h"
#include "viennacl/tools/entry_proxy.hpp"
namespace viennacl
{
template<typename NumericT>
class implicit_matrix_base
{
protected:
typedef vcl_size_t size_type;
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){ }
public:
typedef NumericT const & const_reference;
typedef NumericT cpu_value_type;
size_type size1() const { return size1_; }
size_type size2() const { return size2_; }
viennacl::context context() const { return ctx_; }
NumericT value() const { return value_; }
bool diag() const { return diag_; }
const_reference operator()(size_type i, size_type j) const
{
if (diag_) return (i == j) ? value_ : off_diag_;
return value_;
}
protected:
size_type size1_;
size_type size2_;
NumericT value_;
bool diag_;
NumericT off_diag_;
viennacl::context ctx_;
};
//
// Initializer types
//
template<typename NumericT>
class identity_matrix : public implicit_matrix_base<NumericT>
{
public:
typedef vcl_size_t size_type;
typedef NumericT const & const_reference;
identity_matrix(size_type s, viennacl::context ctx = viennacl::context()) : implicit_matrix_base<NumericT>(s, s, 1, true, ctx){}
};
template<typename NumericT>
class zero_matrix : public implicit_matrix_base<NumericT>
{
public:
typedef vcl_size_t size_type;
typedef NumericT const & const_reference;
zero_matrix(size_type s1, size_type s2, viennacl::context ctx = viennacl::context()) : implicit_matrix_base<NumericT>(s1, s2, 0, false, ctx){}
};
template<typename NumericT>
class scalar_matrix : public implicit_matrix_base<NumericT>
{
public:
typedef vcl_size_t size_type;
typedef NumericT const & const_reference;
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) {}
};
template<class NumericT, typename SizeT, typename DistanceT>
class matrix_base
{
typedef matrix_base<NumericT, SizeT, DistanceT> self_type;
public:
typedef matrix_iterator<row_iteration, self_type > iterator1;
typedef matrix_iterator<col_iteration, self_type > iterator2;
typedef scalar<NumericT> value_type;
typedef NumericT cpu_value_type;
typedef SizeT size_type;
typedef DistanceT difference_type;
typedef viennacl::backend::mem_handle handle_type;
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) {}
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) {}
explicit matrix_base(size_type rows, size_type columns, bool is_row_major, viennacl::context ctx = viennacl::context());
explicit 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): size1_(mat_size1), size2_(mat_size2),
start1_(mat_start1), start2_(mat_start2),
stride1_(mat_stride1), stride2_(mat_stride2),
internal_size1_(mat_internal_size1), internal_size2_(mat_internal_size2),
row_major_fixed_(true), row_major_(is_row_major),
elements_(h) {}
template<typename LHS, typename RHS, typename OP>
explicit matrix_base(matrix_expression<const LHS, const RHS, OP> const & proxy);
// CUDA or host memory:
explicit matrix_base(NumericT * ptr_to_mem, viennacl::memory_types mem_type,
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);
#ifdef VIENNACL_WITH_OPENCL
explicit matrix_base(cl_mem mem, size_type rows, size_type columns, bool is_row_major, viennacl::context ctx = viennacl::context());
explicit matrix_base(cl_mem mem, viennacl::context ctx,
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);
#endif
/* Copy CTOR */
matrix_base(const self_type & other);
/* Conversion CTOR */
template<typename OtherNumericT>
matrix_base(const matrix_base<OtherNumericT, SizeT, DistanceT> & other);
self_type & operator=(const self_type & other);
template<typename OtherNumericT>
self_type & operator=(const matrix_base<OtherNumericT, SizeT, DistanceT> & other);
template<typename LHS, typename RHS, typename OP>
self_type & operator=(const matrix_expression<const LHS, const RHS, OP> & proxy);
// A = trans(B). Currently achieved in CPU memory
self_type & operator=(const matrix_expression< const self_type, const self_type, op_trans> & proxy);
template<typename LHS, typename RHS, typename OP>
self_type & operator+=(const matrix_expression<const LHS, const RHS, OP> & proxy);
template<typename LHS, typename RHS, typename OP>
self_type & operator-=(const matrix_expression<const LHS, const RHS, OP> & proxy);
self_type & operator = (identity_matrix<NumericT> const & m);
self_type & operator = (zero_matrix<NumericT> const & m);
self_type & operator = (scalar_matrix<NumericT> const & m);
//read-write access to an element of the matrix/matrix_range/matrix_slice
entry_proxy<NumericT> operator()(size_type row_index, size_type col_index);
const_entry_proxy<NumericT> operator()(size_type row_index, size_type col_index) const;
self_type & operator += (const self_type & other);
self_type & operator -= (const self_type & other);
self_type & operator *= (char val);
self_type & operator *= (short val);
self_type & operator *= (int val);
self_type & operator *= (long val);
self_type & operator *= (float val);
self_type & operator *= (double val);
self_type & operator /= (char val);
self_type & operator /= (short val);
self_type & operator /= (int val);
self_type & operator /= (long val);
self_type & operator /= (float val);
self_type & operator /= (double val);
matrix_expression<const self_type, const NumericT, op_mult> operator-() const;
size_type size1() const { return size1_;}
size_type size2() const { return size2_; }
size_type start1() const { return start1_;}
size_type start2() const { return start2_; }
size_type stride1() const { return stride1_;}
size_type stride2() const { return stride2_; }
void clear();
size_type internal_size1() const { return internal_size1_; }
size_type internal_size2() const { return internal_size2_; }
size_type internal_size() const { return internal_size1() * internal_size2(); }
handle_type & handle() { return elements_; }
const handle_type & handle() const { return elements_; }
viennacl::memory_types memory_domain() const { return elements_.get_active_handle_id(); }
bool row_major() const { return row_major_; }
void switch_memory_context(viennacl::context new_ctx) { viennacl::backend::switch_memory_context<NumericT>(elements_, new_ctx); }
protected:
void set_handle(viennacl::backend::mem_handle const & h);
void resize(size_type rows, size_type columns, bool preserve = true);
private:
size_type size1_;
size_type size2_;
size_type start1_;
size_type start2_;
size_type stride1_;
size_type stride2_;
size_type internal_size1_;
size_type internal_size2_;
bool row_major_fixed_; //helper flag to make layout of matrix<T, row_major> A; persistent
bool row_major_;
handle_type elements_;
}; //matrix
}
#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