Skip to content

Commit

Permalink
Move platform and device version query to the handle
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Jan 16, 2024
1 parent 2821682 commit 61ba805
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 140 deletions.
18 changes: 0 additions & 18 deletions source/adapters/opencl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,6 @@
#include <set>
#include <unordered_map>

ur_result_t cl_adapter::getDevicesFromContext(
ur_context_handle_t hContext,
std::unique_ptr<std::vector<cl_device_id>> &DevicesInCtx) {

cl_uint DeviceCount = hContext->DeviceCount;

if (DeviceCount < 1) {
return UR_RESULT_ERROR_INVALID_CONTEXT;
}

DevicesInCtx = std::make_unique<std::vector<cl_device_id>>(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) {
Expand Down
6 changes: 0 additions & 6 deletions source/adapters/opencl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

#include <vector>

namespace cl_adapter {
ur_result_t
getDevicesFromContext(ur_context_handle_t hContext,
std::unique_ptr<std::vector<cl_device_id>> &DevicesInCtx);
}

struct ur_context_handle_t_ {
using native_type = cl_context;
native_type Context;
Expand Down
79 changes: 17 additions & 62 deletions source/adapters/opencl/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,6 @@

#include <cassert>

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<std::string> &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,
Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));

Expand All @@ -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);
}
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 38 additions & 8 deletions source/adapters/opencl/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &Exts,
bool &Supported);
} // namespace cl_adapter

struct ur_device_handle_t_ {
using native_type = cl_device_id;
native_type Device;
Expand All @@ -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<std::string> &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;
}
};
2 changes: 1 addition & 1 deletion source/adapters/opencl/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions source/adapters/opencl/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 18 additions & 5 deletions source/adapters/opencl/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

#include <vector>

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;
Expand Down Expand Up @@ -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;
}
};
Loading

0 comments on commit 61ba805

Please sign in to comment.