Failure to load opencl.h for cmdstanr on Linux system

I’m using R 4.1.1 on a Linux server (Ubuntu 18.04.5LTS), and when I enable OpenCL from cmdstanr and try to compile the sample code in the vignette, it stops with the following error.

Compiling Stan program...
In file included from stan/lib/stan_math/stan/math/opencl/err/check_opencl.hpp:7:0,
                 from stan/lib/stan_math/stan/math/opencl/opencl_context.hpp:14,
                 from src/cmdstan/write_opencl_device.hpp:6,
                 from src/cmdstan/command.hpp:19,
                 from src/cmdstan/main.cpp:1:
stan/lib/stan_math/lib/opencl_2.0.15/include/CL/opencl.hpp:530:10: fatal error: CL/opencl.h: No such file or directory
 #include <CL/opencl.h>
          ^~~~~~~~~~~~~
compilation terminated.
make: *** [src/cmdstan/main_opencl.o] Error 1
Error: An error occured during compilation! See the message above for more information.

Since the CUDA toolkit was already installed on the system and the path where opencl.h was installed was known, I tried to install CmdStan by referring to the Windows cases (Cannot compile example Open CL model (Win 10 cmdstan 2.27); openCL cmdstanr Windows error) and specifying the path like below:

path_to_opencl_lib <- "/usr/local/cuda-11.2/targets/x86_64-linux/include/CL/"
cpp_options = list(
  "CXXFLAGS += -fpermissive",
  "PRECOMPILED_HEADERS"=FALSE,
  paste0("LDFLAGS+= -L\"",path_to_opencl_lib,"\" -lOpenCL")
)

install_cmdstan(cores=4, overwrite = TRUE, cpp_options = cpp_options)

But it returns another error after compiling messages and failed to intall CmdStan:

/usr/bin/ld: cannot find -lOpenCL
collect2: error: ld returned 1 exit status
make/command:12: recipe for target 'bin/diagnose' failed
make: *** [bin/diagnose] Error 1
make: *** Waiting for unfinished jobs....

Initially, I thought it was due to the OpenCL path not being linked, but since similar problems do not seem to occur on Linux systems, could there be another cause?

The OS version of the server is as follows:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS"

And return of clinfo -l is:

Platform #0: NVIDIA CUDA
 +-- Device #0: Quadro RTX 8000
 `-- Device #1: Quadro RTX 8000

Hi all,

By looking around together with the server maintainer, we found that the opencl header file was simply not installed. After installing this header file;

sudo apt-get install -y opencl-headers

the stan code in my previous post compiled without any problems.

I would be happy if anyone else has the same problem and would like to refer to it.
Thank you very much for paying attention to my post.

4 Likes

Glad you got it working and thanks for reporting the solution!

We had a similar problem but for our system the headers were build already. clinfo showed the GPU from the start but compilation failed.
However, for anyone stumbling over a similar issue which can’t be solved by @otthii’s comment I will just add what worked for us. Installing some additional packages (via nvidia-opencl-dev) solved the problems.

So, on the system the opencl related packages are:

(installed manually)

cuda-opencl-dev-12-1
nvidia-opencl-dev

(automatically installed as dependencies of the packages above):

cuda-opencl-12-1
ocl-icd-libopencl1
ocl-icd-opencl-dev
opencl-c-headers

Now the example runs properly. Maybe something related to the headers was missing from the cuda-opencl-dev-12.1 install which was then fixed.

R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS

edit:
GPU info via clinfo- l

Platform #0: NVIDIA CUDA
 `-- Device #0: NVIDIA RTX A4000
2 Likes