core.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifndef BOOST_COMPUTE_INTEROP_EIGEN_EIGEN_HPP
  11. #define BOOST_COMPUTE_INTEROP_EIGEN_EIGEN_HPP
  12. #include <Eigen/Core>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/algorithm/copy_n.hpp>
  15. #include <boost/compute/iterator/buffer_iterator.hpp>
  16. #include <boost/compute/type_traits/type_name.hpp>
  17. namespace boost {
  18. namespace compute {
  19. /// Copies \p matrix to \p buffer.
  20. template<class Derived>
  21. inline void eigen_copy_matrix_to_buffer(const Eigen::PlainObjectBase<Derived> &matrix,
  22. buffer_iterator<typename Derived::Scalar> buffer,
  23. command_queue &queue = system::default_queue())
  24. {
  25. ::boost::compute::copy_n(matrix.data(), matrix.size(), buffer, queue);
  26. }
  27. /// Copies \p buffer to \p matrix.
  28. template<class Derived>
  29. inline void eigen_copy_buffer_to_matrix(const buffer_iterator<typename Derived::Scalar> buffer,
  30. Eigen::PlainObjectBase<Derived> &matrix,
  31. command_queue &queue = system::default_queue())
  32. {
  33. ::boost::compute::copy_n(buffer, matrix.size(), matrix.data(), queue);
  34. }
  35. /// Converts an \c Eigen::Matrix4f to a \c float16_.
  36. inline float16_ eigen_matrix4f_to_float16(const Eigen::Matrix4f &matrix)
  37. {
  38. float16_ result;
  39. std::memcpy(&result, matrix.data(), 16 * sizeof(float));
  40. return result;
  41. }
  42. /// Converts an \c Eigen::Matrix4d to a \c double16_.
  43. inline double16_ eigen_matrix4d_to_double16(const Eigen::Matrix4d &matrix)
  44. {
  45. double16_ result;
  46. std::memcpy(&result, matrix.data(), 16 * sizeof(double));
  47. return result;
  48. }
  49. } // end compute namespace
  50. } // end boost namespace
  51. BOOST_COMPUTE_TYPE_NAME(Eigen::Vector2i, int2)
  52. BOOST_COMPUTE_TYPE_NAME(Eigen::Vector4i, int4)
  53. BOOST_COMPUTE_TYPE_NAME(Eigen::Vector2f, float2)
  54. BOOST_COMPUTE_TYPE_NAME(Eigen::Vector4f, float4)
  55. BOOST_COMPUTE_TYPE_NAME(Eigen::Matrix2f, float8)
  56. BOOST_COMPUTE_TYPE_NAME(Eigen::Matrix4f, float16)
  57. BOOST_COMPUTE_TYPE_NAME(Eigen::Vector2d, double2)
  58. BOOST_COMPUTE_TYPE_NAME(Eigen::Vector4d, double4)
  59. BOOST_COMPUTE_TYPE_NAME(Eigen::Matrix2d, double8)
  60. BOOST_COMPUTE_TYPE_NAME(Eigen::Matrix4d, double16)
  61. #endif // BOOST_COMPUTE_INTEROP_EIGEN_EIGEN_HPP