123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- #ifndef BOOST_COMPUTE_KERNEL_HPP
- #define BOOST_COMPUTE_KERNEL_HPP
- #include <string>
- #include <boost/assert.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <boost/optional.hpp>
- #include <boost/compute/cl_ext.hpp> // cl_khr_subgroups
- #include <boost/compute/config.hpp>
- #include <boost/compute/exception.hpp>
- #include <boost/compute/program.hpp>
- #include <boost/compute/platform.hpp>
- #include <boost/compute/type_traits/is_fundamental.hpp>
- #include <boost/compute/detail/diagnostic.hpp>
- #include <boost/compute/detail/get_object_info.hpp>
- #include <boost/compute/detail/assert_cl_success.hpp>
- namespace boost {
- namespace compute {
- namespace detail {
- template<class T> struct set_kernel_arg;
- }
- class kernel
- {
- public:
-
- kernel()
- : m_kernel(0)
- {
- }
-
-
- explicit kernel(cl_kernel kernel, bool retain = true)
- : m_kernel(kernel)
- {
- if(m_kernel && retain){
- clRetainKernel(m_kernel);
- }
- }
-
- kernel(const program &program, const std::string &name)
- {
- cl_int error = 0;
- m_kernel = clCreateKernel(program.get(), name.c_str(), &error);
- if(!m_kernel){
- BOOST_THROW_EXCEPTION(opencl_error(error));
- }
- }
-
- kernel(const kernel &other)
- : m_kernel(other.m_kernel)
- {
- if(m_kernel){
- clRetainKernel(m_kernel);
- }
- }
-
- kernel& operator=(const kernel &other)
- {
- if(this != &other){
- if(m_kernel){
- clReleaseKernel(m_kernel);
- }
- m_kernel = other.m_kernel;
- if(m_kernel){
- clRetainKernel(m_kernel);
- }
- }
- return *this;
- }
- #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
-
- kernel(kernel&& other) BOOST_NOEXCEPT
- : m_kernel(other.m_kernel)
- {
- other.m_kernel = 0;
- }
-
- kernel& operator=(kernel&& other) BOOST_NOEXCEPT
- {
- if(m_kernel){
- clReleaseKernel(m_kernel);
- }
- m_kernel = other.m_kernel;
- other.m_kernel = 0;
- return *this;
- }
- #endif
-
- ~kernel()
- {
- if(m_kernel){
- BOOST_COMPUTE_ASSERT_CL_SUCCESS(
- clReleaseKernel(m_kernel)
- );
- }
- }
- #if defined(BOOST_COMPUTE_CL_VERSION_2_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
-
-
-
-
-
-
- kernel clone()
- {
- cl_int ret = 0;
- cl_kernel k = clCloneKernel(m_kernel, &ret);
- return kernel(k, false);
- }
- #endif
-
- cl_kernel& get() const
- {
- return const_cast<cl_kernel &>(m_kernel);
- }
-
- std::string name() const
- {
- return get_info<std::string>(CL_KERNEL_FUNCTION_NAME);
- }
-
- size_t arity() const
- {
- return get_info<cl_uint>(CL_KERNEL_NUM_ARGS);
- }
-
- program get_program() const
- {
- return program(get_info<cl_program>(CL_KERNEL_PROGRAM));
- }
-
- context get_context() const
- {
- return context(get_info<cl_context>(CL_KERNEL_CONTEXT));
- }
-
-
-
- template<class T>
- T get_info(cl_kernel_info info) const
- {
- return detail::get_object_info<T>(clGetKernelInfo, m_kernel, info);
- }
-
- template<int Enum>
- typename detail::get_object_info_type<kernel, Enum>::type
- get_info() const;
- #if defined(BOOST_COMPUTE_CL_VERSION_1_2) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class T>
- T get_arg_info(size_t index, cl_kernel_arg_info info) const
- {
- return detail::get_object_info<T>(
- clGetKernelArgInfo, m_kernel, info, static_cast<cl_uint>(index)
- );
- }
-
- template<int Enum>
- typename detail::get_object_info_type<kernel, Enum>::type
- get_arg_info(size_t index) const;
- #endif
-
-
-
- template<class T>
- T get_work_group_info(const device &device, cl_kernel_work_group_info info) const
- {
- return detail::get_object_info<T>(clGetKernelWorkGroupInfo, m_kernel, info, device.id());
- }
- #if defined(BOOST_COMPUTE_CL_VERSION_2_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
-
-
-
-
-
-
-
- template<class T>
- boost::optional<T> get_sub_group_info(const device &device, cl_kernel_sub_group_info info,
- const size_t input_size, const void * input) const
- {
- if(device.check_version(2, 1))
- {
- return detail::get_object_info<T>(
- clGetKernelSubGroupInfo, m_kernel, info, device.id(), input_size, input
- );
- }
- else if(!device.check_version(2, 0) || !device.supports_extension("cl_khr_subgroups"))
- {
- return boost::optional<T>();
- }
-
-
- else if(info != CL_KERNEL_MAX_SUB_GROUP_SIZE_FOR_NDRANGE && info != CL_KERNEL_SUB_GROUP_COUNT_FOR_NDRANGE)
- {
- return boost::optional<T>();
- }
- BOOST_COMPUTE_DISABLE_DEPRECATED_DECLARATIONS();
- clGetKernelSubGroupInfoKHR_fn clGetKernelSubGroupInfoKHR_fptr =
- reinterpret_cast<clGetKernelSubGroupInfoKHR_fn>(
- reinterpret_cast<size_t>(
- device.platform().get_extension_function_address("clGetKernelSubGroupInfoKHR")
- )
- );
- BOOST_COMPUTE_ENABLE_DEPRECATED_DECLARATIONS();
- return detail::get_object_info<T>(
- clGetKernelSubGroupInfoKHR_fptr, m_kernel, info, device.id(), input_size, input
- );
- }
-
- template<class T>
- boost::optional<T> get_sub_group_info(const device &device, cl_kernel_sub_group_info info) const
- {
- return get_sub_group_info<T>(device, info, 0, 0);
- }
-
- template<class T>
- boost::optional<T> get_sub_group_info(const device &device, cl_kernel_sub_group_info info,
- const size_t input) const
- {
- return get_sub_group_info<T>(device, info, sizeof(size_t), &input);
- }
- #endif
- #if defined(BOOST_COMPUTE_CL_VERSION_2_0) && !defined(BOOST_COMPUTE_CL_VERSION_2_1)
-
-
-
-
-
- template<class T>
- boost::optional<T> get_sub_group_info(const device &device, cl_kernel_sub_group_info info,
- const size_t input_size, const void * input) const
- {
- if(!device.check_version(2, 0) || !device.supports_extension("cl_khr_subgroups"))
- {
- return boost::optional<T>();
- }
- BOOST_COMPUTE_DISABLE_DEPRECATED_DECLARATIONS();
- clGetKernelSubGroupInfoKHR_fn clGetKernelSubGroupInfoKHR_fptr =
- reinterpret_cast<clGetKernelSubGroupInfoKHR_fn>(
- reinterpret_cast<size_t>(
- device.platform().get_extension_function_address("clGetKernelSubGroupInfoKHR")
- )
- );
- BOOST_COMPUTE_ENABLE_DEPRECATED_DECLARATIONS();
- return detail::get_object_info<T>(
- clGetKernelSubGroupInfoKHR_fptr, m_kernel, info, device.id(), input_size, input
- );
- }
- #endif
- #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
-
- template<class T>
- boost::optional<T> get_sub_group_info(const device &device, cl_kernel_sub_group_info info,
- const std::vector<size_t> input) const
- {
- BOOST_ASSERT(input.size() > 0);
- return get_sub_group_info<T>(device, info, input.size() * sizeof(size_t), &input[0]);
- }
- #endif
-
-
-
- void set_arg(size_t index, size_t size, const void *value)
- {
- BOOST_ASSERT(index < arity());
- cl_int ret = clSetKernelArg(m_kernel,
- static_cast<cl_uint>(index),
- size,
- value);
- if(ret != CL_SUCCESS){
- BOOST_THROW_EXCEPTION(opencl_error(ret));
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class T>
- void set_arg(size_t index, const T &value)
- {
-
-
- detail::set_kernel_arg<T>()(*this, index, value);
- }
-
- void set_arg(size_t index, const cl_mem mem)
- {
- set_arg(index, sizeof(cl_mem), static_cast<const void *>(&mem));
- }
-
- void set_arg(size_t index, const cl_sampler sampler)
- {
- set_arg(index, sizeof(cl_sampler), static_cast<const void *>(&sampler));
- }
-
- void set_arg_svm_ptr(size_t index, void* ptr)
- {
- #ifdef BOOST_COMPUTE_CL_VERSION_2_0
- cl_int ret = clSetKernelArgSVMPointer(m_kernel, static_cast<cl_uint>(index), ptr);
- if(ret != CL_SUCCESS){
- BOOST_THROW_EXCEPTION(opencl_error(ret));
- }
- #else
- (void) index;
- (void) ptr;
- BOOST_THROW_EXCEPTION(opencl_error(CL_INVALID_ARG_VALUE));
- #endif
- }
- #ifndef BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
-
- template<class... T>
- void set_args(T&&... args)
- {
- BOOST_ASSERT(sizeof...(T) <= arity());
- _set_args<0>(args...);
- }
- #endif
- #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
-
-
-
-
-
- void set_exec_info(cl_kernel_exec_info info, size_t size, const void *value)
- {
- cl_int ret = clSetKernelExecInfo(m_kernel, info, size, value);
- if(ret != CL_SUCCESS){
- BOOST_THROW_EXCEPTION(opencl_error(ret));
- }
- }
- #endif
-
- bool operator==(const kernel &other) const
- {
- return m_kernel == other.m_kernel;
- }
-
- bool operator!=(const kernel &other) const
- {
- return m_kernel != other.m_kernel;
- }
-
- operator cl_kernel() const
- {
- return m_kernel;
- }
-
- static kernel create_with_source(const std::string &source,
- const std::string &name,
- const context &context)
- {
- return program::build_with_source(source, context).create_kernel(name);
- }
- private:
- #ifndef BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
-
- template<size_t N>
- void _set_args()
- {
- }
-
- template<size_t N, class T, class... Args>
- void _set_args(T&& arg, Args&&... rest)
- {
- set_arg(N, arg);
- _set_args<N+1>(rest...);
- }
- #endif
- private:
- cl_kernel m_kernel;
- };
- inline kernel program::create_kernel(const std::string &name) const
- {
- return kernel(*this, name);
- }
- BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(kernel,
- ((std::string, CL_KERNEL_FUNCTION_NAME))
- ((cl_uint, CL_KERNEL_NUM_ARGS))
- ((cl_uint, CL_KERNEL_REFERENCE_COUNT))
- ((cl_context, CL_KERNEL_CONTEXT))
- ((cl_program, CL_KERNEL_PROGRAM))
- )
- #ifdef BOOST_COMPUTE_CL_VERSION_1_2
- BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(kernel,
- ((std::string, CL_KERNEL_ATTRIBUTES))
- )
- #endif
- #ifdef BOOST_COMPUTE_CL_VERSION_1_2
- #define BOOST_COMPUTE_DETAIL_DEFINE_KERNEL_GET_ARG_INFO_SPECIALIZATION(result_type, value) \
- namespace detail { \
- template<> struct get_object_info_type<kernel, value> { typedef result_type type; }; \
- } \
- template<> inline result_type kernel::get_arg_info<value>(size_t index) const { \
- return get_arg_info<result_type>(index, value); \
- }
- BOOST_COMPUTE_DETAIL_DEFINE_KERNEL_GET_ARG_INFO_SPECIALIZATION(cl_kernel_arg_address_qualifier, CL_KERNEL_ARG_ADDRESS_QUALIFIER)
- BOOST_COMPUTE_DETAIL_DEFINE_KERNEL_GET_ARG_INFO_SPECIALIZATION(cl_kernel_arg_access_qualifier, CL_KERNEL_ARG_ACCESS_QUALIFIER)
- BOOST_COMPUTE_DETAIL_DEFINE_KERNEL_GET_ARG_INFO_SPECIALIZATION(std::string, CL_KERNEL_ARG_TYPE_NAME)
- BOOST_COMPUTE_DETAIL_DEFINE_KERNEL_GET_ARG_INFO_SPECIALIZATION(cl_kernel_arg_type_qualifier, CL_KERNEL_ARG_TYPE_QUALIFIER)
- BOOST_COMPUTE_DETAIL_DEFINE_KERNEL_GET_ARG_INFO_SPECIALIZATION(std::string, CL_KERNEL_ARG_NAME)
- #endif
- namespace detail {
- template<class T>
- struct set_kernel_arg
- {
- typename boost::enable_if<is_fundamental<T> >::type
- operator()(kernel &kernel_, size_t index, const T &value)
- {
- kernel_.set_arg(index, sizeof(T), &value);
- }
- };
- template<>
- struct set_kernel_arg<char>
- {
- void operator()(kernel &kernel_, size_t index, const char c)
- {
- kernel_.set_arg(index, sizeof(char), &c);
- }
- };
- }
- }
- }
- #endif
|