| 56 | } |
| 57 | |
| 58 | void test_copy_if_in_sphere(compute::command_queue &queue) |
| 59 | { |
| 60 | using boost::compute::float4_; |
| 61 | |
| 62 | // create input and output vectors on the device |
| 63 | const compute::context &context = queue.get_context(); |
| 64 | compute::vector<float4_> input_points(PERF_N, context); |
| 65 | compute::vector<float4_> output_points(PERF_N, context); |
| 66 | |
| 67 | // generate random numbers in a cube |
| 68 | float radius = 5.0f; |
| 69 | compute::default_random_engine rng(queue); |
| 70 | compute::uniform_real_distribution<float> d(-radius, +radius); |
| 71 | d.generate( |
| 72 | compute::make_buffer_iterator<float>(input_points.get_buffer(), 0), |
| 73 | compute::make_buffer_iterator<float>(input_points.get_buffer(), PERF_N * 4), |
| 74 | rng, |
| 75 | queue |
| 76 | ); |
| 77 | |
| 78 | // predicate which returns true if the point lies within the sphere |
| 79 | BOOST_COMPUTE_CLOSURE(bool, is_in_sphere, (float4_ point), (radius), |
| 80 | { |
| 81 | // ignore fourth component |
| 82 | point.w = 0; |
| 83 | |
| 84 | return length(point) < radius; |
| 85 | }); |
| 86 | |
| 87 | perf_timer t; |
| 88 | for(size_t trial = 0; trial < PERF_TRIALS; trial++){ |
| 89 | t.start(); |
| 90 | compute::vector<float4_>::iterator i = compute::copy_if( |
| 91 | input_points.begin(), |
| 92 | input_points.end(), |
| 93 | output_points.begin(), |
| 94 | is_in_sphere, |
| 95 | queue |
| 96 | ); |
| 97 | queue.finish(); |
| 98 | t.stop(); |
| 99 | |
| 100 | float ratio = float(std::distance(output_points.begin(), i)) / PERF_N; |
| 101 | if(PERF_N > 1000 && (ratio < 0.5f || ratio > 0.6f)){ |
| 102 | std::cerr << "error: ratio is " << ratio << std::endl; |
| 103 | std::cerr << "error: ratio should be around 50-60%" << std::endl; |
| 104 | } |
| 105 | } |
| 106 | std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl; |
| 107 | } |
| 108 | |
| 109 | int main(int argc, char *argv[]) |
| 110 | { |