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
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 
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 template<typename CPUMatrixT, typename ScalarT, typename IndexT>
212 void copy(sliced_ell_matrix<ScalarT, IndexT> const & gpu_matrix, CPUMatrixT & cpu_matrix )
213 {
214  assert( (viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
215  assert( (viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
216 
217  if (gpu_matrix.size1() > 0 && gpu_matrix.size2() > 0)
218  {
219  std::vector<NumericT> elements(gpu_matrix.internal_nnz());
220  viennacl::backend::typesafe_host_array<unsigned int> coords(gpu_matrix.handle2(), gpu_matrix.internal_nnz());
221 
222  viennacl::backend::memory_read(gpu_matrix.handle(), 0, sizeof(NumericT) * elements.size(), &(elements[0]));
223  viennacl::backend::memory_read(gpu_matrix.handle2(), 0, coords.raw_size(), coords.get());
224 
225  for (vcl_size_t row = 0; row < gpu_matrix.size1(); row++)
226  {
227  for (vcl_size_t ind = 0; ind < gpu_matrix.internal_maxnnz(); ind++)
228  {
229  vcl_size_t offset = gpu_matrix.internal_size1() * ind + row;
230 
231  if (elements[offset] == static_cast<NumericT>(0.0))
232  continue;
233 
234  if (coords[offset] >= gpu_matrix.size2())
235  {
236  std::cerr << "ViennaCL encountered invalid data " << offset << " " << ind << " " << row << " " << coords[offset] << " " << gpu_matrix.size2() << std::endl;
237  return;
238  }
239 
240  cpu_matrix(row, coords[offset]) = elements[offset];
241  }
242  }
243  }
244 } */
245 
246 
247 //
248 // Specify available operations:
249 //
250 
253 namespace linalg
254 {
255 namespace detail
256 {
257  // x = A * y
258  template<typename ScalarT, typename IndexT>
259  struct op_executor<vector_base<ScalarT>, op_assign, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
260  {
261  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
262  {
263  // check for the special case x = A * x
264  if (viennacl::traits::handle(lhs) == viennacl::traits::handle(rhs.rhs()))
265  {
266  viennacl::vector<ScalarT> temp(lhs);
267  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
268  lhs = temp;
269  }
270  else
271  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), lhs);
272  }
273  };
274 
275  template<typename ScalarT, typename IndexT>
276  struct op_executor<vector_base<ScalarT>, op_inplace_add, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
277  {
278  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
279  {
280  viennacl::vector<ScalarT> temp(lhs);
281  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
282  lhs += temp;
283  }
284  };
285 
286  template<typename ScalarT, typename IndexT>
287  struct op_executor<vector_base<ScalarT>, op_inplace_sub, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> >
288  {
289  static void apply(vector_base<ScalarT> & lhs, vector_expression<const sliced_ell_matrix<ScalarT, IndexT>, const vector_base<ScalarT>, op_prod> const & rhs)
290  {
291  viennacl::vector<ScalarT> temp(lhs);
292  viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
293  lhs -= temp;
294  }
295  };
296 
297 
298  // x = A * vec_op
299  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
300  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> >
301  {
302  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)
303  {
305  viennacl::linalg::prod_impl(rhs.lhs(), temp, lhs);
306  }
307  };
308 
309  // x = A * vec_op
310  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
311  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> >
312  {
313  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)
314  {
316  viennacl::vector<ScalarT> temp_result(lhs);
317  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
318  lhs += temp_result;
319  }
320  };
321 
322  // x = A * vec_op
323  template<typename ScalarT, typename IndexT, typename LHS, typename RHS, typename OP>
324  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> >
325  {
326  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)
327  {
329  viennacl::vector<ScalarT> temp_result(lhs);
330  viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
331  lhs -= temp_result;
332  }
333  };
334 
335 } // namespace detail
336 } // namespace linalg
337 
339 }
340 
341 #endif
342 
343 
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.
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)