52 template<
typename ScalarType>
56 return (s1 - s2) /
std::max(fabs(s1), std::fabs(s2));
60 template<
typename ScalarType>
63 std::vector<ScalarType> v2_cpu(v2.
size());
67 for (
unsigned int i=0;i<v1.size(); ++i)
69 if (
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) > 0 )
74 v2_cpu[i] = std::fabs(v2_cpu[i] - v1[i]) /
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) );
79 if (v2_cpu[i] > 0.0001)
82 std::cout <<
"Error at entry " << i <<
": Should: " << v1[i] <<
" vs. Is: " << v2[i] << std::endl;
89 for (std::size_t i=0; i<v2_cpu.size(); ++i)
90 norm_inf = std::max<ScalarType>(norm_inf, std::fabs(v2_cpu[i]));
96 template<
typename IndexT,
typename NumericT,
typename SparseMatrixT>
97 NumericT diff(std::vector<std::map<IndexT, NumericT> > & cpu_A, SparseMatrixT & vcl_A)
99 typedef typename std::map<IndexT, NumericT>::const_iterator RowIterator;
101 std::vector<std::map<IndexT, NumericT> > from_gpu(vcl_A.size1());
109 for (std::size_t i=0; i<cpu_A.size(); ++i)
112 for (RowIterator it = cpu_A[i].begin(); it != cpu_A[i].end(); ++it)
117 NumericT val_gpu_A = from_gpu[i][it->first];
121 current_error = std::fabs(val_cpu_A - val_gpu_A) / max_val;
122 if (current_error > error)
123 error = current_error;
129 for (std::size_t i=0; i<from_gpu.size(); ++i)
132 for (RowIterator it = from_gpu[i].begin(); it != from_gpu[i].end(); ++it)
137 NumericT val_cpu_A = cpu_A[i][it->first];
141 current_error = std::fabs(val_cpu_A - val_gpu_A) / max_val;
142 if (current_error > error)
143 error = current_error;
151 template<
typename IndexT,
typename NumericT,
typename VectorT>
154 typedef typename std::map<IndexT, NumericT>::const_iterator RowIterator;
156 for (std::size_t i=0; i<A.size(); ++i)
159 for (RowIterator it = A[i].begin(); it != A[i].end(); ++it)
160 val += it->second * y[it->first];
161 x[i] = alpha * val + beta * x[i];
168 template<
typename NumericT,
typename Epsilon >
169 int test(Epsilon
const& epsilon)
171 int retval = EXIT_SUCCESS;
179 std::vector<NumericT> rhs;
180 std::vector<NumericT> result;
181 std::vector<std::map<unsigned int, NumericT> > std_matrix;
185 std::cout <<
"Error reading Matrix file" << std::endl;
189 std::cout <<
"done reading matrix" << std::endl;
192 rhs.resize(std_matrix.size());
193 for (std::size_t i=0; i<rhs.size(); ++i)
195 std_matrix[i][
static_cast<unsigned int>(i)] =
NumericT(0.5);
196 rhs[i] =
NumericT(1) + randomNumber();
215 std::cout <<
"Testing products: compressed_matrix" << std::endl;
223 if ( std::fabs(
diff(result, vcl_result)) > epsilon )
225 std::cout <<
"# Error at operation: matrix-vector product with compressed_matrix" << std::endl;
226 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result)) << std::endl;
227 retval = EXIT_FAILURE;
230 std::cout <<
"Testing products: coordinate_matrix" << std::endl;
231 for (std::size_t i=0; i<rhs.size(); ++i)
240 if ( std::fabs(
diff(result, vcl_result)) > epsilon )
242 std::cout <<
"# Error at operation: matrix-vector product with coordinate_matrix" << std::endl;
243 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result)) << std::endl;
244 retval = EXIT_FAILURE;
253 std::cout <<
"Testing products: ell_matrix" << std::endl;
254 for (std::size_t i=0; i<rhs.size(); ++i)
264 if ( std::fabs(
diff(result, vcl_result)) > epsilon )
266 std::cout <<
"# Error at operation: matrix-vector product with ell_matrix" << std::endl;
267 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result)) << std::endl;
268 retval = EXIT_FAILURE;
277 std::cout <<
"Testing products: hyb_matrix" << std::endl;
278 for (std::size_t i=0; i<rhs.size(); ++i)
287 if ( std::fabs(
diff(result, vcl_result)) > epsilon )
289 std::cout <<
"# Error at operation: matrix-vector product with hyb_matrix" << std::endl;
290 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result)) << std::endl;
291 retval = EXIT_FAILURE;
297 copy(rhs.begin(), rhs.end(), vcl_rhs.begin());
298 copy(result.begin(), result.end(), vcl_result.begin());
299 copy(result.begin(), result.end(), vcl_result2.begin());
300 copy(std_matrix, vcl_compressed_matrix);
301 copy(std_matrix, vcl_coordinate_matrix);
302 copy(std_matrix, vcl_ell_matrix);
303 copy(std_matrix, vcl_hyb_matrix);
305 std::cout <<
"Testing scaled additions of products and vectors: compressed_matrix" << std::endl;
306 for (std::size_t i=0; i<rhs.size(); ++i)
315 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
317 std::cout <<
"# Error at operation: matrix-vector product (compressed_matrix) with scaled additions" << std::endl;
318 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
319 retval = EXIT_FAILURE;
323 std::cout <<
"Testing scaled additions of products and vectors: coordinate_matrix" << std::endl;
324 copy(result.begin(), result.end(), vcl_result.begin());
325 for (std::size_t i=0; i<rhs.size(); ++i)
334 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
336 std::cout <<
"# Error at operation: matrix-vector product (coordinate_matrix) with scaled additions" << std::endl;
337 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
338 retval = EXIT_FAILURE;
341 std::cout <<
"Testing scaled additions of products and vectors: ell_matrix" << std::endl;
342 copy(result.begin(), result.end(), vcl_result.begin());
343 for (std::size_t i=0; i<rhs.size(); ++i)
352 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
354 std::cout <<
"# Error at operation: matrix-vector product (ell_matrix) with scaled additions" << std::endl;
355 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
356 retval = EXIT_FAILURE;
359 std::cout <<
"Testing scaled additions of products and vectors: hyb_matrix" << std::endl;
360 copy(result.begin(), result.end(), vcl_result.begin());
361 for (std::size_t i=0; i<rhs.size(); ++i)
370 if ( std::fabs(
diff(result, vcl_result2)) > epsilon )
372 std::cout <<
"# Error at operation: matrix-vector product (hyb_matrix) with scaled additions" << std::endl;
373 std::cout <<
" diff: " << std::fabs(
diff(result, vcl_result2)) << std::endl;
374 retval = EXIT_FAILURE;
386 std::cout << std::endl;
387 std::cout <<
"----------------------------------------------" << std::endl;
388 std::cout <<
"----------------------------------------------" << std::endl;
389 std::cout <<
"## Test :: Sparse Matrices" << std::endl;
390 std::cout <<
"----------------------------------------------" << std::endl;
391 std::cout <<
"----------------------------------------------" << std::endl;
392 std::cout << std::endl;
394 int retval = EXIT_SUCCESS;
396 std::cout << std::endl;
397 std::cout <<
"----------------------------------------------" << std::endl;
398 std::cout << std::endl;
401 NumericT epsilon =
static_cast<NumericT
>(1E-4);
402 std::cout <<
"# Testing setup:" << std::endl;
403 std::cout <<
" eps: " << epsilon << std::endl;
404 std::cout <<
" numeric: float" << std::endl;
405 retval = test<NumericT>(epsilon);
406 if ( retval == EXIT_SUCCESS )
407 std::cout <<
"# Test passed" << std::endl;
411 std::cout << std::endl;
412 std::cout <<
"----------------------------------------------" << std::endl;
413 std::cout << std::endl;
415 #ifdef VIENNACL_WITH_OPENCL
421 NumericT epsilon = 1.0E-13;
422 std::cout <<
"# Testing setup:" << std::endl;
423 std::cout <<
" eps: " << epsilon << std::endl;
424 std::cout <<
" numeric: double" << std::endl;
425 retval = test<NumericT>(epsilon);
426 if ( retval == EXIT_SUCCESS )
427 std::cout <<
"# Test passed" << std::endl;
431 std::cout << std::endl;
432 std::cout <<
"----------------------------------------------" << std::endl;
433 std::cout << std::endl;
435 #ifdef VIENNACL_WITH_OPENCL
437 std::cout <<
"No double precision support, skipping test..." << std::endl;
441 std::cout << std::endl;
442 std::cout <<
"------- Test completed --------" << std::endl;
443 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.
void sparse_prod(std::vector< std::map< IndexT, NumericT > > const &A, VectorT const &y, VectorT &x, NumericT alpha, NumericT beta)
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) ...
A small collection of sequential random number generators.
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...