ddemidov / vexcl
1
#define BOOST_TEST_MODULE VectorPointer
2
#include <boost/test/unit_test.hpp>
3
#include <vexcl/vector.hpp>
4
#include <vexcl/vector_pointer.hpp>
5
#include <vexcl/element_index.hpp>
6
#include <vexcl/tagged_terminal.hpp>
7
#include <vexcl/temporary.hpp>
8
#include <vexcl/constants.hpp>
9
#include <vexcl/function.hpp>
10
#include "context_setup.hpp"
11

12 6
BOOST_AUTO_TEST_CASE(nbody)
13
{
14 6
    const size_t n = 1024;
15

16 6
    std::vector<vex::command_queue> queue(1, ctx.queue(0));
17

18 6
    std::vector<double> X = random_vector<double>(n);
19

20 6
    vex::vector<double> x(queue, X);
21 6
    vex::vector<double> y(queue, n);
22

23 6
    VEX_FUNCTION(double, nbody, (size_t, n)(size_t, j)(double*, x),
24
            double sum = 0;
25
            for(size_t i = 0; i < n; ++i)
26
                if (i != j) sum += x[i];
27
                    return sum;
28
            );
29

30 6
    y = nbody(n, vex::element_index(), vex::raw_pointer(x));
31

32 6
    check_sample(y, [&](size_t idx, double v) {
33 6
            double sum = 0;
34 6
            for(size_t i = 0; i < n; ++i)
35 6
                if (i != idx) sum += X[i];
36 6
            BOOST_CHECK_CLOSE(v, sum, 1e-8);
37 6
            });
38
}
39

40 6
BOOST_AUTO_TEST_CASE(manual_stencil)
41
{
42
    const size_t n = 1024;
43

44 6
    std::vector<vex::command_queue> queue(1, ctx.queue(0));
45

46 6
    std::vector<double> X = random_vector<double>(n);
47

48 6
    vex::vector<double> x(queue, X);
49 6
    vex::vector<double> y(queue, n);
50

51 6
    VEX_CONSTANT(nil, 0);
52 6
    VEX_CONSTANT(one, 1);
53 6
    VEX_CONSTANT(two, 2);
54

55 6
    auto N = vex::tag<1>( x.size() );
56 6
    auto p = vex::raw_pointer(x);
57

58 6
    auto i     = vex::make_temp<1>( vex::element_index() );
59 6
    auto left  = vex::make_temp<2>( if_else(i > nil(), i - one(), i ) );
60 6
    auto right = vex::make_temp<3>( if_else(i + one() < N, i + one(), i ) );
61

62
    // Use pointer arithmetics
63 6
    y = *(p + i) * two() - *(p + left) - *(p + right);
64

65 6
    check_sample(y, [&](size_t idx, double v) {
66 6
            double xc = X[idx];
67 6
            double xl = X[idx > 0 ? idx - 1 : idx];
68 6
            double xr = X[idx + 1 < n ? idx + 1 : idx];
69 6
            BOOST_CHECK_CLOSE(v, 2 * xc - xr - xl, 1e-8);
70 6
            });
71

72
    // Same thing with index operators
73 6
    y = p[i] * two() - p[left] - p[right];
74

75 6
    check_sample(y, [&](size_t idx, double v) {
76 6
            double xc = X[idx];
77 6
            double xl = X[idx > 0 ? idx - 1 : idx];
78 6
            double xr = X[idx + 1 < n ? idx + 1 : idx];
79 6
            BOOST_CHECK_CLOSE(v, 2 * xc - xr - xl, 1e-8);
80 6
            });
81
}
82

83
#ifndef VEXCL_BACKEND_CUDA
84 6
BOOST_AUTO_TEST_CASE(constant_pointer)
85
{
86 6
    std::vector<vex::command_queue> queue(1, ctx.queue(0));
87

88
    const size_t n = 1024;
89

90 6
    int host_data[] = {1, 2, 3, 4};
91 6
    vex::vector<int> x(queue, 4, host_data, vex::backend::MEM_READ_ONLY);
92 6
    vex::vector<int> y(queue, n);
93

94 6
    auto X = vex::constant_pointer(x);
95 6
    y = X[vex::element_index() % 4];
96

97 6
    check_sample(y, [&](size_t idx, int v) {
98 6
            BOOST_CHECK_EQUAL(v, host_data[idx % 4]);
99 6
            });
100
}
101

102 6
BOOST_AUTO_TEST_CASE(pass_constant_pointer_to_function)
103
{
104 6
    std::vector<vex::command_queue> queue(1, ctx.queue(0));
105

106
    const size_t N = 1024;
107

108 6
    int host_data[] = {1, 2, 3, 4};
109

110 6
    vex::vector<int> x(queue, 4, host_data, vex::backend::MEM_READ_ONLY);
111 6
    vex::vector<int> y(queue, N);
112

113 6
    VEX_FUNCTION(int, foo, (int, idx)(vex::constant_ptr<int>, x),
114
            return x[idx % 4];
115
            );
116

117 6
    y = foo(vex::element_index(), vex::constant_pointer(x));
118

119 6
    check_sample(y, [&](size_t idx, int v) {
120 6
            BOOST_CHECK_EQUAL(v, host_data[idx % 4]);
121 6
            });
122
}
123
#endif
124

125 6
BOOST_AUTO_TEST_SUITE_END()
126

Read our documentation on viewing source code .

Loading