콘텐츠로 이동

Python Device API

함수 시그니처 비고
get_device_count () -> int 유효한 디바이스 ID는 0 .. count-1
set_device (device_id: int) -> None 호출 스레드의 현재 디바이스를 설정
get_device () -> int
get_device_attribute (attr: HaDeviceAttr, device_id: int) -> int 디바이스 속성 하나를 반환(칩 세대는 HaDeviceAttr.VERSION). 값이 없으면 RuntimeError(HaNotSupported)
reset_device (device_id: int) -> None 해당 디바이스의 할당·스트림·이벤트를 무효화
reset_all_devices () -> None atexit 훅으로도 등록됨

예제

get_device

이 스레드가 설정한 값을 되읽습니다. 설정한 적 없으면 예외입니다.

예제
1
2
3
4
import hart

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

get_device_attribute

디바이스 속성 하나를 읽습니다. 속성 종류는 HaDeviceAttr를 참고하십시오.

디바이스가 보고할 수 없는 속성은 0 같은 자리표시 값 대신 RuntimeError(HaNotSupported)를 발생시킵니다. 가상 디바이스 ID는 디바이스 노드 이름의 번호와 같지 않을 수 있습니다(예: 일부 카드만 할당된 환경에서 ID 1이 /dev/ha2일 수 있음). 노드를 찾을 때는 ID로 경로를 만들지 말고 PCI_* 속성으로 확인하십시오.

예제
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)

# 칩 세대: HaDeviceVersion 값을 정수로 반환
version = hart.HaDeviceVersion(hart.get_device_attribute(hart.HaDeviceAttr.VERSION, 0))

get_device_count

디바이스 수와 유효한 ID 범위를 확인합니다.

예제
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):  # 유효한 ID는 0 .. count-1
    hart.set_device(device_id)

reset_all_devices

프로세스 전체에 영향을 줍니다. 다른 스레드가 쓰는 디바이스도 리셋합니다.

예제
1
2
3
4
import hart

stop_workers()  # 먼저 정지시킬 것 -- 런타임이 대신 멈춰주지 않음
hart.reset_all_devices()

reset_device

리셋은 그 디바이스의 모든 것을 무효화하므로 핸들을 먼저 정리합니다.

예제
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)  # 이 줄 이전의 핸들은 전부 낡음

set_device

현재 디바이스는 스레드마다 따로 지정되므로, 스레드 풀의 스레드에는 직접 설정하기 전까지 현재 디바이스가 없습니다.

예제
 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)  # 이걸 안 하면 첫 호출이 예외
    buf = hart.mem_alloc(size)
    run_step(buf)


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

함께 보기