-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_discover.cpp
57 lines (49 loc) · 2.05 KB
/
gpu_discover.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <level_zero/ze_api.h>
#include <cstdlib>
#include <iostream>
#define L0_SAFE_CALL(call) \
{ \
auto status = (call); \
if (status) { \
std::cerr << "L0 error: " << std::hex << (int)status << " "; \
std::cerr << std::dec << __FILE__ << ":" << __LINE__ << std::endl; \
exit(status); \
} \
}
int main() {
// Initialize the driver
L0_SAFE_CALL(zeInit(0));
// Discover all the driver instances
uint32_t driverCount = 0;
L0_SAFE_CALL(zeDriverGet(&driverCount, nullptr));
ze_driver_handle_t *allDrivers = (ze_driver_handle_t *)malloc(driverCount * sizeof(ze_driver_handle_t));
L0_SAFE_CALL(zeDriverGet(&driverCount, allDrivers));
// Find a driver instance with a GPU device
ze_driver_handle_t hDriver = nullptr;
ze_device_handle_t hDevice = nullptr;
for (uint32_t i = 0; i < driverCount; ++i) {
uint32_t deviceCount = 0;
L0_SAFE_CALL(zeDeviceGet(allDrivers[i], &deviceCount, nullptr));
ze_device_handle_t *allDevices = (ze_device_handle_t *)malloc(deviceCount * sizeof(ze_device_handle_t));
L0_SAFE_CALL(zeDeviceGet(allDrivers[i], &deviceCount, allDevices));
for (uint32_t d = 0; d < deviceCount; ++d) {
ze_device_properties_t device_properties;
L0_SAFE_CALL(zeDeviceGetProperties(allDevices[d], &device_properties));
if (ZE_DEVICE_TYPE_GPU == device_properties.type) {
hDriver = allDrivers[i];
hDevice = allDevices[d];
break;
}
}
free(allDevices);
if (nullptr != hDriver) {
break;
}
}
free(allDrivers);
if (nullptr == hDevice) {
std::cerr << "No GPU devices found" << std::endl;
return 0;
}
std::cout << "GPU found." << std::endl;
}