1 #ifndef VIENNACL_COORDINATE_MATRIX_HPP_
2 #define VIENNACL_COORDINATE_MATRIX_HPP_
46 template<
typename CPUMatrixT,
typename NumericT,
unsigned int AlignmentV>
47 void copy(
const CPUMatrixT & cpu_matrix,
56 if ( cpu_matrix.size1() > 0 && cpu_matrix.size2() > 0 )
59 for (
typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
60 for (
typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
64 gpu_matrix.nonzeros_ = num_entries;
65 gpu_matrix.rows_ = cpu_matrix.size1();
66 gpu_matrix.cols_ = cpu_matrix.size2();
70 std::vector<NumericT> elements(gpu_matrix.
internal_nnz());
75 group_boundaries.
set(0, 0);
76 for (
typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
78 for (
typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
80 coord_buffer.set(2*data_index, col_it.index1());
81 coord_buffer.set(2*data_index + 1, col_it.index2());
82 elements[data_index] = *col_it;
86 while (data_index >
vcl_size_t(static_cast<double>(current_fraction + 1) / static_cast<double>(group_num)) * num_entries)
87 group_boundaries.
set(++current_fraction, data_index);
91 group_boundaries.set(group_num, data_index);
109 template<
typename NumericT,
unsigned int AlignmentV>
110 void copy(
const std::vector< std::map<unsigned int, NumericT> > & cpu_matrix,
114 for (
vcl_size_t i=0; i<cpu_matrix.size(); ++i)
116 if (cpu_matrix[i].
size() > 0)
117 max_col = std::max<vcl_size_t>(max_col, (cpu_matrix[i].rbegin())->first);
133 template<
typename CPUMatrixT,
typename NumericT,
unsigned int AlignmentV>
135 CPUMatrixT & cpu_matrix )
140 if ( gpu_matrix.
size1() > 0 && gpu_matrix.
size2() > 0 )
144 std::vector<NumericT> elements(gpu_matrix.
nnz());
152 for (
vcl_size_t index = 0; index < gpu_matrix.
nnz(); ++index)
153 cpu_matrix(coord_buffer[2*index], coord_buffer[2*index+1]) = elements[index];
163 template<
typename NumericT,
unsigned int AlignmentV>
165 std::vector< std::map<unsigned int, NumericT> > & cpu_matrix)
167 if (cpu_matrix.size() == 0)
168 cpu_matrix.resize(gpu_matrix.
size1());
170 assert(cpu_matrix.size() == gpu_matrix.
size1() && bool(
"Matrix dimension mismatch!"));
173 copy(gpu_matrix, temp);
185 template<
class NumericT,
unsigned int AlignmentV >
202 #ifdef VIENNACL_WITH_OPENCL
205 group_boundaries_.opencl_handle().context(ctx.opencl_context());
206 coord_buffer_.opencl_handle().context(ctx.opencl_context());
207 elements_.opencl_handle().context(ctx.opencl_context());
220 rows_(rows), cols_(cols), nonzeros_(nonzeros)
234 #ifdef VIENNACL_WITH_OPENCL
237 group_boundaries_.opencl_handle().context(ctx.opencl_context());
238 coord_buffer_.opencl_handle().context(ctx.opencl_context());
239 elements_.opencl_handle().context(ctx.opencl_context());
252 : rows_(rows), cols_(cols), nonzeros_(0)
258 #ifdef VIENNACL_WITH_OPENCL
261 group_boundaries_.opencl_handle().context(ctx.opencl_context());
262 coord_buffer_.opencl_handle().context(ctx.opencl_context());
263 elements_.opencl_handle().context(ctx.opencl_context());
272 if (new_nonzeros > nonzeros_)
279 vcl_size_t internal_new_nnz = viennacl::tools::align_to_multiple<vcl_size_t>(new_nonzeros, AlignmentV);
287 nonzeros_ = new_nonzeros;
299 assert (new_size1 > 0 && new_size2 > 0);
301 if (new_size1 < rows_ || new_size2 < cols_)
303 std::vector<std::map<unsigned int, NumericT> > stl_sparse_matrix;
305 stl_sparse_matrix.resize(rows_);
307 if (preserve && rows_ > 0)
310 stl_sparse_matrix.resize(new_size1);
313 if (new_size2 < cols_ && rows_ > 0)
315 for (
vcl_size_t i=0; i<stl_sparse_matrix.size(); ++i)
317 std::list<unsigned int> to_delete;
318 for (
typename std::map<unsigned int, NumericT>::iterator it = stl_sparse_matrix[i].begin();
319 it != stl_sparse_matrix[i].end();
322 if (it->first >= new_size2)
323 to_delete.push_back(it->first);
326 for (std::list<unsigned int>::iterator it = to_delete.begin(); it != to_delete.end(); ++it)
327 stl_sparse_matrix[i].erase(*it);
346 std::vector<NumericT> host_elements(1);
374 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
375 template<
typename CPUMatrixT>
378 template<
typename CPUMatrixT,
typename NumericT2,
unsigned int AlignmentV2>
411 template<
typename T,
unsigned int A>
412 struct op_executor<vector_base<T>,
op_assign, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
414 static void apply(vector_base<T> & lhs, vector_expression<
const coordinate_matrix<T, A>,
const vector_base<T>, op_prod>
const & rhs)
428 template<
typename T,
unsigned int A>
429 struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
431 static void apply(vector_base<T> & lhs, vector_expression<
const coordinate_matrix<T, A>,
const vector_base<T>, op_prod>
const & rhs)
445 template<
typename T,
unsigned int A>
446 struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
448 static void apply(vector_base<T> & lhs, vector_expression<
const coordinate_matrix<T, A>,
const vector_base<T>, op_prod>
const & rhs)
464 template<
typename T,
unsigned int A,
typename LHS,
typename RHS,
typename OP>
465 struct op_executor<vector_base<T>,
op_assign, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
467 static void apply(vector_base<T> & lhs, vector_expression<
const coordinate_matrix<T, A>,
const vector_expression<const LHS, const RHS, OP>, op_prod>
const & rhs)
475 template<
typename T,
unsigned int A,
typename LHS,
typename RHS,
typename OP>
476 struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
478 static void apply(vector_base<T> & lhs, vector_expression<
const coordinate_matrix<T, A>,
const vector_expression<const LHS, const RHS, OP>, op_prod>
const & rhs)
488 template<
typename T,
unsigned int A,
typename LHS,
typename RHS,
typename OP>
489 struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
491 static void apply(vector_base<T> & lhs, vector_expression<
const coordinate_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.
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...
const handle_type & handle12() const
Returns the OpenCL handle to the (row, column) index array.
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
coordinate_matrix(viennacl::context ctx)
coordinate_matrix()
Default construction of a coordinate matrix. No memory is allocated.
This file provides the forward declarations for the main types used within ViennaCL.
vcl_size_t nnz() const
Returns the number of nonzero entries.
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.
void resize(vcl_size_t new_size1, vcl_size_t new_size2, bool preserve=true)
Resize the matrix.
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
vcl_size_t element_size(memory_types)
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
vcl_size_t internal_nnz() const
Returns the number of internal nonzero entries.
coordinate_matrix(vcl_size_t rows, vcl_size_t cols, vcl_size_t nonzeros=0, viennacl::context ctx=viennacl::context())
Construction of a coordinate matrix with the supplied number of rows and columns. If the number of no...
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
vcl_size_t size2() const
Returns the number of columns.
scalar< typename viennacl::tools::CHECK_SCALAR_TEMPLATE_ARGUMENT< NumericT >::ResultType > value_type
Implementations of operations using sparse matrices.
viennacl::backend::mem_handle handle_type
void clear()
Resets all entries in the matrix back to zero without changing the matrix size. Resets the sparsity p...
const handle_type & handle3() const
Returns the OpenCL handle to the group start index array.
viennacl::memory_types memory_type() const
friend void copy(const CPUMatrixT &cpu_matrix, coordinate_matrix< NumericT2, AlignmentV2 > &gpu_matrix)
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...
void memory_copy(mem_handle const &src_buffer, mem_handle &dst_buffer, vcl_size_t src_offset, vcl_size_t dst_offset, vcl_size_t bytes_to_copy)
Copies 'bytes_to_copy' bytes from address 'src_buffer + src_offset' to memory starting at address 'ds...
void reserve(vcl_size_t new_nonzeros)
Allocate memory for the supplied number of nonzeros in the matrix. Old values are preserved...
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
const handle_type & handle() const
Returns the OpenCL handle to the matrix entry array.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
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) ...
void set(vcl_size_t index, U value)
vcl_size_t groups() const
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.
coordinate_matrix(vcl_size_t rows, vcl_size_t cols, viennacl::context ctx)
Construction of a coordinate matrix with the supplied number of rows and columns in the supplied cont...
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...
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.
vcl_size_t size1() const
Returns the number of rows.
void memory_shallow_copy(mem_handle const &src_buffer, mem_handle &dst_buffer)
A 'shallow' copy operation from an initialized buffer to an uninitialized buffer. The uninitialized b...
A sparse square matrix, where entries are stored as triplets (i,j, val), where i and j are the row an...