From 8f21274bf9efac184d2a34c9a0968ed6cc8448c1 Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Tue, 9 Apr 2024 15:30:34 +0200 Subject: [PATCH 1/5] Added missing interface for CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR property --- layers/11_semaemu/emulate.cpp | 58 +++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index 8791398..f718457 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -48,6 +48,8 @@ typedef struct _cl_semaphore_khr ptrdiff_t numProperties = 0; cl_semaphore_type_khr type = ~0; + std::vector devices; + if( properties ) { const cl_semaphore_properties_khr* check = properties; @@ -78,10 +80,11 @@ typedef struct _cl_semaphore_khr else { found_CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR = true; - ++check; - while(*check++ != CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR) + check++; + while(*check != CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR) { - // TODO: validate device handles. + devices.push_back(((cl_device_id*)check)[0]); + check++; } } break; @@ -91,6 +94,38 @@ typedef struct _cl_semaphore_khr } } numProperties = check - properties + 1; + + // validate device handles. + if (!devices.empty()) { + std::vector types; + for (int i = devices.size() - 1; i >= 0; i--) { + size_t num_types_size = 0; + clGetDeviceInfo(devices[i], + CL_DEVICE_SEMAPHORE_TYPES_KHR, + 0, nullptr, &num_types_size); + + if (!num_types_size) + continue; + + types.resize(num_types_size / sizeof(cl_semaphore_type_khr)); + clGetDeviceInfo( + devices[i], CL_DEVICE_SEMAPHORE_TYPES_KHR, num_types_size, + types.data(), nullptr); + + bool capable = false; + for (auto sema_type : types) { + if (type == sema_type) { + capable = true; + break; + } + } + + if (!capable) { + errorCode = CL_INVALID_DEVICE; + break; + } + } + } } switch( type ) { @@ -111,6 +146,12 @@ typedef struct _cl_semaphore_khr semaphore->Properties.begin(), properties, properties + numProperties ); + + if (!devices.empty()) + semaphore->Devices.assign( + devices.begin(), + devices.end() ); + } return semaphore; } @@ -124,6 +165,7 @@ typedef struct _cl_semaphore_khr const cl_context Context; const cl_semaphore_type_khr Type; std::vector Properties; + std::vector Devices; std::atomic RefCount; cl_event Event; @@ -349,6 +391,16 @@ cl_int CL_API_CALL clGetSemaphoreInfoKHR_EMU( ptr ); } break; + case CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR: + { + auto ptr = (cl_device_id*)param_value; + return writeVectorToMemory( + param_value_size, + semaphore->Devices, + param_value_size_ret, + ptr ); + } + break; default: return CL_INVALID_VALUE; } From 7f8e6141d47f41d704a65b9f56c5beaffd851627 Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Mon, 6 May 2024 13:54:30 +0200 Subject: [PATCH 2/5] approach to correct CI checks errors around semaphore emulation layer to handle CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR flag --- layers/11_semaemu/emulate.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index f718457..cec6865 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -100,17 +100,17 @@ typedef struct _cl_semaphore_khr std::vector types; for (int i = devices.size() - 1; i >= 0; i--) { size_t num_types_size = 0; - clGetDeviceInfo(devices[i], - CL_DEVICE_SEMAPHORE_TYPES_KHR, - 0, nullptr, &num_types_size); + clGetDeviceInfo_override(devices[i], + CL_DEVICE_SEMAPHORE_TYPES_KHR, 0, + nullptr, &num_types_size, &errorCode); if (!num_types_size) continue; types.resize(num_types_size / sizeof(cl_semaphore_type_khr)); - clGetDeviceInfo( + clGetDeviceInfo_override( devices[i], CL_DEVICE_SEMAPHORE_TYPES_KHR, num_types_size, - types.data(), nullptr); + types.data(), nullptr, &errorCode); bool capable = false; for (auto sema_type : types) { From a82ba928921991faef81661a6baff593a47c8169 Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Tue, 21 May 2024 10:45:09 +0200 Subject: [PATCH 3/5] Corrections related to code review --- include/layer_util.hpp | 23 +++++++++++++++++++ layers/11_semaemu/emulate.cpp | 43 ++++++++++++----------------------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/include/layer_util.hpp b/include/layer_util.hpp index 7e3c036..c1b8e7c 100644 --- a/include/layer_util.hpp +++ b/include/layer_util.hpp @@ -134,3 +134,26 @@ static inline bool checkStringForExtension( return supported; } + +static bool isDeviceWithinContext(const cl_context context, + const cl_device_id device) +{ + cl_uint numDevices = 0; + cl_int error = clGetContextInfo(context, CL_CONTEXT_NUM_DEVICES, + sizeof(cl_uint), &numDevices, NULL); + if(error!=CL_SUCCESS || numDevices == 0) + return false; + + std::vector devices(numDevices, 0); + error = + clGetContextInfo(context, CL_CONTEXT_DEVICES, + numDevices * sizeof(cl_device_id), devices.data(), NULL); + if(error!=CL_SUCCESS) + return false; + + for ( auto dev : devices ) + if ( dev == device ) + return true; + + return false; +} diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index cec6865..f995403 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -97,33 +97,22 @@ typedef struct _cl_semaphore_khr // validate device handles. if (!devices.empty()) { - std::vector types; - for (int i = devices.size() - 1; i >= 0; i--) { - size_t num_types_size = 0; - clGetDeviceInfo_override(devices[i], - CL_DEVICE_SEMAPHORE_TYPES_KHR, 0, - nullptr, &num_types_size, &errorCode); - - if (!num_types_size) - continue; - - types.resize(num_types_size / sizeof(cl_semaphore_type_khr)); - clGetDeviceInfo_override( - devices[i], CL_DEVICE_SEMAPHORE_TYPES_KHR, num_types_size, - types.data(), nullptr, &errorCode); - - bool capable = false; - for (auto sema_type : types) { - if (type == sema_type) { - capable = true; + // for now - if CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR is specified + // as part of sema_props, but it does not identify exactly one + // valid device + if (devices.size() > 1) { + errorCode = CL_INVALID_DEVICE; + } else { + // if a device identified by CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR + // is not one of the devices within context + std::vector types; + for (auto device : devices) { + if (device == nullptr || + !isDeviceWithinContext(context, device)) { + errorCode = CL_INVALID_DEVICE; break; } } - - if (!capable) { - errorCode = CL_INVALID_DEVICE; - break; - } } } } @@ -147,11 +136,7 @@ typedef struct _cl_semaphore_khr properties, properties + numProperties ); - if (!devices.empty()) - semaphore->Devices.assign( - devices.begin(), - devices.end() ); - + semaphore->Devices=devices; } return semaphore; } From a3f87709d4a64c5981c19e82496ed23447d2555f Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Tue, 21 May 2024 11:35:51 +0200 Subject: [PATCH 4/5] Corrections due to failed CI check --- include/layer_util.hpp | 23 ----------------------- layers/11_semaemu/emulate.cpp | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/include/layer_util.hpp b/include/layer_util.hpp index c1b8e7c..7e3c036 100644 --- a/include/layer_util.hpp +++ b/include/layer_util.hpp @@ -134,26 +134,3 @@ static inline bool checkStringForExtension( return supported; } - -static bool isDeviceWithinContext(const cl_context context, - const cl_device_id device) -{ - cl_uint numDevices = 0; - cl_int error = clGetContextInfo(context, CL_CONTEXT_NUM_DEVICES, - sizeof(cl_uint), &numDevices, NULL); - if(error!=CL_SUCCESS || numDevices == 0) - return false; - - std::vector devices(numDevices, 0); - error = - clGetContextInfo(context, CL_CONTEXT_DEVICES, - numDevices * sizeof(cl_device_id), devices.data(), NULL); - if(error!=CL_SUCCESS) - return false; - - for ( auto dev : devices ) - if ( dev == device ) - return true; - - return false; -} diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index f995403..dc69a99 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -35,6 +35,28 @@ SLayerContext& getLayerContext(void) return c; } +static bool isDeviceWithinContext(const cl_context context, + const cl_device_id device) { + cl_uint numDevices = 0; + cl_int error = g_pNextDispatch->clGetContextInfo( + context, CL_CONTEXT_NUM_DEVICES, sizeof(cl_uint), &numDevices, NULL); + if (error != CL_SUCCESS || numDevices == 0) + return false; + + std::vector devices(numDevices, 0); + error = g_pNextDispatch->clGetContextInfo(context, CL_CONTEXT_DEVICES, + numDevices * sizeof(cl_device_id), + devices.data(), NULL); + if (error != CL_SUCCESS) + return false; + + for (auto dev : devices) + if (dev == device) + return true; + + return false; +} + typedef struct _cl_semaphore_khr { static _cl_semaphore_khr* create( From d86e0bfb7555f6e79aa6793f2a043265a936c91e Mon Sep 17 00:00:00 2001 From: Marcin Hajder Date: Mon, 3 Jun 2024 09:12:36 +0200 Subject: [PATCH 5/5] Take into account additional property field to distinguish between CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR and array terminator --- layers/11_semaemu/emulate.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/layers/11_semaemu/emulate.cpp b/layers/11_semaemu/emulate.cpp index dc69a99..f7a1f44 100644 --- a/layers/11_semaemu/emulate.cpp +++ b/layers/11_semaemu/emulate.cpp @@ -108,6 +108,7 @@ typedef struct _cl_semaphore_khr devices.push_back(((cl_device_id*)check)[0]); check++; } + check++; } break; default: