Nvitop GPU实时监测可视化工具

# 安装nvitop
sudo apt install nvitop #安装nvitop
# 安装成功后直接启动监控
nvitop

上图为展示界面with option --colorfull

nvitop -h #查看参数介绍
optional arguments:
  --help, -h            Show this help message and exit.
  --version, -V         Show nvitop's version number and exit.
  --once, -1            Report query data only once.
  --monitor [{auto,full,compact}], -m [{auto,full,compact}]
                        Run as a resource monitor. Continuously report query data and handle user inputs.
                        If the argument is omitted, the value from `NVITOP_MONITOR_MODE` will be used.
                        (default fallback mode: auto)
  --interval SEC        Process status update interval in seconds. (default: 2)
  --ascii, --no-unicode, -U
                        Use ASCII characters only, which is useful for terminals without Unicode support.

coloring:
  --colorful            Use gradient colors to get spectrum-like bar charts. This option is only available
                        when the terminal supports 256 colors. You may need to set environment variable
                        `TERM="xterm-256color"`. Note that the terminal multiplexer, such as `tmux`, may
                        override the `TREM` variable.
  --force-color         Force colorize even when `stdout` is not a TTY terminal.
  --light               Tweak visual results for light theme terminals in monitor mode.
                        Set variable `NVITOP_MONITOR_MODE="light"` on light terminals for convenience.
  --gpu-util-thresh th1 th2
                        Thresholds of GPU utilization to determine the load intensity.
                        Coloring rules: light < th1 % <= moderate < th2 % <= heavy.
                        ( 1 <= th1 < th2 <= 99, defaults: 10 75 )
  --mem-util-thresh th1 th2
                        Thresholds of GPU memory percent to determine the load intensity.
                        Coloring rules: light < th1 % <= moderate < th2 % <= heavy.
                        ( 1 <= th1 < th2 <= 99, defaults: 10 80 )

device filtering:
  --only idx [idx ...], -o idx [idx ...]
                        Only show the specified devices, suppress option `--only-visible`.
  --only-visible, -ov   Only show devices in the `CUDA_VISIBLE_DEVICES` environment variable.

process filtering:
  --compute, -c         Only show GPU processes with the compute context. (type: 'C' or 'C+G')
  --only-compute, -C    Only show GPU processes exactly with the compute context. (type: 'C' only)
  --graphics, -g        Only show GPU processes with the graphics context. (type: 'G' or 'C+G')
  --only-graphics, -G   Only show GPU processes exactly with the graphics context. (type: 'G' only)
  --user [USERNAME [USERNAME ...]], -u [USERNAME [USERNAME ...]]
                        Only show processes of the given users (or `$USER` for no argument).
  --pid PID [PID ...], -p PID [PID ...]
                        Only show processes of the given PIDs.
# 如果出现importError,缺少nvidia-ml-py3库报错,可以参考以下解决方法
pip3 freeze # 查看当前库
pip3 install --force-reinstall nvidia-ml-py==11.450.51

#若仍然报错,参考以下方法
pip3 uninstall nvidia-ml-py3 pynvml
pip3 install --force-reinstall nvidia-ml-py==11.450.51
# 将监控信息写入Tensorboard

import os

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter

from nvitop import CudaDevice, ResourceMetricCollector
from nvitop.callbacks.tensorboard import add_scalar_dict

# Build networks and prepare datasets
...

# Logger and status collector
writer = SummaryWriter()
collector = ResourceMetricCollector(devices=CudaDevice.all(),  # log all visible CUDA devices and use the CUDA ordinal
                                    root_pids={os.getpid()},   # only log the descendant processes of the current process
                                    interval=1.0)              # snapshot interval for background daemon thread

# Start training
global_step = 0
for epoch in range(num_epoch):
    with collector(tag='train'):
        for batch in train_dataset:
            with collector(tag='batch'):
                metrics = train(net, batch)
                global_step += 1
                add_scalar_dict(writer, 'train', metrics, global_step=global_step)
                add_scalar_dict(writer, 'resources',      # tag='resources/train/batch/...'
                                collector.collect(),
                                global_step=global_step)

        add_scalar_dict(writer, 'resources',              # tag='resources/train/...'
                        collector.collect(),
                        global_step=epoch)

    with collector(tag='validate'):
        metrics = validate(net, validation_dataset)
        add_scalar_dict(writer, 'validate', metrics, global_step=epoch)
        add_scalar_dict(writer, 'resources',              # tag='resources/validate/...'
                        collector.collect(),
                        global_step=epoch)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python在处理大规模数据时,通常会比较慢。因此,为了提高性能,许多人会使用GPU进行加速计算。在Python中,可以使用许多库来实现GPU加速计算,例如Numba、PyCUDA和PyOpenCL等。 Numba是一个用于将Python代码编译成机器代码的库,它可以显著提高代码的运行速度。Numba支持使用CUDA和ROCm框架进行GPU加速计算。 下面是一个使用Numba进行GPU加速计算的例子: ```python import numpy as np from numba import jit, cuda # 声明函数为CUDA函数 @cuda.jit def add(a, b, c): # 获取线程索引 idx = cuda.threadIdx.x # 获取块索引 idy = cuda.blockIdx.x # 获取块大小 block_size = cuda.blockDim.x # 计算全局索引 index = idx + idy * block_size # 执行加法操作 c[index] = a[index] + b[index] # 定义数组的大小 N = 1024**2 # 定义输入数组 a = np.ones(N) b = np.ones(N) # 定义输出数组 c = np.zeros(N) # 定义块大小和线程大小 threads_per_block = 1024 blocks_per_grid = (N + (threads_per_block - 1)) // threads_per_block # 进行加法操作 add[blocks_per_grid, threads_per_block](a, b, c) # 输出结果 print(c) ``` 这个例子使用Numba将一个加法函数编译成CUDA函数,并在GPU上进行运算。可以看到,使用GPU加速计算的结果比使用CPU的结果要快得多。 在实际应用中,使用GPU加速计算的速度提升取决于数据的大小和计算的复杂度。对于一些简单的计算,使用GPU加速可能没有太大的优势。但是,对于一些大规模的、复杂的计算,使用GPU加速可以显著提高运算速度。 对于可视化方面,Python中有很多可视化库可以使用,例如Matplotlib、Seaborn和Plotly等。这些库通常不会直接使用GPU加速,但是可以使用一些技巧来提高可视化的速度。例如,可以使用多线程或多进程来加速计算,或者使用分布式计算来加速处理大规模数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值