# Partial specialization error when compiling model with opencl enabled

**URL:** <https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250>\
**Category:** Interfaces\
**Tags:** gpu, installation\
**Created:** [March 12, 2021, 12:51am UTC](https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250 "2021-03-12T00:51:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewGhazi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrewghazi/32/18855_2.png) [@andrewGhazi](https://discourse.mc-stan.org/u/andrewGhazi)\
**Post date:** [March 12, 2021, 12:51am UTC](https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250/1 "2021-03-12T00:51:19Z")

</div>

Hi there. I’m getting a partial specialization error during model compilation when opencl is turned on. The error I get is shown below. I’m trying to run the example found [here](https://github.com/bstatcomp/stan_gpu_install_docs/blob/master/install_docs/INSTALL_CMDSTANR.md) after following their installation instructions. I also tried editing cmdstan-2.26.1/make/local as per [these instructions](https://github.com/stan-dev/math/wiki/Quick-Start#enabling-gpus), but ended up getting a similar error when I tried `python3 ./runCmdStanTests.py test/unit -f opencl`. The model runs normally when I don’t use opencl. My GPU (RTX 3070) is pretty new if it’s possible that that could be related?

Operating System: Linux Mint 20.1  
Interface Version: cmdstanr 0.3.0.9000 + cmdstan 2.26.1  
Compiler/Toolkit: g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 and GNU Make 4.2.1

For reference, the opencl logistic regression example code is this:

```
library(cmdstanr)

data_path <- "./lr_glm.data.json"
model_path <- "./lr_glm.stan"

opencl_options = list(
  stan_opencl = TRUE,
  opencl_platform_id = 0, # replace the ID based on step 3
  opencl_device_id = 0 # replace the ID based on step 3
)

mod <- cmdstan_model(model_path, cpp_options = opencl_options, quiet = FALSE) # Fails here

fit <- mod$sample(data = data_path, num_samples=500, num_warmup = 500, num_chains = 1, num_cores = 1)

print(fit$summary())

```

This is the error message that shows up when the model tries to compile:

```
Compiling Stan program...

--- Compiling the main object file. This might take up to a minute. ---
g++ -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes -I stan/lib/stan_math/lib/opencl_2.2.0 -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_5.6.1/include -DBOOST_DISABLE_ASSERTS -DSTAN_OPENCL -DOPENCL_DEVICE_ID=0 -DOPENCL_PLATFORM_ID=0 -DCL_HPP_TARGET_OPENCL_VERSION=120 -DCL_HPP_MINIMUM_OPENCL_VERSION=120 -DCL_HPP_ENABLE_EXCEPTIONS -Wno-ignored-attributes -c -o src/cmdstan/main_opencl.o src/cmdstan/main.cpp
In file included from stan/lib/stan_math/stan/math/opencl/prim.hpp:86,
                 from stan/lib/stan_math/stan/math/prim.hpp:7,
                 from stan/src/stan/io/dump.hpp:7,
                 from src/cmdstan/command.hpp:24,
                 from src/cmdstan/main.cpp:1:
stan/lib/stan_math/stan/math/opencl/scalar_type.hpp:14:8: error: partial specialization of ‘struct stan::scalar_type<T, typename std::enable_if<stan::math::conjunction<stan::is_kernel_expression_and_not_scalar<T, void> >::value, void>::type>’ after instantiation of ‘struct stan::scalar_type<stan::math::constant_<int>, void>’ [-fpermissive]
   14 | struct scalar_type<T, require_all_kernel_expressions_and_none_scalar_t<T>> {
      | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from stan/lib/stan_math/stan/math/opencl/prim.hpp:86,
                 from stan/lib/stan_math/stan/math/prim.hpp:7,
                 from stan/src/stan/io/dump.hpp:7,
                 from src/cmdstan/command.hpp:24,
                 from src/cmdstan/main.cpp:1:
stan/lib/stan_math/stan/math/opencl/scalar_type.hpp:14:8: error: partial specialization of ‘struct stan::scalar_type<T, typename std::enable_if<stan::math::conjunction<stan::is_kernel_expression_and_not_scalar<T, void> >::value, void>::type>’ after instantiation of ‘struct stan::scalar_type<stan::math::constant_<int>, void>’ [-fpermissive]
   14 | struct scalar_type<T, require_all_kernel_expressions_and_none_scalar_t<T>> {
      | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [make/program:14: src/cmdstan/main_opencl.o] Error 1
make: *** [make/program:14: src/cmdstan/main_opencl.o] Error 1
Error: An error occured during compilation! See the message above for more information.

```

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [March 15, 2021, 2:37pm UTC](https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250/2 "2021-03-15T14:37:31Z")

</div>

@rok_cesnovar could perhaps be of help here :)

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [March 16, 2021, 11:22am UTC](https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250/3 "2021-03-16T11:22:49Z")

</div>

Hm, lets try do debug this a bit.

> [@andrewGhazi](#):
>
> My GPU (RTX 3070) is pretty new if it’s possible that that could be related?

No, if anything, newer should work better/easier.

Can you run `clinfo` in the terminal and paste the output?

---

<div class="post-metadata">

**Author:** ![andrewGhazi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrewghazi/32/18855_2.png) [@andrewGhazi](https://discourse.mc-stan.org/u/andrewGhazi)\
**Post date:** [March 16, 2021, 2:13pm UTC](https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250/4 "2021-03-16T14:13:22Z")

</div>

Here’s the output:

```
ghazi@ghazi-desktop2:~$ clinfo
Number of platforms 1
  Platform Name NVIDIA CUDA
  Platform Vendor NVIDIA Corporation
  Platform Version OpenCL 1.2 CUDA 11.2.136
  Platform Profile FULL_PROFILE
  Platform Extensions cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_device_uuid
  Platform Extensions function suffix NV

  Platform Name NVIDIA CUDA
Number of devices 1
  Device Name GeForce RTX 3070
  Device Vendor NVIDIA Corporation
  Device Vendor ID 0x10de
  Device Version OpenCL 1.2 CUDA
  Driver Version 460.39
  Device OpenCL C Version OpenCL C 1.2 
  Device Type GPU
  Device Topology (NV) PCI-E, 09:00.0
  Device Profile FULL_PROFILE
  Device Available Yes
  Compiler Available Yes
  Linker Available Yes
  Max compute units 46
  Max clock frequency 1815MHz
  Compute Capability (NV) 8.6
  Device Partition (core)
    Max number of sub-devices 1
    Supported partition types None
    Supported affinity domains (n/a)
  Max work item dimensions 3
  Max work item sizes 1024x1024x64
  Max work group size 1024
  Preferred work group size multiple 32
  Warp size (NV) 32
  Preferred / native vector sizes                 
    char 1 / 1       
    short 1 / 1       
    int 1 / 1       
    long 1 / 1       
    half 0 / 0 (n/a)
    float 1 / 1       
    double 1 / 1 (cl_khr_fp64)
  Half-precision Floating-point support (n/a)
  Single-precision Floating-point support (core)
    Denormals Yes
    Infinity and NANs Yes
    Round to nearest Yes
    Round to zero Yes
    Round to infinity Yes
    IEEE754-2008 fused multiply-add Yes
    Support is emulated in software No
    Correctly-rounded divide and sqrt operations Yes
  Double-precision Floating-point support (cl_khr_fp64)
    Denormals Yes
    Infinity and NANs Yes
    Round to nearest Yes
    Round to zero Yes
    Round to infinity Yes
    IEEE754-2008 fused multiply-add Yes
    Support is emulated in software No
  Address bits 64, Little-Endian
  Global memory size 8366915584 (7.792GiB)
  Error Correction support No
  Max memory allocation 2091728896 (1.948GiB)
  Unified memory for Host and Device No
  Integrated memory (NV) No
  Minimum alignment for any data type 128 bytes
  Alignment of base address 4096 bits (512 bytes)
  Global Memory cache type Read/Write
  Global Memory cache size 1318912 (1.258MiB)
  Global Memory cache line size 128 bytes
  Image support Yes
    Max number of samplers per kernel 32
    Max size for 1D images from buffer 268435456 pixels
    Max 1D or 2D image array size 2048 images
    Max 2D image size 32768x32768 pixels
    Max 3D image size 16384x16384x16384 pixels
    Max number of read image args 256
    Max number of write image args 32
  Local memory type Local
  Local memory size 49152 (48KiB)
  Registers per block (NV) 65536
  Max number of constant args 9
  Max constant buffer size 65536 (64KiB)
  Max size of kernel argument 4352 (4.25KiB)
  Queue properties                                
    Out-of-order execution Yes
    Profiling Yes
  Prefer user sync for interop No
  Profiling timer resolution 1000ns
  Execution capabilities                          
    Run OpenCL kernels Yes
    Run native kernels No
    Kernel execution timeout (NV) Yes
  Concurrent copy and kernel execution (NV) Yes
    Number of async copy engines 2
  printf() buffer size 1048576 (1024KiB)
  Built-in kernels (n/a)
  Device Extensions cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_device_uuid

NULL platform behavior
  clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...) NVIDIA CUDA
  clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...) Success [NV]
  clCreateContext(NULL, ...) [default] Success [NV]
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT) No platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU) No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU) No platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR) No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM) Invalid device type for platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL) No platform

ICD loader properties
  ICD loader Name OpenCL ICD Loader
  ICD loader Vendor OCL Icd free software
  ICD loader Version 2.2.11
  ICD loader Profile OpenCL 2.1

```

---

<div class="post-metadata">

**Author:** ![jmaronas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jmaronas/32/9949_2.png) [@jmaronas](https://discourse.mc-stan.org/u/jmaronas)\
**Post date:** [March 16, 2021, 5:06pm UTC](https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250/5 "2021-03-16T17:06:10Z")

</div>

Okay, following up my post here: [Stan is not working on GPU in Linux - #4 by jmaronas](https://discourse.mc-stan.org/t/stan-is-not-working-on-gpu-in-linux/21331/4)

my clinfo -l gives:

Platform #0: NVIDIA CUDA  
`-- Device #0: GeForce GTX 1080

my clinfo gives:

```
Number of platforms 1
  Platform Name NVIDIA CUDA
  Platform Vendor NVIDIA Corporation
  Platform Version OpenCL 1.2 CUDA 11.2.153
  Platform Profile FULL_PROFILE
  Platform Extensions cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_device_uuid
  Platform Extensions function suffix NV

  Platform Name NVIDIA CUDA
Number of devices 1
  Device Name GeForce GTX 1080
  Device Vendor NVIDIA Corporation
  Device Vendor ID 0x10de
  Device Version OpenCL 1.2 CUDA
  Driver Version 460.56
  Device OpenCL C Version OpenCL C 1.2 
  Device Type GPU
  Device Topology (NV) PCI-E, 01:00.0
  Device Profile FULL_PROFILE
  Device Available Yes
  Compiler Available Yes
  Linker Available Yes
  Max compute units 20
  Max clock frequency 1733MHz
  Compute Capability (NV) 6.1
  Device Partition (core)
    Max number of sub-devices 1
    Supported partition types None
    Supported affinity domains (n/a)
  Max work item dimensions 3
  Max work item sizes 1024x1024x64
  Max work group size 1024
  Preferred work group size multiple 32
  Warp size (NV) 32
  Preferred / native vector sizes                 
    char 1 / 1       
    short 1 / 1       
    int 1 / 1       
    long 1 / 1       
    half 0 / 0 (n/a)
    float 1 / 1       
    double 1 / 1 (cl_khr_fp64)
  Half-precision Floating-point support (n/a)
  Single-precision Floating-point support (core)
    Denormals Yes
    Infinity and NANs Yes
    Round to nearest Yes
    Round to zero Yes
    Round to infinity Yes
    IEEE754-2008 fused multiply-add Yes
    Support is emulated in software No
    Correctly-rounded divide and sqrt operations Yes
  Double-precision Floating-point support (cl_khr_fp64)
    Denormals Yes
    Infinity and NANs Yes
    Round to nearest Yes
    Round to zero Yes
    Round to infinity Yes
    IEEE754-2008 fused multiply-add Yes
    Support is emulated in software No
  Address bits 64, Little-Endian
  Global memory size 8513978368 (7.929GiB)
  Error Correction support No
  Max memory allocation 2128494592 (1.982GiB)
  Unified memory for Host and Device No
  Integrated memory (NV) No
  Minimum alignment for any data type 128 bytes
  Alignment of base address 4096 bits (512 bytes)
  Global Memory cache type Read/Write
  Global Memory cache size 983040 (960KiB)
  Global Memory cache line size 128 bytes
  Image support Yes
    Max number of samplers per kernel 32
    Max size for 1D images from buffer 268435456 pixels
    Max 1D or 2D image array size 2048 images
    Max 2D image size 16384x32768 pixels
    Max 3D image size 16384x16384x16384 pixels
    Max number of read image args 256
    Max number of write image args 16
  Local memory type Local
  Local memory size 49152 (48KiB)
  Registers per block (NV) 65536
  Max number of constant args 9
  Max constant buffer size 65536 (64KiB)
  Max size of kernel argument 4352 (4.25KiB)
  Queue properties                                
    Out-of-order execution Yes
    Profiling Yes
  Prefer user sync for interop No
  Profiling timer resolution 1000ns
  Execution capabilities                          
    Run OpenCL kernels Yes
    Run native kernels No
    Kernel execution timeout (NV) No
  Concurrent copy and kernel execution (NV) Yes
    Number of async copy engines 2
  printf() buffer size 1048576 (1024KiB)
  Built-in kernels (n/a)
  Device Extensions cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_device_uuid

NULL platform behavior
  clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...) No platform
  clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...) No platform
  clCreateContext(NULL, ...) [default] No platform
  clCreateContext(NULL, ...) [other] Success [NV]
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT) No platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU) No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU) No platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR) No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM) Invalid device type for platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL) No platform

```

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [March 16, 2021, 5:37pm UTC](https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250/6 "2021-03-16T17:37:12Z")

</div>

See [Stan is not working on GPU in Linux - #6 by rok\_cesnovar](https://discourse.mc-stan.org/t/stan-is-not-working-on-gpu-in-linux/21331/6) for the fix. Should be irrelevant in the next release. Thanks for the reports @jmaronas@andrewGhazi and sorry for the troubles.

Thanks for the tag @torkar

---

<div class="post-metadata">

**Author:** ![andrewGhazi](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrewghazi/32/18855_2.png) [@andrewGhazi](https://discourse.mc-stan.org/u/andrewGhazi)\
**Post date:** [March 16, 2021, 6:11pm UTC](https://discourse.mc-stan.org/t/partial-specialization-error-when-compiling-model-with-opencl-enabled/21250/7 "2021-03-16T18:11:48Z")

</div>

That works, thank you!
