Skip to content

Commit

Permalink
corrections related to review bashbaug#113
Browse files Browse the repository at this point in the history
  • Loading branch information
shajder committed May 21, 2024
1 parent b57878e commit e107dc6
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 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 Down Expand Up @@ -105,34 +127,23 @@ typedef struct _cl_semaphore_khr
errorCode = CL_INVALID_VALUE;

// validate device handles.
if (errorCode == CL_SUCCESS && !devices.empty()) {
std::vector<cl_semaphore_type_khr> 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;
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;
}
}

if (!capable) {
errorCode = CL_INVALID_DEVICE;
break;
}
}
}
}
Expand All @@ -157,11 +168,7 @@ typedef struct _cl_semaphore_khr
properties,
properties + numProperties );

if (!devices.empty())
semaphore->Devices.assign(
devices.begin(),
devices.end() );

semaphore->Devices=devices;
}
return semaphore;
}
Expand Down

0 comments on commit e107dc6

Please sign in to comment.