7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GPUが熱くなって落ちる前に休ませるpythonプログラム

Last updated at Posted at 2018-11-04

大した話じゃないんですが、この4 GPU system (1080Ti)
gpu.jpg
を4つを完全並列でフルに使ってpycudaで計算すると時々落ちるのです。開けっ放しにしたり置く場所を変えてたりしたんですが、やっぱり時々落ちる。どれか一つが落ちると再起動しないとならなくて面倒くさいので、この4つのGPUの最大温度を取得して、ある温度maxtを超えたら、settに落ち着くまで、sleepさせるパスタコードを書いたのでメモ。といってもnvidia-smiをsubprocessでよんでsleepさせるというなんかアレなコードですが、もっと良さそうな何かを知っていたら教えてください。

これが元になっています。複数GPUの最大温度がmaxtを越えてたら、sett度になるまでsleepさせ30秒ごとにチェックするという単純なコードですが意外とうまくいきます。

gpucheck.py

import subprocess
import numpy as np
DEFAULT_ATTRIBUTES = (
    'index',
    'temperature.gpu',
)

def get_gpu_temperature(nvidia_smi_path='nvidia-smi', keys=DEFAULT_ATTRIBUTES, no_units=True):
    nu_opt = '' if not no_units else ',nounits'
    cmd = '%s --query-gpu=%s --format=csv,noheader%s' % (nvidia_smi_path, ','.join(keys), nu_opt)
    output = subprocess.check_output(cmd, shell=True)
    lines = (output).decode("utf-8").split('\n')
    lines = [ line.strip() for line in lines if line.strip() != '' ]

    return [ { k: v for k, v in zip(keys, line.split(', ')) } for line in lines ]

def getmaxgput_gput():
    a=get_gpu_temperature()
    gputemp=[]
    for i in range(0,len(a)):
        gputemp.append(float(a[i]["temperature.gpu"]))
    gputemp=np.array(gputemp)
    maxgput=np.max(gputemp)
    return maxgput

if __name__ == "__main__":
    from time import sleep 

    #### GPU HEALTH CHECK #######
    maxt=85.0 #max temperature C
    sett=40.0 #setting C
    sleeptime=30.0 #sec
    maxgput=getmaxgput_gput()
    if maxgput > maxt:
        print("Fever. Waiting for cool down of the fevered GPU. ")
        fever = True
        while fever :
            sleep(sleeptime)
            maxgput=getmaxgput_gput()
            print(maxgput,"[C]")
            if maxgput < sett:
                fever = False

7
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?