天天看點

ubuntu 伺服器顯示 計算顯示卡的剩餘 顯存空間 并執行計劃任務

在Ubuntu伺服器上需要運作gpu計算任務,由于需要無人值守的在顯存空間滿足條件的時候運作程式,于是有了自動顯示顯存的程式及自動運作任務的程式:

自動顯示顯存:

import os
import re
import time

memory_need = 10800  # 需求顯存大小
sec = 10  # 每幾秒檢查一次顯示卡空餘

flag = False


while True:
    #print(time.asctime(time.localtime(time.time())))
    output = os.popen('gpustat')
    text = output.read()
    list = text.split('\n')
    #res = re.compile(r'\d+')
    for i, m in enumerate(list[1:-1]):
        #memory = re.findall(res, m)
        #m1 = int(memory[5])
        #m2 = int(memory[4])
        m1 = m.split('|')[2].split('/')[0].strip()
        m2 = m.split('|')[2].split('/')[1].strip().split()[0]
        m1 = int(m1)
        m2 = int(m2)

        print('顯示卡 %d 剩餘記憶體空間 %d (MB)'%(i, m2-m1))

    break      
import os
import time


cmds = ["python x.py",
        "python y.py",
        ]


#############################################


memory_need = 10800  # 需求顯存大小
sec = 10  # 每幾秒檢查一次顯示卡空餘


while True:
    output = os.popen('gpustat')
import os
import time


cmds = ["python x.py",
        "python y.py",
        ]


#############################################


memory_need = 10800  # 需求顯存大小
sec = 10  # 每幾秒檢查一次顯示卡空餘


while True:
    output = os.popen('gpustat')
    text = output.read()
    list = text.split('\n')
    for i, m in enumerate(list[1:-1]):
        m1 = m.split('|')[2].split('/')[0].strip()
        m2 = m.split('|')[2].split('/')[1].strip().split()[0]
        m1 = int(m1)
        m2 = int(m2)
        #print('顯示卡 %d 剩餘記憶體空間 %d (MB)'%(i, m2-m1))


        if (len(cmds) != 0)  and ((m2 - m1) > memory_need):
            response = os.system('CUDA_VISIBLE_DEVICES=%d %s' % (i, cmds[0]))
            if response == 0:
                response.pop(0)

    if (len(cmds) != 0):
        time.sleep(sec)
    else:
        break