在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
================================================