JayBeams  0.1
Another project to have fun coding.
ut_build_simple_kernel.cpp
Go to the documentation of this file.
3 
4 #include <boost/compute/context.hpp>
5 #include <boost/test/unit_test.hpp>
6 #include <sstream>
7 
8 // Define a program with two kernels, nothing fancy
9 char const valid_program[] = R"""(
10 __kernel void add_float(
11  __global float *dst, __global float const *src, unsigned int const N) {
12  int row = get_global_id(0);
13  if (row < N) {
14  dst[row] = dst[row] + src[row];
15  }
16 }
17 
18 __kernel void add_int(
19  __global int *dst, __global int const *src,
20  unsigned int const N) {
21  int row = get_global_id(0);
22  if (row < N) {
23  dst[row] = dst[row] + src[row];
24  }
25 }
26 )""";
27 
28 // Define an invalid program
29 char const invalid_program[] = R"""(
30 __kernel void add_float(
31  __global float *dst, __global float const* src, unsigned int const N) {
32  int row = get_global_id(0);
33  if (row < N) {
34  dest[row] = dst[row] + src[row]; /* oops typo in the lhs */
35  }
36 }
37 )""";
38 
39 /**
40  * @test Make sure jb::opencl::build_simple_kernel works as expected.
41  */
43  boost::compute::device device = jb::opencl::device_selector();
44  BOOST_TEST_MESSAGE("Running with device=" << device.name());
45  boost::compute::context context(device);
46 
47  BOOST_CHECK_NO_THROW(jb::opencl::build_simple_kernel(
48  context, device, valid_program, "add_int"));
49  BOOST_CHECK_NO_THROW(jb::opencl::build_simple_kernel(
50  context, device, valid_program, "add_float"));
51  BOOST_CHECK_THROW(
53  context, device, invalid_program, "add_float"),
54  std::exception);
55 
56  std::istringstream is(valid_program);
57  BOOST_CHECK_NO_THROW(
58  jb::opencl::build_simple_kernel(context, device, is, "add_int"));
59  is.str(valid_program);
60  is.clear();
61  BOOST_CHECK_NO_THROW(
62  jb::opencl::build_simple_kernel(context, device, is, "add_float"));
63  is.str(invalid_program);
64  is.clear();
65  BOOST_CHECK_THROW(
66  jb::opencl::build_simple_kernel(context, device, is, "add_float"),
67  std::exception);
68 }
69 
70 /**
71  * @test Make sure jb::opencl::build_simple_program works as expected.
72  */
74  boost::compute::device device = jb::opencl::device_selector();
75  BOOST_TEST_MESSAGE("Running with device=" << device.name());
76  boost::compute::context context(device);
77 
78  boost::compute::program program;
79  BOOST_CHECK_NO_THROW(
80  program =
82  BOOST_CHECK_NO_THROW(boost::compute::kernel(program, "add_float"));
83  BOOST_CHECK_NO_THROW(boost::compute::kernel(program, "add_int"));
84 
85  BOOST_CHECK_THROW(
87  std::exception);
88 
89  std::istringstream is(valid_program);
90  BOOST_CHECK_NO_THROW(
91  program = jb::opencl::build_simple_program(context, device, is));
92  BOOST_CHECK_NO_THROW(boost::compute::kernel(program, "add_float"));
93  BOOST_CHECK_NO_THROW(boost::compute::kernel(program, "add_int"));
94 
95  is.str(invalid_program);
96  is.clear();
97  BOOST_CHECK_THROW(
98  jb::opencl::build_simple_program(context, device, is), std::exception);
99 }
boost::compute::device device_selector(config const &cfg)
Select an OpenCL device matching the current configuration.
BOOST_AUTO_TEST_CASE(build_simple_kernel)
boost::compute::kernel build_simple_kernel(boost::compute::context context, boost::compute::device device, char const *code, char const *kernel_name)
Build a simple program (one where everything is in a single string) and get a kernel from it...
char const valid_program[]
char const invalid_program[]
boost::compute::program build_simple_program(boost::compute::context context, boost::compute::device device, char const *code)
Convenience function to build a simple program and return it.