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
57 template<
typename ScalarType>
62 return (s1 - s2) /
std::max(std::fabs(s1), std::fabs(s2));
68 template<
typename ScalarType>
73 return (s1 - s2) /
std::max(std::fabs(s1), std::fabs(s2));
79 template<
typename ScalarType>
84 return (s1 - s2) /
std::max(std::fabs(s1), std::fabs(s2));
90 template<
typename ScalarType,
typename ViennaCLVectorType>
91 ScalarType diff(ublas::vector<ScalarType>
const &
v1, ViennaCLVectorType
const & vcl_vec)
93 ublas::vector<ScalarType> v2_cpu(vcl_vec.size());
97 for (
unsigned int i=0;i<v1.size(); ++i)
99 if (
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) > 0 )
100 v2_cpu[i] = std::fabs(v2_cpu[i] - v1[i]) /
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) );
108 template<
typename ScalarType,
typename ViennaCLVectorType>
109 ScalarType diff(ublas::vector_slice<ublas::vector<ScalarType> >
const &
v1, ViennaCLVectorType
const & vcl_vec)
111 ublas::vector<ScalarType> v2_cpu(vcl_vec.size());
115 for (
unsigned int i=0;i<
v1.size(); ++i)
117 if (
std::max( std::fabs(v2_cpu[i]), std::fabs(
v1[i]) ) > 0 )
118 v2_cpu[i] = std::fabs(v2_cpu[i] -
v1[i]) /
std::max( std::fabs(v2_cpu[i]), std::fabs(
v1[i]) );
127 template<
typename T1,
typename T2>
128 int check(T1
const & t1, T2
const & t2,
double epsilon)
130 int retval = EXIT_SUCCESS;
132 double temp = std::fabs(
diff(t1, t2));
135 std::cout <<
"# Error! Relative difference: " << temp << std::endl;
136 retval = EXIT_FAILURE;
145 template<
typename NumericT,
typename Epsilon,
146 typename UblasVectorType1,
typename UblasVectorType2,
typename UblasVectorType3,
typename UblasVectorType4,
147 typename ViennaCLVectorType1,
typename ViennaCLVectorType2,
typename ViennaCLVectorType3,
typename ViennaCLVectorType4 >
148 int test(Epsilon
const& epsilon,
149 UblasVectorType1 & ublas_v1, UblasVectorType2 & ublas_v2, UblasVectorType3 & ublas_v3, UblasVectorType4 & ublas_v4,
150 ViennaCLVectorType1 & vcl_v1, ViennaCLVectorType2 & vcl_v2, ViennaCLVectorType3 & vcl_v3, ViennaCLVectorType4 & vcl_v4)
152 int retval = EXIT_SUCCESS;
154 for (std::size_t i=0; i<ublas_v1.size(); ++i)
156 ublas_v1[i] = NumericT(1.0) + random<NumericT>();
157 ublas_v2[i] = NumericT(1.0) + random<NumericT>();
158 ublas_v3[i] = NumericT(1.0) + random<NumericT>();
159 ublas_v4[i] = NumericT(1.0) + random<NumericT>();
167 std::cout <<
"Checking for successful copy..." << std::endl;
168 if (
check(ublas_v1, vcl_v1, epsilon) != EXIT_SUCCESS)
170 if (
check(ublas_v2, vcl_v2, epsilon) != EXIT_SUCCESS)
172 if (
check(ublas_v3, vcl_v3, epsilon) != EXIT_SUCCESS)
174 if (
check(ublas_v4, vcl_v4, epsilon) != EXIT_SUCCESS)
177 ublas::vector<NumericT> ref_result = ublas::scalar_vector<NumericT>(40, 0.0);
180 std::cout <<
"Testing inner_prod with two vectors..." << std::endl;
184 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
186 std::cout << ref_result << std::endl;
187 std::cout << result << std::endl;
194 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
196 std::cout << ref_result << std::endl;
197 std::cout << result << std::endl;
202 std::cout <<
"Testing inner_prod with three vectors..." << std::endl;
207 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
209 std::cout << ref_result << std::endl;
210 std::cout << result << std::endl;
218 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
220 std::cout << ref_result << std::endl;
221 std::cout << result << std::endl;
225 std::cout <<
"Testing inner_prod with four vectors..." << std::endl;
231 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
233 std::cout << ref_result << std::endl;
234 std::cout << result << std::endl;
243 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
245 std::cout << ref_result << std::endl;
246 std::cout << result << std::endl;
250 std::cout <<
"Testing inner_prod with five vectors..." << std::endl;
257 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
259 std::cout << ref_result << std::endl;
260 std::cout << result << std::endl;
270 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
272 std::cout << ref_result << std::endl;
273 std::cout << result << std::endl;
278 std::cout <<
"Testing inner_prod with eight vectors..." << std::endl;
287 std::vector<viennacl::vector_base<NumericT>
const *> vecs1(8);
298 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
300 std::cout << ref_result << std::endl;
301 std::cout << result << std::endl;
313 std::vector<viennacl::vector_base<NumericT>
const *> vecs2(8);
324 if (
check(ref_result, result, epsilon) != EXIT_SUCCESS)
326 std::cout << ref_result << std::endl;
327 std::cout << result << std::endl;
337 template<
typename NumericT,
typename Epsilon >
338 int test(Epsilon
const& epsilon)
340 int retval = EXIT_SUCCESS;
341 std::size_t
size = 8 * 1337;
343 std::cout <<
"Running tests for vector of size " << size << std::endl;
348 ublas::vector<NumericT> ublas_full_vec1(size);
349 ublas::vector<NumericT> ublas_full_vec2(ublas_full_vec1.size());
351 for (std::size_t i=0; i<ublas_full_vec1.size(); ++i)
353 ublas_full_vec1[i] = NumericT(1.0) + random<NumericT>();
354 ublas_full_vec2[i] = NumericT(1.0) + random<NumericT>();
357 ublas::slice s1( ublas_full_vec1.size() / 8, 3, ublas_full_vec1.size() / 8);
358 ublas::slice s2(2 * ublas_full_vec2.size() / 8, 1, ublas_full_vec2.size() / 8);
359 ublas::slice s3(4 * ublas_full_vec1.size() / 8, 2, ublas_full_vec1.size() / 8);
360 ublas::slice s4(3 * ublas_full_vec2.size() / 8, 4, ublas_full_vec2.size() / 8);
361 ublas::vector_slice< ublas::vector<NumericT> > ublas_slice_vec1(ublas_full_vec1,
s1);
362 ublas::vector_slice< ublas::vector<NumericT> > ublas_slice_vec2(ublas_full_vec2,
s2);
363 ublas::vector_slice< ublas::vector<NumericT> > ublas_slice_vec3(ublas_full_vec1, s3);
364 ublas::vector_slice< ublas::vector<NumericT> > ublas_slice_vec4(ublas_full_vec2, s4);
372 viennacl::fast_copy(ublas_full_vec1.begin(), ublas_full_vec1.end(), vcl_full_vec1.begin());
373 viennacl::copy (ublas_full_vec2.begin(), ublas_full_vec2.end(), vcl_full_vec2.begin());
375 viennacl::slice vcl_s1( vcl_full_vec1.size() / 8, 3, vcl_full_vec1.size() / 8);
376 viennacl::slice vcl_s2(2 * vcl_full_vec2.size() / 8, 1, vcl_full_vec2.size() / 8);
377 viennacl::slice vcl_s3(4 * vcl_full_vec1.size() / 8, 2, vcl_full_vec1.size() / 8);
378 viennacl::slice vcl_s4(3 * vcl_full_vec2.size() / 8, 4, vcl_full_vec2.size() / 8);
389 ublas::vector<NumericT> ublas_short_vec1(ublas_slice_vec1);
390 ublas::vector<NumericT> ublas_short_vec2(ublas_slice_vec2);
391 ublas::vector<NumericT> ublas_short_vec3 = ublas_slice_vec2 + ublas_slice_vec1;
392 ublas::vector<NumericT> ublas_short_vec4 = ublas_short_vec1 + ublas_slice_vec2;
394 std::cout <<
"Testing creation of vectors from slice..." << std::endl;
395 if (
check(ublas_short_vec1, vcl_short_vec1, epsilon) != EXIT_SUCCESS)
397 if (
check(ublas_short_vec2, vcl_short_vec2, epsilon) != EXIT_SUCCESS)
399 if (
check(ublas_short_vec3, vcl_short_vec3, epsilon) != EXIT_SUCCESS)
401 if (
check(ublas_short_vec4, vcl_short_vec4, epsilon) != EXIT_SUCCESS)
409 std::cout <<
" ** [vector|vector|vector|vector] **" << std::endl;
410 retval = test<NumericT>(epsilon,
411 ublas_short_vec1, ublas_short_vec2, ublas_short_vec2, ublas_short_vec2,
412 vcl_short_vec1, vcl_short_vec2, vcl_short_vec3, vcl_short_vec4);
413 if (retval != EXIT_SUCCESS)
416 std::cout <<
" ** [vector|vector|vector|slice] **" << std::endl;
417 retval = test<NumericT>(epsilon,
418 ublas_short_vec1, ublas_short_vec2, ublas_short_vec2, ublas_slice_vec2,
419 vcl_short_vec1, vcl_short_vec2, vcl_short_vec3, vcl_slice_vec4);
420 if (retval != EXIT_SUCCESS)
423 std::cout <<
" ** [vector|vector|slice|vector] **" << std::endl;
424 retval = test<NumericT>(epsilon,
425 ublas_short_vec1, ublas_short_vec2, ublas_slice_vec2, ublas_short_vec2,
426 vcl_short_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_short_vec4);
427 if (retval != EXIT_SUCCESS)
430 std::cout <<
" ** [vector|vector|slice|slice] **" << std::endl;
431 retval = test<NumericT>(epsilon,
432 ublas_short_vec1, ublas_short_vec2, ublas_slice_vec2, ublas_slice_vec2,
433 vcl_short_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_slice_vec4);
434 if (retval != EXIT_SUCCESS)
437 std::cout <<
" ** [vector|slice|vector|vector] **" << std::endl;
438 retval = test<NumericT>(epsilon,
439 ublas_short_vec1, ublas_slice_vec2, ublas_short_vec2, ublas_short_vec2,
440 vcl_short_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_short_vec4);
441 if (retval != EXIT_SUCCESS)
444 std::cout <<
" ** [vector|slice|vector|slice] **" << std::endl;
445 retval = test<NumericT>(epsilon,
446 ublas_short_vec1, ublas_slice_vec2, ublas_short_vec2, ublas_slice_vec2,
447 vcl_short_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_slice_vec4);
448 if (retval != EXIT_SUCCESS)
451 std::cout <<
" ** [vector|slice|slice|vector] **" << std::endl;
452 retval = test<NumericT>(epsilon,
453 ublas_short_vec1, ublas_slice_vec2, ublas_slice_vec2, ublas_short_vec2,
454 vcl_short_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_short_vec4);
455 if (retval != EXIT_SUCCESS)
458 std::cout <<
" ** [vector|slice|slice|slice] **" << std::endl;
459 retval = test<NumericT>(epsilon,
460 ublas_short_vec1, ublas_slice_vec2, ublas_slice_vec2, ublas_slice_vec2,
461 vcl_short_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_slice_vec4);
462 if (retval != EXIT_SUCCESS)
469 std::cout <<
" ** [slice|vector|vector|vector] **" << std::endl;
470 retval = test<NumericT>(epsilon,
471 ublas_slice_vec1, ublas_short_vec2, ublas_short_vec2, ublas_short_vec2,
472 vcl_slice_vec1, vcl_short_vec2, vcl_short_vec3, vcl_short_vec4);
473 if (retval != EXIT_SUCCESS)
476 std::cout <<
" ** [slice|vector|vector|slice] **" << std::endl;
477 retval = test<NumericT>(epsilon,
478 ublas_slice_vec1, ublas_short_vec2, ublas_short_vec2, ublas_slice_vec2,
479 vcl_slice_vec1, vcl_short_vec2, vcl_short_vec3, vcl_slice_vec4);
480 if (retval != EXIT_SUCCESS)
483 std::cout <<
" ** [slice|vector|slice|vector] **" << std::endl;
484 retval = test<NumericT>(epsilon,
485 ublas_slice_vec1, ublas_short_vec2, ublas_slice_vec2, ublas_short_vec2,
486 vcl_slice_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_short_vec4);
487 if (retval != EXIT_SUCCESS)
490 std::cout <<
" ** [slice|vector|slice|slice] **" << std::endl;
491 retval = test<NumericT>(epsilon,
492 ublas_slice_vec1, ublas_short_vec2, ublas_slice_vec2, ublas_slice_vec2,
493 vcl_slice_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_slice_vec4);
494 if (retval != EXIT_SUCCESS)
497 std::cout <<
" ** [slice|slice|vector|vector] **" << std::endl;
498 retval = test<NumericT>(epsilon,
499 ublas_slice_vec1, ublas_slice_vec2, ublas_short_vec2, ublas_short_vec2,
500 vcl_slice_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_short_vec4);
501 if (retval != EXIT_SUCCESS)
504 std::cout <<
" ** [slice|slice|vector|slice] **" << std::endl;
505 retval = test<NumericT>(epsilon,
506 ublas_slice_vec1, ublas_slice_vec2, ublas_short_vec2, ublas_slice_vec2,
507 vcl_slice_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_slice_vec4);
508 if (retval != EXIT_SUCCESS)
511 std::cout <<
" ** [slice|slice|slice|vector] **" << std::endl;
512 retval = test<NumericT>(epsilon,
513 ublas_slice_vec1, ublas_slice_vec2, ublas_slice_vec2, ublas_short_vec2,
514 vcl_slice_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_short_vec4);
515 if (retval != EXIT_SUCCESS)
518 std::cout <<
" ** [slice|slice|slice|slice] **" << std::endl;
519 retval = test<NumericT>(epsilon,
520 ublas_slice_vec1, ublas_slice_vec2, ublas_slice_vec2, ublas_slice_vec2,
521 vcl_slice_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_slice_vec4);
522 if (retval != EXIT_SUCCESS)
535 std::cout << std::endl;
536 std::cout <<
"----------------------------------------------" << std::endl;
537 std::cout <<
"----------------------------------------------" << std::endl;
538 std::cout <<
"## Test :: Vector multiple inner products" << std::endl;
539 std::cout <<
"----------------------------------------------" << std::endl;
540 std::cout <<
"----------------------------------------------" << std::endl;
541 std::cout << std::endl;
543 int retval = EXIT_SUCCESS;
545 std::cout << std::endl;
546 std::cout <<
"----------------------------------------------" << std::endl;
547 std::cout << std::endl;
549 typedef float NumericT;
550 NumericT epsilon =
static_cast<NumericT
>(1.0E-4);
551 std::cout <<
"# Testing setup:" << std::endl;
552 std::cout <<
" eps: " << epsilon << std::endl;
553 std::cout <<
" numeric: float" << std::endl;
554 retval = test<NumericT>(epsilon);
555 if ( retval == EXIT_SUCCESS )
556 std::cout <<
"# Test passed" << std::endl;
560 std::cout << std::endl;
561 std::cout <<
"----------------------------------------------" << std::endl;
562 std::cout << std::endl;
563 #ifdef VIENNACL_WITH_OPENCL
568 typedef double NumericT;
569 NumericT epsilon = 1.0E-12;
570 std::cout <<
"# Testing setup:" << std::endl;
571 std::cout <<
" eps: " << epsilon << std::endl;
572 std::cout <<
" numeric: double" << std::endl;
573 retval = test<NumericT>(epsilon);
574 if ( retval == EXIT_SUCCESS )
575 std::cout <<
"# Test passed" << std::endl;
579 std::cout << std::endl;
580 std::cout <<
"----------------------------------------------" << std::endl;
581 std::cout << std::endl;
584 std::cout << std::endl;
585 std::cout <<
"------- Test completed --------" << std::endl;
586 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) ...
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)