docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker配置挂载GPU

docker镜像配置挂载GPU的实现步骤

作者:鸿儒517

本文记录了在普通Docker容器中成功挂载本地GPU设备并运行GPU程序的完整过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

我在普通docker中挂载本地GPU设备,然后成功测试运行GPU程序,特此记录

1、测试GPU程序源码

首先我写了一个简单的GPU测试程序

#include <iostream>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
// CUDA 核函数(Kernel):运行在GPU上
__global__ void vectorAdd(const float *a, const float *b, float *c, int n) {
    // 计算当前线程的全局索引
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    // 确保索引在数组范围内,然后执行加法
    if (idx < n) {
        c[idx] = a[idx] + b[idx];
    }
}
int main() {
    // 定义向量长度
    const int n = 10000;
    const size_t size = n * sizeof(float);
    // 在主机(CPU)上分配并初始化内存
    float *h_a = new float[n];
    float *h_b = new float[n];
    float *h_c = new float[n];
    for (int i = 0; i < n; i++) {
        h_a[i] = static_cast<float>(i);       // 例如: 0.0, 1.0, 2.0...
        h_b[i] = static_cast<float>(i * 2);   // 例如: 0.0, 2.0, 4.0...
    }
    // 在设备(GPU)上分配内存
    float *d_a = nullptr;
    float *d_b = nullptr;
    float *d_c = nullptr;
    cudaMalloc(&d_a, size);
    cudaMalloc(&d_b, size);
    cudaMalloc(&d_c, size);
    // 将数据从主机内存复制到设备内存
    cudaMemcpy(d_a, h_a, size, cudaMemcpyHostToDevice);
    cudaMemcpy(d_b, h_b, size, cudaMemcpyHostToDevice);
    // 设置GPU线程的组织结构
    int threadsPerBlock = 256; // 每个线程块包含256个线程
    // 计算需要多少个线程块才能覆盖整个向量
    int blocksPerGrid = (n + threadsPerBlock - 1) / threadsPerBlock;
    // 在GPU上启动核函数(Kernel)
    vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(d_a, d_b, d_c, n);
    // 等待GPU上所有线程计算完成
    cudaDeviceSynchronize();
    // 将计算结果从设备内存复制回主机内存
    cudaMemcpy(h_c, d_c, size, cudaMemcpyDeviceToHost);
    // (可选)验证前10个结果是否正确
    std::cout << "Checking the first 10 results..." << std::endl;
    std::cout << "t" << "CPU" << "tt" << "GPU" << std::endl;
    for (int i = 0; i < 10; i++) {
        float expected = h_a[i] + h_b[i];
        if (std::abs(h_c[i] - expected) > 1e-5) {
            std::cout << "Error at index " << i << ": " << h_c[i] << " != " << expected << std::endl;
        } else {
            std::cout << "t" << expected << "tt" << h_c[i] << " [PASS]" << std::endl;
        }
    }
    // 释放所有申请的内存
    delete[] h_a;
    delete[] h_b;
    delete[] h_c;
    cudaFree(d_a);
    cudaFree(d_b);
    cudaFree(d_c);
    std::cout << "Vector addition completed successfully!" << std::endl;
    return 0;
}

如果在带GPU的电脑上正常编译运行,结果应该如下

./GPUTest_exe 
Checking the first 10 results...
tCPUttGPU
t0tt0 [PASS]
t3tt3 [PASS]
t6tt6 [PASS]
t9tt9 [PASS]
t12tt12 [PASS]
t15tt15 [PASS]
t18tt18 [PASS]
t21tt21 [PASS]
t24tt24 [PASS]
t27tt27 [PASS]
Vector addition completed successfully!

2、docker中配置GPU设备及驱动

先给出成功执行命令,后面来一步步解释关键共享GPU代码

docker run --memory=10g --memory-swap=10g --rm \
-v /Bin_Release:/JX \
-v /usr/local/corex:/usr/local/corex \
-v /usr/local/cuda-10.2:/usr/local/cuda-10.2 \
-v /usr/lib64:/usr/lib64 \
-e LD_LIBRARY_PATH=/usr/local/corex/lib64:/usr/local/cuda-10.2/lib64 \
--device=/dev/iluvatar0 \
--privileged \
-w /docker_test:v1.0 \
./GPU_Bin/GPUTest_exe

1、用-v 共享编译GPU的时候需要的动态库目录到容器中

-v /usr/local/corex:/usr/local/corex 
-v /usr/local/cuda-10.2:/usr/local/cuda-10.2 

2、路径虽然共享进去了,可能出现找不到动态库的情况,所以要把动态库添加到环境变量中

-e LD_LIBRARY_PATH=/usr/local/corex/lib64:/usr/local/cuda-10.2/lib64 \

3、添加GPU 设备

--device=/dev/iluvatar0
#有些设备是corex ,则需要配置
--device=/dev/corex

4、加了 --privileged,给 GPU 完整权限

3、异常测试

3.1、驱动异常

异常表现:

error while loading shared libraries: libcudart.so.10.2: cannot open shared object file

解决方案:
将动态库设置到环境变量中即可

-e LD_LIBRARY_PATH=/usr/local/corex/lib64:/usr/local/cuda-10.2/lib64

3.2、计算结果异常

如果出现下面结果,则说明GPU动态库没有问题,GPU设备可能没有挂载,导致计算结果都是0的异常值

异常表现:

./GPU_Bin/GPUTest_exe
Checking the first 10 results...
tCPUttGPU
t0tt0 [PASS]
Error at index 1: 0 != 3
Error at index 2: 0 != 6
Error at index 3: 0 != 9
Error at index 4: 0 != 12
Error at index 5: 0 != 15
Error at index 6: 0 != 18
Error at index 7: 0 != 21
Error at index 8: 0 != 24
Error at index 9: 0 != 27
Vector addition completed successfully!

解决方案:

--device=/dev/corex

3.3、设备异常

异常表现

./GPU_Bin/GPUTest_exedocker: Error response from daemon: error gathering device information while adding custom device "/dev/corex": no such file or directory.

说明设备中没有corex设备

解决方案:
这时候要找到真正的GPU设备,然后挂载进去,可以将/dev文件夹设备都复制出来,让豆包帮你找到真正的GPU设备

到此这篇关于docker镜像配置挂载GPU的实现步骤的文章就介绍到这了,更多相关docker配置挂载GPU内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文