|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 |
|
6 | | -ClockId = nvml.ClockId |
7 | | -ClocksEventReasons = nvml.ClocksEventReasons |
8 | | -ClockType = nvml.ClockType |
| 6 | +class ClockId(StrEnum): |
| 7 | + """ |
| 8 | + Clock Ids. These are used in combination with :class:`ClockType` to specify a single clock value. |
| 9 | + """ |
| 10 | + CURRENT = "current" |
| 11 | + CUSTOMER_BOOST_MAX = "customer_boost_max" |
| 12 | + # APP_CLOCK_TARGET and APP_CLOCK_DEFAULT are deprecated so not included here |
| 13 | +ClockId.CURRENT.__doc__ = """ |
| 14 | +Current actual clock value. |
| 15 | +""" |
| 16 | +ClockId.CUSTOMER_BOOST_MAX.__doc__ = """ |
| 17 | +OEM-defined maximum clock rate |
| 18 | +""" |
| 19 | +cdef dict _CLOCK_ID_MAPPING = { |
| 20 | + ClockId.CURRENT: nvml.ClockId.CURRENT, |
| 21 | + ClockId.CUSTOMER_BOOST_MAX: nvml.ClockId.CUSTOMER_BOOST_MAX, |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +class ClocksEventReasons(StrEnum): |
| 26 | + """ |
| 27 | + Reasons for a clocks event. These are used in combination with :class:`ClockType` to specify the reason for a clocks event. |
| 28 | + """ |
| 29 | + NONE = "none" |
| 30 | + GPU_IDLE = "gpu_idle" |
| 31 | + APPLICATIONS_CLOCKS_SETTING = "applications_clocks_setting" |
| 32 | + SW_POWER_CAP = "sw_power_cap" |
| 33 | + HW_SLOWDOWN = "hw_slowdown" |
| 34 | + SYNC_BOOST = "sync_boost" |
| 35 | + SW_THERMAL_SLOWDOWN = "sw_thermal_slowdown" |
| 36 | + HW_THERMAL_SLOWDOWN = "hw_thermal_slowdown" |
| 37 | + HW_POWER_BRAKE_SLOWDOWN = "hw_power_brake_slowdown" |
| 38 | + DISPLAY_CLOCK_SETTING = "display_clock_setting" |
| 39 | +cdef dict _CLOCKS_EVENT_REASONS_MAPPING = { |
| 40 | + nvml.ClocksEventReasons.EVENT_REASON_NONE: ClocksEventReasons.NONE, |
| 41 | + nvml.ClocksEventReasons.EVENT_REASON_GPU_IDLE: ClocksEventReasons.GPU_IDLE, |
| 42 | + nvml.ClocksEventReasons.EVENT_REASON_APPLICATIONS_CLOCKS_SETTING: ClocksEventReasons.APPLICATIONS_CLOCKS_SETTING, |
| 43 | + nvml.ClocksEventReasons.EVENT_REASON_SW_POWER_CAP: ClocksEventReasons.SW_POWER_CAP, |
| 44 | + nvml.ClocksEventReasons.THROTTLE_REASON_HW_SLOWDOWN: ClocksEventReasons.HW_SLOWDOWN, |
| 45 | + nvml.ClocksEventReasons.EVENT_REASON_SYNC_BOOST: ClocksEventReasons.SYNC_BOOST, |
| 46 | + nvml.ClocksEventReasons.EVENT_REASON_SW_THERMAL_SLOWDOWN: ClocksEventReasons.SW_THERMAL_SLOWDOWN, |
| 47 | + nvml.ClocksEventReasons.THROTTLE_REASON_HW_THERMAL_SLOWDOWN: ClocksEventReasons.HW_THERMAL_SLOWDOWN, |
| 48 | + nvml.ClocksEventReasons.THROTTLE_REASON_HW_POWER_BRAKE_SLOWDOWN: ClocksEventReasons.HW_POWER_BRAKE_SLOWDOWN, |
| 49 | + nvml.ClocksEventReasons.EVENT_REASON_DISPLAY_CLOCK_SETTING: ClocksEventReasons.DISPLAY_CLOCK_SETTING, |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +class ClockType(StrEnum): |
| 54 | + """ |
| 55 | + Clock types. All speeds are in Mhz. |
| 56 | + """ |
| 57 | + GRAPHICS = "graphics" |
| 58 | + SM = "sm" |
| 59 | + MEMORY = "memory" |
| 60 | + VIDEO = "video" |
| 61 | +cdef dict _CLOCK_TYPE_MAPPING = { |
| 62 | + ClockType.GRAPHICS: nvml.ClockType.CLOCK_GRAPHICS, |
| 63 | + ClockType.SM: nvml.ClockType.CLOCK_SM, |
| 64 | + ClockType.MEMORY: nvml.ClockType.CLOCK_MEM, |
| 65 | + ClockType.VIDEO: nvml.ClockType.CLOCK_VIDEO, |
| 66 | +} |
9 | 67 |
|
10 | 68 |
|
11 | 69 | cdef class ClockOffsets: |
@@ -48,26 +106,40 @@ cdef class ClockInfo: |
48 | 106 | cdef intptr_t _handle |
49 | 107 | cdef int _clock_type |
50 | 108 |
|
51 | | - def __init__(self, handle, clock_type: ClockType): |
| 109 | + def __init__(self, handle, clock_type: ClockType | str): |
52 | 110 | self._handle = handle |
| 111 | + try: |
| 112 | + clock_type = _CLOCK_TYPE_MAPPING[clock_type] |
| 113 | + except KeyError: |
| 114 | + raise ValueError( |
| 115 | + f"Invalid clock type: {clock_type}. " |
| 116 | + f"Must be one of {list(ClockType.__members__.values())}" |
| 117 | + ) from None |
53 | 118 | self._clock_type = int(clock_type) |
54 | 119 |
|
55 | | - def get_current_mhz(self, clock_id: ClockId = ClockId.CURRENT) -> int: |
| 120 | + def get_current_mhz(self, clock_id: ClockId | str = ClockId.CURRENT) -> int: |
56 | 121 | """ |
57 | 122 | Get the current clock speed of a specific clock domain, in MHz. |
58 | 123 |
|
59 | 124 | For Kepler™ or newer fully supported devices. |
60 | 125 |
|
61 | 126 | Parameters |
62 | 127 | ---------- |
63 | | - clock_id: :class:`ClockId` |
64 | | - The clock ID to query. |
| 128 | + clock_id: :class:`ClockId` | str |
| 129 | + The clock ID to query. Defaults to the current clock value. |
65 | 130 |
|
66 | 131 | Returns |
67 | 132 | ------- |
68 | 133 | int |
69 | 134 | The clock speed in MHz. |
70 | 135 | """ |
| 136 | + try: |
| 137 | + clock_id = _CLOCK_ID_MAPPING[clock_id] |
| 138 | + except KeyError: |
| 139 | + raise ValueError( |
| 140 | + f"Invalid clock ID: {clock_id}. " |
| 141 | + f"Must be one of {list(ClockId.__members__.values())}" |
| 142 | + ) from None |
71 | 143 | return nvml.device_get_clock(self._handle, self._clock_type, clock_id) |
72 | 144 |
|
73 | 145 | def get_max_mhz(self) -> int: |
@@ -99,37 +171,41 @@ cdef class ClockInfo: |
99 | 171 | """ |
100 | 172 | return nvml.device_get_max_customer_boost_clock(self._handle, self._clock_type) |
101 | 173 |
|
102 | | - def get_min_max_clock_of_pstate_mhz(self, pstate: Pstates) -> tuple[int, int]: |
| 174 | + def get_min_max_clock_of_pstate_mhz(self, pstate: int) -> tuple[int, int]: |
103 | 175 | """ |
104 | 176 | Get the minimum and maximum clock speeds for this clock domain |
105 | 177 | at a given performance state (Pstate), in MHz. |
106 | 178 |
|
107 | 179 | Parameters |
108 | 180 | ---------- |
109 | | - pstate: :class:`Pstates` |
110 | | - The performance state to query. |
| 181 | + pstate: int |
| 182 | + The performance state to query. Must be an int between 0 and 15, |
| 183 | + where 0 is the highest performance state (P0) and 15 is the lowest |
| 184 | + (P15). |
111 | 185 |
|
112 | 186 | Returns |
113 | 187 | ------- |
114 | 188 | tuple[int, int] |
115 | 189 | A tuple containing the minimum and maximum clock speeds in MHz. |
116 | 190 | """ |
117 | | - return nvml.device_get_min_max_clock_of_p_state(self._handle, self._clock_type, pstate) |
| 191 | + return nvml.device_get_min_max_clock_of_p_state(self._handle, self._clock_type, _pstate_to_enum(pstate)) |
118 | 192 |
|
119 | | - def get_offsets(self, pstate: Pstates) -> ClockOffsets: |
| 193 | + def get_offsets(self, pstate: int) -> ClockOffsets: |
120 | 194 | """ |
121 | 195 | Retrieve min, max and current clock offset of some clock domain for a given Pstate. |
122 | 196 |
|
123 | 197 | For Maxwell™ or newer fully supported devices. |
124 | 198 |
|
125 | 199 | Parameters |
126 | 200 | ---------- |
127 | | - pstate: :class:`Pstates` |
128 | | - The performance state to query. |
| 201 | + pstate: int |
| 202 | + The performance state to query. Must be an int between 0 and 15, |
| 203 | + where 0 is the highest performance state (P0) and 15 is the lowest |
| 204 | + (P15). |
129 | 205 |
|
130 | 206 | Returns |
131 | 207 | ------- |
132 | 208 | :obj:`~_device.ClockOffsets` |
133 | 209 | An object with the min, max and current clock offset. |
134 | 210 | """ |
135 | | - return ClockOffsets(nvml.device_get_clock_offsets(self._handle, self._clock_type, pstate)) |
| 211 | + return ClockOffsets(nvml.device_get_clock_offsets(self._handle, self._clock_type, _pstate_to_enum(pstate))) |
0 commit comments