33 #include <boost/numeric/ublas/io.hpp>
34 #include <boost/numeric/ublas/vector.hpp>
35 #include <boost/numeric/ublas/vector_proxy.hpp>
41 #define VIENNACL_WITH_UBLAS 1
56 template<
typename ScalarType>
61 return (s1 - s2) /
std::max(std::fabs(s1), std::fabs(s2));
67 template<
typename ScalarType>
72 return (s1 - s2) /
std::max(std::fabs(s1), std::fabs(s2));
78 template<
typename ScalarType>
83 return (s1 - s2) /
std::max(std::fabs(s1), std::fabs(s2));
89 template<
typename ScalarType,
typename ViennaCLVectorType>
90 ScalarType diff(ublas::vector<ScalarType>
const &
v1, ViennaCLVectorType
const & vcl_vec)
92 ublas::vector<ScalarType> v2_cpu(vcl_vec.size());
96 for (
unsigned int i=0;i<v1.size(); ++i)
98 if (
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) > 0 )
99 v2_cpu[i] = std::fabs(v2_cpu[i] - v1[i]) /
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) );
107 template<
typename ScalarType,
typename ViennaCLVectorType>
108 ScalarType diff(ublas::vector_slice<ublas::vector<ScalarType> >
const &
v1, ViennaCLVectorType
const & vcl_vec)
110 ublas::vector<ScalarType> v2_cpu(vcl_vec.size());
114 for (
unsigned int i=0;i<
v1.size(); ++i)
116 if (
std::max( std::fabs(v2_cpu[i]), std::fabs(
v1[i]) ) > 0 )
117 v2_cpu[i] = std::fabs(v2_cpu[i] -
v1[i]) /
std::max( std::fabs(v2_cpu[i]), std::fabs(
v1[i]) );
126 template<
typename T1,
typename T2>
127 int check(T1
const & t1, T2
const & t2,
double epsilon)
129 int retval = EXIT_SUCCESS;
131 double temp = std::fabs(
diff(t1, t2));
134 std::cout <<
"# Error! Relative difference: " << temp << std::endl;
135 retval = EXIT_FAILURE;
144 template<
typename NumericT,
typename Epsilon,
145 typename UblasVectorType1,
typename UblasVectorType2,
typename UblasVectorType3,
typename UblasVectorType4,
146 typename ViennaCLVectorType1,
typename ViennaCLVectorType2,
typename ViennaCLVectorType3,
typename ViennaCLVectorType4 >
147 int test(Epsilon
const& epsilon,
148 UblasVectorType1 & ublas_v1, UblasVectorType2 & ublas_v2, UblasVectorType3 & ublas_v3, UblasVectorType4 & ublas_v4,
149 ViennaCLVectorType1 & vcl_v1, ViennaCLVectorType2 & vcl_v2, ViennaCLVectorType3 & vcl_v3, ViennaCLVectorType4 & vcl_v4)
151 int retval = EXIT_SUCCESS;
155 for (std::size_t i=0; i<ublas_v1.size(); ++i)
157 ublas_v1[i] =
NumericT(1.0) + randomNumber();
158 ublas_v2[i] =
NumericT(1.0) + randomNumber();
159 ublas_v3[i] =
NumericT(1.0) + randomNumber();
160 ublas_v4[i] =
NumericT(1.0) + randomNumber();
168 std::cout <<
"Checking for successful copy..." << std::endl;
169 if (
check(ublas_v1, vcl_v1, epsilon) != EXIT_SUCCESS)
171 if (
check(ublas_v2, vcl_v2, epsilon) != EXIT_SUCCESS)
173 if (
check(ublas_v3, vcl_v3, epsilon) != EXIT_SUCCESS)
175 if (
check(ublas_v4, vcl_v4, epsilon) != EXIT_SUCCESS)
178 ublas::vector<NumericT> ref_result = ublas::scalar_vector<NumericT>(40, 0.0);
181 std::cout <<
"Testing inner_prod with two vectors..." << std::endl;
185 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
187 std::cout << ref_result << std::endl;
188 std::cout << result << std::endl;
195 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
197 std::cout << ref_result << std::endl;
198 std::cout << result << std::endl;
203 std::cout <<
"Testing inner_prod with three vectors..." << std::endl;
208 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
210 std::cout << ref_result << std::endl;
211 std::cout << result << std::endl;
219 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
221 std::cout << ref_result << std::endl;
222 std::cout << result << std::endl;
226 std::cout <<
"Testing inner_prod with four vectors..." << std::endl;
232 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
234 std::cout << ref_result << std::endl;
235 std::cout << result << std::endl;
244 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
246 std::cout << ref_result << std::endl;
247 std::cout << result << std::endl;
251 std::cout <<
"Testing inner_prod with five vectors..." << std::endl;
258 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
260 std::cout << ref_result << std::endl;
261 std::cout << result << std::endl;
271 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
273 std::cout << ref_result << std::endl;
274 std::cout << result << std::endl;
279 std::cout <<
"Testing inner_prod with eight vectors..." << std::endl;
288 std::vector<viennacl::vector_base<NumericT>
const *> vecs1(8);
299 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
301 std::cout << ref_result << std::endl;
302 std::cout << result << std::endl;
314 std::vector<viennacl::vector_base<NumericT>
const *> vecs2(8);
325 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
327 std::cout << ref_result << std::endl;
328 std::cout << result << std::endl;
338 template<
typename NumericT,
typename Epsilon >
339 int test(Epsilon
const& epsilon)
343 int retval = EXIT_SUCCESS;
344 std::size_t
size = 8 * 1337;
346 std::cout <<
"Running tests for vector of size " << size << std::endl;
351 ublas::vector<NumericT> ublas_full_vec1(size);
352 ublas::vector<NumericT> ublas_full_vec2(ublas_full_vec1.size());
354 for (std::size_t i=0; i<ublas_full_vec1.size(); ++i)
356 ublas_full_vec1[i] =
NumericT(1.0) + randomNumber();
357 ublas_full_vec2[i] =
NumericT(1.0) + randomNumber();
360 ublas::slice s1( ublas_full_vec1.size() / 8, 3, ublas_full_vec1.size() / 8);
361 ublas::slice s2(2 * ublas_full_vec2.size() / 8, 1, ublas_full_vec2.size() / 8);
362 ublas::slice s3(4 * ublas_full_vec1.size() / 8, 2, ublas_full_vec1.size() / 8);
363 ublas::slice s4(3 * ublas_full_vec2.size() / 8, 4, ublas_full_vec2.size() / 8);
364 ublas::vector_slice< ublas::vector<NumericT> > ublas_slice_vec1(ublas_full_vec1,
s1);
365 ublas::vector_slice< ublas::vector<NumericT> > ublas_slice_vec2(ublas_full_vec2,
s2);
366 ublas::vector_slice< ublas::vector<NumericT> > ublas_slice_vec3(ublas_full_vec1, s3);
367 ublas::vector_slice< ublas::vector<NumericT> > ublas_slice_vec4(ublas_full_vec2, s4);
375 viennacl::fast_copy(ublas_full_vec1.begin(), ublas_full_vec1.end(), vcl_full_vec1.begin());
376 viennacl::copy (ublas_full_vec2.begin(), ublas_full_vec2.end(), vcl_full_vec2.begin());
378 viennacl::slice vcl_s1( vcl_full_vec1.size() / 8, 3, vcl_full_vec1.size() / 8);
379 viennacl::slice vcl_s2(2 * vcl_full_vec2.size() / 8, 1, vcl_full_vec2.size() / 8);
380 viennacl::slice vcl_s3(4 * vcl_full_vec1.size() / 8, 2, vcl_full_vec1.size() / 8);
381 viennacl::slice vcl_s4(3 * vcl_full_vec2.size() / 8, 4, vcl_full_vec2.size() / 8);
392 ublas::vector<NumericT> ublas_short_vec1(ublas_slice_vec1);
393 ublas::vector<NumericT> ublas_short_vec2(ublas_slice_vec2);
394 ublas::vector<NumericT> ublas_short_vec3 = ublas_slice_vec2 + ublas_slice_vec1;
395 ublas::vector<NumericT> ublas_short_vec4 = ublas_short_vec1 + ublas_slice_vec2;
397 std::cout <<
"Testing creation of vectors from slice..." << std::endl;
398 if (
check(ublas_short_vec1, vcl_short_vec1, epsilon) != EXIT_SUCCESS)
400 if (
check(ublas_short_vec2, vcl_short_vec2, epsilon) != EXIT_SUCCESS)
402 if (
check(ublas_short_vec3, vcl_short_vec3, epsilon) != EXIT_SUCCESS)
404 if (
check(ublas_short_vec4, vcl_short_vec4, epsilon) != EXIT_SUCCESS)
412 std::cout <<
" ** [vector|vector|vector|vector] **" << std::endl;
413 retval = test<NumericT>(epsilon,
414 ublas_short_vec1, ublas_short_vec2, ublas_short_vec2, ublas_short_vec2,
415 vcl_short_vec1, vcl_short_vec2, vcl_short_vec3, vcl_short_vec4);
416 if (retval != EXIT_SUCCESS)
419 std::cout <<
" ** [vector|vector|vector|slice] **" << std::endl;
420 retval = test<NumericT>(epsilon,
421 ublas_short_vec1, ublas_short_vec2, ublas_short_vec2, ublas_slice_vec2,
422 vcl_short_vec1, vcl_short_vec2, vcl_short_vec3, vcl_slice_vec4);
423 if (retval != EXIT_SUCCESS)
426 std::cout <<
" ** [vector|vector|slice|vector] **" << std::endl;
427 retval = test<NumericT>(epsilon,
428 ublas_short_vec1, ublas_short_vec2, ublas_slice_vec2, ublas_short_vec2,
429 vcl_short_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_short_vec4);
430 if (retval != EXIT_SUCCESS)
433 std::cout <<
" ** [vector|vector|slice|slice] **" << std::endl;
434 retval = test<NumericT>(epsilon,
435 ublas_short_vec1, ublas_short_vec2, ublas_slice_vec2, ublas_slice_vec2,
436 vcl_short_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_slice_vec4);
437 if (retval != EXIT_SUCCESS)
440 std::cout <<
" ** [vector|slice|vector|vector] **" << std::endl;
441 retval = test<NumericT>(epsilon,
442 ublas_short_vec1, ublas_slice_vec2, ublas_short_vec2, ublas_short_vec2,
443 vcl_short_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_short_vec4);
444 if (retval != EXIT_SUCCESS)
447 std::cout <<
" ** [vector|slice|vector|slice] **" << std::endl;
448 retval = test<NumericT>(epsilon,
449 ublas_short_vec1, ublas_slice_vec2, ublas_short_vec2, ublas_slice_vec2,
450 vcl_short_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_slice_vec4);
451 if (retval != EXIT_SUCCESS)
454 std::cout <<
" ** [vector|slice|slice|vector] **" << std::endl;
455 retval = test<NumericT>(epsilon,
456 ublas_short_vec1, ublas_slice_vec2, ublas_slice_vec2, ublas_short_vec2,
457 vcl_short_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_short_vec4);
458 if (retval != EXIT_SUCCESS)
461 std::cout <<
" ** [vector|slice|slice|slice] **" << std::endl;
462 retval = test<NumericT>(epsilon,
463 ublas_short_vec1, ublas_slice_vec2, ublas_slice_vec2, ublas_slice_vec2,
464 vcl_short_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_slice_vec4);
465 if (retval != EXIT_SUCCESS)
472 std::cout <<
" ** [slice|vector|vector|vector] **" << std::endl;
473 retval = test<NumericT>(epsilon,
474 ublas_slice_vec1, ublas_short_vec2, ublas_short_vec2, ublas_short_vec2,
475 vcl_slice_vec1, vcl_short_vec2, vcl_short_vec3, vcl_short_vec4);
476 if (retval != EXIT_SUCCESS)
479 std::cout <<
" ** [slice|vector|vector|slice] **" << std::endl;
480 retval = test<NumericT>(epsilon,
481 ublas_slice_vec1, ublas_short_vec2, ublas_short_vec2, ublas_slice_vec2,
482 vcl_slice_vec1, vcl_short_vec2, vcl_short_vec3, vcl_slice_vec4);
483 if (retval != EXIT_SUCCESS)
486 std::cout <<
" ** [slice|vector|slice|vector] **" << std::endl;
487 retval = test<NumericT>(epsilon,
488 ublas_slice_vec1, ublas_short_vec2, ublas_slice_vec2, ublas_short_vec2,
489 vcl_slice_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_short_vec4);
490 if (retval != EXIT_SUCCESS)
493 std::cout <<
" ** [slice|vector|slice|slice] **" << std::endl;
494 retval = test<NumericT>(epsilon,
495 ublas_slice_vec1, ublas_short_vec2, ublas_slice_vec2, ublas_slice_vec2,
496 vcl_slice_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_slice_vec4);
497 if (retval != EXIT_SUCCESS)
500 std::cout <<
" ** [slice|slice|vector|vector] **" << std::endl;
501 retval = test<NumericT>(epsilon,
502 ublas_slice_vec1, ublas_slice_vec2, ublas_short_vec2, ublas_short_vec2,
503 vcl_slice_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_short_vec4);
504 if (retval != EXIT_SUCCESS)
507 std::cout <<
" ** [slice|slice|vector|slice] **" << std::endl;
508 retval = test<NumericT>(epsilon,
509 ublas_slice_vec1, ublas_slice_vec2, ublas_short_vec2, ublas_slice_vec2,
510 vcl_slice_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_slice_vec4);
511 if (retval != EXIT_SUCCESS)
514 std::cout <<
" ** [slice|slice|slice|vector] **" << std::endl;
515 retval = test<NumericT>(epsilon,
516 ublas_slice_vec1, ublas_slice_vec2, ublas_slice_vec2, ublas_short_vec2,
517 vcl_slice_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_short_vec4);
518 if (retval != EXIT_SUCCESS)
521 std::cout <<
" ** [slice|slice|slice|slice] **" << std::endl;
522 retval = test<NumericT>(epsilon,
523 ublas_slice_vec1, ublas_slice_vec2, ublas_slice_vec2, ublas_slice_vec2,
524 vcl_slice_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_slice_vec4);
525 if (retval != EXIT_SUCCESS)
538 std::cout << std::endl;
539 std::cout <<
"----------------------------------------------" << std::endl;
540 std::cout <<
"----------------------------------------------" << std::endl;
541 std::cout <<
"## Test :: Vector multiple inner products" << std::endl;
542 std::cout <<
"----------------------------------------------" << std::endl;
543 std::cout <<
"----------------------------------------------" << std::endl;
544 std::cout << std::endl;
546 int retval = EXIT_SUCCESS;
548 std::cout << std::endl;
549 std::cout <<
"----------------------------------------------" << std::endl;
550 std::cout << std::endl;
553 NumericT epsilon =
static_cast<NumericT
>(1.0E-4);
554 std::cout <<
"# Testing setup:" << std::endl;
555 std::cout <<
" eps: " << epsilon << std::endl;
556 std::cout <<
" numeric: float" << std::endl;
557 retval = test<NumericT>(epsilon);
558 if ( retval == EXIT_SUCCESS )
559 std::cout <<
"# Test passed" << std::endl;
563 std::cout << std::endl;
564 std::cout <<
"----------------------------------------------" << std::endl;
565 std::cout << std::endl;
566 #ifdef VIENNACL_WITH_OPENCL
572 NumericT epsilon = 1.0E-12;
573 std::cout <<
"# Testing setup:" << std::endl;
574 std::cout <<
" eps: " << epsilon << std::endl;
575 std::cout <<
" numeric: double" << std::endl;
576 retval = test<NumericT>(epsilon);
577 if ( retval == EXIT_SUCCESS )
578 std::cout <<
"# Test passed" << std::endl;
582 std::cout << std::endl;
583 std::cout <<
"----------------------------------------------" << std::endl;
584 std::cout << std::endl;
587 std::cout << std::endl;
588 std::cout <<
"------- Test completed --------" << std::endl;
589 std::cout << std::endl;
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...
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
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.
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
Generic interface for the l^1-norm. See viennacl/linalg/vector_operations.hpp for implementations...
viennacl::vector< float > v1
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
int test(Epsilon const &epsilon, UblasVectorType1 &ublas_v1, UblasVectorType2 &ublas_v2, UblasVectorType3 &ublas_v3, UblasVectorType4 &ublas_v4, ViennaCLVectorType1 &vcl_v1, ViennaCLVectorType2 &vcl_v2, ViennaCLVectorType3 &vcl_v3, ViennaCLVectorType4 &vcl_v4)
Tuple class holding pointers to multiple vectors. Mainly used as a temporary object returned from vie...
Class for representing strided subvectors of a bigger vector x.
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
matrix_range< MatrixType > project(MatrixType const &A, viennacl::range const &r1, viennacl::range const &r2)
vector_tuple< ScalarT > tie(vector_base< ScalarT > const &v0, vector_base< ScalarT > const &v1)
Proxy classes for vectors.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
Represents a vector consisting of scalars 's' only, i.e. v[i] = s for all i. To be used as an initial...
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.
ScalarType diff(ScalarType const &s1, ScalarType const &s2)
int check(T1 const &t1, T2 const &t2, double epsilon)
A slice class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Generic interface for the l^infty-norm. See viennacl/linalg/vector_operations.hpp for implementations...
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)