Skip to content

Commit ab61e80

Browse files
committed
Move platform and device version query to the handle
1 parent 2821682 commit ab61e80

File tree

8 files changed

+91
-139
lines changed

8 files changed

+91
-139
lines changed

source/adapters/opencl/context.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,6 @@
1414
#include <set>
1515
#include <unordered_map>
1616

17-
ur_result_t cl_adapter::getDevicesFromContext(
18-
ur_context_handle_t hContext,
19-
std::unique_ptr<std::vector<cl_device_id>> &DevicesInCtx) {
20-
21-
cl_uint DeviceCount = hContext->DeviceCount;
22-
23-
if (DeviceCount < 1) {
24-
return UR_RESULT_ERROR_INVALID_CONTEXT;
25-
}
26-
27-
DevicesInCtx = std::make_unique<std::vector<cl_device_id>>(DeviceCount);
28-
for (size_t i = 0; i < DeviceCount; i++) {
29-
(*DevicesInCtx)[i] = hContext->Devices[i]->get();
30-
}
31-
32-
return UR_RESULT_SUCCESS;
33-
}
34-
3517
UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
3618
uint32_t DeviceCount, const ur_device_handle_t *phDevices,
3719
const ur_context_properties_t *, ur_context_handle_t *phContext) {

source/adapters/opencl/context.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414

1515
#include <vector>
1616

17-
namespace cl_adapter {
18-
ur_result_t
19-
getDevicesFromContext(ur_context_handle_t hContext,
20-
std::unique_ptr<std::vector<cl_device_id>> &DevicesInCtx);
21-
}
22-
2317
struct ur_context_handle_t_ {
2418
using native_type = cl_context;
2519
native_type Context;

source/adapters/opencl/device.cpp

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,6 @@
1212

1313
#include <cassert>
1414

15-
ur_result_t cl_adapter::getDeviceVersion(cl_device_id Dev,
16-
oclv::OpenCLVersion &Version) {
17-
18-
size_t DevVerSize = 0;
19-
CL_RETURN_ON_FAILURE(
20-
clGetDeviceInfo(Dev, CL_DEVICE_VERSION, 0, nullptr, &DevVerSize));
21-
22-
std::string DevVer(DevVerSize, '\0');
23-
CL_RETURN_ON_FAILURE(clGetDeviceInfo(Dev, CL_DEVICE_VERSION, DevVerSize,
24-
DevVer.data(), nullptr));
25-
26-
Version = oclv::OpenCLVersion(DevVer);
27-
if (!Version.isValid()) {
28-
return UR_RESULT_ERROR_INVALID_DEVICE;
29-
}
30-
31-
return UR_RESULT_SUCCESS;
32-
}
33-
34-
ur_result_t cl_adapter::checkDeviceExtensions(
35-
cl_device_id Dev, const std::vector<std::string> &Exts, bool &Supported) {
36-
size_t ExtSize = 0;
37-
CL_RETURN_ON_FAILURE(
38-
clGetDeviceInfo(Dev, CL_DEVICE_EXTENSIONS, 0, nullptr, &ExtSize));
39-
40-
std::string ExtStr(ExtSize, '\0');
41-
42-
CL_RETURN_ON_FAILURE(clGetDeviceInfo(Dev, CL_DEVICE_EXTENSIONS, ExtSize,
43-
ExtStr.data(), nullptr));
44-
45-
Supported = true;
46-
for (const std::string &Ext : Exts) {
47-
if (!(Supported = (ExtStr.find(Ext) != std::string::npos))) {
48-
break;
49-
}
50-
}
51-
52-
return UR_RESULT_SUCCESS;
53-
}
54-
5515
UR_APIEXPORT ur_result_t UR_APICALL
5616
urDeviceGet(ur_platform_handle_t hPlatform, ur_device_type_t DeviceType,
5717
[[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,
351311
}
352312
case UR_DEVICE_INFO_DEVICE_ID: {
353313
bool Supported = false;
354-
CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions(
355-
hDevice->get(), {"cl_khr_pci_bus_info"}, Supported));
314+
CL_RETURN_ON_FAILURE(
315+
hDevice->checkDeviceExtensions({"cl_khr_pci_bus_info"}, Supported));
356316

357317
if (!Supported) {
358318
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
@@ -367,7 +327,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
367327

368328
case UR_DEVICE_INFO_BACKEND_RUNTIME_VERSION: {
369329
oclv::OpenCLVersion Version;
370-
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), Version));
330+
CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(Version));
371331

372332
const std::string Results = std::to_string(Version.getMajor()) + "." +
373333
std::to_string(Version.getMinor());
@@ -470,7 +430,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
470430
/* Corresponding OpenCL query is only available starting with OpenCL 2.1
471431
* and we have to emulate it on older OpenCL runtimes. */
472432
oclv::OpenCLVersion DevVer;
473-
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer));
433+
CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer));
474434

475435
if (DevVer >= oclv::V2_1) {
476436
cl_uint CLValue;
@@ -499,8 +459,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
499459
* UR type: ur_device_fp_capability_flags_t */
500460
if (propName == UR_DEVICE_INFO_HALF_FP_CONFIG) {
501461
bool Supported;
502-
CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions(
503-
hDevice->get(), {"cl_khr_fp16"}, Supported));
462+
CL_RETURN_ON_FAILURE(
463+
hDevice->checkDeviceExtensions({"cl_khr_fp16"}, Supported));
504464

505465
if (!Supported) {
506466
return UR_RESULT_ERROR_INVALID_ENUMERATION;
@@ -519,7 +479,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
519479
/* This query is missing before OpenCL 3.0. Check version and handle
520480
* appropriately */
521481
oclv::OpenCLVersion DevVer;
522-
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer));
482+
CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer));
523483

524484
/* Minimum required capability to be returned. For OpenCL 1.2, this is all
525485
* that is required */
@@ -576,7 +536,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
576536
UR_MEMORY_SCOPE_CAPABILITY_FLAG_WORK_GROUP;
577537

578538
oclv::OpenCLVersion DevVer;
579-
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer));
539+
CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer));
580540

581541
cl_device_atomic_capabilities CLCapabilities;
582542
if (DevVer >= oclv::V3_0) {
@@ -627,7 +587,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
627587
UR_MEMORY_ORDER_CAPABILITY_FLAG_ACQ_REL;
628588

629589
oclv::OpenCLVersion DevVer;
630-
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer));
590+
CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer));
631591

632592
cl_device_atomic_capabilities CLCapabilities;
633593
if (DevVer >= oclv::V3_0) {
@@ -674,7 +634,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
674634
UR_MEMORY_SCOPE_CAPABILITY_FLAG_WORK_GROUP;
675635

676636
oclv::OpenCLVersion DevVer;
677-
CL_RETURN_ON_FAILURE(cl_adapter::getDeviceVersion(hDevice->get(), DevVer));
637+
CL_RETURN_ON_FAILURE(hDevice->getDeviceVersion(DevVer));
678638

679639
cl_device_atomic_capabilities CLCapabilities;
680640
if (DevVer >= oclv::V3_0) {
@@ -725,8 +685,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
725685
}
726686
case UR_DEVICE_INFO_ATOMIC_64: {
727687
bool Supported = false;
728-
CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions(
729-
hDevice->get(),
688+
CL_RETURN_ON_FAILURE(hDevice->checkDeviceExtensions(
730689
{"cl_khr_int64_base_atomics", "cl_khr_int64_extended_atomics"},
731690
Supported));
732691

@@ -743,8 +702,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
743702
}
744703
case UR_DEVICE_INFO_MEM_CHANNEL_SUPPORT: {
745704
bool Supported = false;
746-
CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions(
747-
hDevice->get(), {"cl_intel_mem_channel_property"}, Supported));
705+
CL_RETURN_ON_FAILURE(hDevice->checkDeviceExtensions(
706+
{"cl_intel_mem_channel_property"}, Supported));
748707

749708
return ReturnValue(Supported);
750709
}
@@ -769,8 +728,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
769728
}
770729
case UR_DEVICE_INFO_HOST_PIPE_READ_WRITE_SUPPORTED: {
771730
bool Supported = false;
772-
CL_RETURN_ON_FAILURE(cl_adapter::checkDeviceExtensions(
773-
hDevice->get(), {"cl_intel_program_scope_host_pipe"}, Supported));
731+
CL_RETURN_ON_FAILURE(hDevice->checkDeviceExtensions(
732+
{"cl_intel_program_scope_host_pipe"}, Supported));
774733
return ReturnValue(Supported);
775734
}
776735
case UR_DEVICE_INFO_QUEUE_PROPERTIES:
@@ -1064,18 +1023,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetGlobalTimestamps(
10641023
ur_device_handle_t hDevice, uint64_t *pDeviceTimestamp,
10651024
uint64_t *pHostTimestamp) {
10661025
oclv::OpenCLVersion DevVer, PlatVer;
1067-
cl_platform_id Platform;
10681026
cl_device_id DeviceId = hDevice->get();
10691027

10701028
// TODO: Cache OpenCL version for each device and platform
1071-
auto RetErr = clGetDeviceInfo(DeviceId, CL_DEVICE_PLATFORM,
1072-
sizeof(cl_platform_id), &Platform, nullptr);
1073-
CL_RETURN_ON_FAILURE(RetErr);
10741029

1075-
RetErr = cl_adapter::getDeviceVersion(DeviceId, DevVer);
1030+
auto RetErr = hDevice->getDeviceVersion(DevVer);
10761031
CL_RETURN_ON_FAILURE(RetErr);
10771032

1078-
RetErr = cl_adapter::getPlatformVersion(Platform, PlatVer);
1033+
RetErr = hDevice->Platform->getPlatformVersion(PlatVer);
10791034

10801035
if (PlatVer < oclv::V2_1 || DevVer < oclv::V2_1) {
10811036
return UR_RESULT_ERROR_INVALID_OPERATION;

source/adapters/opencl/device.hpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@
1212
#include "common.hpp"
1313
#include "platform.hpp"
1414

15-
namespace cl_adapter {
16-
ur_result_t getDeviceVersion(cl_device_id Dev, oclv::OpenCLVersion &Version);
17-
18-
ur_result_t checkDeviceExtensions(cl_device_id Dev,
19-
const std::vector<std::string> &Exts,
20-
bool &Supported);
21-
} // namespace cl_adapter
22-
2315
struct ur_device_handle_t_ {
2416
using native_type = cl_device_id;
2517
native_type Device;
@@ -41,4 +33,42 @@ struct ur_device_handle_t_ {
4133
~ur_device_handle_t_() {}
4234

4335
native_type get() { return Device; }
36+
37+
ur_result_t getDeviceVersion(oclv::OpenCLVersion &Version) {
38+
size_t DevVerSize = 0;
39+
CL_RETURN_ON_FAILURE(
40+
clGetDeviceInfo(Device, CL_DEVICE_VERSION, 0, nullptr, &DevVerSize));
41+
42+
std::string DevVer(DevVerSize, '\0');
43+
CL_RETURN_ON_FAILURE(clGetDeviceInfo(Device, CL_DEVICE_VERSION, DevVerSize,
44+
DevVer.data(), nullptr));
45+
46+
Version = oclv::OpenCLVersion(DevVer);
47+
if (!Version.isValid()) {
48+
return UR_RESULT_ERROR_INVALID_DEVICE;
49+
}
50+
51+
return UR_RESULT_SUCCESS;
52+
}
53+
54+
ur_result_t checkDeviceExtensions(const std::vector<std::string> &Exts,
55+
bool &Supported) {
56+
size_t ExtSize = 0;
57+
CL_RETURN_ON_FAILURE(
58+
clGetDeviceInfo(Device, CL_DEVICE_EXTENSIONS, 0, nullptr, &ExtSize));
59+
60+
std::string ExtStr(ExtSize, '\0');
61+
62+
CL_RETURN_ON_FAILURE(clGetDeviceInfo(Device, CL_DEVICE_EXTENSIONS, ExtSize,
63+
ExtStr.data(), nullptr));
64+
65+
Supported = true;
66+
for (const std::string &Ext : Exts) {
67+
if (!(Supported = (ExtStr.find(Ext) != std::string::npos))) {
68+
break;
69+
}
70+
}
71+
72+
return UR_RESULT_SUCCESS;
73+
}
4474
};

source/adapters/opencl/platform.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,6 @@
1010

1111
#include "platform.hpp"
1212

13-
ur_result_t cl_adapter::getPlatformVersion(cl_platform_id Plat,
14-
oclv::OpenCLVersion &Version) {
15-
16-
size_t PlatVerSize = 0;
17-
CL_RETURN_ON_FAILURE(
18-
clGetPlatformInfo(Plat, CL_PLATFORM_VERSION, 0, nullptr, &PlatVerSize));
19-
20-
std::string PlatVer(PlatVerSize, '\0');
21-
CL_RETURN_ON_FAILURE(clGetPlatformInfo(Plat, CL_PLATFORM_VERSION, PlatVerSize,
22-
PlatVer.data(), nullptr));
23-
24-
Version = oclv::OpenCLVersion(PlatVer);
25-
if (!Version.isValid()) {
26-
return UR_RESULT_ERROR_INVALID_PLATFORM;
27-
}
28-
29-
return UR_RESULT_SUCCESS;
30-
}
31-
3213
static cl_int mapURPlatformInfoToCL(ur_platform_info_t URPropName) {
3314

3415
switch (URPropName) {

source/adapters/opencl/platform.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414

1515
#include <vector>
1616

17-
namespace cl_adapter {
18-
ur_result_t getPlatformVersion(cl_platform_id Plat,
19-
oclv::OpenCLVersion &Version);
20-
} // namespace cl_adapter
21-
2217
struct ur_platform_handle_t_ {
2318
using native_type = cl_platform_id;
2419
native_type Platform = nullptr;
@@ -63,4 +58,22 @@ struct ur_platform_handle_t_ {
6358

6459
return UR_RESULT_SUCCESS;
6560
}
61+
62+
ur_result_t getPlatformVersion(oclv::OpenCLVersion &Version) {
63+
64+
size_t PlatVerSize = 0;
65+
CL_RETURN_ON_FAILURE(clGetPlatformInfo(Platform, CL_PLATFORM_VERSION, 0,
66+
nullptr, &PlatVerSize));
67+
68+
std::string PlatVer(PlatVerSize, '\0');
69+
CL_RETURN_ON_FAILURE(clGetPlatformInfo(
70+
Platform, CL_PLATFORM_VERSION, PlatVerSize, PlatVer.data(), nullptr));
71+
72+
Version = oclv::OpenCLVersion(PlatVer);
73+
if (!Version.isValid()) {
74+
return UR_RESULT_ERROR_INVALID_PLATFORM;
75+
}
76+
77+
return UR_RESULT_SUCCESS;
78+
}
6679
};

0 commit comments

Comments
 (0)