-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathze.c
177 lines (139 loc) · 4.87 KB
/
ze.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2024 Intel Corporation. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ze_api.h>
#include "ze.h"
int ze_status_to_string(const uint32_t error, char* out, uint32_t out_size)
{
char* description;
switch (error) {
case ZE_RESULT_SUCCESS:
description = "success"; break;
case ZE_RESULT_NOT_READY:
description = "not ready"; break;
case ZE_RESULT_ERROR_DEVICE_LOST:
description = "device lost"; break;
case ZE_RESULT_ERROR_DEVICE_REQUIRES_RESET:
description = "device requires reset"; break;
case ZE_RESULT_ERROR_DEVICE_IN_LOW_POWER_STATE:
description = "device in low power state"; break;
case ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS:
description = "insufficient permissions"; break;
case ZE_RESULT_ERROR_NOT_AVAILABLE:
description = "not available"; break;
case ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE:
description = "dependency unavailable"; break;
case ZE_RESULT_ERROR_UNINITIALIZED:
description = "uninitialized"; break;
case ZE_RESULT_ERROR_UNSUPPORTED_VERSION:
description = "unsupported version"; break;
case ZE_RESULT_ERROR_UNSUPPORTED_FEATURE:
description = "unsupported feature"; break;
case ZE_RESULT_ERROR_INVALID_ARGUMENT:
description = "invalid argument"; break;
case ZE_RESULT_ERROR_INVALID_NULL_POINTER:
description = "invalid null pointer"; break;
case ZE_RESULT_ERROR_INVALID_NULL_HANDLE:
description = "invalid null handle"; break;
case ZE_RESULT_ERROR_UNKNOWN:
description = "unknown"; break;
default:
description = "not known"; break;
}
return snprintf(out, out_size -1, "%s (0x%X)", description, error);
}
static ze_driver_handle_t initialize_ze(void)
{
ze_result_t res = zeInit(ZE_INIT_FLAG_GPU_ONLY);
if (res != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeInit failed: 0x%X \n", res);
return 0;
}
uint32_t count = 0;
if (zeDriverGet(&count, NULL) != ZE_RESULT_SUCCESS || count == 0) {
fprintf(stderr, "zeDriverGet failed or no drivers\n");
return 0;
}
count = 1;
ze_driver_handle_t handle;
if (zeDriverGet(&count, &handle) != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeDriverGet failed\n");
return 0;
}
return handle;
}
bool ze_try_initialize(void)
{
if (getenv("UNITTEST") != NULL) {
return false;
}
return zeInit(0) == ZE_RESULT_SUCCESS;
}
/// @brief Retrieve indices for Intel levelzero devices
/// @param indices Pointer to an array to store indices
/// @param indices_size Size of the array
/// @return Number of indices stored
int ze_intel_device_indices(uint32_t* indices, uint32_t indices_size, uint32_t *error)
{
if (getenv("UNITTEST") != NULL) {
return 0;
}
if (indices == NULL || 0 == indices_size) {
*error = ZE_RESULT_ERROR_INVALID_NULL_POINTER;
return 0;
}
ze_driver_handle_t handle = initialize_ze();
if (handle == 0) {
*error = ZE_RESULT_ERROR_INVALID_NULL_POINTER;
return 0;
}
ze_result_t res = 0;
uint32_t count = 0;
res = zeDeviceGet(handle, &count, NULL);
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return 0;
}
if (count == 0) {
*error = ZE_RESULT_ERROR_DEVICE_LOST;
return 0;
}
ze_device_handle_t dev_handle[count];
res = zeDeviceGet(handle, &count, dev_handle);
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return 0;
}
if (count > indices_size) {
count = indices_size;
}
int intel_device_count = 0;
// Iterate over the devices and add Intel indices to be returned
for (uint32_t i = 0; i < count; ++i) {
ze_device_handle_t dev_h = dev_handle[i];
ze_device_properties_t dev_prop;
memset(&dev_prop, 0, sizeof(ze_device_properties_t));
res = zeDeviceGetProperties(dev_h, &dev_prop);
if (res != ZE_RESULT_SUCCESS) {
continue;
}
if (dev_prop.vendorId == VENDOR_ID_INTEL) {
indices[intel_device_count] = i;
intel_device_count++;
}
}
return intel_device_count;
}