Skip to content

Commit 192e940

Browse files
authored
Merge pull request #1044 from aarongreig/aaron/clCTSFixMegaBranch
[OpenCL] Combined CTS fixes
2 parents 2f44433 + f65473d commit 192e940

17 files changed

+870
-240
lines changed

source/adapters/opencl/common.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ ur_result_t mapCLErrorToUR(cl_int Result) {
6060
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
6161
case CL_INVALID_MEM_OBJECT:
6262
return UR_RESULT_ERROR_INVALID_MEM_OBJECT;
63+
case CL_INVALID_QUEUE_PROPERTIES:
64+
return UR_RESULT_ERROR_INVALID_QUEUE_PROPERTIES;
65+
case CL_INVALID_BUFFER_SIZE:
66+
return UR_RESULT_ERROR_INVALID_BUFFER_SIZE;
67+
case CL_INVALID_IMAGE_SIZE:
68+
return UR_RESULT_ERROR_INVALID_IMAGE_SIZE;
69+
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
70+
case CL_INVALID_IMAGE_DESCRIPTOR:
71+
return UR_RESULT_ERROR_INVALID_IMAGE_FORMAT_DESCRIPTOR;
72+
case CL_IMAGE_FORMAT_NOT_SUPPORTED:
73+
return UR_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT;
74+
case CL_PROFILING_INFO_NOT_AVAILABLE:
75+
return UR_RESULT_ERROR_PROFILING_INFO_NOT_AVAILABLE;
76+
case CL_LINK_PROGRAM_FAILURE:
77+
return UR_RESULT_ERROR_PROGRAM_LINK_FAILURE;
78+
case CL_INVALID_ARG_INDEX:
79+
return UR_RESULT_ERROR_INVALID_KERNEL_ARGUMENT_INDEX;
6380
default:
6481
return UR_RESULT_ERROR_UNKNOWN;
6582
}

source/adapters/opencl/context.cpp

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
#include "context.hpp"
1212

13+
#include <mutex>
14+
#include <set>
15+
#include <unordered_map>
16+
1317
ur_result_t cl_adapter::getDevicesFromContext(
1418
ur_context_handle_t hContext,
1519
std::unique_ptr<std::vector<cl_device_id>> &DevicesInCtx) {
@@ -89,10 +93,17 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName,
8993
case UR_CONTEXT_INFO_NUM_DEVICES:
9094
case UR_CONTEXT_INFO_DEVICES:
9195
case UR_CONTEXT_INFO_REFERENCE_COUNT: {
92-
93-
CL_RETURN_ON_FAILURE(
96+
size_t CheckPropSize = 0;
97+
auto ClResult =
9498
clGetContextInfo(cl_adapter::cast<cl_context>(hContext), CLPropName,
95-
propSize, pPropValue, pPropSizeRet));
99+
propSize, pPropValue, &CheckPropSize);
100+
if (pPropValue && CheckPropSize != propSize) {
101+
return UR_RESULT_ERROR_INVALID_SIZE;
102+
}
103+
CL_RETURN_ON_FAILURE(ClResult);
104+
if (pPropSizeRet) {
105+
*pPropSizeRet = CheckPropSize;
106+
}
96107
return UR_RESULT_SUCCESS;
97108
}
98109
default:
@@ -130,8 +141,53 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle(
130141
}
131142

132143
UR_APIEXPORT ur_result_t UR_APICALL urContextSetExtendedDeleter(
133-
[[maybe_unused]] ur_context_handle_t hContext,
134-
[[maybe_unused]] ur_context_extended_deleter_t pfnDeleter,
135-
[[maybe_unused]] void *pUserData) {
136-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
144+
ur_context_handle_t hContext, ur_context_extended_deleter_t pfnDeleter,
145+
void *pUserData) {
146+
static std::unordered_map<ur_context_handle_t,
147+
std::set<ur_context_extended_deleter_t>>
148+
ContextCallbackMap;
149+
static std::mutex ContextCallbackMutex;
150+
151+
{
152+
std::lock_guard<std::mutex> Lock(ContextCallbackMutex);
153+
// Callbacks can only be registered once and we need to avoid double
154+
// allocating.
155+
if (ContextCallbackMap.count(hContext) &&
156+
ContextCallbackMap[hContext].count(pfnDeleter)) {
157+
return UR_RESULT_SUCCESS;
158+
}
159+
160+
ContextCallbackMap[hContext].insert(pfnDeleter);
161+
}
162+
163+
struct ContextCallback {
164+
void execute() {
165+
pfnDeleter(pUserData);
166+
{
167+
std::lock_guard<std::mutex> Lock(*CallbackMutex);
168+
(*CallbackMap)[hContext].erase(pfnDeleter);
169+
if ((*CallbackMap)[hContext].empty()) {
170+
CallbackMap->erase(hContext);
171+
}
172+
}
173+
delete this;
174+
}
175+
ur_context_handle_t hContext;
176+
ur_context_extended_deleter_t pfnDeleter;
177+
void *pUserData;
178+
std::unordered_map<ur_context_handle_t,
179+
std::set<ur_context_extended_deleter_t>> *CallbackMap;
180+
std::mutex *CallbackMutex;
181+
};
182+
auto Callback =
183+
new ContextCallback({hContext, pfnDeleter, pUserData, &ContextCallbackMap,
184+
&ContextCallbackMutex});
185+
auto ClCallback = [](cl_context, void *pUserData) {
186+
auto *C = static_cast<ContextCallback *>(pUserData);
187+
C->execute();
188+
};
189+
CL_RETURN_ON_FAILURE(clSetContextDestructorCallback(
190+
cl_adapter::cast<cl_context>(hContext), ClCallback, Callback));
191+
192+
return UR_RESULT_SUCCESS;
137193
}

source/adapters/opencl/device.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
345345

346346
return ReturnValue(URDeviceType);
347347
}
348+
case UR_DEVICE_INFO_DEVICE_ID: {
349+
bool Supported = false;
350+
CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions(
351+
cl_adapter::cast<cl_device_id>(hDevice), {"cl_khr_pci_bus_info"},
352+
Supported));
353+
354+
if (!Supported) {
355+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
356+
}
357+
358+
cl_device_pci_bus_info_khr PciInfo = {};
359+
CL_RETURN_ON_FAILURE(clGetDeviceInfo(
360+
cl_adapter::cast<cl_device_id>(hDevice), CL_DEVICE_PCI_BUS_INFO_KHR,
361+
sizeof(PciInfo), &PciInfo, nullptr));
362+
return ReturnValue(PciInfo.pci_device);
363+
}
364+
348365
case UR_DEVICE_INFO_BACKEND_RUNTIME_VERSION: {
349366
oclv::OpenCLVersion Version;
350367
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(
@@ -760,6 +777,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
760777

761778
return ReturnValue(Supported);
762779
}
780+
case UR_DEVICE_INFO_VIRTUAL_MEMORY_SUPPORT: {
781+
return ReturnValue(false);
782+
}
783+
case UR_DEVICE_INFO_HOST_PIPE_READ_WRITE_SUPPORTED: {
784+
bool Supported = false;
785+
CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions(
786+
cl_adapter::cast<cl_device_id>(hDevice),
787+
{"cl_intel_program_scope_host_pipe"}, Supported));
788+
return ReturnValue(Supported);
789+
}
763790
case UR_DEVICE_INFO_QUEUE_PROPERTIES:
764791
case UR_DEVICE_INFO_QUEUE_ON_DEVICE_PROPERTIES:
765792
case UR_DEVICE_INFO_QUEUE_ON_HOST_PROPERTIES:
@@ -775,7 +802,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
775802
/* CL type: cl_bitfield / enum
776803
* UR type: ur_flags_t (uint32_t) */
777804

778-
cl_bitfield CLValue;
805+
cl_bitfield CLValue = 0;
779806
CL_RETURN_ON_FAILURE(
780807
clGetDeviceInfo(cl_adapter::cast<cl_device_id>(hDevice), CLPropName,
781808
sizeof(cl_bitfield), &CLValue, nullptr));
@@ -898,13 +925,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
898925
* sycl/doc/extensions/supported/sycl_ext_intel_device_info.md */
899926
case UR_DEVICE_INFO_UUID:
900927
/* This enums have no equivalent in OpenCL */
901-
case UR_DEVICE_INFO_DEVICE_ID:
928+
case UR_DEVICE_INFO_MAX_REGISTERS_PER_WORK_GROUP:
902929
case UR_DEVICE_INFO_GLOBAL_MEM_FREE:
903930
case UR_DEVICE_INFO_MEMORY_CLOCK_RATE:
904931
case UR_DEVICE_INFO_MEMORY_BUS_WIDTH:
905-
case UR_DEVICE_INFO_ASYNC_BARRIER:
906-
case UR_DEVICE_INFO_HOST_PIPE_READ_WRITE_SUPPORTED: {
907-
return UR_RESULT_ERROR_INVALID_ENUMERATION;
932+
case UR_DEVICE_INFO_ASYNC_BARRIER: {
933+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
908934
}
909935
default: {
910936
return UR_RESULT_ERROR_INVALID_ENUMERATION;

0 commit comments

Comments
 (0)