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

7 6
BOOST_AUTO_TEST_CASE(custom_kernel)
8
{
9 6
    const cl_ulong n = 1024;
10

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

13 6
    vex::vector<int> x(queue, n);
14

15
    // Single kernel per program
16
    {
17 6
        vex::backend::source_generator src(queue[0]);
18

19 6
        src.begin_kernel("zeros");
20 6
        src.begin_kernel_parameters();
21 6
        src.parameter<size_t>("n");
22 6
        src.parameter<int*>("x");
23 6
        src.end_kernel_parameters();
24 6
        src.grid_stride_loop("idx", "n").open("{");
25 6
        src.new_line() << "x[idx] = 0;";
26 6
        src.close("}");
27 6
        src.end_kernel();
28

29 6
        vex::backend::kernel zeros(queue[0], src.str(), "zeros");
30

31
#ifdef BOOST_NO_VARIADIC_TEMPLATES
32
        zeros.push_arg(n);
33
        zeros.push_arg(x(0));
34
        zeros(queue[0]);
35
#else
36 6
        zeros(queue[0], n, x(0));
37
#endif
38

39 6
        check_sample(x, [](size_t, int v) { BOOST_CHECK_EQUAL(v, 0); });
40
    }
41

42
    // A couple of kernels per program
43
    {
44 6
        vex::backend::source_generator src(queue[0]);
45

46 6
        src.begin_kernel("ones");
47 6
        src.begin_kernel_parameters();
48 6
        src.parameter<size_t>("n");
49 6
        src.parameter<int*>("x");
50 6
        src.end_kernel_parameters();
51 6
        src.grid_stride_loop("idx", "n").open("{");
52 6
        src.new_line() << "x[idx] = 1;";
53 6
        src.close("}");
54 6
        src.end_kernel();
55

56 6
        src.begin_kernel("twos");
57 6
        src.begin_kernel_parameters();
58 6
        src.parameter<size_t>("n");
59 6
        src.parameter<int*>("x");
60 6
        src.end_kernel_parameters();
61 6
        src.grid_stride_loop("idx", "n").open("{");
62 6
        src.new_line() << "x[idx] = 2;";
63 6
        src.close("}");
64 6
        src.end_kernel();
65

66 6
        auto program = vex::backend::build_sources(queue[0], src.str());
67

68 6
        vex::backend::kernel ones(queue[0], program, "ones");
69 6
        vex::backend::kernel twos(queue[0], program, "twos");
70

71
#ifdef BOOST_NO_VARIADIC_TEMPLATES
72
        ones.push_arg(n);
73
        ones.push_arg(x(0));
74
        ones(queue[0]);
75
#else
76 6
        ones(queue[0], n, x(0));
77
#endif
78

79 6
        check_sample(x, [](size_t, int v) { BOOST_CHECK_EQUAL(v, 1); });
80

81
#ifdef BOOST_NO_VARIADIC_TEMPLATES
82
        twos.push_arg(n);
83
        twos.push_arg(x(0));
84
        twos(queue[0]);
85
#else
86 6
        twos(queue[0], n, x(0));
87
#endif
88

89 6
        check_sample(x, [](size_t, int v) { BOOST_CHECK_EQUAL(v, 2); });
90
    }
91
}
92

93 6
BOOST_AUTO_TEST_SUITE_END()

Read our documentation on viewing source code .

Loading