ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-03-21 14:35:10  阅读:169  来源: 互联网

标签:脚本 __ power gpu split memory 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有