#include <cstring>
#include <iostream>
#include <vector>
#include <ze_api.h>
#include <zet_api.h>
int main()
{
ze_result_t status = zeInit(ZE_INIT_FLAG_GPU_ONLY);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zeInit failed with error code %x.\n", status);
exit(EXIT_FAILURE);
}
uint32_t driverCount = 0;
status = zeDriverGet(&driverCount, nullptr);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zeDriverGet failed with error code %x (Attempted to obtain the number of drivers available).\n", status);
exit(EXIT_FAILURE);
}
if (driverCount == 0) {
printf("No Intel drivers detected on the machine. Exiting.\n");
exit(EXIT_FAILURE);
}
printf("Driver count is: %d\n", driverCount);
std::vector<ze_driver_handle_t> driverList(driverCount, nullptr);
status = zeDriverGet(&driverCount, driverList.data());
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zeDriverGet failed with error code %x (Attempted to obtain the actual driver instances).\n", status);
exit(EXIT_FAILURE);
}
for (ze_driver_handle_t driver : driverList) {
ze_context_handle_t context;
ze_context_desc_t ctxDesc;
ctxDesc.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC;
ctxDesc.pNext = nullptr;
ctxDesc.flags = 0;
status = zeContextCreate(driver, &ctxDesc, &context);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zeContextCreate failed with error code %x.\n", status);
exit(EXIT_FAILURE);
}
uint32_t deviceCount = 0;
status = zeDeviceGet(driver, &deviceCount, nullptr);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zeDeviceGet failed with error code %x (Attempted to obtain the total number of devices available).\n", status);
exit(EXIT_FAILURE);
}
if (deviceCount == 0) {
printf("No Intel GPUs detected on the machine. Exiting.\n");
exit(EXIT_FAILURE);
}
printf("Device count is: %d\n", deviceCount);
std::vector<ze_device_handle_t> deviceList(deviceCount, nullptr);
status = zeDeviceGet(driver, &deviceCount, deviceList.data());
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zeDeviceGet failed with error code %x (Attempted to obtain the handle of devices).\n", status);
exit(EXIT_FAILURE);
}
for (ze_device_handle_t device : deviceList) {
ze_device_properties_t props;
status = zeDeviceGetProperties(device, &props);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zeDeviceGetProperties failed with error code %x.\n", status);
}
if ((props.type != ZE_DEVICE_TYPE_GPU) || (strstr(props.name, "Intel") == nullptr)) {
continue;
}
uint32_t groupCount = 0;
status = zetMetricGroupGet(device, &groupCount, nullptr);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zetMetricGroupGet failed with error code %x.\n", status);
exit(EXIT_FAILURE);
}
if (groupCount == 0) {
printf("Group count is 0. Exiting.\n");
exit(EXIT_FAILURE);
}
printf("Group count is: %d\n", groupCount);
std::vector<zet_metric_group_handle_t> groupHandles(groupCount, nullptr);
status = zetMetricGroupGet(device, &groupCount, groupHandles.data());
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zetMetricGroupSet failed with error code %x.\n", status);
exit(EXIT_FAILURE);
}
for (zet_metric_group_handle_t handle : groupHandles) {
zet_metric_group_properties_t properties;
status = zetMetricGroupGetProperties(handle, &properties);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zetMetricGroupGetProperties failed with error code %x.\n", status);
exit(EXIT_FAILURE);
}
if (properties.samplingType != ZET_METRIC_GROUP_SAMPLING_TYPE_FLAG_EVENT_BASED) {
continue;
}
int numGroupsToActivate = 1;
status = zetContextActivateMetricGroups(context, device, numGroupsToActivate, &handle);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zetContextActivateMetricGroup failed with error code %x.\n", status);
exit(EXIT_FAILURE);
}
zet_metric_group_handle_t group = handle;
zet_metric_query_pool_desc_t metricQueryPoolDesc;
metricQueryPoolDesc.stype = ZET_STRUCTURE_TYPE_METRIC_QUERY_POOL_DESC;
metricQueryPoolDesc.type = ZET_METRIC_QUERY_POOL_TYPE_PERFORMANCE;
metricQueryPoolDesc.count = 1024;
metricQueryPoolDesc.pNext = nullptr;
zet_metric_query_pool_handle_t queryPool;
status = zetMetricQueryPoolCreate(context, device, group, &metricQueryPoolDesc, &queryPool);
if (status != ZE_RESULT_SUCCESS) {
printf("Call to zetMetricQueryPoolCreate failed with error code %x.\n", status);
exit(EXIT_FAILURE);
}
zet_tracer_exp_desc_t tracerDesc;
tracerDesc.stype = ZET_STRUCTURE_TYPE_TRACER_EXP_DESC;
tracerDesc.pUserData = queryPool;
zet_tracer_exp_handle_t tracer;
status = zetTracerExpCreate(context, &tracerDesc, &tracer);
if (status == ZE_RESULT_ERROR_UNINITIALIZED) {
printf("Call to zetTracerExpCreate results in ZE_RESULT_ERROR_UNINITIALIZED.\n");
exit(EXIT_FAILURE);
}
else {
printf("Call to zetTracerExpCreate did not result in ZE_RESULT_ERROR_UNINITIALIZED, code now is: %x.\n", status);
exit(EXIT_FAILURE);
}
}
}
}
return 0;
}
Driver count is: 1
Device count is: 1
Group count is: 1433
Call to zetTracerExpCreate results in ZE_RESULT_ERROR_UNINITIALIZED.
Issue
On an Intel Data Center Max 1100, I encounter the error code
ZE_RESULT_ERROR_UNINITIALIZEDwhen running the below application code:Output:
Compilation
To successfully compile the above application code you must:
ZET_ENABLE_METRICSequal to 1.g++ -I/path/to/include/level_zero scriptname.cpp -o scriptname -L/path/to/level_zero/libs -lze_loader