• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
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
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #define BOOST_TEST_MODULE TestInteropEigen
12 #include <boost/test/unit_test.hpp>
13 
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>
19 
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
22 
23 namespace bcl = boost::compute;
24 
BOOST_AUTO_TEST_CASE(eigen)25 BOOST_AUTO_TEST_CASE(eigen)
26 {
27     Eigen::MatrixXf mat(3, 3);
28     mat << 1, 2, 3,
29            6, 5, 4,
30            7, 8, 9;
31 
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));
36 
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));
41 
42     // set matrix to zero and copy data back from gpu buffer
43     mat.setZero();
44     bcl::eigen_copy_buffer_to_matrix(vec.begin(), mat, queue);
45     BOOST_CHECK(mat.isZero() == false);
46     BOOST_CHECK_EQUAL(mat.sum(), 45);
47 }
48 
BOOST_AUTO_TEST_CASE(eigen_types)49 BOOST_AUTO_TEST_CASE(eigen_types)
50 {
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);
55 }
56 
BOOST_AUTO_TEST_CASE(multiply_matrix4)57 BOOST_AUTO_TEST_CASE(multiply_matrix4)
58 {
59     std::vector<Eigen::Vector4f> host_vectors;
60     std::vector<Eigen::Matrix4f> host_matrices;
61 
62     Eigen::Matrix4f matrix;
63     matrix << 1, 2, 0, 3,
64               2, 1, 2, 0,
65               0, 3, 1, 2,
66               2, 0, 2, 1;
67 
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));
72 
73     // store the eigen 4x4 matrix as a float16
74     bcl::float16_ M =
75         bcl::eigen_matrix4f_to_float16(matrix);
76 
77     // returns the result of M*x
78     BOOST_COMPUTE_CLOSURE(Eigen::Vector4f, transform4x4, (const Eigen::Vector4f x), (M),
79     {
80         float4 r;
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);
85         return r;
86     });
87 
88     bcl::vector<Eigen::Vector4f> vectors(4, context);
89     bcl::vector<Eigen::Vector4f> results(4, context);
90 
91     bcl::copy(host_vectors.begin(), host_vectors.end(), vectors.begin(), queue);
92 
93     bcl::transform(
94         vectors.begin(), vectors.end(), results.begin(), transform4x4, queue
95     );
96 
97     std::vector<Eigen::Vector4f> host_results(4);
98     bcl::copy(results.begin(), results.end(), host_results.begin(), queue);
99 
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]);
104 }
105 
106 BOOST_AUTO_TEST_SUITE_END()
107