File: perf_copy_if.cpp

package info (click to toggle)
boost1.62 1.62.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 686,420 kB
  • sloc: cpp: 2,609,004; xml: 972,558; ansic: 53,674; python: 32,437; sh: 8,829; asm: 3,071; cs: 2,121; makefile: 964; perl: 859; yacc: 472; php: 132; ruby: 94; f90: 55; sql: 13; csh: 6
file content (122 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#include <boost/compute/core.hpp>
#include <boost/compute/closure.hpp>
#include <boost/compute/algorithm/copy_if.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/random/default_random_engine.hpp>
#include <boost/compute/random/uniform_int_distribution.hpp>
#include <boost/compute/random/uniform_real_distribution.hpp>

#include "perf.hpp"

namespace compute = boost::compute;

void test_copy_if_odd(compute::command_queue &queue)
{
    // create input and output vectors on the device
    const compute::context &context = queue.get_context();
    compute::vector<int> input(PERF_N, context);
    compute::vector<int> output(PERF_N, context);

    // generate random numbers between 1 and 10
    compute::default_random_engine rng(queue);
    compute::uniform_int_distribution<int> d(1, 10);
    d.generate(input.begin(), input.end(), rng, queue);

    BOOST_COMPUTE_FUNCTION(bool, is_odd, (int x),
    {
        return x & 1;
    });

    perf_timer t;
    for(size_t trial = 0; trial < PERF_TRIALS; trial++){
        t.start();
        compute::vector<int>::iterator i = compute::copy_if(
            input.begin(), input.end(), output.begin(), is_odd, queue
        );
        queue.finish();
        t.stop();

        float ratio = float(std::distance(output.begin(), i)) / PERF_N;
        if(PERF_N > 1000 && (ratio < 0.45f || ratio > 0.55f)){
            std::cerr << "error: ratio is " << ratio << std::endl;
            std::cerr << "error: ratio should be around 45-55%" << std::endl;
        }
    }
    std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
}

void test_copy_if_in_sphere(compute::command_queue &queue)
{
    using boost::compute::float4_;

    // create input and output vectors on the device
    const compute::context &context = queue.get_context();
    compute::vector<float4_> input_points(PERF_N, context);
    compute::vector<float4_> output_points(PERF_N, context);

    // generate random numbers in a cube
    float radius = 5.0f;
    compute::default_random_engine rng(queue);
    compute::uniform_real_distribution<float> d(-radius, +radius);
    d.generate(
        compute::make_buffer_iterator<float>(input_points.get_buffer(), 0),
        compute::make_buffer_iterator<float>(input_points.get_buffer(), PERF_N * 4),
        rng,
        queue
    );

    // predicate which returns true if the point lies within the sphere
    BOOST_COMPUTE_CLOSURE(bool, is_in_sphere, (float4_ point), (radius),
    {
        // ignore fourth component
        point.w = 0;

        return length(point) < radius;
    });

    perf_timer t;
    for(size_t trial = 0; trial < PERF_TRIALS; trial++){
        t.start();
        compute::vector<float4_>::iterator i = compute::copy_if(
            input_points.begin(),
            input_points.end(),
            output_points.begin(),
            is_in_sphere,
            queue
        );
        queue.finish();
        t.stop();

        float ratio = float(std::distance(output_points.begin(), i)) / PERF_N;
        if(PERF_N > 1000 && (ratio < 0.5f || ratio > 0.6f)){
            std::cerr << "error: ratio is " << ratio << std::endl;
            std::cerr << "error: ratio should be around 50-60%" << std::endl;
        }
    }
    std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
}

int main(int argc, char *argv[])
{
    perf_parse_args(argc, argv);

    // setup context and queue for the default device
    boost::compute::device device = boost::compute::system::default_device();
    boost::compute::context context(device);
    boost::compute::command_queue queue(context, device);
    std::cout << "device: " << device.name() << std::endl;

    test_copy_if_odd(queue);

    return 0;
}