# Cmdstan gpu support

**URL:** <https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318>\
**Category:** CmdStan\
**Created:** [October 9, 2019, 3:09pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318 "2019-10-09T15:09:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![linas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/linas/32/5342_2.png) [@linas](https://discourse.mc-stan.org/u/linas)\
**Post date:** [October 9, 2019, 3:09pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/1 "2019-10-09T15:09:50Z")

</div>

I nstalled cmdstan-2.20.0 from tarball and modified make/local as in [https://github.com/stan-dev/math/wiki/OpenCL-GPU-Routines](https://github.com/stan-dev/math/wiki/OpenCL-GPU-Routines). However make build gives me this message. Please advise.

g++ -std=c++1y -pthread -Wno-sign-compare -I stan/lib/stan\_math/lib/opencl\_1.2.8 -O3 -I src -I stan/src -I stan/lib/stan\_math/ -I stan/lib/stan\_math/lib/eigen\_3.3.3 -I stan/lib/stan\_math/lib/boost\_1.69.0 -I stan/lib/stan\_math/lib/sundials\_4.1.0/include -DBOOST\_RESULT\_OF\_USE\_TR1 -DBOOST\_NO\_DECLTYPE -DBOOST\_DISABLE\_ASSERTS -DBOOST\_PHOENIX\_NO\_VARIADIC\_EXPRESSION -DSTAN\_OPENCL -DOPENCL\_DEVICE\_ID= -DOPENCL\_PLATFORM\_ID= -DCL\_USE\_DEPRECATED\_OPENCL\_1\_2\_APIS -D\_\_CL\_ENABLE\_EXCEPTIONS -Wno-ignored-attributes -c -o bin/cmdstan/stansummary.o src/cmdstan/stansummary.cpp  
In file included from stan/lib/stan\_math/stan/math/prim/mat/fun/mdivide\_left\_tri.hpp:10:0,  
from stan/lib/stan\_math/stan/math/prim/mat/fun/mdivide\_left\_tri\_low.hpp:6,  
from stan/lib/stan\_math/stan/math/prim/mat/fun/chol2inv.hpp:7,  
from stan/lib/stan\_math/stan/math/prim/mat.hpp:68,  
from stan/src/stan/mcmc/chains.hpp:5,  
from src/cmdstan/stansummary.cpp:5:  
stan/lib/stan\_math/stan/math/opencl/opencl\_context.hpp: In constructor ‘stan::math::opencl\_context\_base::opencl\_context\_base()’:  
stan/lib/stan\_math/stan/math/opencl/opencl\_context.hpp:104:30: error: expected primary-expression before ‘\>=’ token  
if (OPENCL\_PLATFORM\_ID \>= platforms\_.size()) {  
^~  
stan/lib/stan\_math/stan/math/opencl/opencl\_context.hpp:108:48: error: expected primary-expression before ‘]’ token  
platform\_ = platforms\_[OPENCL\_PLATFORM\_ID];  
^  
stan/lib/stan\_math/stan/math/opencl/opencl\_context.hpp:115:28: error: expected primary-expression before ‘\>=’ token  
if (OPENCL\_DEVICE\_ID \>= devices\_.size()) {  
^~  
stan/lib/stan\_math/stan/math/opencl/opencl\_context.hpp:119:42: error: expected primary-expression before ‘]’ token  
device\_ = devices\_[OPENCL\_DEVICE\_ID];  
^  
stan/lib/stan\_math/stan/math/opencl/opencl\_context.hpp: In member function ‘std::\_\_cxx11::string stan::math::opencl\_context::description() const’:  
stan/lib/stan\_math/stan/math/opencl/opencl\_context.hpp:227:48: error: expected primary-expression before ‘\<\<’ token  
msg \<\< "Platform ID: " \<\< OPENCL\_DEVICE\_ID \<\< “\n”;

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [October 9, 2019, 3:12pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/2 "2019-10-09T15:12:40Z")

</div>

What do you have for opencl in make/local?

---

<div class="post-metadata">

**Author:** ![linas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/linas/32/5342_2.png) [@linas](https://discourse.mc-stan.org/u/linas)\
**Post date:** [October 9, 2019, 3:15pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/3 "2019-10-09T15:15:01Z")

</div>

STAN\_OPENCL=true  
OPENCL\_DEVICE\_ID={0} OPENCL\_PLATFORM\_ID={0}

clinfo -l  
Platform #0: NVIDIA CUDA  
`-- Device #0: GeForce GTX 1050 Ti

---

<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:** [October 9, 2019, 3:19pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/4 "2019-10-09T15:19:40Z")

</div>

Hi,

the curly brackets are not needed there.

An example of a valid one make/local would be:

STAN\_OPENCL = true  
OPENCL\_DEVICE\_ID = 0  
OPENCL\_PLATFORM\_ID = 0

---

<div class="post-metadata">

**Author:** ![linas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/linas/32/5342_2.png) [@linas](https://discourse.mc-stan.org/u/linas)\
**Post date:** [October 9, 2019, 3:35pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/5 "2019-10-09T15:35:34Z")

</div>

It works. Thank you. Make gives a warning that clock skew was detected but I guess this is because of make/local.

---

<div class="post-metadata">

**Author:** ![linas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/linas/32/5342_2.png) [@linas](https://discourse.mc-stan.org/u/linas)\
**Post date:** [October 9, 2019, 3:41pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/6 "2019-10-09T15:41:00Z")

</div>

When I tried to compile my model it gave bunch of errors such as (although openCL is included):  
g++ -std=c++1y -pthread -Wno-sign-compare -I stan/lib/stan\_math/lib/opencl\_1.2.8 -O3 -I src -I stan/src -I stan/lib/stan\_math/ -I stan/lib/stan\_math/lib/eigen\_3.3.3 -I stan/lib/stan\_math/lib/boost\_1.69.0 -I stan/lib/stan\_math/lib/sundials\_4.1.0/include -DBOOST\_RESULT\_OF\_USE\_TR1 -DBOOST\_NO\_DECLTYPE -DBOOST\_DISABLE\_ASSERTS -DBOOST\_PHOENIX\_NO\_VARIADIC\_EXPRESSION -DSTAN\_OPENCL -DOPENCL\_DEVICE\_ID=0 -DOPENCL\_PLATFORM\_ID=0 -DCL\_USE\_DEPRECATED\_OPENCL\_1\_2\_APIS -D\_\_CL\_ENABLE\_EXCEPTIONS -Wno-ignored-attributes -lOpenCL src/cmdstan/main.o stan/lib/stan\_math/lib/sundials\_4.1.0/lib/libsundials\_nvecserial.a stan/lib/stan\_math/lib/sundials\_4.1.0/lib/libsundials\_cvodes.a stan/lib/stan\_math/lib/sundials\_4.1.0/lib/libsundials\_idas.a /home/eval/lmockus/cmdstan-2.20.0.gpu/cliff/cliff.o -o /home/eval/lmockus/cmdstan-2.20.0.gpu/cliff/cliff  
src/cmdstan/main.o: In function `cl::detail::getPlatformVersion(_cl_platform_id*)': main.cpp:(.text+0x38): undefined reference to `clGetPlatformInfo’  
main.cpp:(.text+0x63): undefined reference to `clGetPlatformInfo’

---

<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:** [October 9, 2019, 3:58pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/7 "2019-10-09T15:58:59Z")

</div>

That means that the linker cant find the OpenCL library to link. Are you using Windows or Linux? On Linux that should work if you installed the driver normally. On windows you need to set a flag.

Windows flag: LDFLAGS\_OPENCL= -L"$(CUDA\_PATH)\lib\x64" -lOpenCL

---

<div class="post-metadata">

**Author:** ![linas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/linas/32/5342_2.png) [@linas](https://discourse.mc-stan.org/u/linas)\
**Post date:** [October 9, 2019, 4:00pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/8 "2019-10-09T16:00:26Z")

</div>

Linux  
I just checked libOpenCL.so is in /usr/lib/x86\_64-linux-gnu  
I installed it as in [https://askubuntu.com/questions/796770/how-to-install-libopencl-so-on-ubuntu](https://askubuntu.com/questions/796770/how-to-install-libopencl-so-on-ubuntu)

---

<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:** [October 9, 2019, 4:12pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/9 "2019-10-09T16:12:08Z")

</div>

Yeah, that all seems fine.

I keep forgetting that we had a bug in 2.20, that we fixed a day or two after the release, but there was no hotfix release. The next release is coming in 9 days.

For the time being I would recommend cloning the latest develop (git clone --single-branch [https://github.com/stan-dev/cmdstan.git](https://github.com/stan-dev/cmdstan.git) --recursive). That one does require git unfortunately.

Can you share anything about the model you are trying to speed up? Thanks.

---

<div class="post-metadata">

**Author:** ![linas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/linas/32/5342_2.png) [@linas](https://discourse.mc-stan.org/u/linas)\
**Post date:** [October 9, 2019, 4:34pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/10 "2019-10-09T16:34:51Z")

</div>

git is fine. Do you recommend git?

The model is in [cliff.stan](https://discourse.mc-stan.org/uploads/short-url/1z9yM1EQp5VbQ7XZyXhCftijRIh.stan) (3.3 KB)  
It is a time series model with neural network instead of ar(1). It runs very slowly but uses matrix mult so I thought GPU might speed it up. I am also thinking about adding a threading in order to use all available cores.

---

<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:** [October 9, 2019, 4:41pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/11 "2019-10-09T16:41:33Z")

</div>

Yes, I would recommend cloning with git.

There are quite a few matrix multiplications in here, so you should see some speedup here, depending on the sizes. At the moment I think you would benefit from threading more, given my quick inspection of the model. Except if the matrix multiplications are 200x200 times 200x200 or larger.

If you are interested in threading I recommend this tutorial: [https://github.com/rmcelreath/cmdstan\_map\_rect\_tutorial](https://github.com/rmcelreath/cmdstan_map_rect_tutorial)

---

<div class="post-metadata">

**Author:** ![linas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/linas/32/5342_2.png) [@linas](https://discourse.mc-stan.org/u/linas)\
**Post date:** [October 9, 2019, 5:24pm UTC](https://discourse.mc-stan.org/t/cmdstan-gpu-support/11318/12 "2019-10-09T17:24:04Z")

</div>

Actually it is 200x10 matrices. Refactoring into map\_rect form is bit more complicated - the model is big enough already. Design matrix for each year (X.) is different for each year and the calculations have to be done year by year. Perhaps each shard should contain data for each year? I am just thinking loudly. It means that each shard should have unequal number of data points. Hopefully durable… The problem I am encountering is “trace ran beyond…” which kills sampler - I posted about it few days ago - so when it is resolved I will proceed with multithreading.
