Skip to content

Commit 24bbb35

Browse files
committed
Add clone() to kernel class
This adds wrapper for clCloneKernel() OpenCL 2.1 API function to the kernel class.
1 parent c736d6e commit 24bbb35

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

include/boost/compute/kernel.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ class kernel
128128
}
129129
}
130130

131+
#if defined(BOOST_COMPUTE_CL_VERSION_2_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
132+
/// Creates a new kernel object based on a shallow copy of
133+
/// the undelying OpenCL kernel object.
134+
///
135+
/// \opencl_version_warning{2,1}
136+
///
137+
/// \see_opencl_ref{clCloneKernel}
138+
kernel clone()
139+
{
140+
cl_int ret = 0;
141+
cl_kernel k = clCloneKernel(m_kernel, &ret);
142+
return kernel(k, false);
143+
}
144+
#endif // BOOST_COMPUTE_CL_VERSION_2_1
145+
131146
/// Returns a reference to the underlying OpenCL kernel object.
132147
cl_kernel& get() const
133148
{

test/opencl_version_check.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414
#define REQUIRES_OPENCL_VERSION(major, minor) \
1515
if (!device.check_version(major, minor)) return
1616

17+
#define REQUIRES_OPENCL_PLATFORM_VERSION(major, minor) \
18+
if (!device.platform().check_version(major, minor)) return
19+
1720
#endif

test/test_kernel.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <boost/compute/utility/source.hpp>
1818

1919
#include "context_setup.hpp"
20+
#include "check_macros.hpp"
2021

2122
namespace compute = boost::compute;
2223

@@ -288,4 +289,25 @@ BOOST_AUTO_TEST_CASE(get_sub_group_info_core)
288289
}
289290
#endif // BOOST_COMPUTE_CL_VERSION_2_1
290291

292+
#ifdef BOOST_COMPUTE_CL_VERSION_2_1
293+
BOOST_AUTO_TEST_CASE(clone_kernel)
294+
{
295+
REQUIRES_OPENCL_PLATFORM_VERSION(2, 1);
296+
297+
compute::kernel k1 = compute::kernel::create_with_source(
298+
"__kernel void test(__global int * x) { x[get_global_id(0)] = get_global_id(0); }",
299+
"test", context
300+
);
301+
302+
compute::buffer x(context, 5 * sizeof(compute::int_));
303+
k1.set_arg(0, x);
304+
305+
// Clone k1 kernel
306+
compute::kernel k2 = k1.clone();
307+
// After clone k2 0th argument (__global float * x) should be set,
308+
// so we should be able to enqueue k2 kernel without problems
309+
queue.enqueue_1d_range_kernel(k2, 0, x.size() / sizeof(compute::int_), 0).wait();
310+
}
311+
#endif // BOOST_COMPUTE_CL_VERSION_2_1
312+
291313
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)