1 #ifndef VIENNACL_COMPRESSED_compressed_compressed_matrix_HPP_
2 #define VIENNACL_COMPRESSED_compressed_compressed_matrix_HPP_
40 template<
typename CPUMatrixT,
typename NumericT>
52 std::vector<NumericT> elements(nonzeros);
57 for (
typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1();
58 row_it != cpu_matrix.end1();
61 bool row_empty =
true;
63 for (
typename CPUMatrixT::const_iterator2 col_it = row_it.begin();
64 col_it != row_it.end();
68 if (entry < 0 || entry > 0)
72 assert(row_index < nonzero_rows &&
bool(
"Provided count of nonzero rows exceeded!"));
75 row_buffer.set(row_index, data_index);
76 row_indices.set(row_index, col_it.index1());
80 col_buffer.set(data_index, col_it.index2());
81 elements[data_index] = entry;
86 row_buffer.set(row_index, data_index);
88 gpu_matrix.
set(row_buffer.get(),
114 template<
typename CPUMatrixT,
typename NumericT>
115 void copy(
const CPUMatrixT & cpu_matrix,
120 if ( cpu_matrix.size1() > 0 && cpu_matrix.size2() > 0 )
125 for (
typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1();
126 row_it != cpu_matrix.end1();
129 bool row_empty =
true;
130 for (
typename CPUMatrixT::const_iterator2 col_it = row_it.begin();
131 col_it != row_it.end();
135 if (val < 0 || val > 0)
148 if (num_entries == 0)
163 template<
typename SizeT,
typename NumericT>
164 void copy(
const std::vector< std::map<SizeT, NumericT> > & cpu_matrix,
170 for (
vcl_size_t i=0; i<cpu_matrix.size(); ++i)
172 if (cpu_matrix[i].
size() > 0)
174 nonzeros += cpu_matrix[i].size();
175 if (cpu_matrix[i].
size() > 0)
176 max_col = std::max<vcl_size_t>(max_col, (cpu_matrix[i].rbegin())->first);
198 template<
typename CPUMatrixT,
typename NumericT>
200 CPUMatrixT & cpu_matrix )
202 assert( (cpu_matrix.size1() == gpu_matrix.
size1()) &&
bool(
"Size mismatch") );
203 assert( (cpu_matrix.size2() == gpu_matrix.
size2()) &&
bool(
"Size mismatch") );
205 if ( gpu_matrix.
size1() > 0 && gpu_matrix.
size2() > 0 )
211 std::vector<NumericT> elements(gpu_matrix.
nnz());
222 for (
vcl_size_t i = 1; i < row_buffer.size(); ++i)
224 while (data_index < row_buffer[i])
226 if (col_buffer[data_index] >= gpu_matrix.
size2())
228 std::cerr <<
"ViennaCL encountered invalid data at colbuffer[" << data_index <<
"]: " << col_buffer[data_index] << std::endl;
232 NumericT val = elements[data_index];
233 if (val < 0 || val > 0)
234 cpu_matrix(row_indices[i-1], col_buffer[data_index]) = val;
247 template<
typename NumericT>
249 std::vector< std::map<unsigned int, NumericT> > & cpu_matrix)
252 copy(gpu_matrix, temp);
264 template<
class NumericT>
284 : rows_(rows), cols_(cols), nonzero_rows_(nonzero_rows), nonzeros_(nonzeros)
291 #ifdef VIENNACL_WITH_OPENCL
294 row_buffer_.opencl_handle().context(ctx.opencl_context());
295 row_indices_.opencl_handle().context(ctx.opencl_context());
296 col_buffer_.opencl_handle().context(ctx.opencl_context());
297 elements_.opencl_handle().context(ctx.opencl_context());
318 : rows_(rows), cols_(cols), nonzeros_(0)
324 #ifdef VIENNACL_WITH_OPENCL
327 row_buffer_.opencl_handle().context(ctx.opencl_context());
328 col_buffer_.opencl_handle().context(ctx.opencl_context());
329 elements_.opencl_handle().context(ctx.opencl_context());
345 #ifdef VIENNACL_WITH_OPENCL
348 row_buffer_.opencl_handle().context(ctx.opencl_context());
349 row_indices_.opencl_handle().context(ctx.opencl_context());
350 col_buffer_.opencl_handle().context(ctx.opencl_context());
351 elements_.opencl_handle().context(ctx.opencl_context());
357 #ifdef VIENNACL_WITH_OPENCL
360 rows_(rows), cols_(cols), nonzero_rows_(nonzero_rows), nonzeros_(nonzeros)
363 row_buffer_.opencl_handle() = mem_row_buffer;
364 row_buffer_.opencl_handle().inc();
365 row_buffer_.
raw_size(
sizeof(cl_uint) * (nonzero_rows + 1));
368 row_indices_.opencl_handle() = mem_row_indices;
369 row_indices_.opencl_handle().inc();
370 row_indices_.
raw_size(
sizeof(cl_uint) * nonzero_rows);
373 col_buffer_.opencl_handle() = mem_col_buffer;
374 col_buffer_.opencl_handle().inc();
375 col_buffer_.
raw_size(
sizeof(cl_uint) * nonzeros);
378 elements_.opencl_handle() = mem_elements;
379 elements_.opencl_handle().inc();
388 assert( (rows_ == 0 || rows_ == other.
size1()) &&
bool(
"Size mismatch") );
389 assert( (cols_ == 0 || cols_ == other.
size2()) &&
bool(
"Size mismatch") );
391 rows_ = other.
size1();
392 cols_ = other.
size2();
393 nonzero_rows_ = other.
nnz1();
394 nonzeros_ = other.
nnz();
396 viennacl::backend::typesafe_memory_copy<unsigned int>(other.row_buffer_, row_buffer_);
397 viennacl::backend::typesafe_memory_copy<unsigned int>(other.row_indices_, row_indices_);
398 viennacl::backend::typesafe_memory_copy<unsigned int>(other.col_buffer_, col_buffer_);
399 viennacl::backend::typesafe_memory_copy<NumericT>(other.elements_, elements_);
416 void set(
const void * row_jumper,
417 const void * row_indices,
418 const void * col_buffer,
425 assert( (rows > 0) &&
bool(
"Error in compressed_compressed_matrix::set(): Number of rows must be larger than zero!"));
426 assert( (cols > 0) &&
bool(
"Error in compressed_compressed_matrix::set(): Number of columns must be larger than zero!"));
427 assert( (nonzero_rows > 0) &&
bool(
"Error in compressed_compressed_matrix::set(): Number of nonzero rows must be larger than zero!"));
428 assert( (nonzeros > 0) &&
bool(
"Error in compressed_compressed_matrix::set(): Number of nonzeros must be larger than zero!"));
436 nonzeros_ = nonzeros;
437 nonzero_rows_ = nonzero_rows;
448 std::vector<NumericT> host_elements(1);
469 const handle_type &
handle1()
const {
return row_buffer_; }
471 const handle_type &
handle2()
const {
return col_buffer_; }
473 const handle_type &
handle3()
const {
return row_indices_; }
475 const handle_type &
handle()
const {
return elements_; }
478 handle_type &
handle1() {
return row_buffer_; }
480 handle_type &
handle2() {
return col_buffer_; }
482 handle_type &
handle3() {
return row_indices_; }
484 handle_type &
handle() {
return elements_; }
488 viennacl::backend::switch_memory_context<unsigned int>(row_buffer_, new_ctx);
489 viennacl::backend::switch_memory_context<unsigned int>(row_indices_, new_ctx);
490 viennacl::backend::switch_memory_context<unsigned int>(col_buffer_, new_ctx);
491 viennacl::backend::switch_memory_context<NumericT>(elements_, new_ctx);
505 handle_type row_buffer_;
506 handle_type row_indices_;
507 handle_type col_buffer_;
508 handle_type elements_;
525 struct op_executor<vector_base<T>,
op_assign, vector_expression<const compressed_compressed_matrix<T>, const vector_base<T>, op_prod> >
527 static void apply(vector_base<T> & lhs, vector_expression<
const compressed_compressed_matrix<T>,
const vector_base<T>, op_prod>
const & rhs)
542 struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const compressed_compressed_matrix<T>, const vector_base<T>, op_prod> >
544 static void apply(vector_base<T> & lhs, vector_expression<
const compressed_compressed_matrix<T>,
const vector_base<T>, op_prod>
const & rhs)
553 struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const compressed_compressed_matrix<T>, const vector_base<T>, op_prod> >
555 static void apply(vector_base<T> & lhs, vector_expression<
const compressed_compressed_matrix<T>,
const vector_base<T>, op_prod>
const & rhs)
565 template<
typename T,
typename LHS,
typename RHS,
typename OP>
566 struct op_executor<vector_base<T>,
op_assign, vector_expression<const compressed_compressed_matrix<T>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
568 static void apply(vector_base<T> & lhs, vector_expression<
const compressed_compressed_matrix<T>,
const vector_expression<const LHS, const RHS, OP>, op_prod>
const & rhs)
576 template<
typename T,
typename LHS,
typename RHS,
typename OP>
577 struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const compressed_compressed_matrix<T>, vector_expression<const LHS, const RHS, OP>, op_prod> >
579 static void apply(vector_base<T> & lhs, vector_expression<
const compressed_compressed_matrix<T>, vector_expression<const LHS, const RHS, OP>, op_prod>
const & rhs)
589 template<
typename T,
typename LHS,
typename RHS,
typename OP>
590 struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const compressed_compressed_matrix<T>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
592 static void apply(vector_base<T> & lhs, vector_expression<
const compressed_compressed_matrix<T>,
const vector_expression<const LHS, const RHS, OP>, op_prod>
const & rhs)
const vcl_size_t & nnz() const
Returns the number of nonzero entries.
Helper class implementing an array on the host. Default case: No conversion necessary.
vcl_size_t element_size() const
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
scalar< typename viennacl::tools::CHECK_SCALAR_TEMPLATE_ARGUMENT< NumericT >::ResultType > value_type
void switch_memory_context(viennacl::context new_ctx)
void set(const void *row_jumper, const void *row_indices, const void *col_buffer, const NumericT *elements, vcl_size_t rows, vcl_size_t cols, vcl_size_t nonzero_rows, vcl_size_t nonzeros)
Sets the row, column and value arrays of the compressed matrix.
const handle_type & handle2() const
Returns the OpenCL handle to the column index array.
const handle_type & handle1() const
Returns the OpenCL handle to the row index array.
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
A proxy class for entries in a vector.
This file provides the forward declarations for the main types used within ViennaCL.
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.
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
handle_type & handle3()
Returns the OpenCL handle to the row index array.
vcl_size_t element_size(memory_types)
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
const handle_type & handle() const
Returns the OpenCL handle to the matrix entry array.
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
handle_type & handle()
Returns the OpenCL handle to the matrix entry array.
const handle_type & handle3() const
Returns the OpenCL handle to the row index array.
A sparse square matrix in compressed sparse rows format optimized for the case that only a few rows c...
Implementations of operations using sparse matrices.
compressed_compressed_matrix(viennacl::context ctx)
compressed_compressed_matrix(vcl_size_t rows, vcl_size_t cols, vcl_size_t nonzero_rows=0, vcl_size_t nonzeros=0, viennacl::context ctx=viennacl::context())
Construction of a compressed matrix with the supplied number of rows and columns. If the number of no...
compressed_compressed_matrix & operator=(compressed_compressed_matrix const &other)
Assignment a compressed matrix from possibly another memory domain.
void clear()
Resets all entries in the matrix back to zero without changing the matrix size. Resets the sparsity p...
const vcl_size_t & size2() const
Returns the number of columns.
const vcl_size_t & size1() const
Returns the number of rows.
viennacl::memory_types memory_type() const
void copy_impl(const CPUMatrixT &cpu_matrix, compressed_compressed_matrix< NumericT > &gpu_matrix, vcl_size_t nonzero_rows, vcl_size_t nonzeros)
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...
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
handle_type & handle2()
Returns the OpenCL handle to the column index array.
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
const vcl_size_t & nnz1() const
Returns the number of nonzero entries.
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
vcl_size_t raw_size() const
Returns the number of bytes of the currently active buffer.
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...
viennacl::memory_types memory_context() const
void prod_impl(const matrix_base< NumericT > &mat, const vector_base< NumericT > &vec, vector_base< NumericT > &result)
Carries out matrix-vector multiplication.
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
compressed_compressed_matrix()
Default construction of a compressed matrix. No memory is allocated.
compressed_compressed_matrix(vcl_size_t rows, vcl_size_t cols, viennacl::context ctx)
Construction of a compressed matrix with the supplied number of rows and columns. If the number of no...
viennacl::backend::mem_handle handle_type
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...
handle_type & handle1()
Returns the OpenCL handle to the row index array.