]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/compute/test/extra/test_interop_eigen.cpp
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #define BOOST_TEST_MODULE TestInteropEigen
12 #include <boost/test/unit_test.hpp>
14 #include <boost/compute/closure.hpp>
15 #include <boost/compute/system.hpp>
16 #include <boost/compute/container/vector.hpp>
17 #include <boost/compute/algorithm/transform.hpp>
18 #include <boost/compute/interop/eigen.hpp>
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
23 namespace bcl
= boost::compute
;
25 BOOST_AUTO_TEST_CASE(eigen
)
27 Eigen::MatrixXf
mat(3, 3);
32 // copy matrix to gpu buffer
33 bcl::vector
<float> vec(9, context
);
34 bcl::eigen_copy_matrix_to_buffer(mat
, vec
.begin(), queue
);
35 CHECK_RANGE_EQUAL(float, 9, vec
, (1, 6, 7, 2, 5, 8, 3, 4, 9));
37 // transpose matrix and then copy to gpu buffer
38 mat
= mat
.transpose().eval();
39 bcl::eigen_copy_matrix_to_buffer(mat
, vec
.begin(), queue
);
40 CHECK_RANGE_EQUAL(float, 9, vec
, (1, 2, 3, 6, 5, 4, 7, 8, 9));
42 // set matrix to zero and copy data back from gpu buffer
44 bcl::eigen_copy_buffer_to_matrix(vec
.begin(), mat
, queue
);
45 BOOST_CHECK(mat
.isZero() == false);
46 BOOST_CHECK_EQUAL(mat
.sum(), 45);
49 BOOST_AUTO_TEST_CASE(eigen_types
)
51 BOOST_CHECK(std::strcmp(bcl::type_name
<Eigen::Vector2i
>(), "int2") == 0);
52 BOOST_CHECK(std::strcmp(bcl::type_name
<Eigen::Vector2f
>(), "float2") == 0);
53 BOOST_CHECK(std::strcmp(bcl::type_name
<Eigen::Vector4f
>(), "float4") == 0);
54 BOOST_CHECK(std::strcmp(bcl::type_name
<Eigen::Vector4d
>(), "double4") == 0);
57 BOOST_AUTO_TEST_CASE(multiply_matrix4
)
59 std::vector
<Eigen::Vector4f
> host_vectors
;
60 std::vector
<Eigen::Matrix4f
> host_matrices
;
62 Eigen::Matrix4f matrix
;
68 host_vectors
.push_back(Eigen::Vector4f(1, 2, 3, 4));
69 host_vectors
.push_back(Eigen::Vector4f(4, 3, 2, 1));
70 host_vectors
.push_back(Eigen::Vector4f(1, 2, 3, 4));
71 host_vectors
.push_back(Eigen::Vector4f(4, 3, 2, 1));
73 // store the eigen 4x4 matrix as a float16
75 bcl::eigen_matrix4f_to_float16(matrix
);
77 // returns the result of M*x
78 BOOST_COMPUTE_CLOSURE(Eigen::Vector4f
, transform4x4
, (const Eigen::Vector4f x
), (M
),
81 r
.x
= dot(M
.s048c
, x
);
82 r
.y
= dot(M
.s159d
, x
);
83 r
.z
= dot(M
.s26ae
, x
);
84 r
.w
= dot(M
.s37bf
, x
);
88 bcl::vector
<Eigen::Vector4f
> vectors(4, context
);
89 bcl::vector
<Eigen::Vector4f
> results(4, context
);
91 bcl::copy(host_vectors
.begin(), host_vectors
.end(), vectors
.begin(), queue
);
94 vectors
.begin(), vectors
.end(), results
.begin(), transform4x4
, queue
97 std::vector
<Eigen::Vector4f
> host_results(4);
98 bcl::copy(results
.begin(), results
.end(), host_results
.begin(), queue
);
100 BOOST_CHECK((matrix
* host_vectors
[0]) == host_results
[0]);
101 BOOST_CHECK((matrix
* host_vectors
[1]) == host_results
[1]);
102 BOOST_CHECK((matrix
* host_vectors
[2]) == host_results
[2]);
103 BOOST_CHECK((matrix
* host_vectors
[3]) == host_results
[3]);
106 BOOST_AUTO_TEST_SUITE_END()