batched_determinant.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <iostream>
  11. #include <Eigen/Core>
  12. #include <Eigen/LU>
  13. #include <boost/compute/function.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/algorithm/transform.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/types/fundamental.hpp>
  18. namespace compute = boost::compute;
  19. // this example shows how to compute the determinant of many 4x4 matrices
  20. // using a determinant function and the transform() algorithm. in OpenCL the
  21. // float16 type can be used to store a 4x4 matrix and the components are laid
  22. // out in the following order:
  23. //
  24. // M = [ s0 s4 s8 sc ]
  25. // [ s1 s5 s9 sd ]
  26. // [ s2 s6 sa se ]
  27. // [ s3 s7 sb sf ]
  28. //
  29. // the input matrices are created using eigen's random matrix and then
  30. // used again at the end to verify the results of the determinant function.
  31. int main()
  32. {
  33. // get default device and setup context
  34. compute::device gpu = compute::system::default_device();
  35. compute::context context(gpu);
  36. compute::command_queue queue(context, gpu);
  37. std::cout << "device: " << gpu.name() << std::endl;
  38. size_t n = 1000;
  39. // create random 4x4 matrices on the host
  40. std::vector<Eigen::Matrix4f> matrices(n);
  41. for(size_t i = 0; i < n; i++){
  42. matrices[i] = Eigen::Matrix4f::Random();
  43. }
  44. // copy matrices to the device
  45. using compute::float16_;
  46. compute::vector<float16_> input(n, context);
  47. compute::copy(
  48. matrices.begin(), matrices.end(), input.begin(), queue
  49. );
  50. // function returning the determinant of a 4x4 matrix.
  51. BOOST_COMPUTE_FUNCTION(float, determinant4x4, (const float16_ m),
  52. {
  53. return m.s0*m.s5*m.sa*m.sf + m.s0*m.s6*m.sb*m.sd + m.s0*m.s7*m.s9*m.se +
  54. m.s1*m.s4*m.sb*m.se + m.s1*m.s6*m.s8*m.sf + m.s1*m.s7*m.sa*m.sc +
  55. m.s2*m.s4*m.s9*m.sf + m.s2*m.s5*m.sb*m.sc + m.s2*m.s7*m.s8*m.sd +
  56. m.s3*m.s4*m.sa*m.sd + m.s3*m.s5*m.s8*m.se + m.s3*m.s6*m.s9*m.sc -
  57. m.s0*m.s5*m.sb*m.se - m.s0*m.s6*m.s9*m.sf - m.s0*m.s7*m.sa*m.sd -
  58. m.s1*m.s4*m.sa*m.sf - m.s1*m.s6*m.sb*m.sc - m.s1*m.s7*m.s8*m.se -
  59. m.s2*m.s4*m.sb*m.sd - m.s2*m.s5*m.s8*m.sf - m.s2*m.s7*m.s9*m.sc -
  60. m.s3*m.s4*m.s9*m.se - m.s3*m.s5*m.sa*m.sc - m.s3*m.s6*m.s8*m.sd;
  61. });
  62. // calculate determinants on the gpu
  63. compute::vector<float> determinants(n, context);
  64. compute::transform(
  65. input.begin(), input.end(), determinants.begin(), determinant4x4, queue
  66. );
  67. // check determinants
  68. std::vector<float> host_determinants(n);
  69. compute::copy(
  70. determinants.begin(), determinants.end(), host_determinants.begin(), queue
  71. );
  72. for(size_t i = 0; i < n; i++){
  73. float det = matrices[i].determinant();
  74. if(std::abs(det - host_determinants[i]) > 1e-6){
  75. std::cerr << "error: wrong determinant at " << i << " ("
  76. << host_determinants[i] << " != " << det << ")"
  77. << std::endl;
  78. return -1;
  79. }
  80. }
  81. return 0;
  82. }