From a89239bc609eda00930bb8ae4837ef9efdedac60 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Wed, 4 Oct 2023 13:56:38 -0700 Subject: [PATCH] add command buffer support for SVM copies and fills (#90) --- layers/10_cmdbufemu/emulate.cpp | 186 +++++++++++++++++++++++++++++++- layers/10_cmdbufemu/emulate.h | 23 ++++ layers/10_cmdbufemu/main.cpp | 2 + 3 files changed, 210 insertions(+), 1 deletion(-) diff --git a/layers/10_cmdbufemu/emulate.cpp b/layers/10_cmdbufemu/emulate.cpp index b485324..ea00903 100644 --- a/layers/10_cmdbufemu/emulate.cpp +++ b/layers/10_cmdbufemu/emulate.cpp @@ -20,7 +20,7 @@ #include "emulate.h" static constexpr cl_version version_cl_khr_command_buffer = - CL_MAKE_VERSION(0, 9, 2); + CL_MAKE_VERSION(0, 9, 4); static constexpr cl_version version_cl_khr_command_buffer_mutable_dispatch = CL_MAKE_VERSION(0, 9, 0); @@ -701,6 +701,106 @@ struct FillImage : Command cl_command_queue queue) : Command(cmdbuf, queue, CL_COMMAND_FILL_IMAGE) {}; }; +struct SVMMemcpy : Command +{ + static std::unique_ptr create( + cl_command_buffer_khr cmdbuf, + cl_command_queue queue, + void* dst_ptr, + const void* src_ptr, + size_t size) + { + auto ret = std::unique_ptr( + new SVMMemcpy(cmdbuf, queue)); + + ret->dst_ptr = dst_ptr; + ret->src_ptr = src_ptr; + ret->size = size; + + return ret; + } + + int playback( + cl_command_queue queue, + std::vector& deps) const override + { + auto wait_list = getEventWaitList(deps); + auto signal = getEventSignalPtr(deps); + return g_pNextDispatch->clEnqueueSVMMemcpy( + queue, + CL_FALSE, + dst_ptr, + src_ptr, + size, + static_cast(wait_list.size()), + wait_list.data(), + signal); + } + + void* dst_ptr = nullptr; + const void* src_ptr = nullptr; + size_t size = 0; + +private: + SVMMemcpy( + cl_command_buffer_khr cmdbuf, + cl_command_queue queue) : Command(cmdbuf, queue, CL_COMMAND_SVM_MEMCPY) {}; +}; + +struct SVMMemFill : Command +{ + static std::unique_ptr create( + cl_command_buffer_khr cmdbuf, + cl_command_queue queue, + void* dst_ptr, + const void* pattern, + size_t pattern_size, + size_t size) + { + auto ret = std::unique_ptr( + new SVMMemFill(cmdbuf, queue)); + + ret->dst_ptr = dst_ptr; + + auto p = reinterpret_cast(pattern); + ret->pattern.reserve(pattern_size); + ret->pattern.insert( + ret->pattern.begin(), + p, + p + pattern_size); + + ret->size = size; + + return ret; + } + + int playback( + cl_command_queue queue, + std::vector& deps) const override + { + auto wait_list = getEventWaitList(deps); + auto signal = getEventSignalPtr(deps); + return g_pNextDispatch->clEnqueueSVMMemFill( + queue, + dst_ptr, + pattern.data(), + pattern.size(), + size, + static_cast(wait_list.size()), + wait_list.data(), + signal); + } + + void* dst_ptr = nullptr; + std::vector pattern; + size_t size = 0; + +private: + SVMMemFill( + cl_command_buffer_khr cmdbuf, + cl_command_queue queue) : Command(cmdbuf, queue, CL_COMMAND_SVM_MEMFILL) {}; +}; + struct NDRangeKernel : Command { static std::unique_ptr create( @@ -1977,6 +2077,90 @@ cl_int CL_API_CALL clCommandFillImageKHR_EMU( return CL_SUCCESS; } +/////////////////////////////////////////////////////////////////////////////// +// +// cl_khr_command_buffer +cl_int CL_API_CALL clCommandSVMMemcpyKHR_EMU( + cl_command_buffer_khr cmdbuf, + cl_command_queue command_queue, + void* dst_ptr, + const void* src_ptr, + size_t size, + cl_uint num_sync_points_in_wait_list, + const cl_sync_point_khr* sync_point_wait_list, + cl_sync_point_khr* sync_point, + cl_mutable_command_khr* mutable_handle) +{ + if( !CommandBuffer::isValid(cmdbuf) ) + { + return CL_INVALID_COMMAND_BUFFER_KHR; + } + if( cl_int errorCode = cmdbuf->checkRecordErrors( + command_queue, + num_sync_points_in_wait_list, + sync_point_wait_list, + mutable_handle) ) + { + return errorCode; + } + + cmdbuf->addCommand( + SVMMemcpy::create( + cmdbuf, + command_queue, + dst_ptr, + src_ptr, + size), + num_sync_points_in_wait_list, + sync_point_wait_list, + sync_point, + mutable_handle); + return CL_SUCCESS; +} + +/////////////////////////////////////////////////////////////////////////////// +// +// cl_khr_command_buffer +cl_int CL_API_CALL clCommandSVMMemFillKHR_EMU( + cl_command_buffer_khr cmdbuf, + cl_command_queue command_queue, + void* dst_ptr, + const void* pattern, + size_t pattern_size, + size_t size, + cl_uint num_sync_points_in_wait_list, + const cl_sync_point_khr* sync_point_wait_list, + cl_sync_point_khr* sync_point, + cl_mutable_command_khr* mutable_handle) +{ + if( !CommandBuffer::isValid(cmdbuf) ) + { + return CL_INVALID_COMMAND_BUFFER_KHR; + } + if( cl_int errorCode = cmdbuf->checkRecordErrors( + command_queue, + num_sync_points_in_wait_list, + sync_point_wait_list, + mutable_handle) ) + { + return errorCode; + } + + cmdbuf->addCommand( + SVMMemFill::create( + cmdbuf, + command_queue, + dst_ptr, + pattern, + pattern_size, + size), + num_sync_points_in_wait_list, + sync_point_wait_list, + sync_point, + mutable_handle); + return CL_SUCCESS; +} + /////////////////////////////////////////////////////////////////////////////// // // cl_khr_command_buffer diff --git a/layers/10_cmdbufemu/emulate.h b/layers/10_cmdbufemu/emulate.h index 387228a..506f195 100644 --- a/layers/10_cmdbufemu/emulate.h +++ b/layers/10_cmdbufemu/emulate.h @@ -147,6 +147,29 @@ cl_int CL_API_CALL clCommandFillImageKHR_EMU( cl_sync_point_khr* sync_point, cl_mutable_command_khr* mutable_handle); +cl_int CL_API_CALL clCommandSVMMemcpyKHR_EMU( + cl_command_buffer_khr command_buffer, + cl_command_queue command_queue, + void* dst_ptr, + const void* src_ptr, + size_t size, + cl_uint num_sync_points_in_wait_list, + const cl_sync_point_khr* sync_point_wait_list, + cl_sync_point_khr* sync_point, + cl_mutable_command_khr* mutable_handle); + +cl_int CL_API_CALL clCommandSVMMemFillKHR_EMU( + cl_command_buffer_khr command_buffer, + cl_command_queue command_queue, + void* svm_ptr, + const void* pattern, + size_t pattern_size, + size_t size, + cl_uint num_sync_points_in_wait_list, + const cl_sync_point_khr* sync_point_wait_list, + cl_sync_point_khr* sync_point, + cl_mutable_command_khr* mutable_handle); + cl_int CL_API_CALL clCommandNDRangeKernelKHR_EMU( cl_command_buffer_khr command_buffer, cl_command_queue command_queue, diff --git a/layers/10_cmdbufemu/main.cpp b/layers/10_cmdbufemu/main.cpp index 9299140..032753d 100644 --- a/layers/10_cmdbufemu/main.cpp +++ b/layers/10_cmdbufemu/main.cpp @@ -139,6 +139,8 @@ clGetExtensionFunctionAddressForPlatform_layer( CHECK_RETURN_EXTENSION_FUNCTION( clCommandCopyImageToBufferKHR ); CHECK_RETURN_EXTENSION_FUNCTION( clCommandFillBufferKHR ); CHECK_RETURN_EXTENSION_FUNCTION( clCommandFillImageKHR ); + CHECK_RETURN_EXTENSION_FUNCTION( clCommandSVMMemcpyKHR ); + CHECK_RETURN_EXTENSION_FUNCTION( clCommandSVMMemFillKHR ); CHECK_RETURN_EXTENSION_FUNCTION( clCommandNDRangeKernelKHR ); CHECK_RETURN_EXTENSION_FUNCTION( clGetCommandBufferInfoKHR );