Unable to Install the Development Versions of StanHeaders and RStan on Windows 10

I have managed to get cmdstanr with OpenCL up and running without any issues. However, I seem to have run into a problem while installing the development versions of StanHeaders and RStan based on the instructions here. Installing StanHeaders using the following code completes successfully.

 remotes::install_git("https://github.com/hsbadr/rstan", subdir = "StanHeaders", ref = "develop")

However, when I test any of the stanFunction examples (for instance, stanFunction("log_sum_exp", x = exp(1), y = pi) I get the following output and associated error:

"C:/rtools40/mingw64/bin/"g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-41~1.0/include" -DNDEBUG   -I"C:/Users/ajn0093/R/win-library/4.0/Rcpp/include" -I"C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include" -I"C:/Users/ajn0093/R/win-library/4.0/RcppEigen/include" -I"C:/Users/ajn0093/R/win-library/4.0/BH/include" -I"C:/Users/ajn0093/AppData/Local/Temp/RtmpEv4RmH/sourceCpp-x86_64-w64-mingw32-1.0.6"  -DUSE_STANC3 -DSTAN_OPENCL    -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c file2e1c85f2557.cpp -o file2e1c85f2557.o
In file included from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/core.hpp:4,
                 from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/fun/acos.hpp:4,
                 from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/fun.hpp:6,
                 from file2e1c85f2557.cpp:9:
C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/core/init_threadpool_tbb.hpp:9:10: fatal error: tbb/tbb_stddef.h: No such file or directory
 #include <tbb/tbb_stddef.h>
          ^~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-41~1.0/etc/x64/Makeconf:245: file2e1c85f2557.o] Error 1
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir,  : 
  Error 1 occurred building shared library.

If I try installing the development branch of StanHeaders by instead using install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos"))) instead and running an emaple, I receive a similar error.

"C:/rtools40/mingw64/bin/"g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-41~1.0/include" -DNDEBUG   -I"C:/Users/ajn0093/R/win-library/4.0/Rcpp/include" -I"C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include" -I"C:/Users/ajn0093/R/win-library/4.0/RcppEigen/include" -I"C:/Users/ajn0093/R/win-library/4.0/BH/include" -I"C:/Users/ajn0093/AppData/Local/Temp/RtmpCqn3yD/sourceCpp-x86_64-w64-mingw32-1.0.6"  -DUSE_STANC3 -DSTAN_OPENCL       -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c file1d1829a9275c.cpp -o file1d1829a9275c.o
In file included from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/core.hpp:4,
                 from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/fun/acos.hpp:4,
                 from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/fun.hpp:6,
                 from file1d1829a9275c.cpp:9:
C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/core/init_threadpool_tbb.hpp:12:10: fatal error: tbb/task_scheduler_init.h: No such file or directory
 #include <tbb/task_scheduler_init.h>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-41~1.0/etc/x64/Makeconf:245: file1d1829a9275c.o] Error 1
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir,  : 
  Error 1 occurred building shared library.

Any subsequent attempt at installing the development version of RStan with OpenCL flags fails entirely and throws the following errors in addition to several thousand lines of warnings.

"C:/rtools40/mingw64/bin/"g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-41~1.0/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -D_REENTRANT -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DRCPP_PARALLEL_USE_TBB=1 -I'C:/Users/ajn0093/R/win-library/4.0/Rcpp/include' -I'C:/Users/ajn0093/R/win-library/4.0/RcppEigen/include' -I'C:/Users/ajn0093/R/win-library/4.0/BH/include' -I'C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include' -I'C:/Users/ajn0093/R/win-library/4.0/RcppParallel/include'  -DUSE_STANC3 -DSTAN_OPENCL    -DRCPP_PARALLEL_USE_TBB=1 -DSTRICT_R_HEADERS   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c sparse_extractors.cpp -o sparse_extractors.o
In file included from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/opencl/matrix_cl.hpp:8,
from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/opencl/kernel_generator/operation_cl.hpp:10,
from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/opencl/kernel_generator.hpp:106,
from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/opencl/prim.hpp:82,
from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim.hpp:7,
from sparse_extractors.cpp:2:
  C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/opencl/opencl_context.hpp:7:2: error: #error OPENCL_DEVICE_ID_NOT_SET
  #error OPENCL_DEVICE_ID_NOT_SET
  ^~~~~
  C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/opencl/opencl_context.hpp:10:2: error: #error OPENCL_PLATFORM_ID_NOT_SET
  #error OPENCL_PLATFORM_ID_NOT_SET
  ^~~~~

The system I am trying to perform the installation on is running Windows 10 Build 19042 and the output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars.win"))) is

CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2
CPPFLAGS += -DUSE_STANC3 -DSTAN_OPENCL
STAN_OPENCL=true
OPENCL_DEVICE_ID=0
OPENCL_PLATFORM_ID=0
LDFLAGS_OPENCL= -L"${CUDA_PATH}/lib/x64" -lOpenCL

Please let me know if any other information is required and I appreciated any assistance with this issue.

Try installing through the development repo:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

Note that OpenCL with the newer versions of RStan is still a WIP, so it may not work for you.

Thank you. I tried your suggestion both with and without the CPPFLAGS += -DUSE_STANC3 -DSTAN_OPENCL line in the makevars file. Both with and without the flags testing StanHeaders via StanHeaders::stanFunction("log_sum_exp", x = exp(1), y = pi) returns the following error

"C:/rtools40/mingw64/bin/"g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-41~1.0/include" -DNDEBUG   -I"C:/Users/ajn0093/R/win-library/4.0/Rcpp/include" -I"C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include" -I"C:/Users/ajn0093/R/win-library/4.0/RcppEigen/include" -I"C:/Users/ajn0093/R/win-library/4.0/BH/include" -I"C:/Users/ajn0093/AppData/Local/Temp/RtmpiS0sWv/sourceCpp-x86_64-w64-mingw32-1.0.6"        -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c file3750ef4aa1.cpp -o file3750ef4aa1.o
In file included from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/core.hpp:4,
                 from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/fun/acos.hpp:4,
                 from C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/fun.hpp:6,
                 from file3750ef4aa1.cpp:9:
C:/Users/ajn0093/R/win-library/4.0/StanHeaders/include/stan/math/prim/core/init_threadpool_tbb.hpp:12:10: fatal error: tbb/task_scheduler_init.h: No such file or directory
 #include <tbb/task_scheduler_init.h>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-41~1.0/etc/x64/Makeconf:245: file3750ef4aa1.o] Error 1
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir,  : 
  Error 1 occurred building shared library.

The RStan installation fails entirely with the flags in place, but succeeds if I remove them and example(stan_model, package = "rstan", run.dontrun = TRUE) runs so I take it that means OpenCL in RStan isn’t an option (at least on Windows systems at this point)?