查看GPU占用率以及指定GPU加速程序

我的linux下查看方式:

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
29
30
31
32
33
[root@localhost bonelee]# ./hello2 &
[1] 139743
[root@localhost bonelee]# Hello World from CPU!
!nvHello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
 
nvidia-smi 就是这个命令
Mon Jun 27 16:59:23 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.130                Driver Version: 384.130                   |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P100-PCIE...  Off  | 00000000:3B:00.0 Off |                    0 |
| N/A   48C    P0    31W / 250W |      0MiB / 16276MiB |      3%(占用率)      Default |
+-------------------------------+----------------------+----------------------+
 
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
[1]+  Done                    ./hello2

 

为了测试GPU占用率,我写了一个代码:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[root@localhost bonelee]# vi hello2.cu
[root@localhost bonelee]# cat hello2.cu
#include <stdio.h>
 
__global__ void helloFromGPU (void)
{
    printf("Hello World from GPU!\n");
    while(1) {
        int a = 999;
        int b = 999;
        int c;
        c = a+b*a+a>>b;
    }
}
 
int main(void)
{
// hello from cpu
printf("Hello World from CPU!\n");
 
helloFromGPU <<<1, 10>>>();
 
cudaDeviceReset();
 
return 0;
}
 
[root@localhost bonelee]# nvcc hello2.cu  -o hello2
hello2.cu(9): warning: variable "c" was set but never used
 
hello2.cu(9): warning: variable "c" was set but never used
 
[root@localhost bonelee]# ./hello2
Hello World from CPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
Hello World from GPU!
^Z
[1]+  Stopped                 ./hello2
[root@localhost bonelee]# nvidia-smi
Mon Jun 27 17:12:18 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.130                Driver Version: 384.130                   |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P100-PCIE...  Off  | 00000000:3B:00.0 Off |                    0 |
| N/A   49C    P0    37W / 250W |    299MiB / 16276MiB |    100%      Default |
+-------------------------------+----------------------+----------------------+
 
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0    145325      C   ./hello2                                     289MiB |
+-----------------------------------------------------------------------------+

  

可以看到GPU占用率是100%。

注:代码说明,上面一行三对尖括号中的1和10 表明了该function将有10个线程

  

查看GPU占用率以及指定GPU加速程序

GPU占用率查看:

方法一:任务管理器

如图,GPU0和GPU1的占用率如下显示。

 

 

 

方法二:GPU-Z软件

      

下面两个GPU,上面是GPU0,下面是GPU1

sensors会话框里的GPU Load就是占用率

大家可以查看GPU0和GPU1的使用与否和使用率

 

方法三:终端查看

在运行中输入cmd,打开终端

输入cd C:\Program Files\NVIDIA Corporation\NVSMI

回车

输入nvidia-smi

输出为

 

其中GPU下的0和1 指不同GPU,Memory-Usage为占用率

 

为了实时查看,可以输入nvidia-smi.exe -l 3

这样就可以每3秒刷新一次,实时显示了。

 

指定GPU运行程序方法:

第一步:

在程序里写出指定GPU(有两种分配方法):

1、

1
2
import os
os.environ["CUDA_VISIBLE_DEVICES"]=0

这样就指定在GPU0下运行程序,如果要指定多个,可以写成os.environ["CUDA_VISIBLE_DEVICES"]=‘0’,‘1’

 

2、(需得是tensorflow)

1
2
3
import tensorflow as tf
 
tf.device('/gpu=0')

这样就指定在GPU0下运行程序,如果要指定GPU利用率,就添加如下代码: 

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)

sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

 意思是在GPU0下以30%利用率运行程序(实际会偏高一点,但会有控制效果)

 

第二步:

打开新的console,分别在不同console下用不同GPU运行,实现两个程序同时跑。

注意:如果程序不大,也可以在新的console下用同一个GPU运行程序,具体的视GPU占用率而定。

posted @   bonelee  阅读(1405)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2018-06-24 f5 ddos cc——Mitigating DDoS Attacks with F5 Technology
2018-06-24 国外DDoS产品的一些调研—— Akamai Arbor Networks Cloudflare DOSarrest F5 Fastly Imperva Link11 Neustar Nexusguard Oracle (Dyn) Radware Verisign
2018-06-24 公有云厂商DDoS防护产品竞品分析——内含CC的一些简单分析,貌似多是基于规则,CC策略细粒度ip/url//ua/refer
2017-06-24 开源DDos 机器学习思路求解的一些源码——TODO 待分析
2017-06-24 大数据DDos检测——DDos攻击本质上是时间序列数据,t+1时刻的数据特点和t时刻强相关,因此用HMM或者CRF来做检测是必然! 和一个句子的分词算法CRF没有区别!
2017-06-24 什么是私有密钥密码技术——密钥加密算法采用同一把密钥进行加密和解密
点击右上角即可分享
微信分享提示