如何在Docker容器中使用OpenCL?多尝试仍遇平台/设备未找到问题
在容器中运行OpenCL程序的问题
我已在本地Windows PC上成功使用OpenCL,现在希望让自己的程序在容器中运行,以下是我的尝试过程:
第一次尝试
Dockerfile
FROM ubuntu:latest RUN apt-get update # 配置非交互式安装环境 RUN export DEBIAN_FRONTEND=noninteractive RUN ln -fs /usr/share/zoneinfo/Australia/Brisbane /etc/localtime RUN apt-get install -y tzdata RUN dpkg-reconfigure --frontend noninteractive tzdata # 安装相关依赖包 RUN apt-get upgrade -y RUN apt-get install -y --no-install-recommends build-essential clinfo cmake gcc make nvidia-modprobe ocl-icd-libopencl1 ocl-icd-opencl-dev opencl-headers virtualenv wget RUN apt-get install -y --no-install-recommends libnvidia-compute-565-server RUN apt install python3.12-venv -y # 添加NVIDIA软件源 RUN rm -rf /var/lib/apt/lists/* RUN rm -f /etc/apt/sources.list.d/cuda.list RUN echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64 /" > /etc/apt/sources.list.d/cuda.list RUN apt-get update RUN apt-get upgrade -y # 创建Python虚拟环境 WORKDIR / RUN mkdir /venv ENV VIRTUAL_ENV=/venv RUN python3 -m venv $VIRTUAL_ENV ENV PATH="$VIRTUAL_ENV/bin:$PATH" RUN pip install --upgrade pip RUN pip3 install numpy conan siphash24 pyopencl
构建与运行命令
docker build . -t test_ocl docker run -it --gpus all test_ocl /bin/bash
clinfo输出
root@7307c8f6cf60:/# clinfo Number of platforms 0 ICD loader properties ICD loader Name OpenCL ICD Loader ICD loader Vendor OCL Icd free software ICD loader Version 2.3.2 ICD loader Profile OpenCL 3.0
nvidia-smi输出
root@7307c8f6cf60:/# nvidia-smi Thu Feb 6 16:23:01 2025 +-----------------------------------------------------------------------------------------+ | NVIDIA-SMI 570.86.09 Driver Version: 571.96 CUDA Version: 12.8 | |-----------------------------------------+------------------------+----------------------+ | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+========================+======================| | 0 NVIDIA RTX A1000 6GB Lap... On | 00000000:01:00.0 On | N/A | | N/A 39C P8 5W / 35W | 194MiB / 6144MiB | 0% Default | | | | N/A | +-----------------------------------------+------------------------+----------------------+ +-----------------------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=========================================================================================| | No running processes found | +-----------------------------------------------------------------------------------------+
Python测试OpenCL
root@7307c8f6cf60:/# /venv/bin/python Python 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pyopencl as cl >>> cl.get_platforms() Traceback (most recent call last): File "<stdin>", line 1, in <module> pyopencl._cl.LogicError: clGetPlatformIDs failed: PLATFORM_NOT_FOUND_KHR
第二次尝试
运行命令与输出
docker run --rm --gpus all nvidia/opencl clinfo Number of platforms 0
第三次尝试(使用ROCm)
运行命令
docker run -it --gpus all rocm/dev-ubuntu-22.04 /bin/bash
clinfo输出
root@f561a9533509:/# clinfo Number of platforms: 1 Platform Profile: FULL_PROFILE Platform Version: OpenCL 2.1 AMD-APP (3635.0) Platform Name: AMD Accelerated Parallel Processing Platform Vendor: Advanced Micro Devices, Inc. Platform Extensions: cl_khr_icd cl_amd_event_callback Platform Name: AMD Accelerated Parallel Processing Number of devices: 0
安装依赖并测试Python
apt update apt upgrade -y pip3 install siphash24 pyopencl
root@f561a9533509:/# python3 Python 3.10.12 (main, Jan 17 2025, 14:35:34) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pyopencl as cl >>> cl.get_platforms() [<pyopencl.Platform 'AMD Accelerated Parallel Processing' at 0x7fa26e577010>]
查看设备
>>> p = cl.get_platforms()[0] >>> p.get_devices() []
第四次尝试(CUDA示例)
运行命令
docker run --gpus all nvcr.io/nvidia/k8s/cuda-sample:nbody nbody -gpu -benchmark
输出
Run "nbody -benchmark [-numbodies=<numBodies>]" to measure performance. -fullscreen (run n-body simulation in fullscreen mode) -fp64 (use double precision floating point values for simulation) -hostmem (stores simulation data in host memory) -benchmark (run benchmark to measure performance) -numbodies=<N> (number of bodies (>= 1) to run in simulation) -device=<d> (where d=0,1,2.... for the CUDA device to use) -numdevices=<i> (where i=(number of CUDA devices > 0) to use for simulation) -compare (compares simulation results running once on the default GPU and once on the CPU) -cpu (run n-body simulation on the CPU) -tipsy=<file.bin> (load a tipsy model file for simulation) NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled. > Windowed mode > Simulation data stored in video memory > Single precision floating point simulation > 1 Devices used for simulation GPU Device 0: "Ampere" with compute capability 8.6 > Compute 8.6 CUDA device: [NVIDIA RTX A1000 6GB Laptop GPU] 20480 bodies, total time for 10 iterations: 19.150 ms = 219.026 billion interactions per second = 4380.514 single-precision GFLOP/s at 20 flops per interaction
这个CUDA示例成功调用了GPU,但我不知道如何在自己的OpenCL代码中复现这种效果?
内容的提问来源于stack exchange,提问作者sav
相关产品推荐
相关产品推荐

