ViennaCL - The Vienna Computing Library  1.6.2
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sliced_ell_matrix.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_SLICED_ELL_MATRIX_HPP_
2 #define VIENNACL_SLICED_ELL_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 
28 #include "viennacl/forwards.h"
29 #include "viennacl/vector.hpp"
30 
31 #include "viennacl/tools/tools.hpp"
32 
34 
35 namespace viennacl
36 {
45 template<typename ScalarT, typename IndexT /* see forwards.h = unsigned int */>
46 class sliced_ell_matrix
47 {
48 public:
52 
53  explicit sliced_ell_matrix() : rows_(0), cols_(0), rows_per_block_(0) {}
54 
56  size_type num_cols,
57  size_type num_rows_per_block_ = 0)
58  : rows_(num_rows),
59  cols_(num_cols),
60  rows_per_block_(num_rows_per_block_) {}
61 
62  explicit sliced_ell_matrix(viennacl::context ctx) : rows_(0), cols_(0), rows_per_block_(0)
63  {
64  columns_per_block_.switch_active_handle_id(ctx.memory_type());
65  column_indices_.switch_active_handle_id(ctx.memory_type());
66  block_start_.switch_active_handle_id(ctx.memory_type());
67  elements_.switch_active_handle_id(ctx.memory_type());
68 
69 #ifdef VIENNACL_WITH_OPENCL
70  if (ctx.memory_type() == OPENCL_MEMORY)
71  {
72  columns_per_block_.opencl_handle().context(ctx.opencl_context());
73  column_indices_.opencl_handle().context(ctx.opencl_context());
74  block_start_.opencl_handle().context(ctx.opencl_context());
75  elements_.opencl_handle().context(ctx.opencl_context());
76  }
77 #endif
78  }
79 
81  void clear()
82  {
83  viennacl::backend::typesafe_host_array<IndexT> host_columns_per_block_buffer(columns_per_block_, rows_ / rows_per_block_ + 1);
84  viennacl::backend::typesafe_host_array<IndexT> host_column_buffer(column_indices_, internal_size1());
85  viennacl::backend::typesafe_host_array<IndexT> host_block_start_buffer(block_start_, (rows_ - 1) / rows_per_block_ + 1);
86  std::vector<ScalarT> host_elements(1);
87 
88  viennacl::backend::memory_create(columns_per_block_, host_columns_per_block_buffer.element_size() * (rows_ / rows_per_block_ + 1), viennacl::traits::context(columns_per_block_), host_columns_per_block_buffer.get());
89  viennacl::backend::memory_create(column_indices_, host_column_buffer.element_size() * internal_size1(), viennacl::traits::context(column_indices_), host_column_buffer.get());
90  viennacl::backend::memory_create(block_start_, host_block_start_buffer.element_size() * ((rows_ - 1) / rows_per_block_ + 1), viennacl::traits::context(block_start_), host_block_start_buffer.get());
91  viennacl::backend::memory_create(elements_, sizeof(ScalarT) * 1, viennacl::traits::context(elements_), &(host_elements[0]));
92  }
93 
94  vcl_size_t internal_size1() const { return viennacl::tools::align_to_multiple<vcl_size_t>(rows_, rows_per_block_); }
95  vcl_size_t internal_size2() const { return cols_; }
96 
97  vcl_size_t size1() const { return rows_; }
98  vcl_size_t size2() const { return cols_; }
99 
100  vcl_size_t rows_per_block() const { return rows_per_block_; }
101 
102  //vcl_size_t nnz() const { return rows_ * maxnnz_; }
103  //vcl_size_t internal_nnz() const { return internal_size1() * internal_maxnnz(); }
104 
105  handle_type & handle1() { return columns_per_block_; }
106  const handle_type & handle1() const { return columns_per_block_; }
107 
108  handle_type & handle2() { return column_indices_; }
109  const handle_type & handle2() const { return column_indices_; }
110 
111  handle_type & handle3() { return block_start_; }
112  const handle_type & handle3() const { return block_start_; }
113 
114  handle_type & handle() { return elements_; }
115  const handle_type & handle() const { return elements_; }
116 
117 #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
118  template<typename CPUMatrixT>
119  friend void copy(CPUMatrixT const & cpu_matrix, sliced_ell_matrix & gpu_matrix );
120 #else
121  template<typename CPUMatrixT, typename ScalarT2, typename IndexT2>
122  friend void copy(CPUMatrixT const & cpu_matrix, sliced_ell_matrix<ScalarT2, IndexT2> & gpu_matrix );
123 #endif
124 
125 private:
126  vcl_size_t rows_;
127  vcl_size_t cols_;
128  vcl_size_t rows_per_block_; //parameter C in the paper by Kreutzer et al.
129 
130  handle_type columns_per_block_;
131  handle_type column_indices_;
132  handle_type block_start_;
133  handle_type elements_;
134 };
135 
136 template<typename CPUMatrixT, typename ScalarT, typename IndexT>
137 void copy(CPUMatrixT const & cpu_matrix, sliced_ell_matrix<ScalarT, IndexT> & gpu_matrix )
138 {
139  assert( (gpu_matrix.size1() == 0 || viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
140  assert( (gpu_matrix.size2() == 0 || viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
141 
142  if (gpu_matrix.rows_per_block() == 0) // not yet initialized by user. Set defaults.
143  {
144  viennacl::context ctx = traits::context(gpu_matrix.handle1());
145  gpu_matrix.rows_per_block_ = 128;
146 
147  if (ctx.memory_type() == CUDA_MEMORY)
148  gpu_matrix.rows_per_block_ = 256;
149  else if (ctx.memory_type() == OPENCL_MEMORY)
150  {
151 #ifdef VIENNACL_WITH_OPENCL
152  if (ctx.opencl_context().current_device().vendor_id() == viennacl::ocl::nvidia_id)
153  gpu_matrix.rows_per_block_ = 256;
154 #endif
155  }
156  }
157 
158  if (viennacl::traits::size1(cpu_matrix) > 0 && viennacl::traits::size2(cpu_matrix) > 0)
159  {
160  //determine max capacity for row
161  IndexT columns_in_current_block = 0;
162  vcl_size_t row_counter_in_current_block = 0;
163  vcl_size_t total_element_buffer_size = 0;
164  viennacl::backend::typesafe_host_array<IndexT> columns_in_block_buffer(gpu_matrix.handle1(), viennacl::traits::size1(cpu_matrix) / gpu_matrix.rows_per_block() + 1);
165  for (typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
166  {
167  ++row_counter_in_current_block;
168  vcl_size_t entries_in_row = 0;
169  for (typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
170  ++entries_in_row;
171 
172  columns_in_current_block = std::max(columns_in_current_block, static_cast<IndexT>(entries_in_row));
173 
174  if ( (row_it.index1() % gpu_matrix.rows_per_block() == gpu_matrix.rows_per_block() - 1)
175  || row_it.index1() == viennacl::traits::size1(cpu_matrix) - 1)
176  {
177  total_element_buffer_size += columns_in_current_block * gpu_matrix.rows_per_block();
178  columns_in_block_buffer.set(row_it.index1() / gpu_matrix.rows_per_block(), columns_in_current_block);
179  columns_in_current_block = 0;
180  }
181  }
182 
183  //setup GPU matrix
184  gpu_matrix.rows_ = cpu_matrix.size1();
185  gpu_matrix.cols_ = cpu_matrix.size2();
186 
187  viennacl::backend::typesafe_host_array<IndexT> coords(gpu_matrix.handle2(), total_element_buffer_size);
188  viennacl::backend::typesafe_host_array<IndexT> block_start(gpu_matrix.handle3(), (viennacl::traits::size1(cpu_matrix) - 1) / gpu_matrix.rows_per_block() + 1);
189  std::vector<ScalarT> elements(total_element_buffer_size, 0);
190 
191  vcl_size_t block_offset = 0;
192  vcl_size_t block_index = 0;
193  vcl_size_t row_in_block = 0;
194  for (typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
195  {
196  vcl_size_t entry_in_row = 0;
197 
198  for (typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
199  {
200  vcl_size_t buffer_index = block_offset + entry_in_row * gpu_matrix.rows_per_block() + row_in_block;
201  coords.set(buffer_index, col_it.index2());
202  elements[buffer_index] = *col_it;
203  entry_in_row++;
204  }
205 
206  ++row_in_block;
207 
208  // check for end of block:
209  if ( (row_it.index1() % gpu_matrix.rows_per_block() == gpu_matrix.rows_per_block() - 1)
210  || row_it.index1() == viennacl::traits::size1(cpu_matrix) - 1)
211  {
212  block_start.set(block_index, static_cast<IndexT>(block_offset));
213  block_offset += columns_in_block_buffer[block_index] * gpu_matrix.rows_per_block();
214  ++block_index;
215  row_in_block = 0;
216  }
217  }
218 
219  viennacl::backend::memory_create(gpu_matrix.handle1(), columns_in_block_buffer.raw_size(), traits::context(gpu_matrix.handle1()), columns_in_block_buffer.get());
220  viennacl::backend::memory_create(gpu_matrix.handle2(), coords.raw_size(), traits::context(gpu_matrix.handle2()), coords.get());
221  viennacl::backend::memory_create(gpu_matrix.handle3(), block_start.raw_size(), traits::context(gpu_matrix.handle3()), block_start.get());
222  viennacl::backend::memory_create(gpu_matrix.handle(), sizeof(ScalarT) * elements.size(), traits::context(gpu_matrix.handle()), &(elements[0]));
223  }
224 }
225 
226 
227 
233 template<typename IndexT, typename NumericT, typename IndexT2>
234 void copy(std::vector< std::map<IndexT, NumericT> > const & cpu_matrix,
236 {
237  tools::const_sparse_matrix_adapter<NumericT, IndexT> temp(cpu_matrix, cpu_matrix.size(), cpu_matrix.size());
238  viennacl::copy(temp, gpu_matrix);
239 }
240 
241 
242 /*
243 template<typename CPUMatrixT, typename ScalarT, typename IndexT>
244 void copy(sliced_ell_matrix<ScalarT, IndexT> const & gpu_matrix, CPUMatrixT & cpu_matrix )
245 {
246  assert( (viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
247  assert( (viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
248 
249  if (gpu_matrix.size1() > 0 && gpu_matrix.size2() > 0)
250  {
251  std::vector<NumericT> elements(gpu_matrix.internal_nnz());
252  viennacl::backend::typesafe_host_array<unsigned int> coords(gpu_matrix.handle2(), gpu_matrix.internal_nnz());
253 
254  viennacl::backend::memory_read(gpu_matrix.handle(), 0, sizeof(NumericT) * elements.size(), &(elements[0]));
255  viennacl::backend::memory_read(gpu_matrix.handle2(), 0, coords.raw_size(), coords.get());
256 
257  for (vcl_size_t row = 0; row < gpu_matrix.size1(); row++)
258  {
259  for (vcl_size_t ind = 0; ind < gpu_matrix.internal_maxnnz(); ind++)
260  {
261  vcl_size_t offset = gpu_matrix.internal_size1() * ind + row;
262 
263  if (elements[offset] == static_cast<NumericT>(0.0))
264  continue;
265 
266  if (coords[offset] >= gpu_matrix.size2())
267  {
268  std::cerr << "ViennaCL encountered invalid data " << offset << " " << ind << " " << row << " " << coords[offset] << " " << gpu_matrix.size2() << std::endl;
269  return;
270  }
271 
272  cpu_matrix(row, coords[offset]) = elements[offset];
273  }
274  }
275  }
276 } */
277 
278 
279 //
280 // Specify available operations:
281 //
282 
285 namespace linalg
286 {
287 namespace detail
288 {
289  // x = A * y
290  template<typename ScalarT, typename IndexT>
291  struct op_executor<vector_base<ScalarT>, op_assign, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
292  {
293  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
294  {
295  // check for the special case x = A * x
296  if (viennacl::traits::handle(lhs) == viennacl::traits::handle(rhs.rhs()))
297  {
298  viennacl::vector<ScalarT> temp(lhs);
299  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
300  lhs = temp;
301  }
302  else
303  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), lhs);
304  }
305  };
306 
307  template<typename ScalarT, typename IndexT>
308  struct op_executor<vector_base<ScalarT>, op_inplace_add, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
309  {
310  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
311  {
312  viennacl::vector<ScalarT> temp(lhs);
313  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
314  lhs += temp;
315  }
316  };
317 
318  template<typename ScalarT, typename IndexT>
319  struct op_executor<vector_base<ScalarT>, op_inplace_sub, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
320  {
321  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
322  {
323  viennacl::vector<ScalarT> temp(lhs);
324  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
325  lhs -= temp;
326  }
327  };
328 
329 
330  // x = A * vec_op
331  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
332  struct op_executor<vector_base<ScalarT>, op_assign, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
333  {
334  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
335  {
337  viennacl::linalg::prod_impl(rhs.lhs(), temp, lhs);
338  }
339  };
340 
341  // x = A * vec_op
342  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
343  struct op_executor<vector_base<ScalarT>, op_inplace_add, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
344  {
345  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
346  {
348  viennacl::vector<ScalarT> temp_result(lhs);
349  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
350  lhs += temp_result;
351  }
352  };
353 
354  // x = A * vec_op
355  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
356  struct op_executor<vector_base<ScalarT>, op_inplace_sub, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
357  {
358  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
359  {
361  viennacl::vector<ScalarT> temp_result(lhs);
362  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
363  lhs -= temp_result;
364  }
365  };
366 
367 } // namespace detail
368 } // namespace linalg
369 
371 }
372 
373 #endif
374 
375 
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
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 & handle1() const
Various little tools used here and there in ViennaCL.
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
const handle_type & handle2() const
This file provides the forward declarations for the main types used within ViennaCL.
T max(const T &lhs, const T &rhs)
Maximum.
Definition: util.hpp:59
vcl_size_t rows_per_block() const
friend void copy(CPUMatrixT const &cpu_matrix, sliced_ell_matrix< ScalarT2, IndexT2 > &gpu_matrix)
scalar< typename viennacl::tools::CHECK_SCALAR_TEMPLATE_ARGUMENT< ScalarT >::ResultType > value_type
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
sliced_ell_matrix(viennacl::context ctx)
Sparse matrix class using the sliced ELLPACK with parameters C, .
Definition: forwards.h:402
Implementations of operations using sparse matrices.
Adapts a constant sparse matrix type made up from std::vector > to basic ub...
Definition: adapter.hpp:183
std::size_t vcl_size_t
Definition: forwards.h:74
sliced_ell_matrix(size_type num_rows, size_type num_cols, size_type num_rows_per_block_=0)
viennacl::memory_types memory_type() const
Definition: context.hpp:76
const handle_type & handle() const
vcl_size_t internal_size2() const
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
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
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 internal_size1() const
viennacl::backend::mem_handle handle_type