def __del__(self): #try to restore to defaults if self._pwr_limit != None and self.pwr_limit != self.pwr_limit_default: self.pwr_limit = self.pwr_limit_default if self.gr_clock_default != None: gr_app_clock = pynvml.nvmlDeviceGetApplicationsClock( self.dev, pynvml.NVML_CLOCK_GRAPHICS) mem_app_clock = pynvml.nvmlDeviceGetApplicationsClock( self.dev, pynvml.NVML_CLOCK_MEM) if gr_app_clock != self.gr_clock_default or mem_app_clock != self.mem_clock_default: self.set_clocks(self.mem_clock_default, self.gr_clock_default)
def set_clocks(self, mem_clock, gr_clock): """Set the memory and graphics clock for this device (may require permission)""" if not mem_clock in self.supported_mem_clocks: raise ValueError("Illegal value for memory clock") if not gr_clock in self.supported_gr_clocks[mem_clock]: raise ValueError("Graphics clock incompatible with memory clock") pynvml.nvmlDeviceSetApplicationsClocks(self.dev, mem_clock, gr_clock) self._gr_clock = pynvml.nvmlDeviceGetApplicationsClock( self.dev, pynvml.NVML_CLOCK_GRAPHICS) self._sm_clock = pynvml.nvmlDeviceGetApplicationsClock( self.dev, pynvml.NVML_CLOCK_SM) self._mem_clock = pynvml.nvmlDeviceGetApplicationsClock( self.dev, pynvml.NVML_CLOCK_MEM)
def __init__(self, device_id=0): """Create object to control device using NVML""" pynvml.nvmlInit() self.dev = pynvml.nvmlDeviceGetHandleByIndex(device_id) try: self._pwr_limit = pynvml.nvmlDeviceGetPowerManagementLimit( self.dev) self.pwr_constraints = pynvml.nvmlDeviceGetPowerManagementLimitConstraints( self.dev) self.pwr_limit_default = pynvml.nvmlDeviceGetPowerManagementDefaultLimit( self.dev) except pynvml.NVMLError_NotSupported: self._pwr_limit = None self.pwr_constraints = [ 1, 0 ] # inverted range to make all range checks fail try: self._persistence_mode = pynvml.nvmlDeviceGetPersistenceMode( self.dev) except pynvml.NVMLError_NotSupported: self._persistence_mode = None try: self._auto_boost = pynvml.nvmlDeviceGetAutoBoostedClocksEnabled( self.dev)[0] # returns [isEnabled, isDefaultEnabled] except pynvml.NVMLError_NotSupported: self._auto_boost = None try: self.gr_clock_default = pynvml.nvmlDeviceGetDefaultApplicationsClock( self.dev, pynvml.NVML_CLOCK_GRAPHICS) self.sm_clock_default = pynvml.nvmlDeviceGetDefaultApplicationsClock( self.dev, pynvml.NVML_CLOCK_SM) self.mem_clock_default = pynvml.nvmlDeviceGetDefaultApplicationsClock( self.dev, pynvml.NVML_CLOCK_MEM) self.supported_mem_clocks = pynvml.nvmlDeviceGetSupportedMemoryClocks( self.dev) self._gr_clock = pynvml.nvmlDeviceGetApplicationsClock( self.dev, pynvml.NVML_CLOCK_GRAPHICS) self._sm_clock = pynvml.nvmlDeviceGetApplicationsClock( self.dev, pynvml.NVML_CLOCK_SM) self._mem_clock = pynvml.nvmlDeviceGetApplicationsClock( self.dev, pynvml.NVML_CLOCK_MEM) #gather the supported gr clocks for each supported mem clock into a dict self.supported_gr_clocks = dict() for mem_clock in self.supported_mem_clocks: supported_gr_clocks = pynvml.nvmlDeviceGetSupportedGraphicsClocks( self.dev, mem_clock) self.supported_gr_clocks[mem_clock] = supported_gr_clocks except pynvml.NVMLError_NotSupported: self.gr_clock_default = None self.sm_clock_default = None self.mem_clock_default = None self.supported_mem_clocks = [] self.supported_gr_clocks = dict()
def mem_clock(self): """Control the graphics clock (may require permission), only values compatible with the graphics clock can be set directly""" return pynvml.nvmlDeviceGetApplicationsClock(self.dev, pynvml.NVML_CLOCK_MEM)