summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/compute/test/test_inplace_reduce.cpp
blob: a0706e3a1c231ac825ad7cc82eac02f5d719d243 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//---------------------------------------------------------------------------//
// Copyright (c) 2013 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.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE TestInplaceReduce
#include <boost/test/unit_test.hpp>

#include <iostream>

#include <boost/compute/system.hpp>
#include <boost/compute/functional.hpp>
#include <boost/compute/algorithm/iota.hpp>
#include <boost/compute/algorithm/detail/inplace_reduce.hpp>
#include <boost/compute/container/vector.hpp>

#include "quirks.hpp"
#include "context_setup.hpp"

BOOST_AUTO_TEST_CASE(sum_int)
{
    if(is_apple_cpu_device(device)) {
        std::cerr
            << "skipping all inplace_reduce tests due to Apple platform"
            << " behavior when local memory is used on a CPU device"
            << std::endl;
        return;
    }

    int data[] = { 1, 5, 3, 4, 9, 3, 5, 3 };
    boost::compute::vector<int> vector(data, data + 8, queue);

    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::plus<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(33));

    vector.assign(data, data + 8);
    vector.push_back(3);
    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::plus<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(36));
}

BOOST_AUTO_TEST_CASE(multiply_int)
{
    if(is_apple_cpu_device(device)) {
        return;
    }

    int data[] = { 1, 5, 3, 4, 9, 3, 5, 3 };
    boost::compute::vector<int> vector(data, data + 8, queue);

    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::multiplies<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(24300));

    vector.assign(data, data + 8);
    vector.push_back(3);
    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::multiplies<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(72900));
}

BOOST_AUTO_TEST_CASE(reduce_iota)
{
    if(is_apple_cpu_device(device)) {
        return;
    }

    // 1 value
    boost::compute::vector<int> vector(1, context);
    boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::plus<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(0));

    // 1000 values
    vector.resize(1000);
    boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::plus<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(499500));

    // 2499 values
    vector.resize(2499);
    boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::plus<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(3121251));

    // 4096 values
    vector.resize(4096);
    boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::plus<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(8386560));

    // 5000 values
    vector.resize(5000);
    boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
    boost::compute::detail::inplace_reduce(vector.begin(),
                                           vector.end(),
                                           boost::compute::plus<int>(),
                                           queue);
    queue.finish();
    BOOST_CHECK_EQUAL(int(vector[0]), int(12497500));
}

BOOST_AUTO_TEST_SUITE_END()