A computer engineer poking at your cerebral cortex.

Keeping your GPU cool with python

Here is my python script that manages my AMD Fans on my mining rigs. Use at your own risk! I’m not responsible if you break your system with this script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012, Dan Sheffner http://digisoftinc.org/
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
 
import commands
import time
import os
 
 
class Fan:
 
    def __init__(self):
 
        self.threshold = 70
        self.tempList = []
        self.fanSpeed = 100
        self.min = 60
        self.numOfCards = 3
        self.sleepTime = 60
 
 
    def getTemp(self):
        command = 'aticonfig --odgt --adapter=all | grep Sensor'
        self.output = commands.getoutput(command)
        outputArray = self.output.split("\n")
 
        for each in outputArray:
            temp = each.split()
            self.tempList.append(temp[4])
 
    def getCurrentSpeed(self):
        self.fanSpeed = open('fanSpeed.txt', 'r').read()
 
    def adjustFanSpeed(self):
        for each2  in self.tempList:
            print each2
            fanChange = False
            if float(each2) > self.threshold and fanChange == False:
 
                #checks to see if the fan is at 100. Nothing to do if it is already set to max.
                if int(self.fanSpeed) <> 100:
                    newSpeed = int(self.fanSpeed) + 5
                    print "increase setting fan speed to " + str(newSpeed)
                    os.system("echo '" + str(newSpeed) + "' > ~/fanSpeed.txt")
                    fanChange = True
                    for each3 in range(self.numOfCards):
                        command = 'export DISPLAY=:0.' + str(each3) + '; aticonfig --pplib-cmd "set fanspeed 0 ' + str(newSpeed) + '"'
                        #print command
                        os.system(command)
 
                else:
                    print "Fan Speed at 100 Max"
                    fanChange = True
 
        if fanChange == False:
 
            #checks to see if the fan is at the minimum you want to set it.
            if int(self.fanSpeed) == self.min:
                print "Minimum fan speed already reached.  You must be running your system well."
            else:
                newSpeed = int(self.fanSpeed) - 5
                print "decrease setting fan speed to " + str(newSpeed)
                os.system("echo '" + str(newSpeed) + "' > ~/fanSpeed.txt")
                for each3 in range(self.numOfCards):
                    command = 'export DISPLAY=:0.' + str(each3) + '; aticonfig --pplib-cmd "set fanspeed 0 ' + str(newSpeed) + '"'
                    #print command
                    os.system(command)
 
    def preFlightCheck(self):
        print "Setting the fan initial speed to min."
        os.system("echo '" + str(self.min) + "' > ~/fanSpeed.txt")
        for each3 in range(self.numOfCards):
                    command = 'export DISPLAY=:0.' + str(each3) + '; aticonfig --pplib-cmd "set fanspeed 0 ' + str(self.min) + '"'
                    #print command
                    os.system(command)
 
    def cleanUp(self):
        self.tempList = []
 
    def run(self):
        print "Created by Dan Sheffner."
 
        # I want to make sure the system fan setting and the fanSpeed.txt
        # match all the time.
        self.preFlightCheck()
 
        while 1==1:
            self.getTemp()
            self.getCurrentSpeed()
            self.adjustFanSpeed()
            self.cleanUp()
 
            print "sleeping for 1 min..."
            time.sleep(self.sleepTime)
 
if __name__ == "__main__":
    f = Fan()
    f.run()