ViennaCL - The Vienna Computing Library  1.6.1
Free open-source GPU-accelerated linear algebra and solver library.
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_(128) {}
54 
55  sliced_ell_matrix(size_type num_rows,
56  size_type num_cols,
57  size_type num_rows_per_block_ = 128)
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_(128)
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 (viennacl::traits::size1(cpu_matrix) > 0 && viennacl::traits::size2(cpu_matrix) > 0)
143  {
144  //determine max capacity for row
145  IndexT columns_in_current_block = 0;
146  vcl_size_t row_counter_in_current_block = 0;
147  vcl_size_t total_element_buffer_size = 0;
148  viennacl::backend::typesafe_host_array<IndexT> columns_in_block_buffer(gpu_matrix.handle1(), viennacl::traits::size1(cpu_matrix) / gpu_matrix.rows_per_block() + 1);
149  for (typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
150  {
151  ++row_counter_in_current_block;
152  vcl_size_t entries_in_row = 0;
153  for (typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
154  ++entries_in_row;
155 
156  columns_in_current_block = std::max(columns_in_current_block, static_cast<IndexT>(entries_in_row));
157 
158  if ( (row_it.index1() % gpu_matrix.rows_per_block() == gpu_matrix.rows_per_block() - 1)
159  || row_it.index1() == viennacl::traits::size1(cpu_matrix) - 1)
160  {
161  total_element_buffer_size += columns_in_current_block * gpu_matrix.rows_per_block();
162  columns_in_block_buffer.set(row_it.index1() / gpu_matrix.rows_per_block(), columns_in_current_block);
163  columns_in_current_block = 0;
164  }
165  }
166 
167  //setup GPU matrix
168  gpu_matrix.rows_ = cpu_matrix.size1();
169  gpu_matrix.cols_ = cpu_matrix.size2();
170 
171  viennacl::backend::typesafe_host_array<IndexT> coords(gpu_matrix.handle2(), total_element_buffer_size);
172  viennacl::backend::typesafe_host_array<IndexT> block_start(gpu_matrix.handle3(), (viennacl::traits::size1(cpu_matrix) - 1) / gpu_matrix.rows_per_block() + 1);
173  std::vector<ScalarT> elements(total_element_buffer_size, 0);
174 
175  vcl_size_t block_offset = 0;
176  vcl_size_t block_index = 0;
177  vcl_size_t row_in_block = 0;
178  for (typename CPUMatrixT::const_iterator1 row_it = cpu_matrix.begin1(); row_it != cpu_matrix.end1(); ++row_it)
179  {
180  vcl_size_t entry_in_row = 0;
181 
182  for (typename CPUMatrixT::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
183  {
184  vcl_size_t buffer_index = block_offset + entry_in_row * gpu_matrix.rows_per_block() + row_in_block;
185  coords.set(buffer_index, col_it.index2());
186  elements[buffer_index] = *col_it;
187  entry_in_row++;
188  }
189 
190  ++row_in_block;
191 
192  // check for end of block:
193  if ( (row_it.index1() % gpu_matrix.rows_per_block() == gpu_matrix.rows_per_block() - 1)
194  || row_it.index1() == viennacl::traits::size1(cpu_matrix) - 1)
195  {
196  block_start.set(block_index, static_cast<IndexT>(block_offset));
197  block_offset += columns_in_block_buffer[block_index] * gpu_matrix.rows_per_block();
198  ++block_index;
199  row_in_block = 0;
200  }
201  }
202 
203  viennacl::backend::memory_create(gpu_matrix.handle1(), columns_in_block_buffer.raw_size(), traits::context(gpu_matrix.handle1()), columns_in_block_buffer.get());
204  viennacl::backend::memory_create(gpu_matrix.handle2(), coords.raw_size(), traits::context(gpu_matrix.handle2()), coords.get());
205  viennacl::backend::memory_create(gpu_matrix.handle3(), block_start.raw_size(), traits::context(gpu_matrix.handle3()), block_start.get());
206  viennacl::backend::memory_create(gpu_matrix.handle(), sizeof(ScalarT) * elements.size(), traits::context(gpu_matrix.handle()), &(elements[0]));
207  }
208 }
209 
210 
211 
217 template<typename IndexT, typename NumericT, typename IndexT2>
218 void copy(std::vector< std::map<IndexT, NumericT> > const & cpu_matrix,
220 {
221  tools::const_sparse_matrix_adapter<NumericT, IndexT> temp(cpu_matrix, cpu_matrix.size(), cpu_matrix.size());
222  viennacl::copy(temp, gpu_matrix);
223 }
224 
225 
226 /*
227 template<typename CPUMatrixT, typename ScalarT, typename IndexT>
228 void copy(sliced_ell_matrix<ScalarT, IndexT> const & gpu_matrix, CPUMatrixT & cpu_matrix )
229 {
230  assert( (viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
231  assert( (viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
232 
233  if (gpu_matrix.size1() > 0 && gpu_matrix.size2() > 0)
234  {
235  std::vector<NumericT> elements(gpu_matrix.internal_nnz());
236  viennacl::backend::typesafe_host_array<unsigned int> coords(gpu_matrix.handle2(), gpu_matrix.internal_nnz());
237 
238  viennacl::backend::memory_read(gpu_matrix.handle(), 0, sizeof(NumericT) * elements.size(), &(elements[0]));
239  viennacl::backend::memory_read(gpu_matrix.handle2(), 0, coords.raw_size(), coords.get());
240 
241  for (vcl_size_t row = 0; row < gpu_matrix.size1(); row++)
242  {
243  for (vcl_size_t ind = 0; ind < gpu_matrix.internal_maxnnz(); ind++)
244  {
245  vcl_size_t offset = gpu_matrix.internal_size1() * ind + row;
246 
247  if (elements[offset] == static_cast<NumericT>(0.0))
248  continue;
249 
250  if (coords[offset] >= gpu_matrix.size2())
251  {
252  std::cerr << "ViennaCL encountered invalid data " << offset << " " << ind << " " << row << " " << coords[offset] << " " << gpu_matrix.size2() << std::endl;
253  return;
254  }
255 
256  cpu_matrix(row, coords[offset]) = elements[offset];
257  }
258  }
259  }
260 } */
261 
262 
263 //
264 // Specify available operations:
265 //
266 
269 namespace linalg
270 {
271 namespace detail
272 {
273  // x = A * y
274  template<typename ScalarT, typename IndexT>
275  struct op_executor<vector_base<ScalarT>, op_assign, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
276  {
277  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
278  {
279  // check for the special case x = A * x
280  if (viennacl::traits::handle(lhs) == viennacl::traits::handle(rhs.rhs()))
281  {
282  viennacl::vector<ScalarT> temp(lhs);
283  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
284  lhs = temp;
285  }
286  else
287  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), lhs);
288  }
289  };
290 
291  template<typename ScalarT, typename IndexT>
292  struct op_executor<vector_base<ScalarT>, op_inplace_add, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
293  {
294  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
295  {
296  viennacl::vector<ScalarT> temp(lhs);
297  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
298  lhs += temp;
299  }
300  };
301 
302  template<typename ScalarT, typename IndexT>
303  struct op_executor<vector_base<ScalarT>, op_inplace_sub, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
304  {
305  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
306  {
307  viennacl::vector<ScalarT> temp(lhs);
308  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
309  lhs -= temp;
310  }
311  };
312 
313 
314  // x = A * vec_op
315  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
316  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> >
317  {
318  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)
319  {
321  viennacl::linalg::prod_impl(rhs.lhs(), temp, lhs);
322  }
323  };
324 
325  // x = A * vec_op
326  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
327  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> >
328  {
329  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)
330  {
332  viennacl::vector<ScalarT> temp_result(lhs);
333  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
334  lhs += temp_result;
335  }
336  };
337 
338  // x = A * vec_op
339  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
340  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> >
341  {
342  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)
343  {
345  viennacl::vector<ScalarT> temp_result(lhs);
346  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
347  lhs -= temp_result;
348  }
349  };
350 
351 } // namespace detail
352 } // namespace linalg
353 
355 }
356 
357 #endif
358 
359 
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
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
sliced_ell_matrix(viennacl::context ctx)
Definition: blas3.hpp:36
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
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
sliced_ell_matrix(size_type num_rows, size_type num_cols, size_type num_rows_per_block_=128)