ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
coordinate_matrix.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_COORDINATE_MATRIX_HPP_
2 #define VIENNACL_COORDINATE_MATRIX_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2015, 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 manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include <map>
26 #include <vector>
27 #include <list>
28 
29 #include "viennacl/forwards.h"
30 #include "viennacl/vector.hpp"
31 
33 
34 namespace viennacl
35 {
36 
37 
38 //provide copy-operation:
46 template<typename CPUMatrixT, typename NumericT, unsigned int AlignmentV>
47 void copy(const CPUMatrixT & cpu_matrix,
49 {
50  assert( (gpu_matrix.size1() == 0 || viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
51  assert( (gpu_matrix.size2() == 0 || viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
52 
53  vcl_size_t group_num = 64;
54 
55  // Step 1: Determine nonzeros:
56  if ( cpu_matrix.size1() > 0 && cpu_matrix.size2() > 0 )
57  {
58  vcl_size_t num_entries = 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)
61  ++num_entries;
62 
63  // Step 2: Set up matrix data:
64  gpu_matrix.nonzeros_ = num_entries;
65  gpu_matrix.rows_ = cpu_matrix.size1();
66  gpu_matrix.cols_ = cpu_matrix.size2();
67 
68  viennacl::backend::typesafe_host_array<unsigned int> group_boundaries(gpu_matrix.handle3(), group_num + 1);
69  viennacl::backend::typesafe_host_array<unsigned int> coord_buffer(gpu_matrix.handle12(), 2*gpu_matrix.internal_nnz());
70  std::vector<NumericT> elements(gpu_matrix.internal_nnz());
71 
72  vcl_size_t data_index = 0;
73  vcl_size_t current_fraction = 0;
74 
75  group_boundaries.set(0, 0);
76  for (typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
77  {
78  for (typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
79  {
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;
83  ++data_index;
84  }
85 
86  while (data_index > vcl_size_t(static_cast<double>(current_fraction + 1) / static_cast<double>(group_num)) * num_entries) //split data equally over 64 groups
87  group_boundaries.set(++current_fraction, data_index);
88  }
89 
90  //write end of last group:
91  group_boundaries.set(group_num, data_index);
92  //group_boundaries[1] = data_index; //for one compute unit
93 
94  //std::cout << "Group boundaries: " << std::endl;
95  //for (vcl_size_t i=0; i<group_boundaries.size(); ++i)
96  // std::cout << group_boundaries[i] << std::endl;
97 
98  viennacl::backend::memory_create(gpu_matrix.group_boundaries_, group_boundaries.raw_size(), traits::context(gpu_matrix.group_boundaries_), group_boundaries.get());
99  viennacl::backend::memory_create(gpu_matrix.coord_buffer_, coord_buffer.raw_size(), traits::context(gpu_matrix.coord_buffer_), coord_buffer.get());
100  viennacl::backend::memory_create(gpu_matrix.elements_, sizeof(NumericT)*elements.size(), traits::context(gpu_matrix.elements_), &(elements[0]));
101  }
102 }
103 
109 template<typename NumericT, unsigned int AlignmentV>
110 void copy(const std::vector< std::map<unsigned int, NumericT> > & cpu_matrix,
112 {
113  copy(tools::const_sparse_matrix_adapter<NumericT>(cpu_matrix, cpu_matrix.size(), cpu_matrix.size()), gpu_matrix);
114 }
115 
116 //gpu to cpu:
126 template<typename CPUMatrixT, typename NumericT, unsigned int AlignmentV>
128  CPUMatrixT & cpu_matrix )
129 {
130  assert( (viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
131  assert( (viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
132 
133  if ( gpu_matrix.size1() > 0 && gpu_matrix.size2() > 0 )
134  {
135  //get raw data from memory:
136  viennacl::backend::typesafe_host_array<unsigned int> coord_buffer(gpu_matrix.handle12(), 2*gpu_matrix.nnz());
137  std::vector<NumericT> elements(gpu_matrix.nnz());
138 
139  //std::cout << "GPU nonzeros: " << gpu_matrix.nnz() << std::endl;
140 
141  viennacl::backend::memory_read(gpu_matrix.handle12(), 0, coord_buffer.raw_size(), coord_buffer.get());
142  viennacl::backend::memory_read(gpu_matrix.handle(), 0, sizeof(NumericT) * elements.size(), &(elements[0]));
143 
144  //fill the cpu_matrix:
145  for (vcl_size_t index = 0; index < gpu_matrix.nnz(); ++index)
146  cpu_matrix(coord_buffer[2*index], coord_buffer[2*index+1]) = elements[index];
147 
148  }
149 }
150 
156 template<typename NumericT, unsigned int AlignmentV>
158  std::vector< std::map<unsigned int, NumericT> > & cpu_matrix)
159 {
160  tools::sparse_matrix_adapter<NumericT> temp(cpu_matrix, gpu_matrix.size1(), gpu_matrix.size2());
161  copy(gpu_matrix, temp);
162 }
163 
164 
166 
173 template<class NumericT, unsigned int AlignmentV /* see forwards.h */ >
175 {
176 public:
180 
182  coordinate_matrix() : rows_(0), cols_(0), nonzeros_(0), group_num_(64) {}
183 
184  explicit coordinate_matrix(viennacl::context ctx) : rows_(0), cols_(0), nonzeros_(0), group_num_(64)
185  {
186  group_boundaries_.switch_active_handle_id(ctx.memory_type());
187  coord_buffer_.switch_active_handle_id(ctx.memory_type());
188  elements_.switch_active_handle_id(ctx.memory_type());
189 
190 #ifdef VIENNACL_WITH_OPENCL
191  if (ctx.memory_type() == OPENCL_MEMORY)
192  {
193  group_boundaries_.opencl_handle().context(ctx.opencl_context());
194  coord_buffer_.opencl_handle().context(ctx.opencl_context());
195  elements_.opencl_handle().context(ctx.opencl_context());
196  }
197 #endif
198  }
199 
208  rows_(rows), cols_(cols), nonzeros_(nonzeros)
209  {
210  if (nonzeros > 0)
211  {
214  viennacl::backend::memory_create(elements_, sizeof(NumericT) * internal_nnz(), ctx);
215  }
216  else
217  {
218  group_boundaries_.switch_active_handle_id(ctx.memory_type());
219  coord_buffer_.switch_active_handle_id(ctx.memory_type());
220  elements_.switch_active_handle_id(ctx.memory_type());
221 
222 #ifdef VIENNACL_WITH_OPENCL
223  if (ctx.memory_type() == OPENCL_MEMORY)
224  {
225  group_boundaries_.opencl_handle().context(ctx.opencl_context());
226  coord_buffer_.opencl_handle().context(ctx.opencl_context());
227  elements_.opencl_handle().context(ctx.opencl_context());
228  }
229 #endif
230  }
231  }
232 
240  : rows_(rows), cols_(cols), nonzeros_(0)
241  {
242  group_boundaries_.switch_active_handle_id(ctx.memory_type());
243  coord_buffer_.switch_active_handle_id(ctx.memory_type());
244  elements_.switch_active_handle_id(ctx.memory_type());
245 
246 #ifdef VIENNACL_WITH_OPENCL
247  if (ctx.memory_type() == OPENCL_MEMORY)
248  {
249  group_boundaries_.opencl_handle().context(ctx.opencl_context());
250  coord_buffer_.opencl_handle().context(ctx.opencl_context());
251  elements_.opencl_handle().context(ctx.opencl_context());
252  }
253 #endif
254  }
255 
256 
258  void reserve(vcl_size_t new_nonzeros)
259  {
260  if (new_nonzeros > nonzeros_) //TODO: Do we need to initialize new memory with zero?
261  {
262  handle_type coord_buffer_old;
263  handle_type elements_old;
264  viennacl::backend::memory_shallow_copy(coord_buffer_, coord_buffer_old);
265  viennacl::backend::memory_shallow_copy(elements_, elements_old);
266 
267  vcl_size_t internal_new_nnz = viennacl::tools::align_to_multiple<vcl_size_t>(new_nonzeros, AlignmentV);
268  viennacl::backend::typesafe_host_array<unsigned int> size_deducer(coord_buffer_);
269  viennacl::backend::memory_create(coord_buffer_, size_deducer.element_size() * 2 * internal_new_nnz, viennacl::traits::context(coord_buffer_));
270  viennacl::backend::memory_create(elements_, sizeof(NumericT) * internal_new_nnz, viennacl::traits::context(elements_));
271 
272  viennacl::backend::memory_copy(coord_buffer_old, coord_buffer_, 0, 0, size_deducer.element_size() * 2 * nonzeros_);
273  viennacl::backend::memory_copy(elements_old, elements_, 0, 0, sizeof(NumericT) * nonzeros_);
274 
275  nonzeros_ = new_nonzeros;
276  }
277  }
278 
285  void resize(vcl_size_t new_size1, vcl_size_t new_size2, bool preserve = true)
286  {
287  assert (new_size1 > 0 && new_size2 > 0);
288 
289  if (new_size1 < rows_ || new_size2 < cols_) //enlarge buffer
290  {
291  std::vector<std::map<unsigned int, NumericT> > stl_sparse_matrix;
292  if (rows_ > 0)
293  stl_sparse_matrix.resize(rows_);
294 
295  if (preserve && rows_ > 0)
296  viennacl::copy(*this, stl_sparse_matrix);
297 
298  stl_sparse_matrix.resize(new_size1);
299 
300  //std::cout << "Cropping STL matrix of size " << stl_sparse_matrix.size() << std::endl;
301  if (new_size2 < cols_ && rows_ > 0)
302  {
303  for (vcl_size_t i=0; i<stl_sparse_matrix.size(); ++i)
304  {
305  std::list<unsigned int> to_delete;
306  for (typename std::map<unsigned int, NumericT>::iterator it = stl_sparse_matrix[i].begin();
307  it != stl_sparse_matrix[i].end();
308  ++it)
309  {
310  if (it->first >= new_size2)
311  to_delete.push_back(it->first);
312  }
313 
314  for (std::list<unsigned int>::iterator it = to_delete.begin(); it != to_delete.end(); ++it)
315  stl_sparse_matrix[i].erase(*it);
316  }
317  //std::cout << "Cropping done..." << std::endl;
318  }
319 
320  rows_ = new_size1;
321  cols_ = new_size2;
322  viennacl::copy(stl_sparse_matrix, *this);
323  }
324 
325  rows_ = new_size1;
326  cols_ = new_size2;
327  }
328 
330  void clear()
331  {
332  viennacl::backend::typesafe_host_array<unsigned int> host_group_buffer(group_boundaries_, 65);
333  viennacl::backend::typesafe_host_array<unsigned int> host_coord_buffer(coord_buffer_, 2);
334  std::vector<NumericT> host_elements(1);
335 
336  viennacl::backend::memory_create(group_boundaries_, host_group_buffer.element_size() * 65, viennacl::traits::context(group_boundaries_), host_group_buffer.get());
337  viennacl::backend::memory_create(coord_buffer_, host_coord_buffer.element_size() * 2, viennacl::traits::context(coord_buffer_), host_coord_buffer.get());
338  viennacl::backend::memory_create(elements_, sizeof(NumericT) * 1, viennacl::traits::context(elements_), &(host_elements[0]));
339 
340  nonzeros_ = 0;
341  group_num_ = 64;
342  }
343 
345  vcl_size_t size1() const { return rows_; }
347  vcl_size_t size2() const { return cols_; }
349  vcl_size_t nnz() const { return nonzeros_; }
351  vcl_size_t internal_nnz() const { return viennacl::tools::align_to_multiple<vcl_size_t>(nonzeros_, AlignmentV); }
352 
354  const handle_type & handle12() const { return coord_buffer_; }
356  const handle_type & handle() const { return elements_; }
358  const handle_type & handle3() const { return group_boundaries_; }
359 
360  vcl_size_t groups() const { return group_num_; }
361 
362 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
363  template<typename CPUMatrixT>
364  friend void copy(const CPUMatrixT & cpu_matrix, coordinate_matrix & gpu_matrix );
365 #else
366  template<typename CPUMatrixT, typename NumericT2, unsigned int AlignmentV2>
367  friend void copy(const CPUMatrixT & cpu_matrix, coordinate_matrix<NumericT2, AlignmentV2> & gpu_matrix );
368 #endif
369 
370 private:
373 
375  coordinate_matrix & operator=(coordinate_matrix const &);
376 
377 
378  vcl_size_t rows_;
379  vcl_size_t cols_;
380  vcl_size_t nonzeros_;
381  vcl_size_t group_num_;
382  handle_type coord_buffer_;
383  handle_type elements_;
384  handle_type group_boundaries_;
385 };
386 
387 
388 //
389 // Specify available operations:
390 //
391 
394 namespace linalg
395 {
396 namespace detail
397 {
398  // x = A * y
399  template<typename T, unsigned int A>
400  struct op_executor<vector_base<T>, op_assign, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
401  {
402  static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
403  {
404  // check for the special case x = A * x
405  if (viennacl::traits::handle(lhs) == viennacl::traits::handle(rhs.rhs()))
406  {
407  viennacl::vector<T> temp(lhs);
408  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
409  lhs = temp;
410  }
411  else
412  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), lhs);
413  }
414  };
415 
416  template<typename T, unsigned int A>
417  struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
418  {
419  static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
420  {
421  viennacl::vector<T> temp(lhs);
422  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
423  lhs += temp;
424  }
425  };
426 
427  template<typename T, unsigned int A>
428  struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
429  {
430  static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
431  {
432  viennacl::vector<T> temp(lhs);
433  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
434  lhs -= temp;
435  }
436  };
437 
438 
439  // x = A * vec_op
440  template<typename T, unsigned int A, typename LHS, typename RHS, typename OP>
441  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> >
442  {
443  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)
444  {
445  viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
446  viennacl::linalg::prod_impl(rhs.lhs(), temp, lhs);
447  }
448  };
449 
450  // x += A * vec_op
451  template<typename T, unsigned int A, typename LHS, typename RHS, typename OP>
452  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> >
453  {
454  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)
455  {
456  viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
457  viennacl::vector<T> temp_result(lhs);
458  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
459  lhs += temp_result;
460  }
461  };
462 
463  // x -= A * vec_op
464  template<typename T, unsigned int A, typename LHS, typename RHS, typename OP>
465  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> >
466  {
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)
468  {
469  viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
470  viennacl::vector<T> temp_result(lhs);
471  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
472  lhs -= temp_result;
473  }
474  };
475 
476 } // namespace detail
477 } // namespace linalg
478 
480 }
481 
482 #endif
Helper class implementing an array on the host. Default case: No conversion necessary.
Definition: util.hpp:92
vcl_size_t element_size() const
Definition: util.hpp:112
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
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.)
Definition: size.hpp:163
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.
Definition: memory.hpp:261
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...
Definition: size.hpp:201
vcl_size_t element_size(memory_types)
Definition: memory.hpp:299
float NumericT
Definition: bisect.cpp:40
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
vcl_size_t internal_nnz() const
Returns the number of internal nonzero entries.
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
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...
Definition: blas3.hpp:36
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
Adapts a constant sparse matrix type made up from std::vector > to basic ub...
Definition: adapter.hpp:183
void clear()
Resets all entries in the matrix back to zero without changing the matrix size. Resets the sparsity p...
std::size_t vcl_size_t
Definition: forwards.h:75
const handle_type & handle3() const
Returns the OpenCL handle to the group start index array.
viennacl::memory_types memory_type() const
Definition: context.hpp:76
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...
Definition: mem_handle.hpp:121
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...
Definition: memory.hpp:140
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.
Definition: context.hpp:40
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)
Definition: util.hpp:115
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
vcl_size_t raw_size() const
Returns the number of bytes of the currently active buffer.
Definition: mem_handle.hpp:230
Adapts a non-const sparse matrix type made up from std::vector > to basic u...
Definition: adapter.hpp:357
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...
Definition: memory.hpp:87
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.
Definition: handle.hpp:41
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...
Definition: memory.hpp:177
A sparse square matrix, where entries are stored as triplets (i,j, val), where i and j are the row an...