今天寫了一個GPU排隊腳本,事實上還是挺實用的。有的服務器是多用戶使用,GPU的資源常常被占據著,很可能在夜間GPU空閑了,但來不及運行自己的腳本。如果沒有和別人共享服務器的話,自己的多個程序想排隊使用GPU,也可以用這個腳本。環境非常簡單,有python就行了:
python3.7
ubuntu16.04
先創建腳本:
1
|
vim narrow_setup.py |
cmd = 'python xxx.py'這句可以設置你要運行的python腳本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# author: muzhan # contact: levio.pku@gmail.com import os import sys import time cmd = 'python ~/hehe.py' def gpu_info(): gpu_status = os.popen( 'nvidia-smi | grep %' ).read().split( '|' ) gpu_memory = int (gpu_status[ 2 ].split( '/' )[ 0 ].split( 'M' )[ 0 ].strip()) gpu_power = int (gpu_status[ 1 ].split( ' ' )[ - 1 ].split( '/' )[ 0 ].split( 'W' )[ 0 ].strip()) return gpu_power, gpu_memory def narrow_setup(interval = 2 ): gpu_power, gpu_memory = gpu_info() i = 0 while gpu_memory > 1000 or gpu_power > 20 : # set waiting condition 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.flush() time.sleep(interval) i + = 1 print ( '\n' + cmd) os.system(cmd) if __name__ = = '__main__' : narrow_setup() |
直接運行腳本:
1
|
python narrow_setup.py |
就可以監聽nvidia-smi中的信息,以伺機觸發python腳本~
運行結果如下:
等待機會中... 如果gpu顯存和功耗低于某個值時,就會觸發python腳本。
以上就是GPU排隊腳本實現一旦空閑就觸發python腳本實現示例的詳細內容,更多關于GPU排隊腳本實現空閑觸發python腳本的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/leviopku/article/details/102958166