1 #ifndef VIENNACL_ELL_MATRIX_HPP_
2 #define VIENNACL_ELL_MATRIX_HPP_
52 template<
typename SCALARTYPE,
unsigned int ALIGNMENT >
67 #ifdef VIENNACL_WITH_OPENCL
70 coords_.opencl_handle().context(ctx.opencl_context());
71 elements_.opencl_handle().context(ctx.opencl_context());
89 handle_type &
handle() {
return elements_; }
90 const handle_type &
handle()
const {
return elements_; }
92 handle_type &
handle2() {
return coords_; }
93 const handle_type &
handle2()
const {
return coords_; }
95 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
96 template <
typename CPU_MATRIX>
97 friend void copy(
const CPU_MATRIX & cpu_matrix,
ell_matrix & gpu_matrix );
99 template <
typename CPU_MATRIX,
typename T,
unsigned int ALIGN>
109 handle_type elements_;
112 template <
typename CPU_MATRIX,
typename SCALARTYPE,
unsigned int ALIGNMENT>
118 if(cpu_matrix.size1() > 0 && cpu_matrix.size2() > 0)
122 for (
typename CPU_MATRIX::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
125 for (
typename CPU_MATRIX::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
130 max_entries_per_row = std::max(max_entries_per_row, num_entries);
134 gpu_matrix.maxnnz_ = max_entries_per_row;
135 gpu_matrix.rows_ = cpu_matrix.size1();
136 gpu_matrix.cols_ = cpu_matrix.size2();
141 std::vector<SCALARTYPE> elements(nnz, 0);
146 for (
typename CPU_MATRIX::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
150 for (
typename CPU_MATRIX::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
152 coords.set(gpu_matrix.
internal_size1() * data_index + col_it.index1(), col_it.index2());
153 elements[gpu_matrix.
internal_size1() * data_index + col_it.index1()] = *col_it;
164 template <
typename CPU_MATRIX,
typename SCALARTYPE,
unsigned int ALIGNMENT>
170 if(gpu_matrix.
size1() > 0 && gpu_matrix.
size2() > 0)
172 std::vector<SCALARTYPE> elements(gpu_matrix.
internal_nnz());
184 if(elements[offset] == static_cast<SCALARTYPE>(0.0))
187 if(coords[offset] >= gpu_matrix.
size2())
189 std::cerr <<
"ViennaCL encountered invalid data " << offset <<
" " << ind <<
" " << row <<
" " << coords[offset] <<
" " << gpu_matrix.
size2() << std::endl;
193 cpu_matrix(row, coords[offset]) = elements[offset];
211 template <
typename T,
unsigned int A>
212 struct op_executor<vector_base<T>, op_assign, vector_expression<const ell_matrix<T, A>, const vector_base<T>, op_prod> >
214 static void apply(vector_base<T> & lhs, vector_expression<
const ell_matrix<T, A>,
const vector_base<T>, op_prod>
const & rhs)
228 template <
typename T,
unsigned int A>
229 struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const ell_matrix<T, A>, const vector_base<T>, op_prod> >
231 static void apply(vector_base<T> & lhs, vector_expression<
const ell_matrix<T, A>,
const vector_base<T>, op_prod>
const & rhs)
239 template <
typename T,
unsigned int A>
240 struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const ell_matrix<T, A>, const vector_base<T>, op_prod> >
242 static void apply(vector_base<T> & lhs, vector_expression<
const ell_matrix<T, A>,
const vector_base<T>, op_prod>
const & rhs)
252 template <
typename T,
unsigned int A,
typename LHS,
typename RHS,
typename OP>
253 struct op_executor<vector_base<T>, op_assign, vector_expression<const ell_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
255 static void apply(vector_base<T> & lhs, vector_expression<
const ell_matrix<T, A>,
const vector_expression<const LHS, const RHS, OP>, op_prod>
const & rhs)
263 template <
typename T,
unsigned int A,
typename LHS,
typename RHS,
typename OP>
264 struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const ell_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
266 static void apply(vector_base<T> & lhs, vector_expression<
const ell_matrix<T, A>,
const vector_expression<const LHS, const RHS, OP>, op_prod>
const & rhs)
276 template <
typename T,
unsigned int A,
typename LHS,
typename RHS,
typename OP>
277 struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const ell_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
279 static void apply(vector_base<T> & lhs, vector_expression<
const ell_matrix<T, A>,
const vector_expression<const LHS, const RHS, OP>, op_prod>
const & rhs)
Helper class implementing an array on the host. Default case: No conversion necessary.
Definition: util.hpp:95
vcl_size_t internal_nnz() const
Definition: ell_matrix.hpp:87
std::size_t vcl_size_t
Definition: forwards.h:58
vcl_size_t size() const
Definition: util.hpp:163
const handle_type & handle() const
Definition: ell_matrix.hpp:90
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:172
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:216
vcl_size_t size1() const
Definition: ell_matrix.hpp:80
vcl_size_t size2() const
Definition: ell_matrix.hpp:81
This file provides the forward declarations for the main types used within ViennaCL.
friend void copy(const CPU_MATRIX &cpu_matrix, ell_matrix< T, ALIGN > &gpu_matrix)
vcl_size_t internal_maxnnz() const
Definition: ell_matrix.hpp:83
void memory_read(mem_handle const &src_buffer, vcl_size_t src_offset, vcl_size_t bytes_to_read, void *ptr, bool async=false)
Reads data from a buffer back to main RAM.
Definition: memory.hpp:261
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
Definition: size.hpp:245
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
vcl_size_t internal_size2() const
Definition: ell_matrix.hpp:78
Definition: forwards.h:480
ell_matrix(viennacl::context ctx)
Definition: ell_matrix.hpp:62
const handle_type & handle2() const
Definition: ell_matrix.hpp:93
Sparse matrix class using the ELLPACK format for storing the nonzeros.
Definition: ell_matrix.hpp:53
handle_type & handle()
Definition: ell_matrix.hpp:89
Implementations of operations using sparse matrices.
viennacl::backend::mem_handle handle_type
Definition: ell_matrix.hpp:56
void copy(std::vector< SCALARTYPE > &cpu_vec, circulant_matrix< SCALARTYPE, ALIGNMENT > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
Definition: circulant_matrix.hpp:150
viennacl::memory_types memory_type() const
Definition: context.hpp:76
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_row > row(const matrix_base< NumericT, F > &A, unsigned int i)
Definition: matrix.hpp:910
vcl_size_t nnz() const
Definition: ell_matrix.hpp:86
A vector class representing a linear memory sequence on the GPU. Inspired by boost::numeric::ublas::v...
Definition: forwards.h:208
ell_matrix()
Definition: ell_matrix.hpp:60
vcl_size_t raw_size() const
Returns the number of bytes of the currently active buffer.
Definition: mem_handle.hpp:203
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:41
vcl_size_t maxnnz() const
Definition: ell_matrix.hpp:84
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
handle_type & handle2()
Definition: ell_matrix.hpp:92
scalar< typename viennacl::tools::CHECK_SCALAR_TEMPLATE_ARGUMENT< SCALARTYPE >::ResultType > value_type
Definition: ell_matrix.hpp:57
vcl_size_t size_type
Definition: ell_matrix.hpp:58
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:62
void prod_impl(const matrix_base< NumericT, F > &mat, const vector_base< NumericT > &vec, vector_base< NumericT > &result)
Carries out matrix-vector multiplication.
Definition: matrix_operations.hpp:350
void memory_create(mem_handle &handle, vcl_size_t size_in_bytes, viennacl::context const &ctx, const void *host_ptr=NULL)
Creates an array of the specified size. If the second argument is provided, the buffer is initialized...
Definition: memory.hpp:87
void switch_active_handle_id(memory_types new_id)
Switches the currently active handle. If no support for that backend is provided, an exception is thr...
Definition: mem_handle.hpp:94
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
vcl_size_t internal_size1() const
Definition: ell_matrix.hpp:77