Skip to content

Python Device API

Function Signature Notes
get_device_count () -> int Valid device IDs are 0 .. count-1
set_device (device_id: int) -> None Sets the current device of the calling thread
get_device () -> int
get_device_attribute (attr: HaDeviceAttr, device_id: int) -> int Returns one device attribute (chip generation is HaDeviceAttr.VERSION). Raises RuntimeError (HaNotSupported) if the value is unavailable
reset_device (device_id: int) -> None Invalidates the allocations, streams, and events of that device
reset_all_devices () -> None Also registered as an atexit hook

Example

get_device

Reads back the value set by this thread. Raises if it was never set.

Example
1
2
3
4
import hart

hart.set_device(0)
assert hart.get_device() == 0

get_device_attribute

Reads one device attribute. See HaDeviceAttr for the attribute kinds.

An attribute the device cannot report raises RuntimeError (HaNotSupported) instead of returning a placeholder such as 0. A virtual device ID may differ from the number in the device node name (for example, in an environment where only some cards are assigned, ID 1 can be /dev/ha2). To find the node, do not build the path from the ID; check the PCI_* attributes.

Example
1
2
3
4
5
6
7
import hart

bus = hart.get_device_attribute(hart.HaDeviceAttr.PCI_BUS, 0)
channels = hart.get_device_attribute(hart.HaDeviceAttr.DRAM_CHANNELS, 0)

# Chip generation: returns the HaDeviceVersion value as an integer
version = hart.HaDeviceVersion(hart.get_device_attribute(hart.HaDeviceAttr.VERSION, 0))

get_device_count

Checks the number of devices and the valid ID range.

Example
1
2
3
4
5
6
7
8
import hart

count = hart.get_device_count()
if count == 0:
    raise RuntimeError("no LPU device found")

for device_id in range(count):  # valid IDs are 0 .. count-1
    hart.set_device(device_id)

reset_all_devices

Affects the whole process. It also resets devices used by other threads.

Example
1
2
3
4
import hart

stop_workers()  # stop them first -- the runtime does not stop them for you
hart.reset_all_devices()

reset_device

A reset invalidates everything on that device, so clean up handles first.

Example
1
2
3
4
5
6
7
import hart

hart.stream_synchronize(stream)
hart.stream_destroy(stream)
hart.mem_free(buf)

hart.reset_device(0)  # every handle created before this line is stale

set_device

The current device is set per thread, so a pool thread has none until it sets one itself.

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import threading

import hart


def worker(device_id):
    hart.set_device(device_id)  # without this, the first call raises
    buf = hart.mem_alloc(size)
    run_step(buf)


threads = [threading.Thread(target=worker, args=(i,)) for i in range(hart.get_device_count())]

See also