From 61ba805ae1b47931e99e1aa1bc74c38404c6e971 Mon Sep 17 00:00:00 2001 From: omarahmed1111 Date: Mon, 15 Jan 2024 15:36:13 +0000 Subject: [PATCH] Move platform and device version query to the handle --- source/adapters/opencl/context.cpp | 18 ------- source/adapters/opencl/context.hpp | 6 --- source/adapters/opencl/device.cpp | 79 +++++++---------------------- source/adapters/opencl/device.hpp | 46 ++++++++++++++--- source/adapters/opencl/memory.cpp | 2 +- source/adapters/opencl/platform.cpp | 19 ------- source/adapters/opencl/platform.hpp | 23 +++++++-- source/adapters/opencl/program.cpp | 33 ++++++------ source/adapters/opencl/queue.cpp | 6 +-- 9 files changed, 92 insertions(+), 140 deletions(-) diff --git a/source/adapters/opencl/context.cpp b/source/adapters/opencl/context.cpp index 020139d0aa..bc26e3580a 100644 --- a/source/adapters/opencl/context.cpp +++ b/source/adapters/opencl/context.cpp @@ -14,24 +14,6 @@ #include #include -ur_result_t cl_adapter::getDevicesFromContext( - ur_context_handle_t hContext, - std::unique_ptr> &DevicesInCtx) { - - cl_uint DeviceCount = hContext->DeviceCount; - - if (DeviceCount < 1) { - return UR_RESULT_ERROR_INVALID_CONTEXT; - } - - DevicesInCtx = std::make_unique>(DeviceCount); - for (size_t i = 0; i < DeviceCount; i++) { - (*DevicesInCtx)[i] = hContext->Devices[i]->get(); - } - - return UR_RESULT_SUCCESS; -} - UR_APIEXPORT ur_result_t UR_APICALL urContextCreate( uint32_t DeviceCount, const ur_device_handle_t *phDevices, const ur_context_properties_t *, ur_context_handle_t *phContext) { diff --git a/source/adapters/opencl/context.hpp b/source/adapters/opencl/context.hpp index 5f6c186db3..ab1d38f518 100644 --- a/source/adapters/opencl/context.hpp +++ b/source/adapters/opencl/context.hpp @@ -14,12 +14,6 @@ #include -namespace cl_adapter { -ur_result_t -getDevicesFromContext(ur_context_handle_t hContext, - std::unique_ptr> &DevicesInCtx); -} - struct ur_context_handle_t_ { using native_type = cl_context; native_type Context; diff --git a/source/adapters/opencl/device.cpp b/source/adapters/opencl/device.cpp index c5a2afd0d4..1ca7471d1d 100644 --- a/source/adapters/opencl/device.cpp +++ b/source/adapters/opencl/device.cpp @@ -12,46 +12,6 @@ #include -ur_result_t cl_adapter::getDeviceVersion(cl_device_id Dev, - oclv::OpenCLVersion &Version) { - - size_t DevVerSize = 0; - CL_RETURN_ON_FAILURE( - clGetDeviceInfo(Dev, CL_DEVICE_VERSION, 0, nullptr, &DevVerSize)); - - std::string DevVer(DevVerSize, '\0'); - CL_RETURN_ON_FAILURE(clGetDeviceInfo(Dev, CL_DEVICE_VERSION, DevVerSize, - DevVer.data(), nullptr)); - - Version = oclv::OpenCLVersion(DevVer); - if (!Version.isValid()) { - return UR_RESULT_ERROR_INVALID_DEVICE; - } - - return UR_RESULT_SUCCESS; -} - -ur_result_t cl_adapter::checkDeviceExtensions( - cl_device_id Dev, const std::vector &Exts, bool &Supported) { - size_t ExtSize = 0; - CL_RETURN_ON_FAILURE( - clGetDeviceInfo(Dev, CL_DEVICE_EXTENSIONS, 0, nullptr, &ExtSize)); - - std::string ExtStr(ExtSize, '\0'); - - CL_RETURN_ON_FAILURE(clGetDeviceInfo(Dev, CL_DEVICE_EXTENSIONS, ExtSize, - ExtStr.data(), nullptr)); - - Supported = true; - for (const std::string &Ext : Exts) { - if (!(Supported = (ExtStr.find(Ext) != std::string::npos))) { - break; - } - } - - return UR_RESULT_SUCCESS; -} - UR_APIEXPORT ur_result_t UR_APICALL urDeviceGet(ur_platform_handle_t hPlatform, ur_device_type_t DeviceType, [[maybe_unused]] uint32_t NumEntries, ur_device_handle_t *phDevices, @@ -351,8 +311,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, } case UR_DEVICE_INFO_DEVICE_ID: { bool Supported = false; - CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions( - hDevice->get(), {"cl_khr_pci_bus_info"}, Supported)); + CL_RETURN_ON_FAILURE( + hDevice->checkDeviceExtensions({"cl_khr_pci_bus_info"}, Supported)); if (!Supported) { return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION; @@ -367,7 +327,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, case UR_DEVICE_INFO_BACKEND_RUNTIME_VERSION: { oclv::OpenCLVersion Version; - CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), Version)); + CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(Version)); const std::string Results = std::to_string(Version.getMajor()) + "." + std::to_string(Version.getMinor()); @@ -470,7 +430,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, /* Corresponding OpenCL query is only available starting with OpenCL 2.1 * and we have to emulate it on older OpenCL runtimes. */ oclv::OpenCLVersion DevVer; - CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer)); + CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer)); if (DevVer >= oclv::V2_1) { cl_uint CLValue; @@ -499,8 +459,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, * UR type: ur_device_fp_capability_flags_t */ if (propName == UR_DEVICE_INFO_HALF_FP_CONFIG) { bool Supported; - CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions( - hDevice->get(), {"cl_khr_fp16"}, Supported)); + CL_RETURN_ON_FAILURE( + hDevice->checkDeviceExtensions({"cl_khr_fp16"}, Supported)); if (!Supported) { return UR_RESULT_ERROR_INVALID_ENUMERATION; @@ -519,7 +479,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, /* This query is missing before OpenCL 3.0. Check version and handle * appropriately */ oclv::OpenCLVersion DevVer; - CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer)); + CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer)); /* Minimum required capability to be returned. For OpenCL 1.2, this is all * that is required */ @@ -576,7 +536,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, UR_MEMORY_SCOPE_CAPABILITY_FLAG_WORK_GROUP; oclv::OpenCLVersion DevVer; - CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer)); + CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer)); cl_device_atomic_capabilities CLCapabilities; if (DevVer >= oclv::V3_0) { @@ -627,7 +587,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, UR_MEMORY_ORDER_CAPABILITY_FLAG_ACQ_REL; oclv::OpenCLVersion DevVer; - CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer)); + CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer)); cl_device_atomic_capabilities CLCapabilities; if (DevVer >= oclv::V3_0) { @@ -674,7 +634,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, UR_MEMORY_SCOPE_CAPABILITY_FLAG_WORK_GROUP; oclv::OpenCLVersion DevVer; - CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer)); + CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer)); cl_device_atomic_capabilities CLCapabilities; if (DevVer >= oclv::V3_0) { @@ -725,8 +685,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, } case UR_DEVICE_INFO_ATOMIC_64: { bool Supported = false; - CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions( - hDevice->get(), + CL_RETURN_ON_FAILURE(hDevice->checkDeviceExtensions( {"cl_khr_int64_base_atomics", "cl_khr_int64_extended_atomics"}, Supported)); @@ -743,8 +702,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, } case UR_DEVICE_INFO_MEM_CHANNEL_SUPPORT: { bool Supported = false; - CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions( - hDevice->get(), {"cl_intel_mem_channel_property"}, Supported)); + CL_RETURN_ON_FAILURE(hDevice->checkDeviceExtensions( + {"cl_intel_mem_channel_property"}, Supported)); return ReturnValue(Supported); } @@ -769,8 +728,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, } case UR_DEVICE_INFO_HOST_PIPE_READ_WRITE_SUPPORTED: { bool Supported = false; - CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions( - hDevice->get(), {"cl_intel_program_scope_host_pipe"}, Supported)); + CL_RETURN_ON_FAILURE(hDevice->checkDeviceExtensions( + {"cl_intel_program_scope_host_pipe"}, Supported)); return ReturnValue(Supported); } case UR_DEVICE_INFO_QUEUE_PROPERTIES: @@ -1064,18 +1023,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetGlobalTimestamps( ur_device_handle_t hDevice, uint64_t *pDeviceTimestamp, uint64_t *pHostTimestamp) { oclv::OpenCLVersion DevVer, PlatVer; - cl_platform_id Platform; cl_device_id DeviceId = hDevice->get(); // TODO: Cache OpenCL version for each device and platform - auto RetErr = clGetDeviceInfo(DeviceId, CL_DEVICE_PLATFORM, - sizeof(cl_platform_id), &Platform, nullptr); - CL_RETURN_ON_FAILURE(RetErr); - RetErr = cl_adapter::getDeviceVersion(DeviceId, DevVer); + auto RetErr = hDevice->getDeviceVersion(DevVer); CL_RETURN_ON_FAILURE(RetErr); - RetErr = cl_adapter::getPlatformVersion(Platform, PlatVer); + RetErr = hDevice->Platform->getPlatformVersion(PlatVer); if (PlatVer < oclv::V2_1 || DevVer < oclv::V2_1) { return UR_RESULT_ERROR_INVALID_OPERATION; diff --git a/source/adapters/opencl/device.hpp b/source/adapters/opencl/device.hpp index 2c4c97193b..f538ce538b 100644 --- a/source/adapters/opencl/device.hpp +++ b/source/adapters/opencl/device.hpp @@ -12,14 +12,6 @@ #include "common.hpp" #include "platform.hpp" -namespace cl_adapter { -ur_result_t getDeviceVersion(cl_device_id Dev, oclv::OpenCLVersion &Version); - -ur_result_t checkDeviceExtensions(cl_device_id Dev, - const std::vector &Exts, - bool &Supported); -} // namespace cl_adapter - struct ur_device_handle_t_ { using native_type = cl_device_id; native_type Device; @@ -41,4 +33,42 @@ struct ur_device_handle_t_ { ~ur_device_handle_t_() {} native_type get() { return Device; } + + ur_result_t getDeviceVersion(oclv::OpenCLVersion &Version) { + size_t DevVerSize = 0; + CL_RETURN_ON_FAILURE( + clGetDeviceInfo(Device, CL_DEVICE_VERSION, 0, nullptr, &DevVerSize)); + + std::string DevVer(DevVerSize, '\0'); + CL_RETURN_ON_FAILURE(clGetDeviceInfo(Device, CL_DEVICE_VERSION, DevVerSize, + DevVer.data(), nullptr)); + + Version = oclv::OpenCLVersion(DevVer); + if (!Version.isValid()) { + return UR_RESULT_ERROR_INVALID_DEVICE; + } + + return UR_RESULT_SUCCESS; + } + + ur_result_t checkDeviceExtensions(const std::vector &Exts, + bool &Supported) { + size_t ExtSize = 0; + CL_RETURN_ON_FAILURE( + clGetDeviceInfo(Device, CL_DEVICE_EXTENSIONS, 0, nullptr, &ExtSize)); + + std::string ExtStr(ExtSize, '\0'); + + CL_RETURN_ON_FAILURE(clGetDeviceInfo(Device, CL_DEVICE_EXTENSIONS, ExtSize, + ExtStr.data(), nullptr)); + + Supported = true; + for (const std::string &Ext : Exts) { + if (!(Supported = (ExtStr.find(Ext) != std::string::npos))) { + break; + } + } + + return UR_RESULT_SUCCESS; + } }; \ No newline at end of file diff --git a/source/adapters/opencl/memory.cpp b/source/adapters/opencl/memory.cpp index 840b63180c..53fcee6777 100644 --- a/source/adapters/opencl/memory.cpp +++ b/source/adapters/opencl/memory.cpp @@ -227,7 +227,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate( ur_context_handle_t hContext, ur_mem_flags_t flags, size_t size, const ur_buffer_properties_t *pProperties, ur_mem_handle_t *phBuffer) { cl_int RetErr = CL_INVALID_OPERATION; - // UR_RETURN_ON_FAILURE(urContextRetain(hContext)); + UR_RETURN_ON_FAILURE(urContextRetain(hContext)); if (pProperties) { // TODO: need to check if all properties are supported by OpenCL RT and // ignore unsupported diff --git a/source/adapters/opencl/platform.cpp b/source/adapters/opencl/platform.cpp index 8fa7056bcb..a6d2659199 100644 --- a/source/adapters/opencl/platform.cpp +++ b/source/adapters/opencl/platform.cpp @@ -10,25 +10,6 @@ #include "platform.hpp" -ur_result_t cl_adapter::getPlatformVersion(cl_platform_id Plat, - oclv::OpenCLVersion &Version) { - - size_t PlatVerSize = 0; - CL_RETURN_ON_FAILURE( - clGetPlatformInfo(Plat, CL_PLATFORM_VERSION, 0, nullptr, &PlatVerSize)); - - std::string PlatVer(PlatVerSize, '\0'); - CL_RETURN_ON_FAILURE(clGetPlatformInfo(Plat, CL_PLATFORM_VERSION, PlatVerSize, - PlatVer.data(), nullptr)); - - Version = oclv::OpenCLVersion(PlatVer); - if (!Version.isValid()) { - return UR_RESULT_ERROR_INVALID_PLATFORM; - } - - return UR_RESULT_SUCCESS; -} - static cl_int mapURPlatformInfoToCL(ur_platform_info_t URPropName) { switch (URPropName) { diff --git a/source/adapters/opencl/platform.hpp b/source/adapters/opencl/platform.hpp index 0957f4562d..16fe833fab 100644 --- a/source/adapters/opencl/platform.hpp +++ b/source/adapters/opencl/platform.hpp @@ -14,11 +14,6 @@ #include -namespace cl_adapter { -ur_result_t getPlatformVersion(cl_platform_id Plat, - oclv::OpenCLVersion &Version); -} // namespace cl_adapter - struct ur_platform_handle_t_ { using native_type = cl_platform_id; native_type Platform = nullptr; @@ -63,4 +58,22 @@ struct ur_platform_handle_t_ { return UR_RESULT_SUCCESS; } + + ur_result_t getPlatformVersion(oclv::OpenCLVersion &Version) { + + size_t PlatVerSize = 0; + CL_RETURN_ON_FAILURE(clGetPlatformInfo(Platform, CL_PLATFORM_VERSION, 0, + nullptr, &PlatVerSize)); + + std::string PlatVer(PlatVerSize, '\0'); + CL_RETURN_ON_FAILURE(clGetPlatformInfo( + Platform, CL_PLATFORM_VERSION, PlatVerSize, PlatVer.data(), nullptr)); + + Version = oclv::OpenCLVersion(PlatVer); + if (!Version.isValid()) { + return UR_RESULT_ERROR_INVALID_PLATFORM; + } + + return UR_RESULT_SUCCESS; + } }; diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index d017e00498..d8dc29a398 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -14,6 +14,8 @@ #include "device.hpp" #include "platform.hpp" +#include + static ur_result_t getDevicesFromProgram( ur_program_handle_t hProgram, std::unique_ptr> &DevicesInProgram) { @@ -36,22 +38,21 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( if (!hContext->DeviceCount || !hContext->Devices[0]->Platform) { return UR_RESULT_ERROR_INVALID_CONTEXT; } - cl_platform_id CurPlatform = hContext->Devices[0]->Platform->get(); + ur_platform_handle_t CurPlatform = hContext->Devices[0]->Platform; oclv::OpenCLVersion PlatVer; - CL_RETURN_ON_FAILURE_AND_SET_NULL( - cl_adapter::getPlatformVersion(CurPlatform, PlatVer), phProgram); + CL_RETURN_ON_FAILURE_AND_SET_NULL(CurPlatform->getPlatformVersion(PlatVer), + phProgram); cl_int Err = CL_SUCCESS; if (PlatVer >= oclv::V2_1) { /* Make sure all devices support CL 2.1 or newer as well. */ for (ur_device_handle_t URDev : hContext->Devices) { - cl_device_id Dev = URDev->get(); oclv::OpenCLVersion DevVer; - CL_RETURN_ON_FAILURE_AND_SET_NULL( - cl_adapter::getDeviceVersion(Dev, DevVer), phProgram); + CL_RETURN_ON_FAILURE_AND_SET_NULL(URDev->getDeviceVersion(DevVer), + phProgram); /* If the device does not support CL 2.1 or greater, we need to make sure * it supports the cl_khr_il_program extension. @@ -59,8 +60,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( if (DevVer < oclv::V2_1) { bool Supported = false; CL_RETURN_ON_FAILURE_AND_SET_NULL( - cl_adapter::checkDeviceExtensions(Dev, {"cl_khr_il_program"}, - Supported), + URDev->checkDeviceExtensions({"cl_khr_il_program"}, Supported), phProgram); if (!Supported) { @@ -80,11 +80,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( * support the cl_khr_il_program extension. */ for (ur_device_handle_t URDev : hContext->Devices) { - cl_device_id Dev = URDev->get(); bool Supported = false; CL_RETURN_ON_FAILURE_AND_SET_NULL( - cl_adapter::checkDeviceExtensions(Dev, {"cl_khr_il_program"}, - Supported), + URDev->checkDeviceExtensions({"cl_khr_il_program"}, Supported), phProgram); if (!Supported) { @@ -96,7 +94,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( cl_program(CL_API_CALL *)(cl_context, const void *, size_t, cl_int *); ApiFuncT FuncPtr = reinterpret_cast(clGetExtensionFunctionAddressForPlatform( - CurPlatform, "clCreateProgramWithILKHR")); + CurPlatform->get(), "clCreateProgramWithILKHR")); assert(FuncPtr != nullptr); cl_program Program = FuncPtr(hContext->get(), pIL, length, &Err); @@ -370,22 +368,21 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants( return UR_RESULT_ERROR_INVALID_CONTEXT; } - std::unique_ptr> DevicesInCtx; - UR_RETURN_ON_FAILURE(cl_adapter::getDevicesFromContext(Ctx, DevicesInCtx)); + std::vector &DevicesInCtx = Ctx->Devices; - cl_platform_id CurPlatform = Ctx->Devices[0]->Platform->get(); + ur_platform_handle_t CurPlatform = Ctx->Devices[0]->Platform; oclv::OpenCLVersion PlatVer; - cl_adapter::getPlatformVersion(CurPlatform, PlatVer); + CurPlatform->getPlatformVersion(PlatVer); bool UseExtensionLookup = false; if (PlatVer < oclv::V2_2) { UseExtensionLookup = true; } else { - for (cl_device_id Dev : *DevicesInCtx) { + for (ur_device_handle_t Dev : DevicesInCtx) { oclv::OpenCLVersion DevVer; - UR_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(Dev, DevVer)); + UR_RETURN_ON_FAILURE(Dev->getDeviceVersion(DevVer)); if (DevVer < oclv::V2_2) { UseExtensionLookup = true; diff --git a/source/adapters/opencl/queue.cpp b/source/adapters/opencl/queue.cpp index c21006fe90..accb50555e 100644 --- a/source/adapters/opencl/queue.cpp +++ b/source/adapters/opencl/queue.cpp @@ -74,7 +74,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate( ur_context_handle_t hContext, ur_device_handle_t hDevice, const ur_queue_properties_t *pProperties, ur_queue_handle_t *phQueue) { - cl_platform_id CurPlatform = hDevice->Platform->get(); + ur_platform_handle_t CurPlatform = hDevice->Platform; cl_command_queue_properties CLProperties = pProperties ? convertURQueuePropertiesToCL(pProperties) : 0; @@ -85,8 +85,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate( CL_QUEUE_ON_DEVICE | CL_QUEUE_ON_DEVICE_DEFAULT; oclv::OpenCLVersion Version; - CL_RETURN_ON_FAILURE_AND_SET_NULL( - cl_adapter::getPlatformVersion(CurPlatform, Version), phQueue); + CL_RETURN_ON_FAILURE_AND_SET_NULL(CurPlatform->getPlatformVersion(Version), + phQueue); cl_int RetErr = CL_INVALID_OPERATION;