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


    我的linux下查看方式:

    [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占用率,我写了一个代码:

    [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、

    import os
    os.environ["CUDA_VISIBLE_DEVICES"]=‘0’
    

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

    2、(需得是tensorflow)

    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占用率而定。

  • 相关阅读:
    JSP学习笔记
    Java之String、StringBuffer、StringBuilder的区别
    Android开发笔记——Handler总结
    深搜_八皇后(HDU_2553)
    广搜_优先队列和记录搜索路径(HDU_1026)
    深搜_素数环(HDU_1016)
    深搜_奇偶减枝(HDU_1010)
    转载 ASP.NET MVC学习之(5):Html.ActionLink
    启动万维网发布服务(W3SVC)
    What is POID
  • 原文地址:https://www.cnblogs.com/bonelee/p/16408848.html
Copyright © 2020-2023  润新知