參考文章:https://www.cnblogs.com/vincent1997/p/10896299.html
https://blog.csdn.net/nkhgl/article/details/83957020
https://blog.csdn.net/u010412858/article/details/83110947
https://blog.csdn.net/qq_18649781/article/details/89405977
0 預(yù)備知識
分屏指令:tmux
這個指令有好多介紹了,就不詳細(xì)贅述了羹呵。https://www.cnblogs.com/kevingrace/p/6496899.html
nvidia-smi
nvidia-smi是Nvidia顯卡命令行管理套件,基于NVML庫,旨在管理和監(jiān)控Nvidia GPU設(shè)備。
1 gpustat
這里推薦一個好用的小工具:gpustat, 直接pip install gpustat即可安裝殿雪,gpustat 基于nvidia-smi朽色,可以提供更美觀簡潔的展示,結(jié)合 watch 命令壶谒,可以動態(tài)實時監(jiān)控 GPU 的使用情況。
watch --color -n1 gpustat -cpu
效果如下:
讓TensorFlow代碼跑在GPU上
GPU占用問題
TensorFlow可能會占用視線可見的所有GPU資源
- 查看gpu占用情況:gpustat
在python代碼中加入:
os.environ['CUDA_VISIBLE_DEVICES'] = '0' os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' - 設(shè)置使用固定的gpu:
CUDA_VISIBLE_DEVICES=1 Only device 1 will be seen CUDA_VISIBLE_DEVICES=0,1 Devices 0 and 1 will be visible CUDA_VISIBLE_DEVICES=”0,1” Same as above, quotation marks are optional CUDA_VISIBLE_DEVICES=0,2,3 Devices 0, 2, 3 will be visible; device 1 is masked - 運行代碼時
CUDA_VISIBLE_DEVICES=0 python3 main.py
T- ensorFlow自己提供的兩種控制GPU資源的方法:
1.在運行過程中動態(tài)申請顯存汗菜,需要多少就申請多少
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
2.限制GPU的使用率
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.4)
config=tf.ConfigProto(gpu_options=gpu_options)
session = tf.Session(config=config)
TensorFlow代碼
目前沒有考慮在代碼各個部分手動分配時GPU還是CPU
所以用 with tf.device(self.device): 把所有網(wǎng)絡(luò)結(jié)構(gòu)包了起來
然后用 config = tf.ConfigProto(gpu_options=gpu_options,allow_soft_placement=True) 讓TensorFlow自己去分配了