ddemidov / vexcl
1
#define BOOST_TEST_MODULE VectorCopy
2
#include <boost/test/unit_test.hpp>
3
#include <vexcl/vector.hpp>
4
#include <vexcl/gather.hpp>
5
#include "context_setup.hpp"
6

7 6
BOOST_AUTO_TEST_CASE(iterate_over_vector)
8
{
9
    const size_t N = 1024;
10

11 6
    vex::vector<double> x(ctx, N);
12 6
    x = 42;
13

14 6
    BOOST_CHECK(42 == *std::min_element(x.begin(), x.end()));
15
}
16

17 6
BOOST_AUTO_TEST_CASE(element_access)
18
{
19
    const size_t N = 1024;
20 6
    vex::vector<double> x(ctx, N);
21

22 6
    for(size_t i = 0; i < N; i++)
23 6
        x[i] = 42;
24

25 6
    check_sample(x, [](size_t, double a) { BOOST_CHECK(a == 42); });
26
}
27

28 6
BOOST_AUTO_TEST_CASE(copy_to_std_vector)
29
{
30
    const size_t N = 1024;
31 6
    vex::vector<double> X(ctx, N);
32 6
    std::vector<double> x(N);
33

34 6
    X = 42;
35 6
    copy(X, x);
36 6
    check_sample(x, [](size_t, double a) { BOOST_CHECK(a == 42); });
37

38 6
    X = 67;
39 6
    vex::copy(X.begin(), X.end(), x.begin());
40 6
    check_sample(x, [](size_t, double a) { BOOST_CHECK(a == 67); });
41
}
42

43 6
BOOST_AUTO_TEST_CASE(copy_from_std_vector)
44
{
45
    const size_t N = 1024;
46

47 6
    std::vector<double> x = random_vector<double>(N);
48 6
    vex::vector<double> X(ctx, N);
49

50 6
    copy(x, X);
51 6
    check_sample(X, x, [](size_t, double a, double b) { BOOST_CHECK(a == b); });
52

53 6
    std::fill(x.begin(), x.end(), 42);
54 6
    vex::copy(x.begin(), x.end(), X.begin());
55 6
    check_sample(X, [](size_t, double a) { BOOST_CHECK(a == 42); });
56
}
57

58 6
BOOST_AUTO_TEST_CASE(map_buffer)
59
{
60
    const size_t N = 1 << 20;
61 6
    vex::vector<size_t> x(ctx, N);
62

63 6
    for(unsigned d = 0; d < ctx.size(); ++d) {
64 6
        auto ptr = x.map(d);
65 6
        for(size_t i = 0; i < x.part_size(d); ++i)
66 6
            ptr[i] = i + x.part_start(d);
67
    }
68

69 6
    check_sample(x, [](size_t idx, size_t a) { BOOST_CHECK(a == idx); });
70
}
71

72 6
BOOST_AUTO_TEST_CASE(gather)
73
{
74
    const size_t n = 1 << 20;
75
    const size_t m = 100;
76

77 6
    std::vector<double> x = random_vector<double>(n);
78 6
    vex::vector<double> X(ctx, x);
79

80 6
    std::vector<size_t> i(m);
81 6
    std::generate(i.begin(), i.end(), [n](){ return rand() % n; });
82

83 6
    for (int sorted = 0; sorted < 2; ++sorted) {
84 6
        if (sorted) {
85 6
            std::sort(i.begin(), i.end());
86 6
            i.resize( std::unique(i.begin(), i.end()) - i.begin() );
87
        }
88

89 6
        std::vector<double>  data(i.size());
90 6
        vex::gather  get(ctx, x.size(), i);
91 6
        vex::scatter put(ctx, x.size(), i);
92

93 6
        get(X, data);
94

95 6
        for(size_t p = 0; p < i.size(); ++p)
96 6
            BOOST_CHECK(data[p] == x[i[p]]);
97

98 6
        vex::vector<double> Y(ctx, n);
99 6
        Y = 0;
100 6
        put(data, Y);
101

102 6
        for(size_t p = 0; p < i.size(); ++p)
103 6
            BOOST_CHECK(Y[i[p]] == x[i[p]]);
104
    }
105
}
106

107 6
BOOST_AUTO_TEST_CASE(std_sort_vex_vector)
108
{
109
    const size_t n = 1 << 10;
110

111 6
    vex::vector<double> x(ctx, random_vector<double>(n));
112

113 6
    std::sort(x.begin(), x.end());
114 6
    BOOST_CHECK(std::is_sorted(x.begin(), x.end()));
115
}
116

117 6
BOOST_AUTO_TEST_SUITE_END()
118

Read our documentation on viewing source code .

Loading