Skip to content

Commit 8979846

Browse files
authored
Merge pull request #720 from jszuppe/pr_kernel_clone
Add clCloneKernel() wrapper
2 parents 9795eab + 24bbb35 commit 8979846

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

include/boost/compute/device.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ class device
407407
return m_id != other.m_id;
408408
}
409409

410-
/// \internal_
410+
/// Returns \c true if the device OpenCL version is major.minor
411+
/// or newer; otherwise returns \c false.
411412
bool check_version(int major, int minor) const
412413
{
413414
std::stringstream stream;

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
{

include/boost/compute/platform.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,23 @@ class platform
211211
return m_platform != other.m_platform;
212212
}
213213

214+
/// Returns \c true if the platform OpenCL version is major.minor
215+
/// or newer; otherwise returns \c false.
216+
bool check_version(int major, int minor) const
217+
{
218+
std::stringstream stream;
219+
stream << version();
220+
221+
int actual_major, actual_minor;
222+
stream.ignore(7); // 'OpenCL '
223+
stream >> actual_major;
224+
stream.ignore(1); // '.'
225+
stream >> actual_minor;
226+
227+
return actual_major > major ||
228+
(actual_major == major && actual_minor >= minor);
229+
}
230+
214231
private:
215232
cl_platform_id m_platform;
216233
};

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)