GPUUtil安装与使用指南

GPUUtil安装与使用指南

gputilA Python module for getting the GPU status from NVIDA GPUs using nvidia-smi programmically in Python项目地址:https://gitcode.com/gh_mirrors/gp/gputil

一、项目目录结构及介绍

GPUUtil,位于GitHub上的地址是https://github.com/anderskm/gputil.git,是一款用于监控和管理GPU资源的开源工具。下面是其主要的目录结构及其简介:

gputil/
├── LICENSE.txt             # 许可证文件,说明了软件使用的授权条款。
├── README.md               # 项目快速入门和基本信息的文档。
├── src                     # 核心源代码目录。
│   ├── GPUtil.py            # 主要的GPU实用程序模块。
│   └── ...                 # 其他相关Python源代码文件。
├── tests                   # 测试套件目录,包含了单元测试等。
│   └── test_GPUtil.py      # 对GPUtil功能进行测试的脚本。
├── setup.py                # Python项目的安装脚本,用于通过pip安装项目。
└── examples                # 示例代码目录,提供了如何使用该库的例子。

二、项目的启动文件介绍

在GPUUtil中,并没有传统意义上的“启动文件”作为应用程序直接运行。用户通常通过导入src.GPUtil中的模块到自己的Python脚本来启动对GPU的监控或管理。例如,一个基本的使用示例可能会从GPUtil.showUtilization()函数开始,来显示GPU的利用率。

要开始使用GPUUtil的功能,可以通过下面的方式在你的Python环境中导入并调用它:

import GPUtil as GPU
 GPUs = GPU.getGPUs()
 for gpu in GPUs:
     print("GPU ID: {} \t Utilization: {}% \t Memory Used: {}MB".format(gpu.id, gpu.load*100, gpu.memoryUsed))

三、项目的配置文件介绍

GPUUtil项目本身并不依赖于外部配置文件来进行常规操作,它的配置主要是通过代码中直接设定参数或者使用环境变量来完成。这意味着用户可以根据需要,在调用特定API时传入参数以调整行为,而不是通过独立的配置文件来设置。例如,若需自定义日志输出或改变某些默认行为,则可能需要修改代码内相应部分或者利用Python的标准库(如logging)进行配置。

总结而言,GPUUtil的设计更侧重于通过编程接口的灵活性来满足配置和使用的需求,而较少依赖于静态的配置文件机制。对于高级使用场景,理解源码结构和API文档变得尤为重要。

gputilA Python module for getting the GPU status from NVIDA GPUs using nvidia-smi programmically in Python项目地址:https://gitcode.com/gh_mirrors/gp/gputil

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 AMD GPU Profiler 工具来获取 AMD GPU 的使用率。以下是获取 AMD GPU 使用率的示例 C 代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // AMD GPU Profiler的头文件 #include "CL/CL.h" #include "CPerfCounter.h" int main() { int deviceCount = 0; cl_device_id* devices = NULL; cl_ulong maxClockFrequency = 0; cl_uint numOfComputeUnits = 0; cl_platform_id platformId = 0; clGetPlatformIDs(1, &platformId, NULL); // 获取所有的AMD GPU设备 clGetDeviceIDs(platformId, CL_DEVICE_TYPE_GPU, 0, NULL, &deviceCount); devices = (cl_device_id*)malloc(sizeof(cl_device_id) * deviceCount); clGetDeviceIDs(platformId, CL_DEVICE_TYPE_GPU, deviceCount, devices, NULL); for (int i = 0; i < deviceCount; ++i) { cl_device_id device = devices[i]; // 获取设备的运行包络信息 cl_ulong maxClockFrequency = 0; clGetDeviceInfo(device, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(cl_ulong), &maxClockFrequency, NULL); cl_uint numOfComputeUnits = 0; clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &numOfComputeUnits, NULL); // 创建 OpenCL 上下文 cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platformId, 0 }; cl_context context = clCreateContext(cps, 1, &device, NULL, NULL, NULL); // 创建命令队列 cl_command_queue commandQueue = clCreateCommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE, NULL); // 创建 Profiling 对象 CPerfCounter timer(numOfComputeUnits, commandQueue); // 创建程序对象和内核对象 const char* kernelSource = "__kernel void gpuUtil(__global float* result)\n" "{\n" " int tid = get_global_id(0);\n" " float a = 0.0f;\n" " while (true)\n" " {\n" " a += tid * 0.1f;\n" " result[tid] = a;\n" " }\n" "}\n"; cl_program program = clCreateProgramWithSource(context, 1, &kernelSource, NULL, NULL); clBuildProgram(program, 1, &device, NULL, NULL, NULL); cl_kernel kernel = clCreateKernel(program, "gpuUtil", NULL); // 创建输出数据缓冲区 cl_mem output = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR, sizeof(float) * numOfComputeUnits, NULL, NULL); // 设置内核参数 clSetKernelArg(kernel, 0, sizeof(cl_mem), &output); // 启动内核 size_t global_work_size[] = { numOfComputeUnits }; clEnqueueNDRangeKernel(commandQueue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, NULL); // 读取结果 float* result = (float*)clEnqueueMapBuffer(commandQueue, output, CL_TRUE, CL_MAP_READ, 0, sizeof(float) * numOfComputeUnits, 0, NULL, NULL, NULL); // 计算 GPU 使用率 double gpuUtilization = timer.GetUtilization(result); printf("AMD GPU %d - Utilization: %.2f%%\n", i + 1, gpuUtilization); // 清理资源 clEnqueueUnmapMemObject(commandQueue, output, result, 0, NULL, NULL); clReleaseMemObject(output); clReleaseKernel(kernel); clReleaseProgram(program); clReleaseCommandQueue(commandQueue); clReleaseContext(context); } free(devices); return 0; } ``` 在上面的示例代码中,我们通过 AMD GPU Profiler 工具来获取GPU使用率。它利用了AMD GPU Profiler的函数库和CPerfCounter类库来计算GPU使用率,并利用OpenCL技术进行核操作。需要注意的是,该示例代码只是一个简单的演示,仅供参考,需要针对具体的GPU进行优化和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

符汝姿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值