test_uniform_real_distribution.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 TestUniformRealDistribution
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/algorithm/count_if.hpp>
  15. #include <boost/compute/algorithm/transform.hpp>
  16. #include <boost/compute/function.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include <boost/compute/random/default_random_engine.hpp>
  19. #include <boost/compute/random/uniform_real_distribution.hpp>
  20. #include <boost/compute/lambda.hpp>
  21. #include <boost/compute/types/fundamental.hpp>
  22. #include "context_setup.hpp"
  23. BOOST_AUTO_TEST_CASE(uniform_real_distribution_doctest)
  24. {
  25. using boost::compute::lambda::_1;
  26. boost::compute::vector<float> vec(128, context);
  27. //! [generate]
  28. // initialize the default random engine
  29. boost::compute::default_random_engine engine(queue);
  30. // setup the uniform distribution to produce floats between 1 and 100
  31. boost::compute::uniform_real_distribution<float> distribution(1.0f, 100.0f);
  32. // generate the random values and store them to 'vec'
  33. distribution.generate(vec.begin(), vec.end(), engine, queue);
  34. //! [generate]
  35. BOOST_CHECK_EQUAL(
  36. boost::compute::count_if(
  37. vec.begin(), vec.end(), _1 < 1.0f || _1 >= 100.0f, queue
  38. ),
  39. size_t(0)
  40. );
  41. }
  42. template<class T>
  43. class range_test_engine
  44. {
  45. public:
  46. explicit range_test_engine(boost::compute::command_queue &queue) {
  47. (void) queue;
  48. }
  49. template<class OutputIterator, class Function>
  50. void generate(OutputIterator first, OutputIterator last, Function op, boost::compute::command_queue &queue)
  51. {
  52. boost::compute::vector<T> tmp(std::distance(first, last), queue.get_context());
  53. BOOST_COMPUTE_FUNCTION(T, max_random, (const T x),
  54. {
  55. if(get_global_id(0) < 1)
  56. return (ValueType) MAX_RANDOM;
  57. else
  58. return (ValueType) 0;
  59. });
  60. max_random.define("MAX_RANDOM", "UINT_MAX");
  61. max_random.define("ValueType", boost::compute::type_name<T>());
  62. boost::compute::transform(tmp.begin(), tmp.end(), tmp.begin(), max_random, queue);
  63. boost::compute::transform(tmp.begin(), tmp.end(), first, op, queue);
  64. }
  65. };
  66. // For checking if result is in the correct range [low, hi)
  67. BOOST_AUTO_TEST_CASE(uniform_real_distribution_range)
  68. {
  69. using boost::compute::lambda::_1;
  70. boost::compute::vector<float> vec(32, context);
  71. // initialize the range_test_engine
  72. range_test_engine<boost::compute::uint_> engine(queue);
  73. // setup the uniform distribution to produce floats between 0.9 and 1.0
  74. boost::compute::uniform_real_distribution<float> distribution(0.9f, 1.0f);
  75. // generate the random values and store them to 'vec'
  76. distribution.generate(vec.begin(), vec.end(), engine, queue);
  77. BOOST_CHECK_EQUAL(
  78. boost::compute::count_if(
  79. vec.begin(), vec.end(), _1 < 0.9f || _1 >= 1.0f, queue
  80. ),
  81. size_t(0)
  82. );
  83. }
  84. BOOST_AUTO_TEST_SUITE_END()