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
sparse.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2014, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17 
18 
19 
24 #ifndef NDEBUG
25  #define NDEBUG
26 #endif
27 
28 //
29 // *** System
30 //
31 #include <iostream>
32 
33 //
34 // *** Boost
35 //
36 #include <boost/numeric/ublas/io.hpp>
37 #include <boost/numeric/ublas/triangular.hpp>
38 #include <boost/numeric/ublas/matrix_sparse.hpp>
39 #include <boost/numeric/ublas/matrix.hpp>
40 #include <boost/numeric/ublas/matrix_proxy.hpp>
41 #include <boost/numeric/ublas/lu.hpp>
42 #include <boost/numeric/ublas/io.hpp>
43 #include <boost/numeric/ublas/operation_sparse.hpp>
44 #include <boost/numeric/ublas/vector_proxy.hpp>
45 
46 //
47 // *** ViennaCL
48 //
49 //#define VIENNACL_DEBUG_ALL
50 #define VIENNACL_WITH_UBLAS 1
51 #include "viennacl/scalar.hpp"
55 #include "viennacl/ell_matrix.hpp"
57 #include "viennacl/hyb_matrix.hpp"
58 #include "viennacl/vector.hpp"
60 #include "viennacl/linalg/prod.hpp"
62 #include "viennacl/linalg/ilu.hpp"
67 
68 //
69 // -------------------------------------------------------------
70 //
71 using namespace boost::numeric;
72 //
73 // -------------------------------------------------------------
74 //
75 template<typename ScalarType>
77 {
78  if (s1 != s2)
79  return (s1 - s2) / std::max(fabs(s1), std::fabs(s2));
80  return 0;
81 }
82 
83 template<typename ScalarType>
84 ScalarType diff(ublas::vector<ScalarType> & v1, viennacl::vector<ScalarType> & v2)
85 {
86  ublas::vector<ScalarType> v2_cpu(v2.size());
88  viennacl::copy(v2.begin(), v2.end(), v2_cpu.begin());
89 
90  for (unsigned int i=0;i<v1.size(); ++i)
91  {
92  if ( std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) > 0 )
93  {
94  //if (std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) < 1e-10 ) //absolute tolerance (avoid round-off issues)
95  // v2_cpu[i] = 0;
96  //else
97  v2_cpu[i] = std::fabs(v2_cpu[i] - v1[i]) / std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) );
98  }
99  else
100  v2_cpu[i] = 0.0;
101 
102  if (v2_cpu[i] > 0.0001)
103  {
104  //std::cout << "Neighbor: " << i-1 << ": " << v1[i-1] << " vs. " << v2_cpu[i-1] << std::endl;
105  std::cout << "Error at entry " << i << ": " << v1[i] << " vs. " << v2_cpu[i] << std::endl;
106  //std::cout << "Neighbor: " << i+1 << ": " << v1[i+1] << " vs. " << v2_cpu[i+1] << std::endl;
107  exit(EXIT_FAILURE);
108  }
109  }
110 
111  return norm_inf(v2_cpu);
112 }
113 
114 
115 template<typename ScalarType, typename VCL_MATRIX>
116 ScalarType diff(ublas::compressed_matrix<ScalarType> & cpu_matrix, VCL_MATRIX & gpu_matrix)
117 {
118  typedef ublas::compressed_matrix<ScalarType> CPU_MATRIX;
119  CPU_MATRIX from_gpu(gpu_matrix.size1(), gpu_matrix.size2());
120 
122  viennacl::copy(gpu_matrix, from_gpu);
123 
124  ScalarType error = 0;
125 
126  //step 1: compare all entries from cpu_matrix with gpu_matrix:
127  //std::cout << "Ublas matrix: " << std::endl;
128  for (typename CPU_MATRIX::const_iterator1 row_it = cpu_matrix.begin1();
129  row_it != cpu_matrix.end1();
130  ++row_it)
131  {
132  //std::cout << "Row " << row_it.index1() << ": " << std::endl;
133  for (typename CPU_MATRIX::const_iterator2 col_it = row_it.begin();
134  col_it != row_it.end();
135  ++col_it)
136  {
137  //std::cout << "(" << col_it.index2() << ", " << *col_it << std::endl;
138  ScalarType current_error = 0;
139 
140  if ( std::max( std::fabs(cpu_matrix(col_it.index1(), col_it.index2())),
141  std::fabs(from_gpu(col_it.index1(), col_it.index2())) ) > 0 )
142  current_error = std::fabs(cpu_matrix(col_it.index1(), col_it.index2()) - from_gpu(col_it.index1(), col_it.index2()))
143  / std::max( std::fabs(cpu_matrix(col_it.index1(), col_it.index2())),
144  std::fabs(from_gpu(col_it.index1(), col_it.index2())) );
145  if (current_error > error)
146  error = current_error;
147  }
148  }
149 
150  //step 2: compare all entries from gpu_matrix with cpu_matrix (sparsity pattern might differ):
151  //std::cout << "ViennaCL matrix: " << std::endl;
152  for (typename CPU_MATRIX::const_iterator1 row_it = from_gpu.begin1();
153  row_it != from_gpu.end1();
154  ++row_it)
155  {
156  //std::cout << "Row " << row_it.index1() << ": " << std::endl;
157  for (typename CPU_MATRIX::const_iterator2 col_it = row_it.begin();
158  col_it != row_it.end();
159  ++col_it)
160  {
161  //std::cout << "(" << col_it.index2() << ", " << *col_it << std::endl;
162  ScalarType current_error = 0;
163 
164  if ( std::max( std::fabs(cpu_matrix(col_it.index1(), col_it.index2())),
165  std::fabs(from_gpu(col_it.index1(), col_it.index2())) ) > 0 )
166  current_error = std::fabs(cpu_matrix(col_it.index1(), col_it.index2()) - from_gpu(col_it.index1(), col_it.index2()))
167  / std::max( std::fabs(cpu_matrix(col_it.index1(), col_it.index2())),
168  std::fabs(from_gpu(col_it.index1(), col_it.index2())) );
169  if (current_error > error)
170  error = current_error;
171  }
172  }
173 
174  return error;
175 }
176 
177 
178 template<typename NumericT, typename VCL_MatrixT, typename Epsilon, typename UblasVectorT, typename VCLVectorT>
180  UblasVectorT & result, UblasVectorT const & rhs,
181  VCLVectorT & vcl_result, VCLVectorT & vcl_rhs)
182 {
183  int retval = EXIT_SUCCESS;
184 
185  ublas::compressed_matrix<NumericT> ublas_matrix2(5, 4);
186  ublas_matrix2(0, 0) = NumericT(2.0); ublas_matrix2(0, 2) = NumericT(-1.0);
187  ublas_matrix2(1, 0) = NumericT(3.0); ublas_matrix2(1, 2) = NumericT(-5.0);
188  ublas_matrix2(2, 1) = NumericT(5.0); ublas_matrix2(2, 2) = NumericT(-2.0);
189  ublas_matrix2(3, 2) = NumericT(1.0); ublas_matrix2(3, 3) = NumericT(-6.0);
190  ublas_matrix2(4, 1) = NumericT(7.0); ublas_matrix2(4, 2) = NumericT(-5.0);
191  project(result, ublas::slice(1, 3, 5)) = ublas::prod(ublas_matrix2, project(rhs, ublas::slice(3, 2, 4)));
192 
193  VCL_MatrixT vcl_sparse_matrix2;
194  viennacl::copy(ublas_matrix2, vcl_sparse_matrix2);
196  vec(0) = rhs(3);
197  vec(1) = rhs(5);
198  vec(2) = rhs(7);
199  vec(3) = rhs(9);
200  viennacl::project(vcl_result, viennacl::slice(1, 3, 5)) = viennacl::linalg::prod(vcl_sparse_matrix2, viennacl::project(vcl_rhs, viennacl::slice(3, 2, 4)));
201 
202  if ( std::fabs(diff(result, vcl_result)) > epsilon )
203  {
204  std::cout << "# Error at operation: matrix-vector product with stided vectors, part 1" << std::endl;
205  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
206  retval = EXIT_FAILURE;
207  }
208  vcl_result(1) = NumericT(1.0);
209  vcl_result(4) = NumericT(1.0);
210  vcl_result(7) = NumericT(1.0);
211  vcl_result(10) = NumericT(1.0);
212  vcl_result(13) = NumericT(1.0);
213 
214  viennacl::project(vcl_result, viennacl::slice(1, 3, 5)) = viennacl::linalg::prod(vcl_sparse_matrix2, vec);
215 
216  if ( std::fabs(diff(result, vcl_result)) > epsilon )
217  {
218  std::cout << "# Error at operation: matrix-vector product with strided vectors, part 2" << std::endl;
219  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
220  retval = EXIT_FAILURE;
221  }
222 
223  return retval;
224 }
225 
226 
227 template< typename NumericT, typename VCL_MATRIX, typename Epsilon >
228 int resize_test(Epsilon const& epsilon)
229 {
230  int retval = EXIT_SUCCESS;
231 
232  ublas::compressed_matrix<NumericT> ublas_matrix(5,5);
233  VCL_MATRIX vcl_matrix;
234 
235  ublas_matrix(0,0) = NumericT(10.0); ublas_matrix(0, 1) = NumericT(0.1); ublas_matrix(0, 2) = NumericT(0.2); ublas_matrix(0, 3) = NumericT(0.3); ublas_matrix(0, 4) = NumericT(0.4);
236  ublas_matrix(1,0) = NumericT(1.0); ublas_matrix(1, 1) = NumericT(1.1); ublas_matrix(1, 2) = NumericT(1.2); ublas_matrix(1, 3) = NumericT(1.3); ublas_matrix(1, 4) = NumericT(1.4);
237  ublas_matrix(2,0) = NumericT(2.0); ublas_matrix(2, 1) = NumericT(2.1); ublas_matrix(2, 2) = NumericT(2.2); ublas_matrix(2, 3) = NumericT(2.3); ublas_matrix(2, 4) = NumericT(2.4);
238  ublas_matrix(3,0) = NumericT(3.0); ublas_matrix(3, 1) = NumericT(3.1); ublas_matrix(3, 2) = NumericT(3.2); ublas_matrix(3, 3) = NumericT(3.3); ublas_matrix(3, 4) = NumericT(3.4);
239  ublas_matrix(4,0) = NumericT(4.0); ublas_matrix(4, 1) = NumericT(4.1); ublas_matrix(4, 2) = NumericT(4.2); ublas_matrix(4, 3) = NumericT(4.3); ublas_matrix(4, 4) = NumericT(4.4);
240 
241  viennacl::copy(ublas_matrix, vcl_matrix);
242  ublas::compressed_matrix<NumericT> other_matrix(ublas_matrix.size1(), ublas_matrix.size2());
243  viennacl::copy(vcl_matrix, other_matrix);
244 
245  std::cout << "Checking for equality after copy..." << std::endl;
246  if ( std::fabs(diff(ublas_matrix, vcl_matrix)) > epsilon )
247  {
248  std::cout << "# Error at operation: equality after copy with sparse matrix" << std::endl;
249  std::cout << " diff: " << std::fabs(diff(ublas_matrix, vcl_matrix)) << std::endl;
250  return EXIT_FAILURE;
251  }
252 
253  std::cout << "Testing resize to larger..." << std::endl;
254  ublas_matrix.resize(10, 10, false); //ublas does not allow preserve = true here
255  ublas_matrix(0,0) = NumericT(10.0); ublas_matrix(0, 1) = NumericT(0.1); ublas_matrix(0, 2) = NumericT(0.2); ublas_matrix(0, 3) = NumericT(0.3); ublas_matrix(0, 4) = NumericT(0.4);
256  ublas_matrix(1,0) = NumericT( 1.0); ublas_matrix(1, 1) = NumericT(1.1); ublas_matrix(1, 2) = NumericT(1.2); ublas_matrix(1, 3) = NumericT(1.3); ublas_matrix(1, 4) = NumericT(1.4);
257  ublas_matrix(2,0) = NumericT( 2.0); ublas_matrix(2, 1) = NumericT(2.1); ublas_matrix(2, 2) = NumericT(2.2); ublas_matrix(2, 3) = NumericT(2.3); ublas_matrix(2, 4) = NumericT(2.4);
258  ublas_matrix(3,0) = NumericT( 3.0); ublas_matrix(3, 1) = NumericT(3.1); ublas_matrix(3, 2) = NumericT(3.2); ublas_matrix(3, 3) = NumericT(3.3); ublas_matrix(3, 4) = NumericT(3.4);
259  ublas_matrix(4,0) = NumericT( 4.0); ublas_matrix(4, 1) = NumericT(4.1); ublas_matrix(4, 2) = NumericT(4.2); ublas_matrix(4, 3) = NumericT(4.3); ublas_matrix(4, 4) = NumericT(4.4);
260  //std::cout << ublas_matrix << std::endl;
261 
262  vcl_matrix.resize(10, 10, true);
263 
264  if ( std::fabs(diff(ublas_matrix, vcl_matrix)) > epsilon )
265  {
266  std::cout << "# Error at operation: resize (to larger) with sparse matrix" << std::endl;
267  std::cout << " diff: " << std::fabs(diff(ublas_matrix, vcl_matrix)) << std::endl;
268  return EXIT_FAILURE;
269  }
270 
271  ublas_matrix(5,5) = NumericT(5.5); ublas_matrix(5, 6) = NumericT(5.6); ublas_matrix(5, 7) = NumericT(5.7); ublas_matrix(5, 8) = NumericT(5.8); ublas_matrix(5, 9) = NumericT(5.9);
272  ublas_matrix(6,5) = NumericT(6.5); ublas_matrix(6, 6) = NumericT(6.6); ublas_matrix(6, 7) = NumericT(6.7); ublas_matrix(6, 8) = NumericT(6.8); ublas_matrix(6, 9) = NumericT(6.9);
273  ublas_matrix(7,5) = NumericT(7.5); ublas_matrix(7, 6) = NumericT(7.6); ublas_matrix(7, 7) = NumericT(7.7); ublas_matrix(7, 8) = NumericT(7.8); ublas_matrix(7, 9) = NumericT(7.9);
274  ublas_matrix(8,5) = NumericT(8.5); ublas_matrix(8, 6) = NumericT(8.6); ublas_matrix(8, 7) = NumericT(8.7); ublas_matrix(8, 8) = NumericT(8.8); ublas_matrix(8, 9) = NumericT(8.9);
275  ublas_matrix(9,5) = NumericT(9.5); ublas_matrix(9, 6) = NumericT(9.6); ublas_matrix(9, 7) = NumericT(9.7); ublas_matrix(9, 8) = NumericT(9.8); ublas_matrix(9, 9) = NumericT(9.9);
276  viennacl::copy(ublas_matrix, vcl_matrix);
277 
278  std::cout << "Testing resize to smaller..." << std::endl;
279  ublas_matrix.resize(7, 7, false); //ublas does not allow preserve = true here
280  ublas_matrix(0,0) = NumericT(10.0); ublas_matrix(0, 1) = NumericT(0.1); ublas_matrix(0, 2) = NumericT(0.2); ublas_matrix(0, 3) = NumericT(0.3); ublas_matrix(0, 4) = NumericT(0.4);
281  ublas_matrix(1,0) = NumericT( 1.0); ublas_matrix(1, 1) = NumericT(1.1); ublas_matrix(1, 2) = NumericT(1.2); ublas_matrix(1, 3) = NumericT(1.3); ublas_matrix(1, 4) = NumericT(1.4);
282  ublas_matrix(2,0) = NumericT( 2.0); ublas_matrix(2, 1) = NumericT(2.1); ublas_matrix(2, 2) = NumericT(2.2); ublas_matrix(2, 3) = NumericT(2.3); ublas_matrix(2, 4) = NumericT(2.4);
283  ublas_matrix(3,0) = NumericT( 3.0); ublas_matrix(3, 1) = NumericT(3.1); ublas_matrix(3, 2) = NumericT(3.2); ublas_matrix(3, 3) = NumericT(3.3); ublas_matrix(3, 4) = NumericT(3.4);
284  ublas_matrix(4,0) = NumericT( 4.0); ublas_matrix(4, 1) = NumericT(4.1); ublas_matrix(4, 2) = NumericT(4.2); ublas_matrix(4, 3) = NumericT(4.3); ublas_matrix(4, 4) = NumericT(4.4);
285  ublas_matrix(5,5) = NumericT( 5.5); ublas_matrix(5, 6) = NumericT(5.6); ublas_matrix(5, 7) = NumericT(5.7); ublas_matrix(5, 8) = NumericT(5.8); ublas_matrix(5, 9) = NumericT(5.9);
286  ublas_matrix(6,5) = NumericT( 6.5); ublas_matrix(6, 6) = NumericT(6.6); ublas_matrix(6, 7) = NumericT(6.7); ublas_matrix(6, 8) = NumericT(6.8); ublas_matrix(6, 9) = NumericT(6.9);
287 
288  vcl_matrix.resize(7, 7);
289 
290  //std::cout << ublas_matrix << std::endl;
291  if ( std::fabs(diff(ublas_matrix, vcl_matrix)) > epsilon )
292  {
293  std::cout << "# Error at operation: resize (to smaller) with sparse matrix" << std::endl;
294  std::cout << " diff: " << std::fabs(diff(ublas_matrix, vcl_matrix)) << std::endl;
295  retval = EXIT_FAILURE;
296  }
297 
298  ublas::vector<NumericT> ublas_vec = ublas::scalar_vector<NumericT>(ublas_matrix.size1(), NumericT(3.1415));
299  viennacl::vector<NumericT> vcl_vec(ublas_matrix.size1());
300 
301 
302  std::cout << "Testing transposed unit lower triangular solve: compressed_matrix" << std::endl;
303  viennacl::copy(ublas_vec, vcl_vec);
304  std::cout << "matrix: " << ublas_matrix << std::endl;
305  std::cout << "vector: " << ublas_vec << std::endl;
306  std::cout << "ViennaCL matrix size: " << vcl_matrix.size1() << " x " << vcl_matrix.size2() << std::endl;
307 
308  std::cout << "ublas..." << std::endl;
309  boost::numeric::ublas::inplace_solve((ublas_matrix), ublas_vec, boost::numeric::ublas::unit_lower_tag());
310  std::cout << "ViennaCL..." << std::endl;
312 
313  /*
314  std::list< viennacl::backend::mem_handle > multifrontal_L_row_index_arrays_;
315  std::list< viennacl::backend::mem_handle > multifrontal_L_row_buffers_;
316  std::list< viennacl::backend::mem_handle > multifrontal_L_col_buffers_;
317  std::list< viennacl::backend::mem_handle > multifrontal_L_element_buffers_;
318  std::list< std::size_t > multifrontal_L_row_elimination_num_list_;
319 
320  viennacl::vector<NumericT> multifrontal_U_diagonal_;
321 
322  viennacl::linalg::detail::multifrontal_setup_L(vcl_matrix,
323  multifrontal_U_diagonal_, //dummy
324  multifrontal_L_row_index_arrays_,
325  multifrontal_L_row_buffers_,
326  multifrontal_L_col_buffers_,
327  multifrontal_L_element_buffers_,
328  multifrontal_L_row_elimination_num_list_);
329 
330  viennacl::linalg::detail::multifrontal_substitute(vcl_vec,
331  multifrontal_L_row_index_arrays_,
332  multifrontal_L_row_buffers_,
333  multifrontal_L_col_buffers_,
334  multifrontal_L_element_buffers_,
335  multifrontal_L_row_elimination_num_list_);
336 
337 
338  std::cout << "ublas..." << std::endl;
339  boost::numeric::ublas::inplace_solve((ublas_matrix), ublas_vec, boost::numeric::ublas::upper_tag());
340  std::cout << "ViennaCL..." << std::endl;
341  std::list< viennacl::backend::mem_handle > multifrontal_U_row_index_arrays_;
342  std::list< viennacl::backend::mem_handle > multifrontal_U_row_buffers_;
343  std::list< viennacl::backend::mem_handle > multifrontal_U_col_buffers_;
344  std::list< viennacl::backend::mem_handle > multifrontal_U_element_buffers_;
345  std::list< std::size_t > multifrontal_U_row_elimination_num_list_;
346 
347  multifrontal_U_diagonal_.resize(vcl_matrix.size1(), false);
348  viennacl::linalg::single_threaded::detail::row_info(vcl_matrix, multifrontal_U_diagonal_, viennacl::linalg::detail::SPARSE_ROW_DIAGONAL);
349  viennacl::linalg::detail::multifrontal_setup_U(vcl_matrix,
350  multifrontal_U_diagonal_,
351  multifrontal_U_row_index_arrays_,
352  multifrontal_U_row_buffers_,
353  multifrontal_U_col_buffers_,
354  multifrontal_U_element_buffers_,
355  multifrontal_U_row_elimination_num_list_);
356 
357  vcl_vec = viennacl::linalg::element_div(vcl_vec, multifrontal_U_diagonal_);
358  viennacl::linalg::detail::multifrontal_substitute(vcl_vec,
359  multifrontal_U_row_index_arrays_,
360  multifrontal_U_row_buffers_,
361  multifrontal_U_col_buffers_,
362  multifrontal_U_element_buffers_,
363  multifrontal_U_row_elimination_num_list_);
364  */
365  for (std::size_t i=0; i<ublas_vec.size(); ++i)
366  {
367  std::cout << ublas_vec[i] << " vs. " << vcl_vec[i] << std::endl;
368  }
369 
370  /*std::cout << "Testing transposed unit upper triangular solve: compressed_matrix" << std::endl;
371  viennacl::copy(ublas_vec, vcl_vec);
372  std::cout << "matrix: " << ublas_matrix << std::endl;
373  std::cout << "vector: " << ublas_vec << std::endl;
374  std::cout << "ViennaCL matrix size: " << vcl_matrix.size1() << " x " << vcl_matrix.size2() << std::endl;
375 
376  std::cout << "ublas..." << std::endl;
377  boost::numeric::ublas::inplace_solve((ublas_matrix), ublas_vec, boost::numeric::ublas::lower_tag());
378  std::cout << "ViennaCL..." << std::endl;
379  viennacl::linalg::inplace_solve((vcl_matrix), vcl_vec, viennacl::linalg::lower_tag());
380 
381  for (std::size_t i=0; i<ublas_vec.size(); ++i)
382  {
383  std::cout << ublas_vec[i] << " vs. " << vcl_vec[i] << std::endl;
384  }*/
385 
386  return retval;
387 }
388 
389 
390 //
391 // -------------------------------------------------------------
392 //
393 template< typename NumericT, typename Epsilon >
394 int test(Epsilon const& epsilon)
395 {
396  std::cout << "Testing resizing of compressed_matrix..." << std::endl;
397  int retval = resize_test<NumericT, viennacl::compressed_matrix<NumericT> >(epsilon);
398  if (retval != EXIT_SUCCESS)
399  return retval;
400  std::cout << "Testing resizing of coordinate_matrix..." << std::endl;
401  //if (retval != EXIT_FAILURE)
402  // retval = resize_test<NumericT, viennacl::coordinate_matrix<NumericT> >(epsilon);
403  //else
404  // return retval;
405 
406  // --------------------------------------------------------------------------
407  ublas::vector<NumericT> rhs;
408  ublas::vector<NumericT> result;
409  ublas::compressed_matrix<NumericT> ublas_matrix;
410 
411  if (viennacl::io::read_matrix_market_file(ublas_matrix, "../examples/testdata/mat65k.mtx") == EXIT_FAILURE)
412  {
413  std::cout << "Error reading Matrix file" << std::endl;
414  return EXIT_FAILURE;
415  }
416 
417  //unsigned int cg_mat_size = cg_mat.size();
418  std::cout << "done reading matrix" << std::endl;
419 
420 
421  rhs.resize(ublas_matrix.size2());
422  for (std::size_t i=0; i<rhs.size(); ++i)
423  {
424  ublas_matrix(i,i) = NumericT(0.5); // Get rid of round-off errors by making row-sums unequal to zero:
425  rhs[i] = NumericT(1) + random<NumericT>();
426  }
427 
428  // add some random numbers to the double-compressed matrix:
429  ublas::compressed_matrix<NumericT> ublas_cc_matrix(ublas_matrix.size1(), ublas_matrix.size2());
430  ublas_cc_matrix(42,199) = NumericT(3.1415);
431  ublas_cc_matrix(31, 69) = NumericT(2.71);
432  ublas_cc_matrix(23, 32) = NumericT(6);
433  ublas_cc_matrix(177,57) = NumericT(4);
434  ublas_cc_matrix(21, 97) = NumericT(-4);
435  ublas_cc_matrix(92, 25) = NumericT(2);
436  ublas_cc_matrix(89, 62) = NumericT(11);
437  ublas_cc_matrix(1, 7) = NumericT(8);
438  ublas_cc_matrix(85, 41) = NumericT(13);
439  ublas_cc_matrix(66, 28) = NumericT(8);
440  ublas_cc_matrix(21, 74) = NumericT(-2);
441 
442 
443  result = rhs;
444 
445 
446  viennacl::vector<NumericT> vcl_rhs(rhs.size());
447  viennacl::vector<NumericT> vcl_result(result.size());
448  viennacl::vector<NumericT> vcl_result2(result.size());
449  viennacl::compressed_matrix<NumericT> vcl_compressed_matrix(rhs.size(), rhs.size());
450  viennacl::compressed_compressed_matrix<NumericT> vcl_compressed_compressed_matrix(rhs.size(), rhs.size());
451  viennacl::coordinate_matrix<NumericT> vcl_coordinate_matrix(rhs.size(), rhs.size());
452  viennacl::ell_matrix<NumericT> vcl_ell_matrix;
453  viennacl::sliced_ell_matrix<NumericT> vcl_sliced_ell_matrix;
454  viennacl::hyb_matrix<NumericT> vcl_hyb_matrix;
455 
456  viennacl::copy(rhs.begin(), rhs.end(), vcl_rhs.begin());
457  viennacl::copy(ublas_matrix, vcl_compressed_matrix);
458  viennacl::copy(ublas_cc_matrix, vcl_compressed_compressed_matrix);
459  viennacl::copy(ublas_matrix, vcl_coordinate_matrix);
460 
461  // --------------------------------------------------------------------------
462  std::cout << "Testing products: ublas" << std::endl;
463  result = viennacl::linalg::prod(ublas_matrix, rhs);
464 
465  std::cout << "Testing products: compressed_matrix" << std::endl;
466  vcl_result = viennacl::linalg::prod(vcl_compressed_matrix, vcl_rhs);
467 
468  if ( std::fabs(diff(result, vcl_result)) > epsilon )
469  {
470  std::cout << "# Error at operation: matrix-vector product with compressed_matrix" << std::endl;
471  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
472  retval = EXIT_FAILURE;
473  }
474 
475  std::cout << "Testing products: compressed_matrix, strided vectors" << std::endl;
476  retval = strided_matrix_vector_product_test<NumericT, viennacl::compressed_matrix<NumericT> >(epsilon, result, rhs, vcl_result, vcl_rhs);
477  if (retval != EXIT_SUCCESS)
478  return retval;
479 
480  //
481  // Triangular solvers for A \ b:
482  //
483  ublas::compressed_matrix<NumericT> ublas_matrix_trans(ublas_matrix.size2(), ublas_matrix.size1(), ublas_matrix.nnz()); // = trans(ublas_matrix); //note: triangular solvers with uBLAS show atrocious performance, while transposed solvers are quite okay. To keep execution times short, we use a double-transpose-trick in the following.
484 
485  // fast transpose:
486  for (typename ublas::compressed_matrix<NumericT>::iterator1 row_it = ublas_matrix.begin1();
487  row_it != ublas_matrix.end1();
488  ++row_it)
489  {
490  for (typename ublas::compressed_matrix<NumericT>::iterator2 col_it = row_it.begin();
491  col_it != row_it.end();
492  ++col_it)
493  {
494  ublas_matrix_trans(col_it.index1(), col_it.index2()) = *col_it;
495  }
496  }
497 
498 
499  std::cout << "Testing unit upper triangular solve: compressed_matrix" << std::endl;
500  result = rhs;
501  viennacl::copy(result, vcl_result);
502  boost::numeric::ublas::inplace_solve(trans(ublas_matrix_trans), result, boost::numeric::ublas::unit_upper_tag());
503  viennacl::linalg::inplace_solve(vcl_compressed_matrix, vcl_result, viennacl::linalg::unit_upper_tag());
504 
505  if ( std::fabs(diff(result, vcl_result)) > epsilon )
506  {
507  std::cout << "# Error at operation: unit upper triangular solve with compressed_matrix" << std::endl;
508  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
509  retval = EXIT_FAILURE;
510  }
511 
512  std::cout << "Testing upper triangular solve: compressed_matrix" << std::endl;
513  result = rhs;
514  viennacl::copy(result, vcl_result);
515  boost::numeric::ublas::inplace_solve(trans(ublas_matrix_trans), result, boost::numeric::ublas::upper_tag());
516  viennacl::linalg::inplace_solve(vcl_compressed_matrix, vcl_result, viennacl::linalg::upper_tag());
517 
518  if ( std::fabs(diff(result, vcl_result)) > epsilon )
519  {
520  std::cout << "# Error at operation: upper triangular solve with compressed_matrix" << std::endl;
521  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
522  retval = EXIT_FAILURE;
523  }
524 
525  std::cout << "Testing unit lower triangular solve: compressed_matrix" << std::endl;
526  result = rhs;
527  viennacl::copy(result, vcl_result);
528  boost::numeric::ublas::inplace_solve(trans(ublas_matrix_trans), result, boost::numeric::ublas::unit_lower_tag());
529  viennacl::linalg::inplace_solve(vcl_compressed_matrix, vcl_result, viennacl::linalg::unit_lower_tag());
530 
531  /*std::list< viennacl::backend::mem_handle > multifrontal_L_row_index_arrays_;
532  std::list< viennacl::backend::mem_handle > multifrontal_L_row_buffers_;
533  std::list< viennacl::backend::mem_handle > multifrontal_L_col_buffers_;
534  std::list< viennacl::backend::mem_handle > multifrontal_L_element_buffers_;
535  std::list< std::size_t > multifrontal_L_row_elimination_num_list_;
536 
537  viennacl::vector<NumericT> multifrontal_U_diagonal_;
538 
539  viennacl::switch_memory_domain(multifrontal_U_diagonal_, viennacl::MAIN_MEMORY);
540  multifrontal_U_diagonal_.resize(vcl_compressed_matrix.size1(), false);
541  viennacl::linalg::single_threaded::detail::row_info(vcl_compressed_matrix, multifrontal_U_diagonal_, viennacl::linalg::detail::SPARSE_ROW_DIAGONAL);
542 
543  viennacl::linalg::detail::multifrontal_setup_L(vcl_compressed_matrix,
544  multifrontal_U_diagonal_, //dummy
545  multifrontal_L_row_index_arrays_,
546  multifrontal_L_row_buffers_,
547  multifrontal_L_col_buffers_,
548  multifrontal_L_element_buffers_,
549  multifrontal_L_row_elimination_num_list_);
550 
551  viennacl::linalg::detail::multifrontal_substitute(vcl_result,
552  multifrontal_L_row_index_arrays_,
553  multifrontal_L_row_buffers_,
554  multifrontal_L_col_buffers_,
555  multifrontal_L_element_buffers_,
556  multifrontal_L_row_elimination_num_list_);*/
557 
558 
559  if ( std::fabs(diff(result, vcl_result)) > epsilon )
560  {
561  std::cout << "# Error at operation: unit lower triangular solve with compressed_matrix" << std::endl;
562  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
563  retval = EXIT_FAILURE;
564  }
565 
566 
567  std::cout << "Testing lower triangular solve: compressed_matrix" << std::endl;
568  result = rhs;
569  viennacl::copy(result, vcl_result);
570  boost::numeric::ublas::inplace_solve(trans(ublas_matrix_trans), result, boost::numeric::ublas::lower_tag());
571  viennacl::linalg::inplace_solve(vcl_compressed_matrix, vcl_result, viennacl::linalg::lower_tag());
572 
573  /*std::list< viennacl::backend::mem_handle > multifrontal_U_row_index_arrays_;
574  std::list< viennacl::backend::mem_handle > multifrontal_U_row_buffers_;
575  std::list< viennacl::backend::mem_handle > multifrontal_U_col_buffers_;
576  std::list< viennacl::backend::mem_handle > multifrontal_U_element_buffers_;
577  std::list< std::size_t > multifrontal_U_row_elimination_num_list_;
578 
579  multifrontal_U_diagonal_.resize(vcl_compressed_matrix.size1(), false);
580  viennacl::linalg::single_threaded::detail::row_info(vcl_compressed_matrix, multifrontal_U_diagonal_, viennacl::linalg::detail::SPARSE_ROW_DIAGONAL);
581  viennacl::linalg::detail::multifrontal_setup_U(vcl_compressed_matrix,
582  multifrontal_U_diagonal_,
583  multifrontal_U_row_index_arrays_,
584  multifrontal_U_row_buffers_,
585  multifrontal_U_col_buffers_,
586  multifrontal_U_element_buffers_,
587  multifrontal_U_row_elimination_num_list_);
588 
589  vcl_result = viennacl::linalg::element_div(vcl_result, multifrontal_U_diagonal_);
590  viennacl::linalg::detail::multifrontal_substitute(vcl_result,
591  multifrontal_U_row_index_arrays_,
592  multifrontal_U_row_buffers_,
593  multifrontal_U_col_buffers_,
594  multifrontal_U_element_buffers_,
595  multifrontal_U_row_elimination_num_list_);*/
596 
597 
598  if ( std::fabs(diff(result, vcl_result)) > epsilon )
599  {
600  std::cout << "# Error at operation: lower triangular solve with compressed_matrix" << std::endl;
601  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
602  retval = EXIT_FAILURE;
603  }
604 
605 /*
606  std::cout << "Testing lower triangular solve: compressed_matrix" << std::endl;
607  result = rhs;
608  viennacl::copy(result, vcl_result);
609  boost::numeric::ublas::inplace_solve(ublas_matrix, result, boost::numeric::ublas::lower_tag());
610  viennacl::linalg::inplace_solve(vcl_compressed_matrix, vcl_result, viennacl::linalg::lower_tag());
611 
612  if ( std::fabs(diff(result, vcl_result)) > epsilon )
613  {
614  std::cout << "# Error at operation: lower triangular solve with compressed_matrix" << std::endl;
615  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
616  retval = EXIT_FAILURE;
617  }*/
618 
619  //
620  // Triangular solvers for A^T \ b
621  //
622 
623  std::cout << "Testing transposed unit upper triangular solve: compressed_matrix" << std::endl;
624  result = rhs;
625  viennacl::copy(result, vcl_result);
626  boost::numeric::ublas::inplace_solve(trans(ublas_matrix), result, boost::numeric::ublas::unit_upper_tag());
627  viennacl::linalg::inplace_solve(trans(vcl_compressed_matrix), vcl_result, viennacl::linalg::unit_upper_tag());
628 
629  if ( std::fabs(diff(result, vcl_result)) > epsilon )
630  {
631  std::cout << "# Error at operation: unit upper triangular solve with compressed_matrix" << std::endl;
632  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
633  retval = EXIT_FAILURE;
634  }
635 
636  std::cout << "Testing transposed upper triangular solve: compressed_matrix" << std::endl;
637  result = rhs;
638  viennacl::copy(result, vcl_result);
639  boost::numeric::ublas::inplace_solve(trans(ublas_matrix), result, boost::numeric::ublas::upper_tag());
640  viennacl::linalg::inplace_solve(trans(vcl_compressed_matrix), vcl_result, viennacl::linalg::upper_tag());
641 
642  if ( std::fabs(diff(result, vcl_result)) > epsilon )
643  {
644  std::cout << "# Error at operation: upper triangular solve with compressed_matrix" << std::endl;
645  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
646  retval = EXIT_FAILURE;
647  }
648 
649 
650  std::cout << "Testing transposed unit lower triangular solve: compressed_matrix" << std::endl;
651  result = rhs;
652  viennacl::copy(result, vcl_result);
653  boost::numeric::ublas::inplace_solve(trans(ublas_matrix), result, boost::numeric::ublas::unit_lower_tag());
654  viennacl::linalg::inplace_solve(trans(vcl_compressed_matrix), vcl_result, viennacl::linalg::unit_lower_tag());
655 
656  if ( std::fabs(diff(result, vcl_result)) > epsilon )
657  {
658  std::cout << "# Error at operation: unit lower triangular solve with compressed_matrix" << std::endl;
659  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
660  retval = EXIT_FAILURE;
661  }
662 
663  std::cout << "Testing transposed lower triangular solve: compressed_matrix" << std::endl;
664  result = rhs;
665  viennacl::copy(result, vcl_result);
666  boost::numeric::ublas::inplace_solve(trans(ublas_matrix), result, boost::numeric::ublas::lower_tag());
667  viennacl::linalg::inplace_solve(trans(vcl_compressed_matrix), vcl_result, viennacl::linalg::lower_tag());
668 
669  if ( std::fabs(diff(result, vcl_result)) > epsilon )
670  {
671  std::cout << "# Error at operation: lower triangular solve with compressed_matrix" << std::endl;
672  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
673  retval = EXIT_FAILURE;
674  }
675 
676  std::cout << "Testing products: compressed_compressed_matrix" << std::endl;
677  result = viennacl::linalg::prod(ublas_cc_matrix, rhs);
678  vcl_result = viennacl::linalg::prod(vcl_compressed_compressed_matrix, vcl_rhs);
679 
680  if ( std::fabs(diff(result, vcl_result)) > epsilon )
681  {
682  std::cout << "# Error at operation: matrix-vector product with compressed_compressed_matrix" << std::endl;
683  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
684  retval = EXIT_FAILURE;
685  }
686 
687  {
688  ublas::compressed_matrix<NumericT> temp(vcl_compressed_compressed_matrix.size1(), vcl_compressed_compressed_matrix.size2());
689  viennacl::copy(vcl_compressed_compressed_matrix, temp);
690 
691  // check that entries are correct by computing the product again:
692  result = viennacl::linalg::prod(temp, rhs);
693 
694  if ( std::fabs(diff(result, vcl_result)) > epsilon )
695  {
696  std::cout << "# Error at operation: matrix-vector product with compressed_compressed_matrix (after copy back)" << std::endl;
697  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
698  retval = EXIT_FAILURE;
699  }
700 
701  }
702 
703 
704 
705 
706  std::cout << "Testing products: coordinate_matrix" << std::endl;
707  result = viennacl::linalg::prod(ublas_matrix, rhs);
708  vcl_result = viennacl::linalg::prod(vcl_coordinate_matrix, vcl_rhs);
709 
710  if ( std::fabs(diff(result, vcl_result)) > epsilon )
711  {
712  std::cout << "# Error at operation: matrix-vector product with coordinate_matrix" << std::endl;
713  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
714  retval = EXIT_FAILURE;
715  }
716 
717  std::cout << "Testing products: coordinate_matrix, strided vectors" << std::endl;
718  //std::cout << " --> SKIPPING <--" << std::endl;
719  retval = strided_matrix_vector_product_test<NumericT, viennacl::coordinate_matrix<NumericT> >(epsilon, result, rhs, vcl_result, vcl_rhs);
720  if (retval != EXIT_SUCCESS)
721  return retval;
722 
723 
724  //std::cout << "Copying ell_matrix" << std::endl;
725  viennacl::copy(ublas_matrix, vcl_ell_matrix);
726  ublas_matrix.clear();
727  viennacl::copy(vcl_ell_matrix, ublas_matrix);// just to check that it's works
728 
729 
730  std::cout << "Testing products: ell_matrix" << std::endl;
731  result = viennacl::linalg::prod(ublas_matrix, rhs);
732  vcl_result.clear();
733  vcl_result = viennacl::linalg::prod(vcl_ell_matrix, vcl_rhs);
734  //viennacl::linalg::prod_impl(vcl_ell_matrix, vcl_rhs, vcl_result);
735  //std::cout << vcl_result << "\n";
736  //std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
737  //std::cout << "First entry of result vector: " << vcl_result[0] << std::endl;
738 
739  if ( std::fabs(diff(result, vcl_result)) > epsilon )
740  {
741  std::cout << "# Error at operation: matrix-vector product with ell_matrix" << std::endl;
742  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
743  retval = EXIT_FAILURE;
744  }
745 
746  std::cout << "Testing products: ell_matrix, strided vectors" << std::endl;
747  retval = strided_matrix_vector_product_test<NumericT, viennacl::ell_matrix<NumericT> >(epsilon, result, rhs, vcl_result, vcl_rhs);
748  if (retval != EXIT_SUCCESS)
749  return retval;
750 
751  //std::cout << "Copying sliced_ell_matrix" << std::endl;
752  viennacl::copy(ublas_matrix, vcl_sliced_ell_matrix);
753  //ublas_matrix.clear();
754  //viennacl::copy(vcl_hyb_matrix, ublas_matrix);// just to check that it's works
755  //viennacl::copy(ublas_matrix, vcl_hyb_matrix);
756 
757  std::cout << "Testing products: sliced_ell_matrix" << std::endl;
758  result = viennacl::linalg::prod(ublas_matrix, rhs);
759  vcl_result.clear();
760  vcl_result = viennacl::linalg::prod(vcl_sliced_ell_matrix, vcl_rhs);
761 
762  if ( std::fabs(diff(result, vcl_result)) > epsilon )
763  {
764  std::cout << "# Error at operation: matrix-vector product with sliced_ell_matrix" << std::endl;
765  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
766  retval = EXIT_FAILURE;
767  }
768 
769  std::cout << "Testing products: sliced_ell_matrix, strided vectors" << std::endl;
770  retval = strided_matrix_vector_product_test<NumericT, viennacl::sliced_ell_matrix<NumericT> >(epsilon, result, rhs, vcl_result, vcl_rhs);
771  if (retval != EXIT_SUCCESS)
772  return retval;
773 
774 
775  //std::cout << "Copying hyb_matrix" << std::endl;
776  viennacl::copy(ublas_matrix, vcl_hyb_matrix);
777  ublas_matrix.clear();
778  viennacl::copy(vcl_hyb_matrix, ublas_matrix);// just to check that it's works
779  viennacl::copy(ublas_matrix, vcl_hyb_matrix);
780 
781  std::cout << "Testing products: hyb_matrix" << std::endl;
782  result = viennacl::linalg::prod(ublas_matrix, rhs);
783  vcl_result.clear();
784  vcl_result = viennacl::linalg::prod(vcl_hyb_matrix, vcl_rhs);
785  //viennacl::linalg::prod_impl(vcl_hyb_matrix, vcl_rhs, vcl_result);
786  //std::cout << vcl_result << "\n";
787  //std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
788  //std::cout << "First entry of result vector: " << vcl_result[0] << std::endl;
789 
790  if ( std::fabs(diff(result, vcl_result)) > epsilon )
791  {
792  std::cout << "# Error at operation: matrix-vector product with hyb_matrix" << std::endl;
793  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
794  retval = EXIT_FAILURE;
795  }
796 
797  std::cout << "Testing products: hyb_matrix, strided vectors" << std::endl;
798  retval = strided_matrix_vector_product_test<NumericT, viennacl::hyb_matrix<NumericT> >(epsilon, result, rhs, vcl_result, vcl_rhs);
799  if (retval != EXIT_SUCCESS)
800  return retval;
801 
802 
803  // --------------------------------------------------------------------------
804  // --------------------------------------------------------------------------
805  NumericT alpha = static_cast<NumericT>(2.786);
806  NumericT beta = static_cast<NumericT>(1.432);
807  copy(rhs.begin(), rhs.end(), vcl_rhs.begin());
808  copy(result.begin(), result.end(), vcl_result.begin());
809  copy(result.begin(), result.end(), vcl_result2.begin());
810 
811  std::cout << "Testing scaled additions of products and vectors" << std::endl;
812  result = alpha * viennacl::linalg::prod(ublas_matrix, rhs) + beta * result;
813  vcl_result2 = alpha * viennacl::linalg::prod(vcl_compressed_matrix, vcl_rhs) + beta * vcl_result;
814 
815  if ( std::fabs(diff(result, vcl_result2)) > epsilon )
816  {
817  std::cout << "# Error at operation: matrix-vector product (compressed_matrix) with scaled additions" << std::endl;
818  std::cout << " diff: " << std::fabs(diff(result, vcl_result2)) << std::endl;
819  retval = EXIT_FAILURE;
820  }
821 
822 
823  vcl_result2.clear();
824  vcl_result2 = alpha * viennacl::linalg::prod(vcl_coordinate_matrix, vcl_rhs) + beta * vcl_result;
825 
826  if ( std::fabs(diff(result, vcl_result2)) > epsilon )
827  {
828  std::cout << "# Error at operation: matrix-vector product (coordinate_matrix) with scaled additions" << std::endl;
829  std::cout << " diff: " << std::fabs(diff(result, vcl_result2)) << std::endl;
830  retval = EXIT_FAILURE;
831  }
832 
833  vcl_result2.clear();
834  vcl_result2 = alpha * viennacl::linalg::prod(vcl_ell_matrix, vcl_rhs) + beta * vcl_result;
835 
836  if ( std::fabs(diff(result, vcl_result2)) > epsilon )
837  {
838  std::cout << "# Error at operation: matrix-vector product (ell_matrix) with scaled additions" << std::endl;
839  std::cout << " diff: " << std::fabs(diff(result, vcl_result2)) << std::endl;
840  retval = EXIT_FAILURE;
841  }
842 
843  vcl_result2.clear();
844  vcl_result2 = alpha * viennacl::linalg::prod(vcl_hyb_matrix, vcl_rhs) + beta * vcl_result;
845 
846  if ( std::fabs(diff(result, vcl_result2)) > epsilon )
847  {
848  std::cout << "# Error at operation: matrix-vector product (hyb_matrix) with scaled additions" << std::endl;
849  std::cout << " diff: " << std::fabs(diff(result, vcl_result2)) << std::endl;
850  retval = EXIT_FAILURE;
851  }
852 
854  ublas_matrix.clear();
855 
856  std::cout << "Testing products after clear(): compressed_matrix" << std::endl;
857  vcl_compressed_matrix.clear();
858  result = ublas::scalar_vector<NumericT>(result.size(), NumericT(1));
859  result = viennacl::linalg::prod(ublas_matrix, rhs);
860  vcl_result = viennacl::linalg::prod(vcl_compressed_matrix, vcl_rhs);
861 
862  if ( std::fabs(diff(result, vcl_result)) > epsilon )
863  {
864  std::cout << "# Error at operation: matrix-vector product with compressed_matrix" << std::endl;
865  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
866  retval = EXIT_FAILURE;
867  }
868 
869  std::cout << "Testing products after clear(): compressed_compressed_matrix" << std::endl;
870  vcl_compressed_compressed_matrix.clear();
871  result = ublas::scalar_vector<NumericT>(result.size(), NumericT(1));
872  result = viennacl::linalg::prod(ublas_matrix, rhs);
873  vcl_result = viennacl::linalg::prod(vcl_compressed_compressed_matrix, vcl_rhs);
874 
875  if ( std::fabs(diff(result, vcl_result)) > epsilon )
876  {
877  std::cout << "# Error at operation: matrix-vector product with compressed_compressed_matrix" << std::endl;
878  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
879  retval = EXIT_FAILURE;
880  }
881 
882  std::cout << "Testing products after clear(): coordinate_matrix" << std::endl;
883  vcl_coordinate_matrix.clear();
884  result = ublas::scalar_vector<NumericT>(result.size(), NumericT(1));
885  result = viennacl::linalg::prod(ublas_matrix, rhs);
886  vcl_result = viennacl::linalg::prod(vcl_coordinate_matrix, vcl_rhs);
887 
888  if ( std::fabs(diff(result, vcl_result)) > epsilon )
889  {
890  std::cout << "# Error at operation: matrix-vector product with coordinate_matrix" << std::endl;
891  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
892  retval = EXIT_FAILURE;
893  }
894 
895  std::cout << "Testing products after clear(): ell_matrix" << std::endl;
896  vcl_ell_matrix.clear();
897  result = ublas::scalar_vector<NumericT>(result.size(), NumericT(1));
898  result = viennacl::linalg::prod(ublas_matrix, rhs);
899  vcl_result = viennacl::linalg::prod(vcl_ell_matrix, vcl_rhs);
900 
901  if ( std::fabs(diff(result, vcl_result)) > epsilon )
902  {
903  std::cout << "# Error at operation: matrix-vector product with ell_matrix" << std::endl;
904  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
905  retval = EXIT_FAILURE;
906  }
907 
908  std::cout << "Testing products after clear(): hyb_matrix" << std::endl;
909  vcl_hyb_matrix.clear();
910  result = ublas::scalar_vector<NumericT>(result.size(), NumericT(1));
911  result = viennacl::linalg::prod(ublas_matrix, rhs);
912  vcl_result = viennacl::linalg::prod(vcl_hyb_matrix, vcl_rhs);
913 
914  if ( std::fabs(diff(result, vcl_result)) > epsilon )
915  {
916  std::cout << "# Error at operation: matrix-vector product with hyb_matrix" << std::endl;
917  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
918  retval = EXIT_FAILURE;
919  }
920 
921  std::cout << "Testing products after clear(): sliced_ell_matrix" << std::endl;
922  vcl_sliced_ell_matrix.clear();
923  result = ublas::scalar_vector<NumericT>(result.size(), NumericT(1));
924  result = viennacl::linalg::prod(ublas_matrix, rhs);
925  vcl_result = viennacl::linalg::prod(vcl_sliced_ell_matrix, vcl_rhs);
926 
927  if ( std::fabs(diff(result, vcl_result)) > epsilon )
928  {
929  std::cout << "# Error at operation: matrix-vector product with sliced_ell_matrix" << std::endl;
930  std::cout << " diff: " << std::fabs(diff(result, vcl_result)) << std::endl;
931  retval = EXIT_FAILURE;
932  }
933 
934 
935  // --------------------------------------------------------------------------
936  return retval;
937 }
938 //
939 // -------------------------------------------------------------
940 //
941 int main()
942 {
943  std::cout << std::endl;
944  std::cout << "----------------------------------------------" << std::endl;
945  std::cout << "----------------------------------------------" << std::endl;
946  std::cout << "## Test :: Sparse Matrices" << std::endl;
947  std::cout << "----------------------------------------------" << std::endl;
948  std::cout << "----------------------------------------------" << std::endl;
949  std::cout << std::endl;
950 
951  int retval = EXIT_SUCCESS;
952 
953  std::cout << std::endl;
954  std::cout << "----------------------------------------------" << std::endl;
955  std::cout << std::endl;
956  {
957  typedef float NumericT;
958  NumericT epsilon = static_cast<NumericT>(1E-4);
959  std::cout << "# Testing setup:" << std::endl;
960  std::cout << " eps: " << epsilon << std::endl;
961  std::cout << " numeric: float" << std::endl;
962  retval = test<NumericT>(epsilon);
963  if ( retval == EXIT_SUCCESS )
964  std::cout << "# Test passed" << std::endl;
965  else
966  return retval;
967  }
968  std::cout << std::endl;
969  std::cout << "----------------------------------------------" << std::endl;
970  std::cout << std::endl;
971 
972 #ifdef VIENNACL_WITH_OPENCL
974 #endif
975  {
976  {
977  typedef double NumericT;
978  NumericT epsilon = 1.0E-12;
979  std::cout << "# Testing setup:" << std::endl;
980  std::cout << " eps: " << epsilon << std::endl;
981  std::cout << " numeric: double" << std::endl;
982  retval = test<NumericT>(epsilon);
983  if ( retval == EXIT_SUCCESS )
984  std::cout << "# Test passed" << std::endl;
985  else
986  return retval;
987  }
988  std::cout << std::endl;
989  std::cout << "----------------------------------------------" << std::endl;
990  std::cout << std::endl;
991  }
992 #ifdef VIENNACL_WITH_OPENCL
993  else
994  std::cout << "No double precision support, skipping test..." << std::endl;
995 #endif
996 
997 
998  std::cout << std::endl;
999  std::cout << "------- Test completed --------" << std::endl;
1000  std::cout << std::endl;
1001 
1002  return retval;
1003 }
Sparse matrix class using a hybrid format composed of the ELL and CSR format for storing the nonzeros...
Definition: forwards.h:405
void clear()
Resets all entries in the matrix back to zero without changing the matrix size. Resets the sparsity p...
void inplace_solve(const matrix_base< NumericT > &A, matrix_base< NumericT > &B, SolverTagT)
Direct inplace solver for triangular systems with multiple right hand sides, i.e. A \ B (MATLAB notat...
A reader and writer for the matrix market format is implemented here.
ScalarType diff(ScalarType &s1, viennacl::scalar< ScalarType > &s2)
Definition: sparse.cpp:76
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:226
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
int test(Epsilon const &epsilon)
Definition: sparse.cpp:394
void trans(matrix_expression< const matrix_base< NumericT, SizeT, DistanceT >, const matrix_base< NumericT, SizeT, DistanceT >, op_trans > const &proxy, matrix_base< NumericT > &temp_trans)
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
int main()
Definition: sparse.cpp:941
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
A tag class representing a lower triangular matrix.
Definition: forwards.h:809
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
Definition: memory.hpp:54
viennacl::scalar< int > s2
viennacl::scalar< float > s1
T max(const T &lhs, const T &rhs)
Maximum.
Definition: util.hpp:59
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:351
Implementation of the coordinate_matrix class.
viennacl::vector< float > v1
Implementation of the hyb_matrix class.
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:91
Sparse matrix class using the ELLPACK format for storing the nonzeros.
Definition: ell_matrix.hpp:53
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
Definition: vector.hpp:827
Implementations of incomplete factorization preconditioners. Convenience header file.
A tag class representing an upper triangular matrix.
Definition: forwards.h:814
void inplace_solve(const matrix_base< NumericT > &A, bool trans_A, matrix_base< NumericT > &B, bool trans_B, SolverTagT tag)
Direct inplace solver for triangular systems with multiple right hand sides, i.e. A \ B (MATLAB notat...
Sparse matrix class using the sliced ELLPACK with parameters C, .
Definition: forwards.h:402
Implementation of the compressed_matrix class.
A sparse square matrix in compressed sparse rows format optimized for the case that only a few rows c...
Implementation of the sliced_ell_matrix class.
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Definition: device.hpp:956
matrix_range< MatrixType > project(MatrixType const &A, viennacl::range const &r1, viennacl::range const &r2)
Implementation of the ell_matrix class.
void prod(const MatrixT1 &A, bool transposed_A, const MatrixT2 &B, bool transposed_B, MatrixT3 &C, ScalarT alpha, ScalarT beta)
Proxy classes for vectors.
Implementation of the compressed_compressed_matrix class (CSR format with a relatively small number o...
void clear()
Resets all entries to zero. Does not change the size of the vector.
viennacl::vector< int > v2
basic_slice slice
Definition: forwards.h:428
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
T norm_inf(std::vector< T, A > const &v1)
Definition: norm_inf.hpp:60
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) ...
int strided_matrix_vector_product_test(Epsilon epsilon, UblasVectorT &result, UblasVectorT const &rhs, VCLVectorT &vcl_result, VCLVectorT &vcl_rhs)
Definition: sparse.cpp:179
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
float ScalarType
Definition: fft_1d.cpp:42
A tag class representing a lower triangular matrix with unit diagonal.
Definition: forwards.h:819
long read_matrix_market_file(MatrixT &mat, const char *file, long index_base=1)
Reads a sparse matrix from a file (MatrixMarket format)
iterator end()
Returns an iterator pointing to the end of the vector (STL like)
Definition: vector.hpp:834
Common routines used within ILU-type preconditioners.
A slice class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Definition: forwards.h:428
Implementation of the ViennaCL scalar class.
int resize_test(Epsilon const &epsilon)
Definition: sparse.cpp:228
A tag class representing an upper triangular matrix with unit diagonal.
Definition: forwards.h:824
A sparse square matrix, where entries are stored as triplets (i,j, val), where i and j are the row an...