其他分享
首页 > 其他分享> > GPU排队脚本,当GPU空闲就触发脚本

GPU排队脚本,当GPU空闲就触发脚本

作者:互联网

 

import os
import sys
import time
 
cmd = 'CUDA_VISIBLE_DEVICES=0 nohup bash ../run.sh --stage 6'  #当GPU空闲时需要跑的脚本
 
 
def gpu_info():
    gpu_status = os.popen('nvidia-smi | grep %').read().split('|') #根据nvidia-smi命令的返回值按照'|'为分隔符建立一个列表
    '''
    结果如:
    ['', ' N/A   64C    P0    68W /  70W ', '   9959MiB / 15079MiB ', '     79%      Default ', 
    '\n', ' N/A   73C    P0   108W /  70W ', '  11055MiB / 15079MiB ', '     63%      Default ', 
    '\n', ' N/A   60C    P0    55W /  70W ', '   3243MiB / 15079MiB ', '     63%      Default ', '\n']
    '''
    gpu_memory = int(gpu_status[2].split('/')[0].split('M')[0].strip()) 
    #获取当前0号GPU功率值:提取标签为2的元素,按照'/'为分隔符后提取标签为0的元素值再按照'M'为分隔符提取标签为0的元素值,返回值为int形式 
    gpu_power = int(gpu_status[1].split('   ')[-1].split('/')[0].split('W')[0].strip())
    #获取0号GPU当前显存使用量
    return gpu_power, gpu_memory
 
 
def narrow_setup(secs=600):  #间隔十分钟检测一次
    gpu_power, gpu_memory = gpu_info()
    i = 0
    while gpu_memory > 1000 or gpu_power > 20:  # false时退出循环
        gpu_power, gpu_memory = gpu_info()
        i = i % 5
        symbol = 'monitoring: ' + '>' * i + ' ' * (10 - i - 1) + '|'
        gpu_power_str = 'gpu power:%d W |' % gpu_power
        gpu_memory_str = 'gpu memory:%d MiB |' % gpu_memory
        sys.stdout.write('\r' + gpu_memory_str + ' ' + gpu_power_str + ' ' + symbol)
        #sys.stdout.write(obj+'\n')等价于print(obj)
        sys.stdout.flush()    #刷新输出
        time.sleep(secs)  #推迟调用线程的运行,通过参数指秒数,表示进程挂起的时间。
        i += 1
    print('\n' + cmd)
    os.system(cmd)
 
 
if __name__ == '__main__':
    narrow_setup()

来源:https://blog.csdn.net/leviopku/article/details/102958166

标签:脚本,__,power,gpu,split,memory,GPU,空闲
来源: https://www.cnblogs.com/Uriel-w/p/16034537.html