test_interop_eigen.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #define BOOST_TEST_MODULE TestInteropEigen
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/closure.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/container/vector.hpp>
  15. #include <boost/compute/algorithm/transform.hpp>
  16. #include <boost/compute/interop/eigen.hpp>
  17. #include "check_macros.hpp"
  18. #include "context_setup.hpp"
  19. namespace bcl = boost::compute;
  20. BOOST_AUTO_TEST_CASE(eigen)
  21. {
  22. Eigen::MatrixXf mat(3, 3);
  23. mat << 1, 2, 3,
  24. 6, 5, 4,
  25. 7, 8, 9;
  26. // copy matrix to gpu buffer
  27. bcl::vector<float> vec(9, context);
  28. bcl::eigen_copy_matrix_to_buffer(mat, vec.begin(), queue);
  29. CHECK_RANGE_EQUAL(float, 9, vec, (1, 6, 7, 2, 5, 8, 3, 4, 9));
  30. // transpose matrix and then copy to gpu buffer
  31. mat = mat.transpose().eval();
  32. bcl::eigen_copy_matrix_to_buffer(mat, vec.begin(), queue);
  33. CHECK_RANGE_EQUAL(float, 9, vec, (1, 2, 3, 6, 5, 4, 7, 8, 9));
  34. // set matrix to zero and copy data back from gpu buffer
  35. mat.setZero();
  36. bcl::eigen_copy_buffer_to_matrix(vec.begin(), mat, queue);
  37. BOOST_CHECK(mat.isZero() == false);
  38. BOOST_CHECK_EQUAL(mat.sum(), 45);
  39. }
  40. BOOST_AUTO_TEST_CASE(eigen_types)
  41. {
  42. BOOST_CHECK(std::strcmp(bcl::type_name<Eigen::Vector2i>(), "int2") == 0);
  43. BOOST_CHECK(std::strcmp(bcl::type_name<Eigen::Vector2f>(), "float2") == 0);
  44. BOOST_CHECK(std::strcmp(bcl::type_name<Eigen::Vector4f>(), "float4") == 0);
  45. BOOST_CHECK(std::strcmp(bcl::type_name<Eigen::Vector4d>(), "double4") == 0);
  46. }
  47. BOOST_AUTO_TEST_CASE(multiply_matrix4)
  48. {
  49. std::vector<Eigen::Vector4f> host_vectors;
  50. std::vector<Eigen::Matrix4f> host_matrices;
  51. Eigen::Matrix4f matrix;
  52. matrix << 1, 2, 0, 3,
  53. 2, 1, 2, 0,
  54. 0, 3, 1, 2,
  55. 2, 0, 2, 1;
  56. host_vectors.push_back(Eigen::Vector4f(1, 2, 3, 4));
  57. host_vectors.push_back(Eigen::Vector4f(4, 3, 2, 1));
  58. host_vectors.push_back(Eigen::Vector4f(1, 2, 3, 4));
  59. host_vectors.push_back(Eigen::Vector4f(4, 3, 2, 1));
  60. // store the eigen 4x4 matrix as a float16
  61. bcl::float16_ M =
  62. bcl::eigen_matrix4f_to_float16(matrix);
  63. // returns the result of M*x
  64. BOOST_COMPUTE_CLOSURE(Eigen::Vector4f, transform4x4, (const Eigen::Vector4f x), (M),
  65. {
  66. float4 r;
  67. r.x = dot(M.s048c, x);
  68. r.y = dot(M.s159d, x);
  69. r.z = dot(M.s26ae, x);
  70. r.w = dot(M.s37bf, x);
  71. return r;
  72. });
  73. bcl::vector<Eigen::Vector4f> vectors(4, context);
  74. bcl::vector<Eigen::Vector4f> results(4, context);
  75. bcl::copy(host_vectors.begin(), host_vectors.end(), vectors.begin(), queue);
  76. bcl::transform(
  77. vectors.begin(), vectors.end(), results.begin(), transform4x4, queue
  78. );
  79. std::vector<Eigen::Vector4f> host_results(4);
  80. bcl::copy(results.begin(), results.end(), host_results.begin(), queue);
  81. BOOST_CHECK((matrix * host_vectors[0]) == host_results[0]);
  82. BOOST_CHECK((matrix * host_vectors[1]) == host_results[1]);
  83. BOOST_CHECK((matrix * host_vectors[2]) == host_results[2]);
  84. BOOST_CHECK((matrix * host_vectors[3]) == host_results[3]);
  85. }
  86. BOOST_AUTO_TEST_SUITE_END()