35 #include <boost/numeric/ublas/io.hpp>
36 #include <boost/numeric/ublas/triangular.hpp>
37 #include <boost/numeric/ublas/matrix_sparse.hpp>
38 #include <boost/numeric/ublas/matrix.hpp>
39 #include <boost/numeric/ublas/matrix_proxy.hpp>
40 #include <boost/numeric/ublas/lu.hpp>
41 #include <boost/numeric/ublas/io.hpp>
42 #include <boost/numeric/ublas/operation_sparse.hpp>
48 #define VIENNACL_WITH_UBLAS 1
74 template<
typename ScalarType>
78 return (s1 - s2) /
std::max(fabs(s1), std::fabs(s2));
82 template<
typename ScalarType>
85 ublas::vector<ScalarType> v2_cpu(v2.
size());
89 for (
unsigned int i=0;i<v1.size(); ++i)
91 if (
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) > 0 )
96 v2_cpu[i] = std::fabs(v2_cpu[i] - v1[i]) /
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) );
101 if (v2_cpu[i] > 0.0001)
104 std::cout <<
"Error at entry " << i <<
": " << v1[i] <<
" vs. " << v2_cpu[i] << std::endl;
114 template<
typename ScalarType,
typename VCL_MATRIX>
115 ScalarType diff(ublas::compressed_matrix<ScalarType> & cpu_matrix, VCL_MATRIX & gpu_matrix)
117 typedef ublas::compressed_matrix<ScalarType> CPU_MATRIX;
127 for (
typename CPU_MATRIX::const_iterator1 row_it = cpu_matrix.begin1();
128 row_it != cpu_matrix.end1();
132 for (
typename CPU_MATRIX::const_iterator2 col_it = row_it.begin();
133 col_it != row_it.end();
139 if (
std::max( std::fabs(cpu_matrix(col_it.index1(), col_it.index2())),
140 std::fabs(from_gpu(col_it.index1(), col_it.index2())) ) > 0 )
141 current_error = std::fabs(cpu_matrix(col_it.index1(), col_it.index2()) - from_gpu(col_it.index1(), col_it.index2()))
142 /
std::max( std::fabs(cpu_matrix(col_it.index1(), col_it.index2())),
143 std::fabs(from_gpu(col_it.index1(), col_it.index2())) );
144 if (current_error > error)
145 error = current_error;
151 for (
typename CPU_MATRIX::const_iterator1 row_it = from_gpu.begin1();
152 row_it != from_gpu.end1();
156 for (
typename CPU_MATRIX::const_iterator2 col_it = row_it.begin();
157 col_it != row_it.end();
163 if (
std::max( std::fabs(cpu_matrix(col_it.index1(), col_it.index2())),
164 std::fabs(from_gpu(col_it.index1(), col_it.index2())) ) > 0 )
165 current_error = std::fabs(cpu_matrix(col_it.index1(), col_it.index2()) - from_gpu(col_it.index1(), col_it.index2()))
166 /
std::max( std::fabs(cpu_matrix(col_it.index1(), col_it.index2())),
167 std::fabs(from_gpu(col_it.index1(), col_it.index2())) );
168 if (current_error > error)
169 error = current_error;
179 template<
typename NumericT,
typename Epsilon >
180 int test(Epsilon
const& epsilon)
182 int retval = EXIT_SUCCESS;
185 NumericT alpha =
static_cast<NumericT
>(2.786);
186 NumericT beta =
static_cast<NumericT
>(1.432);
188 ublas::vector<NumericT> rhs;
189 ublas::vector<NumericT> result;
190 ublas::compressed_matrix<NumericT> ublas_matrix;
194 std::cout <<
"Error reading Matrix file" << std::endl;
198 std::cout <<
"done reading matrix" << std::endl;
201 rhs.resize(ublas_matrix.size2());
202 for (std::size_t i=0; i<rhs.size(); ++i)
204 ublas_matrix(i,i) = NumericT(0.5);
205 rhs[i] = NumericT(1) + random<NumericT>();
224 std::cout <<
"Testing products: compressed_matrix" << std::endl;
232 if ( std::fabs(
diff(result, vcl_result)) > epsilon )
234 std::cout <<
"# Error at operation: matrix-vector product with compressed_matrix" << std::endl;
235 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result)) << std::endl;
236 retval = EXIT_FAILURE;
239 std::cout <<
"Testing products: coordinate_matrix" << std::endl;
240 rhs *= NumericT(1.1);
241 vcl_rhs *= NumericT(1.1);
248 if ( std::fabs(
diff(result, vcl_result)) > epsilon )
250 std::cout <<
"# Error at operation: matrix-vector product with coordinate_matrix" << std::endl;
251 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result)) << std::endl;
252 retval = EXIT_FAILURE;
261 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
263 std::cout <<
"# Error at operation: matrix-vector product (coordinate_matrix) with scaled additions" << std::endl;
264 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
265 retval = EXIT_FAILURE;
270 ublas_matrix.clear();
274 std::cout <<
"Testing products: ell_matrix" << std::endl;
275 rhs *= NumericT(1.1);
276 vcl_rhs *= NumericT(1.1);
284 if ( std::fabs(
diff(result, vcl_result)) > epsilon )
286 std::cout <<
"# Error at operation: matrix-vector product with ell_matrix" << std::endl;
287 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result)) << std::endl;
288 retval = EXIT_FAILURE;
293 ublas_matrix.clear();
297 std::cout <<
"Testing products: hyb_matrix" << std::endl;
298 rhs *= NumericT(1.1);
299 vcl_rhs *= NumericT(1.1);
306 if ( std::fabs(
diff(result, vcl_result)) > epsilon )
308 std::cout <<
"# Error at operation: matrix-vector product with hyb_matrix" << std::endl;
309 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result)) << std::endl;
310 retval = EXIT_FAILURE;
316 copy(rhs.begin(), rhs.end(), vcl_rhs.begin());
317 copy(result.begin(), result.end(), vcl_result.begin());
318 copy(result.begin(), result.end(), vcl_result2.begin());
319 copy(ublas_matrix, vcl_compressed_matrix);
320 copy(ublas_matrix, vcl_coordinate_matrix);
321 copy(ublas_matrix, vcl_ell_matrix);
322 copy(ublas_matrix, vcl_hyb_matrix);
324 std::cout <<
"Testing scaled additions of products and vectors: compressed_matrix" << std::endl;
325 rhs *= NumericT(1.1);
326 vcl_rhs *= NumericT(1.1);
333 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
335 std::cout <<
"# Error at operation: matrix-vector product (compressed_matrix) with scaled additions" << std::endl;
336 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
337 retval = EXIT_FAILURE;
341 std::cout <<
"Testing scaled additions of products and vectors: coordinate_matrix" << std::endl;
342 copy(result.begin(), result.end(), vcl_result.begin());
343 rhs *= NumericT(1.1);
344 vcl_rhs *= NumericT(1.1);
351 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
353 std::cout <<
"# Error at operation: matrix-vector product (coordinate_matrix) with scaled additions" << std::endl;
354 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
355 retval = EXIT_FAILURE;
358 std::cout <<
"Testing scaled additions of products and vectors: ell_matrix" << std::endl;
359 copy(result.begin(), result.end(), vcl_result.begin());
360 rhs *= NumericT(1.1);
361 vcl_rhs *= NumericT(1.1);
368 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
370 std::cout <<
"# Error at operation: matrix-vector product (ell_matrix) with scaled additions" << std::endl;
371 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
372 retval = EXIT_FAILURE;
375 std::cout <<
"Testing scaled additions of products and vectors: hyb_matrix" << std::endl;
376 copy(result.begin(), result.end(), vcl_result.begin());
377 rhs *= NumericT(1.1);
378 vcl_rhs *= NumericT(1.1);
385 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
387 std::cout <<
"# Error at operation: matrix-vector product (hyb_matrix) with scaled additions" << std::endl;
388 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
389 retval = EXIT_FAILURE;
401 std::cout << std::endl;
402 std::cout <<
"----------------------------------------------" << std::endl;
403 std::cout <<
"----------------------------------------------" << std::endl;
404 std::cout <<
"## Test :: Sparse Matrices" << std::endl;
405 std::cout <<
"----------------------------------------------" << std::endl;
406 std::cout <<
"----------------------------------------------" << std::endl;
407 std::cout << std::endl;
409 int retval = EXIT_SUCCESS;
411 std::cout << std::endl;
412 std::cout <<
"----------------------------------------------" << std::endl;
413 std::cout << std::endl;
415 typedef float NumericT;
416 NumericT epsilon =
static_cast<NumericT
>(1E-4);
417 std::cout <<
"# Testing setup:" << std::endl;
418 std::cout <<
" eps: " << epsilon << std::endl;
419 std::cout <<
" numeric: float" << std::endl;
420 retval = test<NumericT>(epsilon);
421 if ( retval == EXIT_SUCCESS )
422 std::cout <<
"# Test passed" << std::endl;
426 std::cout << std::endl;
427 std::cout <<
"----------------------------------------------" << std::endl;
428 std::cout << std::endl;
430 #ifdef VIENNACL_WITH_OPENCL
435 typedef double NumericT;
436 NumericT epsilon = 1.0E-13;
437 std::cout <<
"# Testing setup:" << std::endl;
438 std::cout <<
" eps: " << epsilon << std::endl;
439 std::cout <<
" numeric: double" << std::endl;
440 retval = test<NumericT>(epsilon);
441 if ( retval == EXIT_SUCCESS )
442 std::cout <<
"# Test passed" << std::endl;
446 std::cout << std::endl;
447 std::cout <<
"----------------------------------------------" << std::endl;
448 std::cout << std::endl;
450 #ifdef VIENNACL_WITH_OPENCL
452 std::cout <<
"No double precision support, skipping test..." << std::endl;
456 std::cout << std::endl;
457 std::cout <<
"------- Test completed --------" << std::endl;
458 std::cout << std::endl;
Sparse matrix class using a hybrid format composed of the ELL and CSR format for storing the nonzeros...
A reader and writer for the matrix market format is implemented here.
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
int test(Epsilon const &epsilon)
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Some helper routines for reading/writing/printing scheduler expressions.
A tag class representing assignment.
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
void execute(statement const &s)
viennacl::scalar< int > s2
viennacl::scalar< float > s1
T max(const T &lhs, const T &rhs)
Maximum.
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
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)
Sparse matrix class using the ELLPACK format for storing the nonzeros.
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
Implementations of incomplete factorization preconditioners. Convenience header file.
Implementation of the compressed_matrix class.
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Implementation of the ell_matrix class.
Proxy classes for vectors.
viennacl::vector< int > v2
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
T norm_inf(std::vector< T, A > const &v1)
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) ...
size_type size() const
Returns the length of the vector (cf. std::vector)
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
ScalarType diff(ScalarType &s1, viennacl::scalar< ScalarType > &s2)
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)
Common routines used within ILU-type preconditioners.
Implementation of the ViennaCL scalar class.
A sparse square matrix, where entries are stored as triplets (i,j, val), where i and j are the row an...