谁在使用GPU?

nvidia-smi命令可以查看GPU使用情况,但是只能看到占用每个GPU的进程ID。根据进程ID可以得到进程详情,进程详情中包括用户ID,根据用户ID可以获取用户名称,从而知道哪个用户在使用GPU。

import json
import os
import re
import sys
import time
import typing

import bidict

"""
查看谁在使用GPU
"""


def get_user_id_map() -> typing.Dict[str:str]:
    """获取用户名和用户ID的对应关系"""
    home = os.path.expanduser('~')
    users = bidict.bidict()
    for user_name in os.listdir(os.path.join(home, '..')):
        info = os.popen('id ' + user_name + ' 2>&1').read().strip()
        if 'no such user' in info: continue
        try:
            a = re.search("uid=(\\d+)\((\\w+)\)", info)
            users[a.group(1)] = a.group(2)  # userid==>username
        except Exception as e:
            print(e)
    return users


def nvidia_smi() -> (int, typing.Dict[str:str]):
    """使用nvidia-smi命令查看GPU使用情况,返回GPU个数和各个GPU的进程的描述line"""
    info = os.popen('nvidia-smi').read()
    info = info.split('\n')
    """
    smi信息分成上下两部分
    上面部分:以表格形式展示各个GPU的使用率
    下面部分:展示各个GPU上运行的进程ID 
    """
    space_ind = 0
    for ind, line in enumerate(info):
        if not line.strip():
            space_ind = ind
            break

    first_line = 0
    for ind, line in enumerate(info):
        if line.startswith('|===='):
            first_line = ind
            break

    gpu_count = abs(space_ind - first_line) // 3
    pos = None
    for ind, line in enumerate(info):
        line = line.split()
        if len(line) > 1 and line[1] == 'Processes:':
            pos = ind + 2
            break
    gpu_usage = dict()
    if pos == None:
        return gpu_count, gpu_usage
    for i in range(pos, len(info)):
        line = info[i].split()
        if len(line) > 1:
            thread = line[2]
            gpu_id = int(line[1])
            if gpu_id not in gpu_usage:
                gpu_usage[gpu_id] = []
            gpu_usage[gpu_id].append(thread)
    return gpu_count, gpu_usage


def get_thread_info(thread_id: str):
    """根据thread_id获取thread详细信息"""
    id2user = get_user_id_map()
    thread_info = os.popen('ps -l ' + thread_id).read().split('\n')[1].split()
    thread_user = id2user.get(thread_info[2])
    thread_time = re.search('\\d+', thread_info[12]).group()
    thread_cmd = ' '.join(thread_info[13:])
    return dict(user=thread_user, use_time="{} hours".format(float(thread_time) / 60), thread_id=thread_id, cmd=thread_cmd)


def grep_gpu(task):
    """抢占GPU准备执行某个任务"""
    free_gpu = None
    while free_gpu is None:
        gpu_count, usage = nvidia_smi()
        time.sleep(2)
        for i in range(gpu_count):
            if i not in usage:
                free_gpu = i
                break
    print('free gpu found ! ', free_gpu)
    os.system(task)


def show():
    gpu_count, usage = nvidia_smi()
    for gpu_id in usage:
        usage[gpu_id] = [get_thread_info(thread_id) for thread_id in usage[gpu_id]]
    print('gpu count', gpu_count)
    print(json.dumps(usage, ensure_ascii=0, indent=2))


def run(gpu_id, task):
    os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_id)
    os.system('echo CUDA_VISIBLE_DEVICES:$CUDA_VISIBLE_DEVICES')
    os.system(task)


if __name__ == '__main__':
    print(sys.argv)
    if len(sys.argv) == 1:
        print("""
        GPU utility
        
        gpu show
        gpu grep your command here
        gpu 1 python haha.py
        """)
        exit(0)
    action = sys.argv[1]
    if action == 'show':  # 显示GPU使用情况
        show()
    elif action == 'grep':  # 争夺GPU,得到之后执行命令
        cmd = ' '.join(sys.argv[2:])
        print('grep gpu and run', cmd)
        grep_gpu(cmd)
    elif re.match("\\d+", action):  # 使用gpu_id执行某个action
        gpu_id = int(action)
        cmd = ' '.join(sys.argv[2:])
        print('run on gpu', gpu_id, 'cmd', cmd)
        run(gpu_id, cmd)
    else:
        print("unkown command")

转载于:https://www.cnblogs.com/weiyinfu/p/11087363.html

要同时在两个Python环境中使用GPU,您需要确保以下条件得到满足: 1. 您的系统必须有一个支持GPU的显卡。 2. 您必须安装与您的显卡兼容的最新GPU驱动程序。 3. 您必须安装与您的Python环境兼容的CUDA和cuDNN软件包。这些软件包是用于GPU计算的库,可以在NVIDIA的网站上下载。 4. 您需要安装与您的Python环境兼容的深度学习框架,例如TensorFlow或PyTorch。这些框架都支持GPU计算。 5. 您需要在您的Python代码中设置GPU设备,以便您的代码可以使用GPU进行计算。 如果您在使用Anaconda等Python环境管理器,则需要确保在每个环境中都安装了CUDA和cuDNN软件包,并在每个环境中设置了GPU设备。您可以使用以下命令来设置GPU设备: ```python import tensorflow as tf from tensorflow.keras import backend as K # 设置GPU设备 config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.visible_device_list = "0" # 指定使用GPU设备号 K.set_session(tf.compat.v1.Session(config=config)) ``` 请注意,在这个例子中,我们指定使用GPU设备号为0。如果您有多个GPU设备,则可以将这个号码更改为另一个数字,以指定要使用的设备。 如果您使用的是PyTorch,您可以使用以下命令来设置GPU设备: ```python import torch # 设置GPU设备 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device) ``` 在这个例子中,我们使用了torch.cuda.is_available()函数来检查是否有可用的GPU设备,并将设备号设置为0。如果您有多个GPU设备,则可以将这个号码更改为另一个数字,以指定要使用的设备。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值