Skip to content

Commit

Permalink
Added missing interface for CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR prope…
Browse files Browse the repository at this point in the history
…rty (#113)

* Added missing interface for CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR property

* approach to correct CI checks errors around semaphore emulation layer to handle CL_SEMAPHORE_DEVICE_HANDLE_LIST_KHR flag

* Corrections related to code review

* Corrections due to failed CI check

* Take into account additional property field

to distinguish between CL_SEMAPHORE_DEVICE_HANDLE_LIST_END_KHR and array
terminator
  • Loading branch information
shajder authored Jun 21, 2024
1 parent 2ae7ac2 commit dbc2b85
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions layers/11_semaemu/emulate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cl_device_id> 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(
Expand All @@ -48,6 +70,8 @@ typedef struct _cl_semaphore_khr
ptrdiff_t numProperties = 0;
cl_semaphore_type_khr type = ~0;

std::vector<cl_device_id> devices;

if( properties )
{
const cl_semaphore_properties_khr* check = properties;
Expand Down Expand Up @@ -78,11 +102,13 @@ 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++;
}
check++;
}
break;
default:
Expand All @@ -91,6 +117,27 @@ typedef struct _cl_semaphore_khr
}
}
numProperties = check - properties + 1;

// validate device handles.
if (!devices.empty()) {
// 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<cl_semaphore_type_khr> types;
for (auto device : devices) {
if (device == nullptr ||
!isDeviceWithinContext(context, device)) {
errorCode = CL_INVALID_DEVICE;
break;
}
}
}
}
}
switch( type )
{
Expand All @@ -111,6 +158,8 @@ typedef struct _cl_semaphore_khr
semaphore->Properties.begin(),
properties,
properties + numProperties );

semaphore->Devices=devices;
}
return semaphore;
}
Expand All @@ -124,6 +173,7 @@ typedef struct _cl_semaphore_khr
const cl_context Context;
const cl_semaphore_type_khr Type;
std::vector<cl_semaphore_properties_khr> Properties;
std::vector<cl_device_id> Devices;

std::atomic<cl_uint> RefCount;
cl_event Event;
Expand Down Expand Up @@ -349,6 +399,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;
}
Expand Down

0 comments on commit dbc2b85

Please sign in to comment.