test_transform_reduce.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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 TestTransformReduce
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/lambda.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/functional.hpp>
  15. #include <boost/compute/algorithm/transform_reduce.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include "context_setup.hpp"
  18. namespace compute = boost::compute;
  19. BOOST_AUTO_TEST_CASE(sum_abs_int_doctest)
  20. {
  21. using boost::compute::abs;
  22. using boost::compute::plus;
  23. int data[] = { 1, -2, -3, -4, 5 };
  24. compute::vector<int> vec(data, data + 5, queue);
  25. //! [sum_abs_int]
  26. int sum = 0;
  27. boost::compute::transform_reduce(
  28. vec.begin(), vec.end(), &sum, abs<int>(), plus<int>(), queue
  29. );
  30. //! [sum_abs_int]
  31. BOOST_CHECK_EQUAL(sum, 15);
  32. }
  33. BOOST_AUTO_TEST_CASE(multiply_vector_length)
  34. {
  35. float data[] = { 2.0f, 0.0f, 0.0f, 0.0f,
  36. 0.0f, 3.0f, 0.0f, 0.0f,
  37. 0.0f, 0.0f, 4.0f, 0.0f };
  38. compute::vector<compute::float4_> vector(
  39. reinterpret_cast<compute::float4_ *>(data),
  40. reinterpret_cast<compute::float4_ *>(data) + 3,
  41. queue
  42. );
  43. float product;
  44. compute::transform_reduce(
  45. vector.begin(),
  46. vector.end(),
  47. &product,
  48. compute::length<compute::float4_>(),
  49. compute::multiplies<float>(),
  50. queue
  51. );
  52. BOOST_CHECK_CLOSE(product, 24.0f, 1e-4f);
  53. }
  54. BOOST_AUTO_TEST_CASE(mean_and_std_dev)
  55. {
  56. using compute::lambda::_1;
  57. using compute::lambda::pow;
  58. float data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  59. compute::vector<float> vector(data, data + 10, queue);
  60. float sum;
  61. compute::reduce(
  62. vector.begin(),
  63. vector.end(),
  64. &sum,
  65. compute::plus<float>(),
  66. queue
  67. );
  68. float mean = sum / vector.size();
  69. BOOST_CHECK_CLOSE(mean, 5.5f, 1e-4);
  70. compute::transform_reduce(
  71. vector.begin(),
  72. vector.end(),
  73. &sum,
  74. pow(_1 - mean, 2),
  75. compute::plus<float>(),
  76. queue
  77. );
  78. float variance = sum / vector.size();
  79. BOOST_CHECK_CLOSE(variance, 8.25f, 1e-4);
  80. float std_dev = std::sqrt(variance);
  81. BOOST_CHECK_CLOSE(std_dev, 2.8722813232690143, 1e-4);
  82. }
  83. BOOST_AUTO_TEST_SUITE_END()