|
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 | + |
| 14 | + |
| 15 | +ClockId.CURRENT.__doc__ = "Current actual clock value." |
| 16 | +ClockId.CUSTOMER_BOOST_MAX.__doc__ = "OEM-defined maximum clock rate" |
| 17 | + |
| 18 | + |
| 19 | +_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 | + |
| 40 | + |
| 41 | +_CLOCKS_EVENT_REASONS_MAPPING = { |
| 42 | + nvml.ClocksEventReasons.EVENT_REASON_NONE: ClocksEventReasons.NONE, |
| 43 | + nvml.ClocksEventReasons.EVENT_REASON_GPU_IDLE: ClocksEventReasons.GPU_IDLE, |
| 44 | + nvml.ClocksEventReasons.EVENT_REASON_APPLICATIONS_CLOCKS_SETTING: ClocksEventReasons.APPLICATIONS_CLOCKS_SETTING, |
| 45 | + nvml.ClocksEventReasons.EVENT_REASON_SW_POWER_CAP: ClocksEventReasons.SW_POWER_CAP, |
| 46 | + nvml.ClocksEventReasons.THROTTLE_REASON_HW_SLOWDOWN: ClocksEventReasons.HW_SLOWDOWN, |
| 47 | + nvml.ClocksEventReasons.EVENT_REASON_SYNC_BOOST: ClocksEventReasons.SYNC_BOOST, |
| 48 | + nvml.ClocksEventReasons.EVENT_REASON_SW_THERMAL_SLOWDOWN: ClocksEventReasons.SW_THERMAL_SLOWDOWN, |
| 49 | + nvml.ClocksEventReasons.THROTTLE_REASON_HW_THERMAL_SLOWDOWN: ClocksEventReasons.HW_THERMAL_SLOWDOWN, |
| 50 | + nvml.ClocksEventReasons.THROTTLE_REASON_HW_POWER_BRAKE_SLOWDOWN: ClocksEventReasons.HW_POWER_BRAKE_SLOWDOWN, |
| 51 | + nvml.ClocksEventReasons.EVENT_REASON_DISPLAY_CLOCK_SETTING: ClocksEventReasons.DISPLAY_CLOCK_SETTING, |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +class ClockType(StrEnum): |
| 56 | + """ |
| 57 | + Clock types. All speeds are in Mhz. |
| 58 | + """ |
| 59 | + GRAPHICS = "graphics" |
| 60 | + SM = "sm" |
| 61 | + MEMORY = "memory" |
| 62 | + VIDEO = "video" |
| 63 | + |
| 64 | + |
| 65 | +_CLOCK_TYPE_MAPPING = { |
| 66 | + ClockType.GRAPHICS: nvml.ClockType.CLOCK_GRAPHICS, |
| 67 | + ClockType.SM: nvml.ClockType.CLOCK_SM, |
| 68 | + ClockType.MEMORY: nvml.ClockType.CLOCK_MEM, |
| 69 | + ClockType.VIDEO: nvml.ClockType.CLOCK_VIDEO, |
| 70 | +} |
9 | 71 |
|
10 | 72 |
|
11 | 73 | cdef class ClockOffsets: |
@@ -48,26 +110,40 @@ cdef class ClockInfo: |
48 | 110 | cdef intptr_t _handle |
49 | 111 | cdef int _clock_type |
50 | 112 |
|
51 | | - def __init__(self, handle, clock_type: ClockType): |
| 113 | + def __init__(self, handle, clock_type: ClockType | str): |
52 | 114 | self._handle = handle |
| 115 | + try: |
| 116 | + clock_type = _CLOCK_TYPE_MAPPING[clock_type] |
| 117 | + except KeyError: |
| 118 | + raise ValueError( |
| 119 | + f"Invalid clock type: {clock_type}. " |
| 120 | + f"Must be one of {list(ClockType.__members__.values())}" |
| 121 | + ) from None |
53 | 122 | self._clock_type = int(clock_type) |
54 | 123 |
|
55 | | - def get_current_mhz(self, clock_id: ClockId = ClockId.CURRENT) -> int: |
| 124 | + def get_current_mhz(self, clock_id: ClockId | str = ClockId.CURRENT) -> int: |
56 | 125 | """ |
57 | 126 | Get the current clock speed of a specific clock domain, in MHz. |
58 | 127 |
|
59 | 128 | For Kepler™ or newer fully supported devices. |
60 | 129 |
|
61 | 130 | Parameters |
62 | 131 | ---------- |
63 | | - clock_id: :class:`ClockId` |
64 | | - The clock ID to query. |
| 132 | + clock_id: :class:`ClockId` | str |
| 133 | + The clock ID to query. Defaults to the current clock value. |
65 | 134 |
|
66 | 135 | Returns |
67 | 136 | ------- |
68 | 137 | int |
69 | 138 | The clock speed in MHz. |
70 | 139 | """ |
| 140 | + try: |
| 141 | + clock_id = _CLOCK_ID_MAPPING[clock_id] |
| 142 | + except KeyError: |
| 143 | + raise ValueError( |
| 144 | + f"Invalid clock ID: {clock_id}. " |
| 145 | + f"Must be one of {list(ClockId.__members__.values())}" |
| 146 | + ) from None |
71 | 147 | return nvml.device_get_clock(self._handle, self._clock_type, clock_id) |
72 | 148 |
|
73 | 149 | def get_max_mhz(self) -> int: |
@@ -99,37 +175,41 @@ cdef class ClockInfo: |
99 | 175 | """ |
100 | 176 | return nvml.device_get_max_customer_boost_clock(self._handle, self._clock_type) |
101 | 177 |
|
102 | | - def get_min_max_clock_of_pstate_mhz(self, pstate: Pstates) -> tuple[int, int]: |
| 178 | + def get_min_max_clock_of_pstate_mhz(self, pstate: int) -> tuple[int, int]: |
103 | 179 | """ |
104 | 180 | Get the minimum and maximum clock speeds for this clock domain |
105 | 181 | at a given performance state (Pstate), in MHz. |
106 | 182 |
|
107 | 183 | Parameters |
108 | 184 | ---------- |
109 | | - pstate: :class:`Pstates` |
110 | | - The performance state to query. |
| 185 | + pstate: int |
| 186 | + The performance state to query. Must be an int between 0 and 15, |
| 187 | + where 0 is the highest performance state (P0) and 15 is the lowest |
| 188 | + (P15). |
111 | 189 |
|
112 | 190 | Returns |
113 | 191 | ------- |
114 | 192 | tuple[int, int] |
115 | 193 | A tuple containing the minimum and maximum clock speeds in MHz. |
116 | 194 | """ |
117 | | - return nvml.device_get_min_max_clock_of_p_state(self._handle, self._clock_type, pstate) |
| 195 | + return nvml.device_get_min_max_clock_of_p_state(self._handle, self._clock_type, _pstate_to_enum(pstate)) |
118 | 196 |
|
119 | | - def get_offsets(self, pstate: Pstates) -> ClockOffsets: |
| 197 | + def get_offsets(self, pstate: int) -> ClockOffsets: |
120 | 198 | """ |
121 | 199 | Retrieve min, max and current clock offset of some clock domain for a given Pstate. |
122 | 200 |
|
123 | 201 | For Maxwell™ or newer fully supported devices. |
124 | 202 |
|
125 | 203 | Parameters |
126 | 204 | ---------- |
127 | | - pstate: :class:`Pstates` |
128 | | - The performance state to query. |
| 205 | + pstate: int |
| 206 | + The performance state to query. Must be an int between 0 and 15, |
| 207 | + where 0 is the highest performance state (P0) and 15 is the lowest |
| 208 | + (P15). |
129 | 209 |
|
130 | 210 | Returns |
131 | 211 | ------- |
132 | 212 | :obj:`~_device.ClockOffsets` |
133 | 213 | An object with the min, max and current clock offset. |
134 | 214 | """ |
135 | | - return ClockOffsets(nvml.device_get_clock_offsets(self._handle, self._clock_type, pstate)) |
| 215 | + return ClockOffsets(nvml.device_get_clock_offsets(self._handle, self._clock_type, _pstate_to_enum(pstate))) |
0 commit comments