31 #include <boost/numeric/ublas/io.hpp>
32 #include <boost/numeric/ublas/triangular.hpp>
33 #include <boost/numeric/ublas/matrix_sparse.hpp>
34 #include <boost/numeric/ublas/matrix.hpp>
35 #include <boost/numeric/ublas/matrix_proxy.hpp>
36 #include <boost/numeric/ublas/lu.hpp>
37 #include <boost/numeric/ublas/io.hpp>
44 #define VIENNACL_WITH_UBLAS 1
64 template<
typename ScalarType>
69 return (s1 - s2) /
std::max(std::fabs(s1), std::fabs(s2));
73 template<
typename ScalarType>
76 ublas::vector<ScalarType> v2_cpu(v2.
size());
80 for (std::size_t i=0;i<v1.size(); ++i)
82 if (
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) > 0 )
83 v2_cpu[i] = std::fabs(v2_cpu[i] - v1[i]) /
std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) );
92 template<
typename ScalarType,
typename VCLMatrixType>
95 ublas::matrix<ScalarType> mat2_cpu(mat2.size1(), mat2.size2());
101 for (
unsigned int i = 0; i < mat2_cpu.size1(); ++i)
103 for (
unsigned int j = 0; j < mat2_cpu.size2(); ++j)
105 act = std::fabs(mat2_cpu(i,j) - mat1(i,j)) /
std::max( std::fabs(mat2_cpu(i, j)), std::fabs(mat1(i,j)) );
124 template<
typename NumericT,
typename Epsilon,
125 typename ReferenceMatrixTypeA,
typename ReferenceMatrixTypeB,
typename ReferenceMatrixTypeC,
126 typename MatrixTypeA,
typename MatrixTypeB,
typename MatrixTypeC>
129 ReferenceMatrixTypeA
const & A, ReferenceMatrixTypeA
const & A_trans,
130 ReferenceMatrixTypeB
const & B, ReferenceMatrixTypeB
const & B_trans,
131 ReferenceMatrixTypeC & C,
133 MatrixTypeA
const & vcl_A, MatrixTypeA
const & vcl_A_trans,
134 MatrixTypeB
const & vcl_B, MatrixTypeB
const & vcl_B_trans,
138 int retval = EXIT_SUCCESS;
148 act_diff = std::fabs(
diff(C, vcl_C));
150 if ( act_diff > epsilon )
152 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
153 std::cout <<
" diff: " << act_diff << std::endl;
154 retval = EXIT_FAILURE;
157 std::cout <<
"Test C = A * B passed!" << std::endl;
165 act_diff = std::fabs(
diff(C, vcl_C));
167 if ( act_diff > epsilon )
169 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
170 std::cout <<
" diff: " << act_diff << std::endl;
171 retval = EXIT_FAILURE;
174 std::cout <<
"Test C += A * B passed!" << std::endl;
181 act_diff = std::fabs(
diff(C, vcl_C));
183 if ( act_diff > epsilon )
185 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
186 std::cout <<
" diff: " << act_diff << std::endl;
187 retval = EXIT_FAILURE;
190 std::cout <<
"Test C -= A * B passed!" << std::endl;
202 act_diff = std::fabs(
diff(C, vcl_C));
204 if ( act_diff > epsilon )
206 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
207 std::cout <<
" diff: " << act_diff << std::endl;
208 retval = EXIT_FAILURE;
211 std::cout <<
"Test C = A * trans(B) passed!" << std::endl;
219 act_diff = std::fabs(
diff(C, vcl_C));
221 if ( act_diff > epsilon )
223 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
224 std::cout <<
" diff: " << act_diff << std::endl;
225 retval = EXIT_FAILURE;
228 std::cout <<
"Test C += A * trans(B) passed!" << std::endl;
236 act_diff = std::fabs(
diff(C, vcl_C));
238 if ( act_diff > epsilon )
240 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
241 std::cout <<
" diff: " << act_diff << std::endl;
242 retval = EXIT_FAILURE;
245 std::cout <<
"Test C -= A * trans(B) passed!" << std::endl;
255 act_diff = std::fabs(
diff(C, vcl_C));
257 if ( act_diff > epsilon )
259 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
260 std::cout <<
" diff: " << act_diff << std::endl;
261 retval = EXIT_FAILURE;
264 std::cout <<
"Test C = trans(A) * B passed!" << std::endl;
272 act_diff = std::fabs(
diff(C, vcl_C));
274 if ( act_diff > epsilon )
276 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
277 std::cout <<
" diff: " << act_diff << std::endl;
278 retval = EXIT_FAILURE;
281 std::cout <<
"Test C += trans(A) * B passed!" << std::endl;
289 act_diff = std::fabs(
diff(C, vcl_C));
291 if ( act_diff > epsilon )
293 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
294 std::cout <<
" diff: " << act_diff << std::endl;
295 retval = EXIT_FAILURE;
298 std::cout <<
"Test C -= trans(A) * B passed!" << std::endl;
310 act_diff = std::fabs(
diff(C, vcl_C));
312 if ( act_diff > epsilon )
314 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
315 std::cout <<
" diff: " << act_diff << std::endl;
316 retval = EXIT_FAILURE;
319 std::cout <<
"Test C = trans(A) * trans(B) passed!" << std::endl;
326 act_diff = std::fabs(
diff(C, vcl_C));
328 if ( act_diff > epsilon )
330 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
331 std::cout <<
" diff: " << act_diff << std::endl;
332 retval = EXIT_FAILURE;
335 std::cout <<
"Test C += trans(A) * trans(B) passed!" << std::endl;
343 act_diff = std::fabs(
diff(C, vcl_C));
345 if ( act_diff > epsilon )
347 std::cout <<
"# Error at operation: matrix-matrix product" << std::endl;
348 std::cout <<
" diff: " << act_diff << std::endl;
349 retval = EXIT_FAILURE;
352 std::cout <<
"Test C -= trans(A) * trans(B) passed!" << std::endl;
362 template<
typename NumericT,
typename F_A,
typename F_B,
typename F_C,
typename Epsilon >
369 std::size_t matrix_size1 = 29;
370 std::size_t matrix_size2 = 47;
371 std::size_t matrix_size3 = 33;
382 ublas::matrix<NumericT> A(matrix_size1, matrix_size2);
383 ublas::matrix<NumericT> big_A = ublas::scalar_matrix<NumericT>(4*matrix_size1, 4*matrix_size2,
NumericT(3.1415));
385 ublas::matrix<NumericT> B(matrix_size2, matrix_size3);
386 ublas::matrix<NumericT> big_B = ublas::scalar_matrix<NumericT>(4*matrix_size2, 4*matrix_size3,
NumericT(42.0));
388 ublas::matrix<NumericT> C(matrix_size1, matrix_size3);
391 for (
unsigned int i = 0; i < A.size1(); ++i)
392 for (
unsigned int j = 0; j < A.size2(); ++j)
393 A(i,j) =
static_cast<NumericT>(0.1) * randomNumber();
394 for (
unsigned int i = 0; i < B.size1(); ++i)
395 for (
unsigned int j = 0; j < B.size2(); ++j)
396 B(i,j) =
static_cast<NumericT>(0.1) * randomNumber();
398 ublas::matrix<NumericT> A_trans =
trans(A);
399 ublas::matrix<NumericT> big_A_trans =
trans(big_A);
401 ublas::matrix<NumericT> B_trans =
trans(B);
402 ublas::matrix<NumericT> big_B_trans =
trans(big_B);
488 std::cout <<
"--- Part 1: Testing matrix-matrix products ---" << std::endl;
496 std::cout <<
"Now using A=matrix, B=matrix, C=matrix" << std::endl;
497 ret = test_prod<NumericT>(epsilon,
498 A, A_trans, B, B_trans, C,
502 if (ret != EXIT_SUCCESS)
508 std::cout <<
"Now using A=matrix, B=matrix, C=range" << std::endl;
509 ret = test_prod<NumericT>(epsilon,
510 A, A_trans, B, B_trans, C,
514 if (ret != EXIT_SUCCESS)
519 std::cout <<
"Now using A=matrix, B=matrix, C=slice" << std::endl;
520 ret = test_prod<NumericT>(epsilon,
521 A, A_trans, B, B_trans, C,
525 if (ret != EXIT_SUCCESS)
532 std::cout <<
"Now using A=matrix, B=range, C=matrix" << std::endl;
533 ret = test_prod<NumericT>(epsilon,
534 A, A_trans, B, B_trans, C,
536 vcl_range_B, vcl_range_B_trans,
538 if (ret != EXIT_SUCCESS)
544 std::cout <<
"Now using A=matrix, B=range, C=range" << std::endl;
545 ret = test_prod<NumericT>(epsilon,
546 A, A_trans, B, B_trans, C,
548 vcl_range_B, vcl_range_B_trans,
550 if (ret != EXIT_SUCCESS)
555 std::cout <<
"Now using A=matrix, B=range, C=slice" << std::endl;
556 ret = test_prod<NumericT>(epsilon,
557 A, A_trans, B, B_trans, C,
559 vcl_range_B, vcl_range_B_trans,
561 if (ret != EXIT_SUCCESS)
567 std::cout <<
"Now using A=matrix, B=slice, C=matrix" << std::endl;
568 ret = test_prod<NumericT>(epsilon,
569 A, A_trans, B, B_trans, C,
571 vcl_slice_B, vcl_slice_B_trans,
573 if (ret != EXIT_SUCCESS)
579 std::cout <<
"Now using A=matrix, B=slice, C=range" << std::endl;
580 ret = test_prod<NumericT>(epsilon,
581 A, A_trans, B, B_trans, C,
583 vcl_slice_B, vcl_slice_B_trans,
585 if (ret != EXIT_SUCCESS)
590 std::cout <<
"Now using A=matrix, B=slice, C=slice" << std::endl;
591 ret = test_prod<NumericT>(epsilon,
592 A, A_trans, B, B_trans, C,
594 vcl_slice_B, vcl_slice_B_trans,
596 if (ret != EXIT_SUCCESS)
606 std::cout <<
"Now using A=range, B=matrix, C=matrix" << std::endl;
607 ret = test_prod<NumericT>(epsilon,
608 A, A_trans, B, B_trans, C,
609 vcl_range_A, vcl_range_A_trans,
612 if (ret != EXIT_SUCCESS)
618 std::cout <<
"Now using A=range, B=matrix, C=range" << std::endl;
619 ret = test_prod<NumericT>(epsilon,
620 A, A_trans, B, B_trans, C,
621 vcl_range_A, vcl_range_A_trans,
624 if (ret != EXIT_SUCCESS)
629 std::cout <<
"Now using A=range, B=matrix, C=slice" << std::endl;
630 ret = test_prod<NumericT>(epsilon,
631 A, A_trans, B, B_trans, C,
632 vcl_range_A, vcl_range_A_trans,
635 if (ret != EXIT_SUCCESS)
642 std::cout <<
"Now using A=range, B=range, C=matrix" << std::endl;
643 ret = test_prod<NumericT>(epsilon,
644 A, A_trans, B, B_trans, C,
645 vcl_range_A, vcl_range_A_trans,
646 vcl_range_B, vcl_range_B_trans,
648 if (ret != EXIT_SUCCESS)
654 std::cout <<
"Now using A=range, B=range, C=range" << std::endl;
655 ret = test_prod<NumericT>(epsilon,
656 A, A_trans, B, B_trans, C,
657 vcl_range_A, vcl_range_A_trans,
658 vcl_range_B, vcl_range_B_trans,
660 if (ret != EXIT_SUCCESS)
665 std::cout <<
"Now using A=range, B=range, C=slice" << std::endl;
666 ret = test_prod<NumericT>(epsilon,
667 A, A_trans, B, B_trans, C,
668 vcl_range_A, vcl_range_A_trans,
669 vcl_range_B, vcl_range_B_trans,
671 if (ret != EXIT_SUCCESS)
677 std::cout <<
"Now using A=range, B=slice, C=matrix" << std::endl;
678 ret = test_prod<NumericT>(epsilon,
679 A, A_trans, B, B_trans, C,
680 vcl_range_A, vcl_range_A_trans,
681 vcl_slice_B, vcl_slice_B_trans,
683 if (ret != EXIT_SUCCESS)
689 std::cout <<
"Now using A=range, B=slice, C=range" << std::endl;
690 ret = test_prod<NumericT>(epsilon,
691 A, A_trans, B, B_trans, C,
692 vcl_range_A, vcl_range_A_trans,
693 vcl_slice_B, vcl_slice_B_trans,
695 if (ret != EXIT_SUCCESS)
700 std::cout <<
"Now using A=range, B=slice, C=slice" << std::endl;
701 ret = test_prod<NumericT>(epsilon,
702 A, A_trans, B, B_trans, C,
703 vcl_range_A, vcl_range_A_trans,
704 vcl_slice_B, vcl_slice_B_trans,
706 if (ret != EXIT_SUCCESS)
717 std::cout <<
"Now using A=slice, B=matrix, C=matrix" << std::endl;
718 ret = test_prod<NumericT>(epsilon,
719 A, A_trans, B, B_trans, C,
720 vcl_slice_A, vcl_slice_A_trans,
723 if (ret != EXIT_SUCCESS)
729 std::cout <<
"Now using A=slice, B=matrix, C=range" << std::endl;
730 ret = test_prod<NumericT>(epsilon,
731 A, A_trans, B, B_trans, C,
732 vcl_slice_A, vcl_slice_A_trans,
735 if (ret != EXIT_SUCCESS)
740 std::cout <<
"Now using A=slice, B=matrix, C=slice" << std::endl;
741 ret = test_prod<NumericT>(epsilon,
742 A, A_trans, B, B_trans, C,
743 vcl_slice_A, vcl_slice_A_trans,
746 if (ret != EXIT_SUCCESS)
753 std::cout <<
"Now using A=slice, B=range, C=matrix" << std::endl;
754 ret = test_prod<NumericT>(epsilon,
755 A, A_trans, B, B_trans, C,
756 vcl_slice_A, vcl_slice_A_trans,
757 vcl_range_B, vcl_range_B_trans,
759 if (ret != EXIT_SUCCESS)
765 std::cout <<
"Now using A=slice, B=range, C=range" << std::endl;
766 ret = test_prod<NumericT>(epsilon,
767 A, A_trans, B, B_trans, C,
768 vcl_slice_A, vcl_slice_A_trans,
769 vcl_range_B, vcl_range_B_trans,
771 if (ret != EXIT_SUCCESS)
776 std::cout <<
"Now using A=slice, B=range, C=slice" << std::endl;
777 ret = test_prod<NumericT>(epsilon,
778 A, A_trans, B, B_trans, C,
779 vcl_slice_A, vcl_slice_A_trans,
780 vcl_range_B, vcl_range_B_trans,
782 if (ret != EXIT_SUCCESS)
788 std::cout <<
"Now using A=slice, B=slice, C=matrix" << std::endl;
789 ret = test_prod<NumericT>(epsilon,
790 A, A_trans, B, B_trans, C,
791 vcl_slice_A, vcl_slice_A_trans,
792 vcl_slice_B, vcl_slice_B_trans,
794 if (ret != EXIT_SUCCESS)
800 std::cout <<
"Now using A=slice, B=slice, C=range" << std::endl;
801 ret = test_prod<NumericT>(epsilon,
802 A, A_trans, B, B_trans, C,
803 vcl_slice_A, vcl_slice_A_trans,
804 vcl_slice_B, vcl_slice_B_trans,
806 if (ret != EXIT_SUCCESS)
811 std::cout <<
"Now using A=slice, B=slice, C=slice" << std::endl;
812 ret = test_prod<NumericT>(epsilon,
813 A, A_trans, B, B_trans, C,
814 vcl_slice_A, vcl_slice_A_trans,
815 vcl_slice_B, vcl_slice_B_trans,
817 if (ret != EXIT_SUCCESS)
832 template<
typename NumericT,
typename Epsilon >
833 int test(Epsilon
const& epsilon)
837 std::cout <<
"///////////////////////////////////////" << std::endl;
838 std::cout <<
"/// Now testing A=row, B=row, C=row ///" << std::endl;
839 std::cout <<
"///////////////////////////////////////" << std::endl;
840 ret = test_prod<NumericT, viennacl::row_major, viennacl::row_major, viennacl::row_major>(epsilon);
841 if (ret != EXIT_SUCCESS)
844 std::cout <<
"///////////////////////////////////////" << std::endl;
845 std::cout <<
"/// Now testing A=row, B=row, C=col ///" << std::endl;
846 std::cout <<
"///////////////////////////////////////" << std::endl;
847 ret = test_prod<NumericT, viennacl::row_major, viennacl::row_major, viennacl::column_major>(epsilon);
848 if (ret != EXIT_SUCCESS)
851 std::cout <<
"///////////////////////////////////////" << std::endl;
852 std::cout <<
"/// Now testing A=row, B=col, C=row ///" << std::endl;
853 std::cout <<
"///////////////////////////////////////" << std::endl;
854 ret = test_prod<NumericT, viennacl::row_major, viennacl::column_major, viennacl::row_major>(epsilon);
855 if (ret != EXIT_SUCCESS)
858 std::cout <<
"///////////////////////////////////////" << std::endl;
859 std::cout <<
"/// Now testing A=row, B=col, C=col ///" << std::endl;
860 std::cout <<
"///////////////////////////////////////" << std::endl;
861 ret = test_prod<NumericT, viennacl::row_major, viennacl::column_major, viennacl::column_major>(epsilon);
862 if (ret != EXIT_SUCCESS)
865 std::cout <<
"///////////////////////////////////////" << std::endl;
866 std::cout <<
"/// Now testing A=col, B=row, C=row ///" << std::endl;
867 std::cout <<
"///////////////////////////////////////" << std::endl;
868 ret = test_prod<NumericT, viennacl::column_major, viennacl::row_major, viennacl::row_major>(epsilon);
869 if (ret != EXIT_SUCCESS)
872 std::cout <<
"///////////////////////////////////////" << std::endl;
873 std::cout <<
"/// Now testing A=col, B=row, C=col ///" << std::endl;
874 std::cout <<
"///////////////////////////////////////" << std::endl;
875 ret = test_prod<NumericT, viennacl::column_major, viennacl::row_major, viennacl::column_major>(epsilon);
876 if (ret != EXIT_SUCCESS)
879 std::cout <<
"///////////////////////////////////////" << std::endl;
880 std::cout <<
"/// Now testing A=col, B=col, C=row ///" << std::endl;
881 std::cout <<
"///////////////////////////////////////" << std::endl;
882 ret = test_prod<NumericT, viennacl::column_major, viennacl::column_major, viennacl::row_major>(epsilon);
883 if (ret != EXIT_SUCCESS)
886 std::cout <<
"///////////////////////////////////////" << std::endl;
887 std::cout <<
"/// Now testing A=col, B=col, C=col ///" << std::endl;
888 std::cout <<
"///////////////////////////////////////" << std::endl;
889 ret = test_prod<NumericT, viennacl::column_major, viennacl::column_major, viennacl::column_major>(epsilon);
890 if (ret != EXIT_SUCCESS)
903 std::cout << std::endl;
904 std::cout <<
"----------------------------------------------" << std::endl;
905 std::cout <<
"----------------------------------------------" << std::endl;
906 std::cout <<
"## Test :: BLAS 3 routines" << std::endl;
907 std::cout <<
"----------------------------------------------" << std::endl;
908 std::cout <<
"----------------------------------------------" << std::endl;
909 std::cout << std::endl;
911 int retval = EXIT_SUCCESS;
913 std::cout << std::endl;
914 std::cout <<
"----------------------------------------------" << std::endl;
915 std::cout << std::endl;
918 NumericT epsilon =
NumericT(1.0E-3);
919 std::cout <<
"# Testing setup:" << std::endl;
920 std::cout <<
" eps: " << epsilon << std::endl;
921 std::cout <<
" numeric: float" << std::endl;
922 retval = test<NumericT>(epsilon);
923 if ( retval == EXIT_SUCCESS )
924 std::cout <<
"# Test passed" << std::endl;
928 std::cout << std::endl;
929 std::cout <<
"----------------------------------------------" << std::endl;
930 std::cout << std::endl;
931 #ifdef VIENNACL_WITH_OPENCL
937 NumericT epsilon = 1.0E-11;
938 std::cout <<
"# Testing setup:" << std::endl;
939 std::cout <<
" eps: " << epsilon << std::endl;
940 std::cout <<
" numeric: double" << std::endl;
941 retval = test<NumericT>(epsilon);
942 if ( retval == EXIT_SUCCESS )
943 std::cout <<
"# Test passed" << std::endl;
947 std::cout << std::endl;
948 std::cout <<
"----------------------------------------------" << std::endl;
949 std::cout << std::endl;
952 std::cout << std::endl;
953 std::cout <<
"------- Test completed --------" << std::endl;
954 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...
Class for representing strided submatrices of a bigger matrix A.
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...
Implementation of the dense matrix class.
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.
A tag class representing inplace addition.
viennacl::vector< float > v1
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
int test_prod(Epsilon const &epsilon, ReferenceMatrixTypeA const &A, ReferenceMatrixTypeA const &A_trans, ReferenceMatrixTypeB const &B, ReferenceMatrixTypeB const &B_trans, ReferenceMatrixTypeC &C, MatrixTypeA const &vcl_A, MatrixTypeA const &vcl_A_trans, MatrixTypeB const &vcl_B, MatrixTypeB const &vcl_B_trans, MatrixTypeC &vcl_C)
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
ScalarType diff(ScalarType &s1, viennacl::scalar< ScalarType > &s2)
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Implementations of dense direct solvers are found here.
Proxy classes for matrices.
A tag class representing inplace subtraction.
viennacl::vector< int > v2
void prod(std::vector< std::map< IndexT, NumericT > > const &stl_A, std::vector< std::map< IndexT, NumericT > > const &stl_B, std::vector< std::map< IndexT, NumericT > > &stl_C)
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)
A range class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
int test(Epsilon const &epsilon)
Class for representing non-strided submatrices of a bigger matrix A.
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
iterator end()
Returns an iterator pointing to the end of the vector (STL like)
A slice class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Implementation of the ViennaCL scalar class.