test_inplace_reduce.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 TestInplaceReduce
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/functional.hpp>
  15. #include <boost/compute/algorithm/iota.hpp>
  16. #include <boost/compute/algorithm/detail/inplace_reduce.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include "quirks.hpp"
  19. #include "context_setup.hpp"
  20. BOOST_AUTO_TEST_CASE(sum_int)
  21. {
  22. if(is_apple_cpu_device(device)) {
  23. std::cerr
  24. << "skipping all inplace_reduce tests due to Apple platform"
  25. << " behavior when local memory is used on a CPU device"
  26. << std::endl;
  27. return;
  28. }
  29. int data[] = { 1, 5, 3, 4, 9, 3, 5, 3 };
  30. boost::compute::vector<int> vector(data, data + 8, queue);
  31. boost::compute::detail::inplace_reduce(vector.begin(),
  32. vector.end(),
  33. boost::compute::plus<int>(),
  34. queue);
  35. queue.finish();
  36. BOOST_CHECK_EQUAL(int(vector[0]), int(33));
  37. vector.assign(data, data + 8);
  38. vector.push_back(3);
  39. boost::compute::detail::inplace_reduce(vector.begin(),
  40. vector.end(),
  41. boost::compute::plus<int>(),
  42. queue);
  43. queue.finish();
  44. BOOST_CHECK_EQUAL(int(vector[0]), int(36));
  45. }
  46. BOOST_AUTO_TEST_CASE(multiply_int)
  47. {
  48. if(is_apple_cpu_device(device)) {
  49. return;
  50. }
  51. int data[] = { 1, 5, 3, 4, 9, 3, 5, 3 };
  52. boost::compute::vector<int> vector(data, data + 8, queue);
  53. boost::compute::detail::inplace_reduce(vector.begin(),
  54. vector.end(),
  55. boost::compute::multiplies<int>(),
  56. queue);
  57. queue.finish();
  58. BOOST_CHECK_EQUAL(int(vector[0]), int(24300));
  59. vector.assign(data, data + 8);
  60. vector.push_back(3);
  61. boost::compute::detail::inplace_reduce(vector.begin(),
  62. vector.end(),
  63. boost::compute::multiplies<int>(),
  64. queue);
  65. queue.finish();
  66. BOOST_CHECK_EQUAL(int(vector[0]), int(72900));
  67. }
  68. BOOST_AUTO_TEST_CASE(reduce_iota)
  69. {
  70. if(is_apple_cpu_device(device)) {
  71. return;
  72. }
  73. // 1 value
  74. boost::compute::vector<int> vector(1, context);
  75. boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
  76. boost::compute::detail::inplace_reduce(vector.begin(),
  77. vector.end(),
  78. boost::compute::plus<int>(),
  79. queue);
  80. queue.finish();
  81. BOOST_CHECK_EQUAL(int(vector[0]), int(0));
  82. // 1000 values
  83. vector.resize(1000);
  84. boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
  85. boost::compute::detail::inplace_reduce(vector.begin(),
  86. vector.end(),
  87. boost::compute::plus<int>(),
  88. queue);
  89. queue.finish();
  90. BOOST_CHECK_EQUAL(int(vector[0]), int(499500));
  91. // 2499 values
  92. vector.resize(2499);
  93. boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
  94. boost::compute::detail::inplace_reduce(vector.begin(),
  95. vector.end(),
  96. boost::compute::plus<int>(),
  97. queue);
  98. queue.finish();
  99. BOOST_CHECK_EQUAL(int(vector[0]), int(3121251));
  100. // 4096 values
  101. vector.resize(4096);
  102. boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
  103. boost::compute::detail::inplace_reduce(vector.begin(),
  104. vector.end(),
  105. boost::compute::plus<int>(),
  106. queue);
  107. queue.finish();
  108. BOOST_CHECK_EQUAL(int(vector[0]), int(8386560));
  109. // 5000 values
  110. vector.resize(5000);
  111. boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
  112. boost::compute::detail::inplace_reduce(vector.begin(),
  113. vector.end(),
  114. boost::compute::plus<int>(),
  115. queue);
  116. queue.finish();
  117. BOOST_CHECK_EQUAL(int(vector[0]), int(12497500));
  118. }
  119. BOOST_AUTO_TEST_SUITE_END()