ViennaCL - The Vienna Computing Library  1.6.0
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hyb_matrix.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_HYB_MATRIX_HPP_
2 #define VIENNACL_HYB_MATRIX_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, 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 PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
27 #include "viennacl/forwards.h"
28 #include "viennacl/vector.hpp"
29 
30 #include "viennacl/tools/tools.hpp"
31 
33 
34 namespace viennacl
35 {
37 template<typename NumericT, unsigned int AlignmentV /* see forwards.h for default argument */>
38 class hyb_matrix
39 {
40 public:
43 
44  hyb_matrix() : csr_threshold_(NumericT(0.8)), rows_(0), cols_(0) {}
45 
46  hyb_matrix(viennacl::context ctx) : csr_threshold_(NumericT(0.8)), rows_(0), cols_(0)
47  {
48  ell_coords_.switch_active_handle_id(ctx.memory_type());
49  ell_elements_.switch_active_handle_id(ctx.memory_type());
50 
51  csr_rows_.switch_active_handle_id(ctx.memory_type());
52  csr_cols_.switch_active_handle_id(ctx.memory_type());
53  csr_elements_.switch_active_handle_id(ctx.memory_type());
54 
55 #ifdef VIENNACL_WITH_OPENCL
56  if (ctx.memory_type() == OPENCL_MEMORY)
57  {
58  ell_coords_.opencl_handle().context(ctx.opencl_context());
59  ell_elements_.opencl_handle().context(ctx.opencl_context());
60 
61  csr_rows_.opencl_handle().context(ctx.opencl_context());
62  csr_cols_.opencl_handle().context(ctx.opencl_context());
63  csr_elements_.opencl_handle().context(ctx.opencl_context());
64  }
65 #endif
66  }
67 
69  void clear()
70  {
71  // ELL part:
72  ellnnz_ = 0;
73 
75  std::vector<NumericT> host_elements(internal_size1());
76 
77  viennacl::backend::memory_create(ell_coords_, host_coords_buffer.element_size() * internal_size1(), viennacl::traits::context(ell_coords_), host_coords_buffer.get());
78  viennacl::backend::memory_create(ell_elements_, sizeof(NumericT) * internal_size1(), viennacl::traits::context(ell_elements_), &(host_elements[0]));
79 
80  // CSR part:
81  csrnnz_ = 0;
82 
83  viennacl::backend::typesafe_host_array<unsigned int> host_row_buffer(csr_rows_, rows_ + 1);
84  viennacl::backend::typesafe_host_array<unsigned int> host_col_buffer(csr_cols_, 1);
85  host_elements.resize(1);
86 
87  viennacl::backend::memory_create(csr_rows_, host_row_buffer.element_size() * (rows_ + 1), viennacl::traits::context(csr_rows_), host_row_buffer.get());
88  viennacl::backend::memory_create(csr_cols_, host_col_buffer.element_size() * 1, viennacl::traits::context(csr_cols_), host_col_buffer.get());
89  viennacl::backend::memory_create(csr_elements_, sizeof(NumericT) * 1, viennacl::traits::context(csr_elements_), &(host_elements[0]));
90  }
91 
92  NumericT csr_threshold() const { return csr_threshold_; }
93  void csr_threshold(NumericT thr) { csr_threshold_ = thr; }
94 
95  vcl_size_t internal_size1() const { return viennacl::tools::align_to_multiple<vcl_size_t>(rows_, AlignmentV); }
96  vcl_size_t internal_size2() const { return viennacl::tools::align_to_multiple<vcl_size_t>(cols_, AlignmentV); }
97 
98  vcl_size_t size1() const { return rows_; }
99  vcl_size_t size2() const { return cols_; }
100 
101  vcl_size_t internal_ellnnz() const {return viennacl::tools::align_to_multiple<vcl_size_t>(ellnnz_, AlignmentV); }
102  vcl_size_t ell_nnz() const { return ellnnz_; }
103  vcl_size_t csr_nnz() const { return csrnnz_; }
104 
105  const handle_type & handle() const { return ell_elements_; }
106  const handle_type & handle2() const { return ell_coords_; }
107  const handle_type & handle3() const { return csr_rows_; }
108  const handle_type & handle4() const { return csr_cols_; }
109  const handle_type & handle5() const { return csr_elements_; }
110 
111 public:
112 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
113  template<typename CPUMatrixT>
114  friend void copy(const CPUMatrixT & cpu_matrix, hyb_matrix & gpu_matrix );
115 #else
116  template<typename CPUMatrixT, typename T, unsigned int ALIGN>
117  friend void copy(const CPUMatrixT & cpu_matrix, hyb_matrix<T, ALIGN> & gpu_matrix );
118 #endif
119 
120 private:
121  NumericT csr_threshold_;
122  vcl_size_t rows_;
123  vcl_size_t cols_;
124  vcl_size_t ellnnz_;
125  vcl_size_t csrnnz_;
126 
127  handle_type ell_coords_; // ell coords
128  handle_type ell_elements_; // ell elements
129 
130  handle_type csr_rows_;
131  handle_type csr_cols_;
132  handle_type csr_elements_;
133 };
134 
135 template<typename CPUMatrixT, typename NumericT, unsigned int AlignmentV>
136 void copy(const CPUMatrixT& cpu_matrix, hyb_matrix<NumericT, AlignmentV>& gpu_matrix )
137 {
138  assert( (gpu_matrix.size1() == 0 || viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
139  assert( (gpu_matrix.size2() == 0 || viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
140 
141  if (cpu_matrix.size1() > 0 && cpu_matrix.size2() > 0)
142  {
143  //determine max capacity for row
144  vcl_size_t max_entries_per_row = 0;
145  std::vector<vcl_size_t> hist_entries(cpu_matrix.size2() + 1, 0);
146 
147  for (typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
148  {
149  vcl_size_t num_entries = 0;
150  for (typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
151  {
152  ++num_entries;
153  }
154 
155  hist_entries[num_entries] += 1;
156  max_entries_per_row = std::max(max_entries_per_row, num_entries);
157  }
158 
159  vcl_size_t sum = 0;
160  for (vcl_size_t ind = 0; ind <= max_entries_per_row; ind++)
161  {
162  sum += hist_entries[ind];
163 
164  if (NumericT(sum) >= NumericT(gpu_matrix.csr_threshold()) * NumericT(cpu_matrix.size1()))
165  {
166  max_entries_per_row = ind;
167  break;
168  }
169  }
170 
171  //setup GPU matrix
172  gpu_matrix.ellnnz_ = max_entries_per_row;
173  gpu_matrix.rows_ = cpu_matrix.size1();
174  gpu_matrix.cols_ = cpu_matrix.size2();
175 
176  vcl_size_t nnz = gpu_matrix.internal_size1() * gpu_matrix.internal_ellnnz();
177 
178  viennacl::backend::typesafe_host_array<unsigned int> ell_coords(gpu_matrix.ell_coords_, nnz);
179  viennacl::backend::typesafe_host_array<unsigned int> csr_rows(gpu_matrix.csr_rows_, cpu_matrix.size1() + 1);
180  std::vector<unsigned int> csr_cols;
181 
182  std::vector<NumericT> ell_elements(nnz);
183  std::vector<NumericT> csr_elements;
184 
185  vcl_size_t csr_index = 0;
186 
187  for (typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
188  {
189  vcl_size_t data_index = 0;
190 
191  csr_rows.set(row_it.index1(), csr_index);
192 
193  for (typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
194  {
195  if (data_index < max_entries_per_row)
196  {
197  ell_coords.set(gpu_matrix.internal_size1() * data_index + col_it.index1(), col_it.index2());
198  ell_elements[gpu_matrix.internal_size1() * data_index + col_it.index1()] = *col_it;
199  }
200  else
201  {
202  csr_cols.push_back(static_cast<unsigned int>(col_it.index2()));
203  csr_elements.push_back(*col_it);
204 
205  csr_index++;
206  }
207 
208  data_index++;
209  }
210 
211  }
212 
213  if (csr_cols.empty())
214  {
215  csr_cols.push_back(0);
216  csr_elements.push_back(0);
217  }
218 
219  csr_rows.set(csr_rows.size() - 1, csr_index);
220 
221  gpu_matrix.csrnnz_ = csr_cols.size();
222 
223  viennacl::backend::typesafe_host_array<unsigned int> csr_cols_for_gpu(gpu_matrix.csr_cols_, csr_cols.size());
224  for (vcl_size_t i=0; i<csr_cols.size(); ++i)
225  csr_cols_for_gpu.set(i, csr_cols[i]);
226 
227  viennacl::backend::memory_create(gpu_matrix.ell_coords_, ell_coords.raw_size(), traits::context(gpu_matrix.ell_coords_), ell_coords.get());
228  viennacl::backend::memory_create(gpu_matrix.ell_elements_, sizeof(NumericT) * ell_elements.size(), traits::context(gpu_matrix.ell_elements_), &(ell_elements[0]));
229 
230  viennacl::backend::memory_create(gpu_matrix.csr_rows_, csr_rows.raw_size(), traits::context(gpu_matrix.csr_rows_), csr_rows.get());
231  viennacl::backend::memory_create(gpu_matrix.csr_cols_, csr_cols_for_gpu.raw_size(), traits::context(gpu_matrix.csr_cols_), csr_cols_for_gpu.get());
232  viennacl::backend::memory_create(gpu_matrix.csr_elements_, sizeof(NumericT) * csr_elements.size(), traits::context(gpu_matrix.csr_elements_), &(csr_elements[0]));
233  }
234 }
235 
236 template<typename CPUMatrixT, typename NumericT, unsigned int AlignmentV>
237 void copy(const hyb_matrix<NumericT, AlignmentV>& gpu_matrix, CPUMatrixT& cpu_matrix)
238 {
239  assert( (viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
240  assert( (viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
241 
242  if (gpu_matrix.size1() > 0 && gpu_matrix.size2() > 0)
243  {
244  std::vector<NumericT> ell_elements(gpu_matrix.internal_size1() * gpu_matrix.internal_ellnnz());
245  viennacl::backend::typesafe_host_array<unsigned int> ell_coords(gpu_matrix.handle2(), gpu_matrix.internal_size1() * gpu_matrix.internal_ellnnz());
246 
247  std::vector<NumericT> csr_elements(gpu_matrix.csr_nnz());
248  viennacl::backend::typesafe_host_array<unsigned int> csr_rows(gpu_matrix.handle3(), gpu_matrix.size1() + 1);
249  viennacl::backend::typesafe_host_array<unsigned int> csr_cols(gpu_matrix.handle4(), gpu_matrix.csr_nnz());
250 
251  viennacl::backend::memory_read(gpu_matrix.handle(), 0, sizeof(NumericT) * ell_elements.size(), &(ell_elements[0]));
252  viennacl::backend::memory_read(gpu_matrix.handle2(), 0, ell_coords.raw_size(), ell_coords.get());
253  viennacl::backend::memory_read(gpu_matrix.handle3(), 0, csr_rows.raw_size(), csr_rows.get());
254  viennacl::backend::memory_read(gpu_matrix.handle4(), 0, csr_cols.raw_size(), csr_cols.get());
255  viennacl::backend::memory_read(gpu_matrix.handle5(), 0, sizeof(NumericT) * csr_elements.size(), &(csr_elements[0]));
256 
257 
258  for (vcl_size_t row = 0; row < gpu_matrix.size1(); row++)
259  {
260  for (vcl_size_t ind = 0; ind < gpu_matrix.internal_ellnnz(); ind++)
261  {
262  vcl_size_t offset = gpu_matrix.internal_size1() * ind + row;
263 
264  if (!bool(ell_elements[offset]))
265  continue;
266 
267  if (ell_coords[offset] >= gpu_matrix.size2())
268  {
269  std::cerr << "ViennaCL encountered invalid data " << offset << " " << ind << " " << row << " " << ell_coords[offset] << " " << gpu_matrix.size2() << std::endl;
270  return;
271  }
272 
273  cpu_matrix(row, ell_coords[offset]) = ell_elements[offset];
274  }
275 
276  for (vcl_size_t ind = csr_rows[row]; ind < csr_rows[row+1]; ind++)
277  {
278  if (!bool(csr_elements[ind]))
279  continue;
280 
281  if (csr_cols[ind] >= gpu_matrix.size2())
282  {
283  std::cerr << "ViennaCL encountered invalid data " << std::endl;
284  return;
285  }
286 
287  cpu_matrix(row, csr_cols[ind]) = csr_elements[ind];
288  }
289  }
290  }
291 }
292 
293 
294 //
295 // Specify available operations:
296 //
297 
300 namespace linalg
301 {
302 namespace detail
303 {
304  // x = A * y
305  template<typename T, unsigned int A>
306  struct op_executor<vector_base<T>, op_assign, vector_expression<const hyb_matrix<T, A>, const vector_base<T>, op_prod> >
307  {
308  static void apply(vector_base<T> & lhs, vector_expression<const hyb_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
309  {
310  // check for the special case x = A * x
311  if (viennacl::traits::handle(lhs) == viennacl::traits::handle(rhs.rhs()))
312  {
313  viennacl::vector<T> temp(lhs);
314  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
315  lhs = temp;
316  }
317  else
318  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), lhs);
319  }
320  };
321 
322  template<typename T, unsigned int A>
323  struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const hyb_matrix<T, A>, const vector_base<T>, op_prod> >
324  {
325  static void apply(vector_base<T> & lhs, vector_expression<const hyb_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
326  {
327  viennacl::vector<T> temp(lhs);
328  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
329  lhs += temp;
330  }
331  };
332 
333  template<typename T, unsigned int A>
334  struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const hyb_matrix<T, A>, const vector_base<T>, op_prod> >
335  {
336  static void apply(vector_base<T> & lhs, vector_expression<const hyb_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
337  {
338  viennacl::vector<T> temp(lhs);
339  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
340  lhs -= temp;
341  }
342  };
343 
344 
345  // x = A * vec_op
346  template<typename T, unsigned int A, typename LHS, typename RHS, typename OP>
347  struct op_executor<vector_base<T>, op_assign, vector_expression<const hyb_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
348  {
349  static void apply(vector_base<T> & lhs, vector_expression<const hyb_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
350  {
351  viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
352  viennacl::linalg::prod_impl(rhs.lhs(), temp, lhs);
353  }
354  };
355 
356  // x = A * vec_op
357  template<typename T, unsigned int A, typename LHS, typename RHS, typename OP>
358  struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const hyb_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
359  {
360  static void apply(vector_base<T> & lhs, vector_expression<const hyb_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
361  {
362  viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
363  viennacl::vector<T> temp_result(lhs);
364  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
365  lhs += temp_result;
366  }
367  };
368 
369  // x = A * vec_op
370  template<typename T, unsigned int A, typename LHS, typename RHS, typename OP>
371  struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const hyb_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
372  {
373  static void apply(vector_base<T> & lhs, vector_expression<const hyb_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
374  {
375  viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
376  viennacl::vector<T> temp_result(lhs);
377  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
378  lhs -= temp_result;
379  }
380  };
381 
382 } // namespace detail
383 } // namespace linalg
384 
386 }
387 
388 #endif
vcl_size_t internal_ellnnz() const
Definition: hyb_matrix.hpp:101
Sparse matrix class using a hybrid format composed of the ELL and CSR format for storing the nonzeros...
Definition: forwards.h:405
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:226
const handle_type & handle3() const
Definition: hyb_matrix.hpp:107
Various little tools used here and there in ViennaCL.
const handle_type & handle() const
Definition: hyb_matrix.hpp:105
void clear()
Resets all entries in the matrix back to zero without changing the matrix size. Resets the sparsity p...
Definition: hyb_matrix.hpp:69
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 internal_size1() const
Definition: hyb_matrix.hpp:95
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.
Definition: memory.hpp:261
const handle_type & handle4() const
Definition: hyb_matrix.hpp:108
T max(const T &lhs, const T &rhs)
Maximum.
Definition: util.hpp:59
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
statement sum(scalar< NumericT > const *s, vector_base< NumericT > const *x)
Definition: preset.hpp:246
NumericT csr_threshold() const
Definition: hyb_matrix.hpp:92
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
vcl_size_t csr_nnz() const
Definition: hyb_matrix.hpp:103
const handle_type & handle2() const
Definition: hyb_matrix.hpp:106
viennacl::backend::mem_handle handle_type
Definition: hyb_matrix.hpp:41
vcl_size_t internal_size2() const
Definition: hyb_matrix.hpp:96
scalar< typename viennacl::tools::CHECK_SCALAR_TEMPLATE_ARGUMENT< NumericT >::ResultType > value_type
Definition: hyb_matrix.hpp:42
Implementations of operations using sparse matrices.
vcl_size_t size2() const
Definition: hyb_matrix.hpp:99
std::size_t vcl_size_t
Definition: forwards.h:74
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:853
viennacl::memory_types memory_type() const
Definition: context.hpp:76
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
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:40
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
vcl_size_t ell_nnz() const
Definition: hyb_matrix.hpp:102
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
hyb_matrix(viennacl::context ctx)
Definition: hyb_matrix.hpp:46
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
const handle_type & handle5() const
Definition: hyb_matrix.hpp:109
void prod_impl(const matrix_base< NumericT > &mat, const vector_base< NumericT > &vec, vector_base< NumericT > &result)
Carries out matrix-vector multiplication.
vcl_size_t size1() const
Definition: hyb_matrix.hpp:98
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
friend void copy(const CPUMatrixT &cpu_matrix, hyb_matrix< T, ALIGN > &gpu_matrix)
vcl_size_t raw_size() const
Definition: util.hpp:111
void csr_threshold(NumericT thr)
Definition: hyb_matrix.hpp:93