|
3 | 3 | import ctypes |
4 | 4 | import ctypes.util |
5 | 5 | import logging |
| 6 | +import json |
| 7 | +import uuid |
6 | 8 |
|
7 | 9 | import numpy as np |
8 | 10 |
|
|
32 | 34 |
|
33 | 35 | hipSuccess = 0 |
34 | 36 |
|
| 37 | + |
| 38 | +def extract_safe_properties(props): |
| 39 | + """Extract those properties that are 'safe' (i.e, json serializable)""" |
| 40 | + result = dict() |
| 41 | + |
| 42 | + for key in props.PROPERTIES(): |
| 43 | + try: |
| 44 | + value = getattr(props, key) |
| 45 | + if isinstance(value, (str, int, float)): |
| 46 | + # These are safe |
| 47 | + result[key] = value |
| 48 | + elif isinstance(value, (bytes, bytearray)): |
| 49 | + # bytes/bytearray is typically a string |
| 50 | + result[key] = value.decode("utf-8").rstrip("\x00") |
| 51 | + else: |
| 52 | + # As last option, try to convert to json |
| 53 | + result[key] = json.loads(json.dumps(value)) |
| 54 | + except Exception: |
| 55 | + continue |
| 56 | + |
| 57 | + # Remove the 'reserved' fields as they are just filled with zeros |
| 58 | + result.pop("reserved", None) |
| 59 | + result.pop("hipReserved", None) |
| 60 | + |
| 61 | + return result |
| 62 | + |
| 63 | + |
35 | 64 | class HipFunctions(GPUBackend): |
36 | | - """Class that groups the HIP functions on maintains state about the device.""" |
| 65 | + """Class that groups the HIP functions and maintains state about the device.""" |
37 | 66 |
|
38 | 67 | def __init__(self, device=0, iterations=7, compiler_options=None, observers=None): |
39 | 68 | """Instantiate HipFunctions object used for interacting with the HIP device. |
@@ -72,6 +101,13 @@ def __init__(self, device=0, iterations=7, compiler_options=None, observers=None |
72 | 101 | env["device_name"] = self.name |
73 | 102 | env["iterations"] = self.iterations |
74 | 103 | env["compiler_options"] = compiler_options |
| 104 | + env["pci_bus_id"] = props.pciBusID |
| 105 | + env["pci_domain_id"] = props.pciDomainID |
| 106 | + env["pci_device_id"] = props.pciDeviceID |
| 107 | + env["uuid"] = str(uuid.UUID(bytes=props.uuid.bytes)) |
| 108 | + env["hip_driver_version"] = hip_check(hip.hipDriverGetVersion()) |
| 109 | + env["hip_runtime_version"] = hip_check(hip.hipRuntimeGetVersion()) |
| 110 | + env["device_properties"] = extract_safe_properties(props) |
75 | 111 | self.env = env |
76 | 112 |
|
77 | 113 | # Create stream and events |
|
0 commit comments