ddemidov / vexcl
1
#define BOOST_TEST_MODULE Sort
2
#include <algorithm>
3
#include <boost/iterator/counting_iterator.hpp>
4
#include <boost/test/unit_test.hpp>
5
#include <vexcl/vector.hpp>
6
#include <vexcl/sort.hpp>
7
#include "context_setup.hpp"
8

9 3
BOOST_AUTO_TEST_CASE(sort_keys)
10
{
11
    const size_t n = 1000 * 1000;
12

13 3
    std::vector<float> k = random_vector<float>(n);
14 3
    vex::vector<float> keys(ctx, k);
15

16 3
    vex::sort(keys);
17 3
    vex::copy(keys, k);
18

19 3
    BOOST_CHECK( std::is_sorted(k.begin(), k.end()) );
20
}
21

22 3
BOOST_AUTO_TEST_CASE(sort_keys_vals_default)
23
{
24
    const size_t n = 1000 * 1000;
25

26 3
    std::vector<int  > k = random_vector<int  >(n);
27 3
    std::vector<float> v = random_vector<float>(n);
28 3
    std::vector<int>   p(n);
29

30 3
    vex::vector<int  > keys(ctx, k);
31 3
    vex::vector<float> vals(ctx, v);
32

33 3
    for(size_t i = 0; i < p.size(); ++i) p[i] = static_cast<int>(i);
34 3
    std::stable_sort(p.begin(), p.end(), [&](int i, int j) { return k[i] < k[j]; });
35

36 3
    vex::sort_by_key(keys, vals);
37

38 3
    check_sample(keys, [&](size_t pos, int val) {
39 3
            BOOST_CHECK_EQUAL(val, k[p[pos]]);
40 3
            });
41

42 3
    check_sample(vals, [&](size_t pos, float val) {
43 3
            BOOST_CHECK_EQUAL(val, v[p[pos]]);
44 3
            });
45
}
46

47 3
BOOST_AUTO_TEST_CASE(sort_keys_vals_custom_op)
48
{
49
    const size_t n = 1000 * 1000;
50

51 3
    std::vector<int  > k = random_vector<int  >(n);
52 3
    std::vector<float> v = random_vector<float>(n);
53 3
    std::vector<int>   p(n);
54

55 3
    vex::vector<int  > keys(ctx, k);
56 3
    vex::vector<float> vals(ctx, v);
57

58 3
    for(size_t i = 0; i < p.size(); ++i) p[i] = static_cast<int>(i);
59

60 3
    struct even_first_t {
61
        typedef bool result_type;
62 3
        even_first_t() {}
63

64 3
        VEX_DUAL_FUNCTOR(result_type, (int, a)(int, b),
65
            char bit1 = 1 & a;
66
            char bit2 = 1 & b;
67
            if (bit1 == bit2) return a < b;
68
            return bit1 < bit2;
69
            )
70 3
    } even_first;
71

72 3
    std::stable_sort(p.begin(), p.end(), [&](int i, int j) -> bool {
73 3
            int a = k[i];
74 3
            int b = k[j];
75 3
            return even_first(a, b);
76
            });
77

78 3
    vex::sort_by_key(keys, vals, even_first);
79

80 3
    check_sample(keys, [&](size_t pos, int val) {
81 3
            BOOST_CHECK_EQUAL(val, k[p[pos]]);
82 3
            });
83

84 3
    check_sample(vals, [&](size_t pos, float val) {
85 3
            BOOST_CHECK_EQUAL(val, v[p[pos]]);
86 3
            });
87
}
88

89 3
BOOST_AUTO_TEST_CASE(sort_keys_tuple)
90
{
91
    const size_t n = 1000 * 1000;
92

93 3
    std::vector<int>   k1 = random_vector<int>  (n);
94 3
    std::vector<float> k2 = random_vector<float>(n);
95

96 3
    vex::vector<int>   keys1(ctx, k1);
97 3
    vex::vector<float> keys2(ctx, k2);
98

99 3
    struct less_t {
100
        typedef bool result_type;
101 3
        less_t() {}
102

103 3
        VEX_DUAL_FUNCTOR(result_type, (int, a1)(float, a2)(int, b1)(float, b2),
104
            return (a1 == b1) ? (a2 < b2) : (a1 < b1);
105
            )
106 3
    } less;
107

108 3
    vex::sort(boost::fusion::vector_tie(keys1, keys2), less );
109 3
    vex::copy(keys1, k1);
110 3
    vex::copy(keys2, k2);
111

112 3
    BOOST_CHECK( std::is_sorted(
113
                boost::counting_iterator<size_t>(0),
114
                boost::counting_iterator<size_t>(n),
115
                [&](size_t i, size_t j) {
116
                    return std::make_tuple(k1[i], k2[i]) < std::make_tuple(k1[j], k2[j]);
117
                } ) );
118
}
119

120 3
BOOST_AUTO_TEST_CASE(sort_keys_vals_tuple)
121
{
122
    const size_t n = 1000 * 1000;
123

124 3
    std::vector<int>     k1 = random_vector<int>    (n);
125 3
    std::vector<float>   k2 = random_vector<float>  (n);
126 3
    std::vector<cl_long> v1 = random_vector<cl_long>(n);
127 3
    std::vector<short>   v2 = random_vector<short>  (n);
128

129 3
    vex::vector<int>     keys1(ctx, k1);
130 3
    vex::vector<float>   keys2(ctx, k2);
131 3
    vex::vector<cl_long> vals1(ctx, v1);
132 3
    vex::vector<short>   vals2(ctx, v2);
133

134 3
    std::vector<int> p(n);
135 3
    for(size_t i = 0; i < p.size(); ++i) p[i] = static_cast<int>(i);
136

137 3
    struct less_t {
138
        typedef bool result_type;
139 3
        less_t() {}
140

141 3
        VEX_DUAL_FUNCTOR(result_type, (int, a1)(float, a2)(int, b1)(float, b2),
142
            return (a1 == b1) ? (a2 < b2) : (a1 < b1);
143
            )
144 3
    } less;
145

146 3
    std::stable_sort(p.begin(), p.end(), [&](int i, int j) {
147 3
            return less(k1[i], k2[i], k1[j], k2[j]);
148
            });
149

150 3
    vex::sort_by_key(
151 3
            boost::fusion::vector_tie(keys1, keys2),
152 3
            boost::fusion::vector_tie(vals1, vals2),
153 3
            less
154
            );
155

156 3
    check_sample(keys1, [&](size_t pos, int val) {
157 3
            BOOST_CHECK_EQUAL(val, k1[p[pos]]);
158 3
            });
159

160 3
    check_sample(keys2, [&](size_t pos, float val) {
161 3
            BOOST_CHECK_EQUAL(val, k2[p[pos]]);
162 3
            });
163

164 3
    check_sample(vals1, [&](size_t pos, cl_long val) {
165 3
            BOOST_CHECK_EQUAL(val, v1[p[pos]]);
166 3
            });
167

168 3
    check_sample(vals2, [&](size_t pos, short val) {
169 3
            BOOST_CHECK_EQUAL(val, v2[p[pos]]);
170 3
            });
171
}
172

173 3
BOOST_AUTO_TEST_SUITE_END()

Read our documentation on viewing source code .

Loading