Skip to content

Commit 3a7153f

Browse files
authored
Merge pull request #595 from bgoglin/l0-always-sysman
levelzero: only use Sysman queries instead of similar Core API queries
2 parents 0474e06 + f0a69cd commit 3a7153f

File tree

4 files changed

+38
-132
lines changed

4 files changed

+38
-132
lines changed

hwloc/topology-levelzero.c

+23-116
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,21 @@ hwloc__levelzero_osdev_array_find(struct hwloc_osdev_array *array,
6363

6464
static void
6565
hwloc__levelzero_properties_get(ze_device_handle_t zeh, zes_device_handle_t zesh,
66-
hwloc_obj_t osdev,
67-
int *is_integrated_p)
66+
hwloc_obj_t osdev, ze_device_properties_t *prop)
6867
{
6968
ze_result_t res;
70-
ze_device_properties_t prop;
69+
ze_device_properties_t _prop;
7170
zes_device_properties_t prop2;
7271
int is_subdevice = 0;
73-
int is_integrated = 0;
7472

75-
memset(&prop, 0, sizeof(prop));
76-
res = zeDeviceGetProperties(zeh, &prop);
77-
if (res == ZE_RESULT_SUCCESS) {
73+
if (!prop) {
74+
/* no properties were given, get ours */
75+
memset(&_prop, 0, sizeof(_prop));
76+
res = zeDeviceGetProperties(zeh, &_prop);
77+
if (res == ZE_RESULT_SUCCESS)
78+
prop = &_prop;
79+
}
80+
if (prop) {
7881
/* name is the model name followed by the deviceID
7982
* flags 1<<0 means integrated (vs discrete).
8083
*/
@@ -83,41 +86,35 @@ hwloc__levelzero_properties_get(ze_device_handle_t zeh, zes_device_handle_t zesh
8386
unsigned i;
8487
const char *type;
8588

86-
switch (prop.type) {
89+
switch (prop->type) {
8790
case ZE_DEVICE_TYPE_GPU: type = "GPU"; break;
8891
case ZE_DEVICE_TYPE_CPU: type = "CPU"; break;
8992
case ZE_DEVICE_TYPE_FPGA: type = "FPGA"; break;
9093
case ZE_DEVICE_TYPE_MCA: type = "MCA"; break;
9194
case ZE_DEVICE_TYPE_VPU: type = "VPU"; break;
9295
default:
9396
if (HWLOC_SHOW_ALL_ERRORS())
94-
fprintf(stderr, "hwloc/levelzero: unexpected device type %u\n", (unsigned) prop.type);
97+
fprintf(stderr, "hwloc/levelzero: unexpected device type %u\n", (unsigned) prop->type);
9598
type = "Unknown";
9699
}
97100
hwloc_obj_add_info(osdev, "LevelZeroDeviceType", type);
98-
snprintf(tmp, sizeof(tmp), "%u", prop.numSlices);
101+
snprintf(tmp, sizeof(tmp), "%u", prop->numSlices);
99102
hwloc_obj_add_info(osdev, "LevelZeroNumSlices", tmp);
100-
snprintf(tmp, sizeof(tmp), "%u", prop.numSubslicesPerSlice);
103+
snprintf(tmp, sizeof(tmp), "%u", prop->numSubslicesPerSlice);
101104
hwloc_obj_add_info(osdev, "LevelZeroNumSubslicesPerSlice", tmp);
102-
snprintf(tmp, sizeof(tmp), "%u", prop.numEUsPerSubslice);
105+
snprintf(tmp, sizeof(tmp), "%u", prop->numEUsPerSubslice);
103106
hwloc_obj_add_info(osdev, "LevelZeroNumEUsPerSubslice", tmp);
104-
snprintf(tmp, sizeof(tmp), "%u", prop.numThreadsPerEU);
107+
snprintf(tmp, sizeof(tmp), "%u", prop->numThreadsPerEU);
105108
hwloc_obj_add_info(osdev, "LevelZeroNumThreadsPerEU", tmp);
106109

107110
for(i=0; i<ZE_MAX_DEVICE_UUID_SIZE; i++)
108-
snprintf(uuid+2*i, 3, "%02x", prop.uuid.id[i]);
111+
snprintf(uuid+2*i, 3, "%02x", prop->uuid.id[i]);
109112
hwloc_obj_add_info(osdev, "LevelZeroUUID", uuid);
110113

111-
if (prop.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE)
114+
if (prop->flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE)
112115
is_subdevice = 1;
113-
114-
if (prop.flags & ZE_DEVICE_PROPERTY_FLAG_INTEGRATED)
115-
is_integrated = 1;
116116
}
117117

118-
if (is_integrated_p)
119-
*is_integrated_p = is_integrated;
120-
121118
if (is_subdevice)
122119
/* sysman API on subdevice returns the same as root device, and we don't need those duplicate attributes */
123120
return;
@@ -172,9 +169,9 @@ hwloc__levelzero_cqprops_get(ze_device_handle_t zeh,
172169
}
173170

174171
static int
175-
hwloc__levelzero_memory_get_from_sysman(zes_device_handle_t zesh,
176-
hwloc_obj_t root_osdev,
177-
unsigned nr_osdevs, hwloc_obj_t *sub_osdevs)
172+
hwloc__levelzero_memory_get(zes_device_handle_t zesh,
173+
hwloc_obj_t root_osdev,
174+
unsigned nr_osdevs, hwloc_obj_t *sub_osdevs)
178175
{
179176
zes_mem_handle_t *mh;
180177
uint32_t nr_mems;
@@ -279,95 +276,6 @@ hwloc__levelzero_memory_get_from_sysman(zes_device_handle_t zesh,
279276
return 0;
280277
}
281278

282-
static void
283-
hwloc__levelzero_memory_get_from_coreapi(ze_device_handle_t zeh,
284-
hwloc_obj_t osdev,
285-
int ignore_ddr)
286-
{
287-
ze_device_memory_properties_t *mh;
288-
uint32_t nr_mems;
289-
ze_result_t res;
290-
291-
nr_mems = 0;
292-
res = zeDeviceGetMemoryProperties(zeh, &nr_mems, NULL);
293-
if (res != ZE_RESULT_SUCCESS || !nr_mems)
294-
return;
295-
hwloc_debug("L0/CoreAPI: found %u memories in osdev %s\n",
296-
nr_mems, osdev->name);
297-
298-
mh = malloc(nr_mems * sizeof(*mh));
299-
if (mh) {
300-
res = zeDeviceGetMemoryProperties(zeh, &nr_mems, mh);
301-
if (res == ZE_RESULT_SUCCESS) {
302-
unsigned m;
303-
for(m=0; m<nr_mems; m++) {
304-
const char *_name = mh[m].name;
305-
char name[300], value[64];
306-
/* FIXME: discrete GPUs report 95% of the physical memory (what sysman sees)
307-
* while integrated GPUs report 80% of the host RAM (sysman sees 0), adjust?
308-
*/
309-
hwloc_debug("L0/CoreAPI: found memory name %s size %llu in osdev %s\n",
310-
mh[m].name, (unsigned long long) mh[m].totalSize, osdev->name);
311-
if (!mh[m].totalSize)
312-
continue;
313-
if (ignore_ddr && !strcmp(_name, "DDR"))
314-
continue;
315-
if (!_name[0])
316-
_name = "Memory";
317-
snprintf(name, sizeof(name), "LevelZero%sSize", _name); /* HBM or DDR, or Memory if unknown */
318-
snprintf(value, sizeof(value), "%lluKiB", (unsigned long long) mh[m].totalSize >> 10);
319-
hwloc_obj_add_info(osdev, name, value);
320-
}
321-
}
322-
free(mh);
323-
}
324-
}
325-
326-
327-
static void
328-
hwloc__levelzero_memory_get(ze_device_handle_t zeh, zes_device_handle_t zesh,
329-
hwloc_obj_t root_osdev, int is_integrated,
330-
unsigned nr_subdevices, zes_device_handle_t *subzehs, hwloc_obj_t *sub_osdevs)
331-
{
332-
static int memory_from_coreapi = -1; /* 1 means coreapi, 0 means sysman, -1 means sysman if available or coreapi otherwise */
333-
static int first = 1;
334-
335-
if (first) {
336-
char *env;
337-
env = getenv("HWLOC_L0_COREAPI_MEMORY");
338-
if (env)
339-
memory_from_coreapi = atoi(env);
340-
341-
if (memory_from_coreapi == -1) {
342-
int ret = hwloc__levelzero_memory_get_from_sysman(zesh, root_osdev, nr_subdevices, sub_osdevs);
343-
if (!ret) {
344-
/* sysman worked, we're done, disable coreapi for next time */
345-
hwloc_debug("levelzero: sysman/memory succeeded, disabling coreapi memory queries\n");
346-
memory_from_coreapi = 0;
347-
return;
348-
}
349-
/* sysman failed, enable coreapi */
350-
hwloc_debug("levelzero: sysman/memory failed, enabling coreapi memory queries\n");
351-
memory_from_coreapi = 1;
352-
}
353-
354-
first = 0;
355-
}
356-
357-
if (memory_from_coreapi > 0) {
358-
unsigned k;
359-
int ignore_ddr = (memory_from_coreapi != 2) && is_integrated; /* DDR ignored in integrated GPUs, it's like the host DRAM */
360-
hwloc__levelzero_memory_get_from_coreapi(zeh, root_osdev, ignore_ddr);
361-
for(k=0; k<nr_subdevices; k++)
362-
hwloc__levelzero_memory_get_from_coreapi(subzehs[k], sub_osdevs[k], ignore_ddr);
363-
} else {
364-
hwloc__levelzero_memory_get_from_sysman(zesh, root_osdev, nr_subdevices, sub_osdevs);
365-
/* no need to call hwloc__levelzero_memory_get() on subdevices,
366-
* the call on the root device is enough (and identical to a call on subdevices)
367-
*/
368-
}
369-
}
370-
371279
struct hwloc_levelzero_ports {
372280
unsigned nr_allocated;
373281
unsigned nr;
@@ -551,7 +459,6 @@ hwloc__levelzero_devices_get(struct hwloc_topology *topology,
551459
hwloc_obj_t osdev, parent, *subosdevs = NULL;
552460
ze_device_properties_t props;
553461
zes_uuid_t uuid;
554-
int is_integrated = 0;
555462
ze_bool_t onSubdevice = 0;
556463
uint32_t subdeviceId = 0;
557464

@@ -583,7 +490,7 @@ hwloc__levelzero_devices_get(struct hwloc_topology *topology,
583490
snprintf(buffer, sizeof(buffer), "%u", j);
584491
hwloc_obj_add_info(osdev, "LevelZeroDriverDeviceIndex", buffer);
585492

586-
hwloc__levelzero_properties_get(zeh, zesh, osdev, &is_integrated);
493+
hwloc__levelzero_properties_get(zeh, zesh, osdev, &props);
587494

588495
hwloc__levelzero_cqprops_get(zeh, osdev);
589496

@@ -641,7 +548,7 @@ hwloc__levelzero_devices_get(struct hwloc_topology *topology,
641548
}
642549

643550
/* get all memory info at once */
644-
hwloc__levelzero_memory_get(zeh, zesh, osdev, is_integrated, nr_subdevices, subzehs, subosdevs);
551+
hwloc__levelzero_memory_get(zesh, osdev, nr_subdevices, subosdevs);
645552

646553
/* get all ports info at once */
647554
if (!(hwloc_topology_get_flags(topology) & HWLOC_TOPOLOGY_FLAG_NO_DISTANCES))

include/hwloc/levelzero.h

+8
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ hwloc_levelzero_get_sysman_device_cpuset(hwloc_topology_t topology __hwloc_attri
166166
* topology. If not, the locality of the object may still be found using
167167
* hwloc_levelzero_get_device_cpuset().
168168
*
169+
* \note If the input ZE device is actually a subdevice, then its parent
170+
* (root device) is actually translated, i.e. the main hwloc OS device
171+
* is returned instead of one of its children.
172+
*
169173
* \note The corresponding hwloc PCI device may be found by looking
170174
* at the result parent pointer (unless PCI devices are filtered out).
171175
*
@@ -231,6 +235,10 @@ hwloc_levelzero_get_device_osdev(hwloc_topology_t topology, ze_device_handle_t d
231235
* topology. If not, the locality of the object may still be found using
232236
* hwloc_levelzero_get_device_cpuset().
233237
*
238+
* \note If the input ZES device is actually a subdevice, then its parent
239+
* (root device) is actually translated, i.e. the main hwloc OS device
240+
* is returned instead of one of its children.
241+
*
234242
* \note The corresponding hwloc PCI device may be found by looking
235243
* at the result parent pointer (unless PCI devices are filtered out).
236244
*/

tests/hwloc/levelzero.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ int main(void)
164164
sdvh = malloc(nbdevices * sizeof(*sdvh));
165165
if (!sdvh)
166166
continue;
167-
res = zeDeviceGet(sdrh[i], &nbdevices, sdvh);
167+
res = zesDeviceGet(sdrh[i], &nbdevices, sdvh);
168168
if (res != ZE_RESULT_SUCCESS) {
169169
free(sdvh);
170170
continue;
@@ -185,18 +185,17 @@ int main(void)
185185
printf("found OSDev %s\n", osdev->name);
186186
err = strncmp(osdev->name, "ze", 2);
187187
assert(!err);
188-
assert(atoi(osdev->name+2) == (int) k);
188+
/* don't check the index,
189+
* ZE and ZES device orders may be different inside a single driver.
190+
*/
189191

190192
assert(osdev->attr->osdev.types == (HWLOC_OBJ_OSDEV_COPROC|HWLOC_OBJ_OSDEV_GPU));
191193

192194
assert(has_levelzero_backend);
193195

194-
value = hwloc_obj_get_info_by_name(osdev, "LevelZeroDriverIndex");
195-
assert(value);
196-
assert(atoi(value) == (int) i);
197-
value = hwloc_obj_get_info_by_name(osdev, "LevelZeroDriverDeviceIndex");
198-
assert(value);
199-
assert(atoi(value) == (int) j);
196+
/* don't check LevelZeroDriverIndex and LevelZeroDriverDeviceIndex,
197+
* ZE and ZES device orders may be different inside a single driver.
198+
*/
200199

201200
set = hwloc_bitmap_alloc();
202201
err = hwloc_levelzero_get_sysman_device_cpuset(topology, sdvh[j], set);

tests/hwloc/ports/include/levelzero/level_zero/ze_api.h

-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ typedef enum _ze_device_type {
2828
ZE_DEVICE_TYPE_VPU = 5
2929
} ze_device_type_t;
3030

31-
#define ZE_DEVICE_PROPERTY_FLAG_INTEGRATED (1<<0)
3231
#define ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE (1<<1)
3332

3433
#define ZE_MAX_DEVICE_UUID_SIZE 16
@@ -58,13 +57,6 @@ extern ze_result_t zeDeviceGetCommandQueueGroupProperties(ze_driver_handle_t, ui
5857

5958
extern ze_result_t zeDeviceGetSubDevices(ze_device_handle_t, uint32_t *, ze_device_handle_t*);
6059

61-
typedef struct ze_device_memory_properties {
62-
uint64_t totalSize;
63-
char *name;
64-
} ze_device_memory_properties_t;
65-
66-
extern ze_result_t zeDeviceGetMemoryProperties(ze_device_handle_t, uint32_t *, ze_device_memory_properties_t*);
67-
6860
typedef struct ze_pci_address_ext {
6961
uint32_t domain, bus, device, function;
7062
} ze_pci_address_ext_t;

0 commit comments

Comments
 (0)