dockerdriveropenclnvidiapyopencl

How do I use OpenCL in a docker container


I have successfully used OpenCL on my local windows PC and I would now like to get my program working in a container

First attempt

FROM ubuntu:latest

RUN apt-get update 

#Done to make install non-interactive
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

#Install packages that seem like they might help
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 

#Add NVidia Package Repo
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

#Make python virtual environment
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

Building it

docker build . -t test_ocl 
docker run -it --gpus all test_ocl /bin/bash 

clinfo output:

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                                                             |
+-----------------------------------------------------------------------------------------+

Looking for the platforms using python

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

Second Attempt

docker run --rm --gpus all nvidia/opencl clinfo
Number of platforms                               0

Third Attempt

Using 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

At least this found my CPU this time

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>]

it found the platform

>>> p = cl.get_platforms()[0]
>>> p.get_devices()
[]
>>>

But no device


Fourth Attempt

 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

Perhaps this successfully sent commands to the GPU? Although I'm not sure how I can replicate that in my own code yet?


Solution

  • If anyone else want to know what worked for me see

    https://github.com/umarwaseeem/OpenCL-and-Docker

    I was missing pocl-opencl-icd

    FROM ubuntu:24.04
    
    RUN apt-get update && \
        apt-get install -y pocl-opencl-icd ocl-icd-opencl-dev gcc clinfo
    
    WORKDIR /app
    
    COPY host.c /app/
    COPY Makefile /app/