perf_copy_if.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <boost/compute/core.hpp>
  11. #include <boost/compute/closure.hpp>
  12. #include <boost/compute/algorithm/copy_if.hpp>
  13. #include <boost/compute/container/vector.hpp>
  14. #include <boost/compute/random/default_random_engine.hpp>
  15. #include <boost/compute/random/uniform_int_distribution.hpp>
  16. #include <boost/compute/random/uniform_real_distribution.hpp>
  17. #include "perf.hpp"
  18. namespace compute = boost::compute;
  19. void test_copy_if_odd(compute::command_queue &queue)
  20. {
  21. // create input and output vectors on the device
  22. const compute::context &context = queue.get_context();
  23. compute::vector<int> input(PERF_N, context);
  24. compute::vector<int> output(PERF_N, context);
  25. // generate random numbers between 1 and 10
  26. compute::default_random_engine rng(queue);
  27. compute::uniform_int_distribution<int> d(1, 10);
  28. d.generate(input.begin(), input.end(), rng, queue);
  29. BOOST_COMPUTE_FUNCTION(bool, is_odd, (int x),
  30. {
  31. return x & 1;
  32. });
  33. perf_timer t;
  34. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  35. t.start();
  36. compute::vector<int>::iterator i = compute::copy_if(
  37. input.begin(), input.end(), output.begin(), is_odd, queue
  38. );
  39. queue.finish();
  40. t.stop();
  41. float ratio = float(std::distance(output.begin(), i)) / PERF_N;
  42. if(PERF_N > 1000 && (ratio < 0.45f || ratio > 0.55f)){
  43. std::cerr << "error: ratio is " << ratio << std::endl;
  44. std::cerr << "error: ratio should be around 45-55%" << std::endl;
  45. }
  46. }
  47. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  48. }
  49. void test_copy_if_in_sphere(compute::command_queue &queue)
  50. {
  51. using boost::compute::float4_;
  52. // create input and output vectors on the device
  53. const compute::context &context = queue.get_context();
  54. compute::vector<float4_> input_points(PERF_N, context);
  55. compute::vector<float4_> output_points(PERF_N, context);
  56. // generate random numbers in a cube
  57. float radius = 5.0f;
  58. compute::default_random_engine rng(queue);
  59. compute::uniform_real_distribution<float> d(-radius, +radius);
  60. d.generate(
  61. compute::make_buffer_iterator<float>(input_points.get_buffer(), 0),
  62. compute::make_buffer_iterator<float>(input_points.get_buffer(), PERF_N * 4),
  63. rng,
  64. queue
  65. );
  66. // predicate which returns true if the point lies within the sphere
  67. BOOST_COMPUTE_CLOSURE(bool, is_in_sphere, (float4_ point), (radius),
  68. {
  69. // ignore fourth component
  70. point.w = 0;
  71. return length(point) < radius;
  72. });
  73. perf_timer t;
  74. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  75. t.start();
  76. compute::vector<float4_>::iterator i = compute::copy_if(
  77. input_points.begin(),
  78. input_points.end(),
  79. output_points.begin(),
  80. is_in_sphere,
  81. queue
  82. );
  83. queue.finish();
  84. t.stop();
  85. float ratio = float(std::distance(output_points.begin(), i)) / PERF_N;
  86. if(PERF_N > 1000 && (ratio < 0.5f || ratio > 0.6f)){
  87. std::cerr << "error: ratio is " << ratio << std::endl;
  88. std::cerr << "error: ratio should be around 50-60%" << std::endl;
  89. }
  90. }
  91. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  92. }
  93. int main(int argc, char *argv[])
  94. {
  95. perf_parse_args(argc, argv);
  96. // setup context and queue for the default device
  97. boost::compute::device device = boost::compute::system::default_device();
  98. boost::compute::context context(device);
  99. boost::compute::command_queue queue(context, device);
  100. std::cout << "device: " << device.name() << std::endl;
  101. test_copy_if_odd(queue);
  102. return 0;
  103. }