Skip to content

Commit

Permalink
Port external memory functionality with clCreateBufferWithProperties … (
Browse files Browse the repository at this point in the history
#210)

* Port external memory functionality with clCreateBufferWithProperties wrapper

* Review changes
  • Loading branch information
Mikolaj-Filar-Mobica committed Apr 14, 2023
1 parent aa7ca00 commit 8f53665
Showing 1 changed file with 198 additions and 34 deletions.
232 changes: 198 additions & 34 deletions include/CL/opencl.hpp
@@ -1,5 +1,5 @@
//
// Copyright (c) 2008-2020 The Khronos Group Inc.
// Copyright (c) 2008-2023 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -705,7 +705,6 @@ namespace cl {
*
*/
namespace cl {
class Memory;

#define CL_HPP_CREATE_CL_EXT_FCN_PTR_ALIAS_(name) \
using PFN_##name = name##_fn
Expand All @@ -721,6 +720,11 @@ namespace cl {
clGetExtensionFunctionAddressForPlatform(platform, #name); \
}

#ifdef cl_khr_external_memory
enum class ExternalMemoryType : cl_external_memory_handle_type_khr;
#endif

class Memory;
class Program;
class Device;
class Context;
Expand Down Expand Up @@ -919,7 +923,6 @@ static inline cl_int errHandler (cl_int err, const char * errStr = nullptr)
#define __CREATE_PIPE_ERR CL_HPP_ERR_STR_(clCreatePipe)
#define __GET_PIPE_INFO_ERR CL_HPP_ERR_STR_(clGetPipeInfo)


#define __RETAIN_ERR CL_HPP_ERR_STR_(Retain Object)
#define __RELEASE_ERR CL_HPP_ERR_STR_(Release Object)
#define __FLUSH_ERR CL_HPP_ERR_STR_(clFlush)
Expand All @@ -935,13 +938,18 @@ static inline cl_int errHandler (cl_int err, const char * errStr = nullptr)
#define __SET_PROGRAM_SPECIALIZATION_CONSTANT_ERR CL_HPP_ERR_STR_(clSetProgramSpecializationConstant)
#endif

#ifdef cl_khr_external_memory
#define __ENQUEUE_ACQUIRE_EXTERNAL_MEMORY_ERR CL_HPP_ERR_STR_(clEnqueueAcquireExternalMemObjectsKHR)
#define __ENQUEUE_RELEASE_EXTERNAL_MEMORY_ERR CL_HPP_ERR_STR_(clEnqueueReleaseExternalMemObjectsKHR)
#endif

#ifdef cl_khr_semaphore
#define __GET_SEMAPHORE_KHR_INFO_ERR CL_HPP_ERR_STR_(clGetSemaphoreInfoKHR)
#define __CREATE_SEMAPHORE_KHR_WITH_PROPERTIES_ERR CL_HPP_ERR_STR_(clCreateSemaphoreWithPropertiesKHR)
#define __ENQUEUE_WAIT_SEMAPHORE_KHR_ERR CL_HPP_ERR_STR_(clEnqueueWaitSemaphoresKHR)
#define __ENQUEUE_SIGNAL_SEMAPHORE_KHR_ERR CL_HPP_ERR_STR_(clEnqueueSignalSemaphoresKHR)
#define __RETAIN_SEMAPHORE_KHR_ERR CL_HPP_ERR_STR_(clRetainSemaphoreKHR)
#define __RELEASE_SEMAPHORE_KHR_ERR CL_HPP_ERR_STR_(clReleaseSemaphoreKHR)
#define __GET_SEMAPHORE_KHR_INFO_ERR CL_HPP_ERR_STR_(clGetSemaphoreInfoKHR)
#define __CREATE_SEMAPHORE_KHR_WITH_PROPERTIES_ERR CL_HPP_ERR_STR_(clCreateSemaphoreWithPropertiesKHR)
#define __ENQUEUE_WAIT_SEMAPHORE_KHR_ERR CL_HPP_ERR_STR_(clEnqueueWaitSemaphoresKHR)
#define __ENQUEUE_SIGNAL_SEMAPHORE_KHR_ERR CL_HPP_ERR_STR_(clEnqueueSignalSemaphoresKHR)
#define __RETAIN_SEMAPHORE_KHR_ERR CL_HPP_ERR_STR_(clRetainSemaphoreKHR)
#define __RELEASE_SEMAPHORE_KHR_ERR CL_HPP_ERR_STR_(clReleaseSemaphoreKHR)
#endif
#if defined(cl_khr_command_buffer)
#define __CREATE_COMMAND_BUFFER_KHR_ERR CL_HPP_ERR_STR_(clCreateCommandBufferKHR)
Expand Down Expand Up @@ -1010,6 +1018,14 @@ static inline cl_int errHandler (cl_int err, const char * errStr = nullptr)
#endif // CL_HPP_USER_OVERRIDE_ERROR_STRINGS
//! \endcond

#ifdef cl_khr_external_memory
CL_HPP_CREATE_CL_EXT_FCN_PTR_ALIAS_(clEnqueueAcquireExternalMemObjectsKHR);
CL_HPP_CREATE_CL_EXT_FCN_PTR_ALIAS_(clEnqueueReleaseExternalMemObjectsKHR);

CL_HPP_DEFINE_STATIC_MEMBER_ PFN_clEnqueueAcquireExternalMemObjectsKHR pfn_clEnqueueAcquireExternalMemObjectsKHR = nullptr;
CL_HPP_DEFINE_STATIC_MEMBER_ PFN_clEnqueueReleaseExternalMemObjectsKHR pfn_clEnqueueReleaseExternalMemObjectsKHR = nullptr;
#endif // cl_khr_external_memory

#ifdef cl_khr_semaphore
CL_HPP_CREATE_CL_EXT_FCN_PTR_ALIAS_(clCreateSemaphoreWithPropertiesKHR);
CL_HPP_CREATE_CL_EXT_FCN_PTR_ALIAS_(clReleaseSemaphoreKHR);
Expand Down Expand Up @@ -1508,6 +1524,10 @@ inline cl_int getInfoHelper(Func f, cl_uint name, T* param, int, typename T::cl_
F(cl_platform_info, CL_PLATFORM_SEMAPHORE_TYPES_KHR, cl::vector<cl_semaphore_type_khr>) \
F(cl_device_info, CL_DEVICE_SEMAPHORE_TYPES_KHR, cl::vector<cl_semaphore_type_khr>) \

#define CL_HPP_PARAM_NAME_CL_KHR_EXTERNAL_MEMORY_(F) \
F(cl_device_info, CL_DEVICE_EXTERNAL_MEMORY_IMPORT_HANDLE_TYPES_KHR, cl::vector<cl::ExternalMemoryType>) \
F(cl_platform_info, CL_PLATFORM_EXTERNAL_MEMORY_IMPORT_HANDLE_TYPES_KHR, cl::vector<cl::ExternalMemoryType>)

#define CL_HPP_PARAM_NAME_INFO_3_0_(F) \
F(cl_platform_info, CL_PLATFORM_NUMERIC_VERSION, cl_version) \
F(cl_platform_info, CL_PLATFORM_EXTENSIONS_WITH_VERSION, cl::vector<cl_name_version>) \
Expand Down Expand Up @@ -1611,6 +1631,10 @@ CL_HPP_PARAM_NAME_CL_KHR_EXTENDED_VERSIONING_KHRONLY_(CL_HPP_DECLARE_PARAM_TRAIT
CL_HPP_PARAM_NAME_CL_KHR_SEMAPHORE_(CL_HPP_DECLARE_PARAM_TRAITS_)
#endif // cl_khr_semaphore

#ifdef cl_khr_external_memory
CL_HPP_PARAM_NAME_CL_KHR_EXTERNAL_MEMORY_(CL_HPP_DECLARE_PARAM_TRAITS_)
#endif // cl_khr_external_memory

#if defined(cl_khr_device_uuid)
using uuid_array = array<cl_uchar, CL_UUID_SIZE_KHR>;
using luid_array = array<cl_uchar, CL_LUID_SIZE_KHR>;
Expand Down Expand Up @@ -4089,32 +4113,78 @@ class Buffer : public Memory
}
}

/*! \brief Constructs a Buffer in the default context.
#if CL_HPP_TARGET_OPENCL_VERSION >= 300
/*! \brief Constructs a Buffer in a specified context and with specified properties.
*
* Wraps clCreateBuffer().
* Wraps clCreateBufferWithProperties().
*
* \param properties Optional list of properties for the buffer object and
* their corresponding values. The non-empty list must
* end with 0.
* \param host_ptr Storage to be used if the CL_MEM_USE_HOST_PTR flag was
* specified. Note alignment & exclusivity requirements.
*
* \see Context::getDefault()
* specified. Note alignment & exclusivity requirements.
*/
Buffer(
cl_mem_flags flags,
const Context& context,
const vector<cl_mem_properties>& properties,
cl_mem_flags flags,
size_type size,
void* host_ptr = nullptr,
cl_int* err = nullptr)
{
cl_int error;

Context context = Context::getDefault(err);

object_ = ::clCreateBuffer(context(), flags, size, host_ptr, &error);
if (properties.empty()) {
object_ = ::clCreateBufferWithProperties(context(), nullptr, flags,
size, host_ptr, &error);
}
else {
object_ = ::clCreateBufferWithProperties(
context(), properties.data(), flags, size, host_ptr, &error);
}

detail::errHandler(error, __CREATE_BUFFER_ERR);
if (err != nullptr) {
*err = error;
}
}
#endif

/*! \brief Constructs a Buffer in the default context.
*
* Wraps clCreateBuffer().
*
* \param host_ptr Storage to be used if the CL_MEM_USE_HOST_PTR flag was
* specified. Note alignment & exclusivity requirements.
*
* \see Context::getDefault()
*/
Buffer(
cl_mem_flags flags,
size_type size,
void* host_ptr = nullptr,
cl_int* err = nullptr) : Buffer(Context::getDefault(err), flags, size, host_ptr, err) { }

#if CL_HPP_TARGET_OPENCL_VERSION >= 300
/*! \brief Constructs a Buffer in the default context and with specified properties.
*
* Wraps clCreateBufferWithProperties().
*
* \param properties Optional list of properties for the buffer object and
* their corresponding values. The non-empty list must
* end with 0.
* \param host_ptr Storage to be used if the CL_MEM_USE_HOST_PTR flag was
* specified. Note alignment & exclusivity requirements.
*
* \see Context::getDefault()
*/
Buffer(
const vector<cl_mem_properties>& properties,
cl_mem_flags flags,
size_type size,
void* host_ptr = nullptr,
cl_int* err = nullptr) : Buffer(Context::getDefault(err), properties, flags, size, host_ptr, err) { }
#endif

/*!
* \brief Construct a Buffer from a host container via iterators.
Expand Down Expand Up @@ -6771,6 +6841,25 @@ inline Kernel::Kernel(const Program& program, const char* name, cl_int* err)

}

#ifdef cl_khr_external_memory
enum class ExternalMemoryType : cl_external_memory_handle_type_khr
{
None = 0,

OpaqueFd = CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_FD_KHR,
OpaqueWin32 = CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR,
OpaqueWin32Kmt = CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KMT_KHR,

D3D11Texture = CL_EXTERNAL_MEMORY_HANDLE_D3D11_TEXTURE_KHR,
D3D11TextureKmt = CL_EXTERNAL_MEMORY_HANDLE_D3D11_TEXTURE_KMT_KHR,

D3D12Heap = CL_EXTERNAL_MEMORY_HANDLE_D3D12_HEAP_KHR,
D3D12Resource = CL_EXTERNAL_MEMORY_HANDLE_D3D12_RESOURCE_KHR,

DmaBuf = CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR,
};
#endif

enum class QueueProperties : cl_command_queue_properties
{
None = 0,
Expand Down Expand Up @@ -6839,6 +6928,24 @@ class CommandQueue : public detail::Wrapper<cl_command_queue>
default_ = c;
}

#ifdef cl_khr_external_memory
static std::once_flag ext_memory_initialized_;

static void initMemoryExtension(const cl::Device& device)
{
auto platform = device.getInfo<CL_DEVICE_PLATFORM>();

CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_(platform, clEnqueueAcquireExternalMemObjectsKHR);
CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_(platform, clEnqueueReleaseExternalMemObjectsKHR);

if ((pfn_clEnqueueAcquireExternalMemObjectsKHR == nullptr)
&& (pfn_clEnqueueReleaseExternalMemObjectsKHR == nullptr))
{
detail::errHandler(CL_INVALID_VALUE, __ENQUEUE_ACQUIRE_EXTERNAL_MEMORY_ERR);
}
}
#endif // cl_khr_external_memory

public:
#ifdef CL_HPP_UNIT_TEST_ENABLE
/*! \brief Reset the default.
Expand Down Expand Up @@ -8198,8 +8305,8 @@ class CommandQueue : public detail::Wrapper<cl_command_queue>
* have completed.
*/
cl_int enqueueMarkerWithWaitList(
const vector<Event> *events = 0,
Event *event = 0) const
const vector<Event> *events = nullptr,
Event *event = nullptr) const
{
cl_event tmp;
cl_int err = detail::errHandler(
Expand Down Expand Up @@ -8228,8 +8335,8 @@ class CommandQueue : public detail::Wrapper<cl_command_queue>
* before this command to command_queue, have completed.
*/
cl_int enqueueBarrierWithWaitList(
const vector<Event> *events = 0,
Event *event = 0) const
const vector<Event> *events = nullptr,
Event *event = nullptr) const
{
cl_event tmp;
cl_int err = detail::errHandler(
Expand Down Expand Up @@ -8461,21 +8568,12 @@ class CommandQueue : public detail::Wrapper<cl_command_queue>
const vector<Event>* events = nullptr,
Event* event = nullptr) const
{
size_type elements = 0;
if (mem_objects != nullptr) {
elements = mem_objects->size();
}
vector<cl_mem> mems(elements);
for (unsigned int i = 0; i < elements; i++) {
mems[i] = ((*mem_objects)[i])();
}

cl_event tmp;
cl_int err = detail::errHandler(
::clEnqueueNativeKernel(
object_, userFptr, args.first, args.second,
(mem_objects != nullptr) ? (cl_uint) mem_objects->size() : 0,
mems.data(),
(mem_objects->size() > 0 ) ? reinterpret_cast<const cl_mem *>(mem_objects->data()) : nullptr,
(mem_locs != nullptr && mem_locs->size() > 0) ? (const void **) &mem_locs->front() : nullptr,
(events != nullptr) ? (cl_uint) events->size() : 0,
(events != nullptr && events->size() > 0) ? (cl_event*) &events->front() : nullptr,
Expand Down Expand Up @@ -8664,6 +8762,66 @@ typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueReleaseD3D10ObjectsKHR)(
return detail::errHandler(::clFinish(object_), __FINISH_ERR);
}

#ifdef cl_khr_external_memory
cl_int enqueueAcquireExternalMemObjects(
const vector<Memory>& mem_objects,
const vector<Event>* events_wait = nullptr,
Event *event = nullptr)
{
cl_int err = CL_INVALID_OPERATION;
cl_event tmp;

std::call_once(ext_memory_initialized_, initMemoryExtension, this->getInfo<CL_QUEUE_DEVICE>());

if (pfn_clEnqueueAcquireExternalMemObjectsKHR)
{
err = pfn_clEnqueueAcquireExternalMemObjectsKHR(
object_,
static_cast<cl_uint>(mem_objects.size()),
(mem_objects.size() > 0) ? reinterpret_cast<const cl_mem *>(mem_objects.data()) : nullptr,
(events_wait != nullptr) ? static_cast<cl_uint>(events_wait->size()) : 0,
(events_wait != nullptr && events_wait->size() > 0) ? reinterpret_cast<const cl_event*>(events_wait->data()) : nullptr,
&tmp);
}

detail::errHandler(err, __ENQUEUE_ACQUIRE_EXTERNAL_MEMORY_ERR);

if (event != nullptr && err == CL_SUCCESS)
*event = tmp;

return err;
}

cl_int enqueueReleaseExternalMemObjects(
const vector<Memory>& mem_objects,
const vector<Event>* events_wait = nullptr,
Event *event = nullptr)
{
cl_int err = CL_INVALID_OPERATION;
cl_event tmp;

std::call_once(ext_memory_initialized_, initMemoryExtension, this->getInfo<CL_QUEUE_DEVICE>());

if (pfn_clEnqueueReleaseExternalMemObjectsKHR)
{
err = pfn_clEnqueueReleaseExternalMemObjectsKHR(
object_,
static_cast<cl_uint>(mem_objects.size()),
(mem_objects.size() > 0) ? reinterpret_cast<const cl_mem *>(mem_objects.data()) : nullptr,
(events_wait != nullptr) ? static_cast<cl_uint>(events_wait->size()) : 0,
(events_wait != nullptr && events_wait->size() > 0) ? reinterpret_cast<const cl_event*>(events_wait->data()) : nullptr,
&tmp);
}

detail::errHandler(err, __ENQUEUE_RELEASE_EXTERNAL_MEMORY_ERR);

if (event != nullptr && err == CL_SUCCESS)
*event = tmp;

return err;
}
#endif // cl_khr_external_memory && CL_HPP_TARGET_OPENCL_VERSION >= 300

#ifdef cl_khr_semaphore
cl_int enqueueWaitSemaphores(
const vector<Semaphore> &sema_objects,
Expand All @@ -8679,6 +8837,10 @@ typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueReleaseD3D10ObjectsKHR)(
#endif // cl_khr_semaphore
}; // CommandQueue

#ifdef cl_khr_external_memory
CL_HPP_DEFINE_STATIC_MEMBER_ std::once_flag CommandQueue::ext_memory_initialized_;
#endif

CL_HPP_DEFINE_STATIC_MEMBER_ std::once_flag CommandQueue::default_initialized_;
CL_HPP_DEFINE_STATIC_MEMBER_ CommandQueue CommandQueue::default_;
CL_HPP_DEFINE_STATIC_MEMBER_ cl_int CommandQueue::default_error_ = CL_SUCCESS;
Expand Down Expand Up @@ -11066,8 +11228,9 @@ class MutableCommandKhr : public detail::Wrapper<cl_mutable_command_khr>
#undef __FLUSH_ERR
#undef __FINISH_ERR
#undef __VECTOR_CAPACITY_ERR
#undef __CREATE_SUB_DEVICES_ERR
#undef __CREATE_SUB_DEVICES_ERR
#undef __CREATE_SUB_DEVICES_ERR
#undef __ENQUEUE_ACQUIRE_EXTERNAL_MEMORY_ERR
#undef __ENQUEUE_RELEASE_EXTERNAL_MEMORY_ERR
#undef __ENQUEUE_MARKER_ERR
#undef __ENQUEUE_WAIT_FOR_EVENTS_ERR
#undef __ENQUEUE_BARRIER_ERR
Expand All @@ -11092,6 +11255,7 @@ class MutableCommandKhr : public detail::Wrapper<cl_mutable_command_khr>
#endif //CL_HPP_USER_OVERRIDE_ERROR_STRINGS

// Extensions
#undef CL_HPP_CREATE_CL_EXT_FCN_PTR_ALIAS_
#undef CL_HPP_INIT_CL_EXT_FCN_PTR_
#undef CL_HPP_INIT_CL_EXT_FCN_PTR_PLATFORM_

Expand Down

0 comments on commit 8f53665

Please sign in to comment.