Error when running rstan in linux batch mode

I am trying to run a very simple model with rstan in batch mode. When I try to run the same code, but not in batch mode, I do not have this error.

I checked a similar discussion (Error when running rstan with linux) in the forum, but my knownledge in Linux is limited. My R code and error message are below.

R code:

### Libraries
library(rstan)
library(foreach)
library(doParallel)
library(doRNG)

### Simulation parameters
ncores <- 28
n_sim <- 1000

registerDoParallel(ncores)

set.seed(1234)
results <- foreach(i = 1:n_sim,
                   .combine='c',
                   .packages = "rstan",
                   .verbose = TRUE) %dorng% {
                     
                     scode <- "
                        parameters {
                           real y[2];
                        }
                        model {
                          y[1] ~ normal(0, 1);
                          y[2] ~ double_exponential(0, 2);
                        }
                        "
                     fit1 <- stan(model_code = scode, iter = 10, verbose = TRUE)
                     
                   }

stopImplicitCluster()

Error message:

> ### Libraries
> library(rstan)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
> library(foreach)
> library(doParallel)
Loading required package: iterators
Loading required package: parallel
> library(doRNG)
Loading required package: rngtools
> 
> ### Simulation parameters
> ncores <- 5
> n_sim <- 5
> 
> 
> 
> registerDoParallel(ncores)
> 
> set.seed(1234)
> results <- foreach(i = 1:n_sim,
+                    .combine='c', 
+                    .packages = "rstan",
+                    .verbose = TRUE) %dorng% {
+                      
+                      scode <- "
+                         parameters {
+                            real y[2];
+                         }
+                         model {
+                           y[1] ~ normal(0, 1);
+                           y[2] ~ double_exponential(0, 2);
+                         }
+                         "
+                      fit1 <- stan(model_code = scode, iter = 10, verbose = TRUE)
+                      
+                    }
numValues: 5, numResults: 0, stopped: TRUE
# Original RNG: Mersenne-Twister - Inversion - Rejection [10403, 624, -1394370482, -1723143049, 2071488076, 1659356893, -1081051142, ...]
# Generate random RNGstream seed: OK
# Seed RNGkind is: L'Ecuyer-CMRG - Inversion - Rejection [10407, -305383241, 69318028, -1161659107, -348804806, -1127582349, -343284712]
# Restoring RNG: Mersenne-Twister - Inversion - Rejection [10403, 1, 1407173775, 141192598, 911446336, 1689480195, -1101853151, ...]
* Registered backend: doParallelMC
numValues: 5, numResults: 0, stopped: TRUE

TRANSLATING MODEL '459f18aa440866d112562fa03ce604f7' FROM Stan CODE TO C++ CODE NOW.

TRANSLATING MODEL '459f18aa440866d112562fa03ce604f7' FROM Stan CODE TO C++ CODE NOW.

TRANSLATING MODEL '459f18aa440866d112562fa03ce604f7' FROM Stan CODE TO C++ CODE NOW.

TRANSLATING MODEL '459f18aa440866d112562fa03ce604f7' FROM Stan CODE TO C++ CODE NOW.

TRANSLATING MODEL '459f18aa440866d112562fa03ce604f7' FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model '459f18aa440866d112562fa03ce604f7'.
successful in parsing the Stan model '459f18aa440866d112562fa03ce604f7'.
successful in parsing the Stan model '459f18aa440866d112562fa03ce604f7'.
successful in parsing the Stan model '459f18aa440866d112562fa03ce604f7'.
successful in parsing the Stan model '459f18aa440866d112562fa03ce604f7'.
OS: x86_64, linux-gnu; rstan: 2.21.2; Rcpp: 1.0.7; inline: 0.3.19 
OS: x86_64, linux-gnu; rstan: 2.21.2; Rcpp: 1.0.7; inline: 0.3.19 
OS: x86_64, linux-gnu; rstan: 2.21.2; Rcpp: 1.0.7; inline: 0.3.19 
OS: x86_64, linux-gnu; rstan: 2.21.2; Rcpp: 1.0.7; inline: 0.3.19 
OS: x86_64, linux-gnu; rstan: 2.21.2; Rcpp: 1.0.7; inline: 0.3.19 
Trying to compile a simple C file
Trying to compile a simple C file
Trying to compile a simple C file
Trying to compile a simple C file
Trying to compile a simple C file
Running /hpc/apps/R/4.1.2/lib64/R/bin/R CMD SHLIB foo.c
Running /hpc/apps/R/4.1.2/lib64/R/bin/R CMD SHLIB foo.c
Running /hpc/apps/R/4.1.2/lib64/R/bin/R CMD SHLIB foo.c
Running /hpc/apps/R/4.1.2/lib64/R/bin/R CMD SHLIB foo.c
Running /hpc/apps/R/4.1.2/lib64/R/bin/R CMD SHLIB foo.c
gcc -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -c foo.c -o foo.o
gcc -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -c foo.c -o foo.o
gcc -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -c foo.c -o foo.o
gcc -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -c foo.c -o foo.o
gcc -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -c foo.c -o foo.o
gcc -shared -L/hpc/apps/R/4.1.2/lib64/R/lib -L/hpc/apps/zlib/1.2.8/lib -L/hpc/apps/pcre/8.38/lib -L/hpc/apps/bzip2/1.0.6/lib -L/hpc/apps/curl/7.54.1/lib -L/hpc/apps/cairo/1.14.12/lib -o foo.so foo.o -L/hpc/apps/R/4.1.2/lib64/R/lib -lR
gcc -shared -L/hpc/apps/R/4.1.2/lib64/R/lib -L/hpc/apps/zlib/1.2.8/lib -L/hpc/apps/pcre/8.38/lib -L/hpc/apps/bzip2/1.0.6/lib -L/hpc/apps/curl/7.54.1/lib -L/hpc/apps/cairo/1.14.12/lib -o foo.so foo.o -L/hpc/apps/R/4.1.2/lib64/R/lib -lR
/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [foo.so] Error 1
gcc -shared -L/hpc/apps/R/4.1.2/lib64/R/lib -L/hpc/apps/zlib/1.2.8/lib -L/hpc/apps/pcre/8.38/lib -L/hpc/apps/bzip2/1.0.6/lib -L/hpc/apps/curl/7.54.1/lib -L/hpc/apps/cairo/1.14.12/lib -o foo.so foo.o -L/hpc/apps/R/4.1.2/lib64/R/lib -lR
/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [foo.so] Error 1
gcc -shared -L/hpc/apps/R/4.1.2/lib64/R/lib -L/hpc/apps/zlib/1.2.8/lib -L/hpc/apps/pcre/8.38/lib -L/hpc/apps/bzip2/1.0.6/lib -L/hpc/apps/curl/7.54.1/lib -L/hpc/apps/cairo/1.14.12/lib -o foo.so foo.o -L/hpc/apps/R/4.1.2/lib64/R/lib -lR
gcc -shared -L/hpc/apps/R/4.1.2/lib64/R/lib -L/hpc/apps/zlib/1.2.8/lib -L/hpc/apps/pcre/8.38/lib -L/hpc/apps/bzip2/1.0.6/lib -L/hpc/apps/curl/7.54.1/lib -L/hpc/apps/cairo/1.14.12/lib -o foo.so foo.o -L/hpc/apps/R/4.1.2/lib64/R/lib -lR
/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [foo.so] Error 1
/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [foo.so] Error 1
/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [foo.so] Error 1
 >> setting environment variables: 
PKG_LIBS =  '/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/rstan/lib//libStanServices.a' -L'/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/StanHeaders/lib/' -lStanHeaders -L'/hpc/apps/R/4.1.2/lib64/R/library/RcppParallel/lib/' -ltbb
PKG_CPPFLAGS =   -I"/hpc/apps/R/4.1.2/lib64/R/library/Rcpp/include/"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/unsupported"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/BH/include" -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/StanHeaders/include/src/"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/StanHeaders/include/"  -I"/hpc/apps/R/4.1.2/lib64/R/library/RcppParallel/include/"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 
 >> Program source :
 >> setting environment variables: 
PKG_LIBS =  '/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/rstan/lib//libStanServices.a' -L'/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/StanHeaders/lib/' -lStanHeaders -L'/hpc/apps/R/4.1.2/lib64/R/library/RcppParallel/lib/' -ltbb
PKG_CPPFLAGS =   -I"/hpc/apps/R/4.1.2/lib64/R/library/Rcpp/include/"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/unsupported"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/BH/include" -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/StanHeaders/include/src/"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/StanHeaders/include/"  -I"/hpc/apps/R/4.1.2/lib64/R/library/RcppParallel/include/"  -I"/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/hpc/home/dinizma/R/x86_64-pc-linux-gnu-library/4.1/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 

This incompatibility with running rstan through DoParallel has been reported before, but unfortunately we don’t have a fix or workaround at the moment.

I’d recommend trying with cmdstanr instead, it may be more stable for this use-case: Getting started with CmdStanR • cmdstanr

2 Likes

Thank you. I rewrote my code using R package cmdstanr. Unfortunately, I still get an error message.

My R-code:

### Libraries
library(cmdstanr)
library(foreach)
library(doParallel)
library(doRNG)

### Simulation parameters
ncores <- 5
n_sim <- 5

registerDoParallel(ncores)

file <- file.path("/hpc/home/test/", "test.stan")
mod <- cmdstan_model(file)

set.seed(1234)
results <- foreach(i = 1:n_sim,
                   .combine='c', 
                   .packages = "cmdstanr",
                   .verbose = TRUE) %dorng% {
                     
                     data_list <- list()
                     
                     fit <- mod$sample(
                       data = data_list, 
                       seed = 123, 
                       chains = 4, 
                       parallel_chains = 4,
                       refresh = 500
                     )
                     
                     fit$draws() 
                   }

stopImplicitCluster()

STAN code:

parameters {
  real y[2];
}
model {
  y[1] ~ normal(0, 1);
  y[2] ~ double_exponential(0, 2);
}                       

Error message:

> ### Libraries
> library(cmdstanr)
This is cmdstanr version 0.4.0
- Online documentation and vignettes at mc-stan.org/cmdstanr
- CmdStan path set to: /home/user/.cmdstanr/cmdstan-2.28.2
- Use set_cmdstan_path() to change the path
> library(foreach)
> library(doParallel)
Loading required package: iterators
Loading required package: parallel
> library(doRNG)
Loading required package: rngtools
> 
> ### Simulation parameters
> ncores <- 5
> n_sim <- 5
> 
> registerDoParallel(ncores)
> 
> file <- file.path("/hpc/home/test/", "test.stan")
> mod <- cmdstan_model(file)
In file included from /hpc/apps/gcc/9.1.0/include/c++/9.1.0/x86_64-pc-linux-gnu/bits/c++config.h:524,
                 from /hpc/apps/gcc/9.1.0/include/c++/9.1.0/new:39,
                 from stan/lib/stan_math/lib/eigen_3.3.9/Eigen/Core:82,
                 from stan/lib/stan_math/lib/eigen_3.3.9/Eigen/Dense:1,
                 from stan/lib/stan_math/stan/math/prim/fun/Eigen.hpp:22,
                 from stan/lib/stan_math/stan/math/rev.hpp:4,
                 from stan/lib/stan_math/stan/math.hpp:19,
                 from stan/src/stan/model/model_header.hpp:4,
                 from /localscratch/250933.1.mylab.q/RtmpROVL8w/model-10ffd3c81c828.hpp:3:
/hpc/apps/gcc/9.1.0/include/c++/9.1.0/x86_64-pc-linux-gnu/bits/os_defines.h:39:10: fatal error: features.h: No such file or directory
   39 | #include <features.h>
      |          ^~~~~~~~~~~~
compilation terminated.
make: *** [/localscratch/250933.1.mylab.q/RtmpROVL8w/model-10ffd3c81c828] Error 1
Error: An error occured during compilation! See the message above for more information.
Execution halted

Any thoughts?

Does the cmdstanr model work when run sequentially, or outside of the dorng loop?

The error happens before running the dorng loop. It happens when compiling the model:

mod <- cmdstan_model(file)

The model compiles with no errors when I do not use the batch mode.

As in it errors if you run:

registerDoParallel(ncores)

file <- file.path("/hpc/home/test/", "test.stan")
mod <- cmdstan_model(file)

But not if you run:

file <- file.path("/hpc/home/test/", "test.stan")
mod <- cmdstan_model(file)

registerDoParallel(ncores)

If so, can you just pass the compiled model manually to the foreach loop? Like:

file <- file.path("/hpc/home/test/", "test.stan")
mod <- cmdstan_model(file)

registerDoParallel(ncores)
results <- foreach(i = 1:n_sim,
                   .combine='c', 
                   .export="mod",
                   .packages = "cmdstanr",
                   .verbose = TRUE) %dorng% {
...
}

The cmdstan model should be implicitly captured, but you can manually export it to be safe

1 Like

The model compiled after this change! However I get another error message. I took the model outside of loop to simplify things, but I am still running it in batch mode:

R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> ### Libraries
> library(cmdstanr)
This is cmdstanr version 0.4.0
- Online documentation and vignettes at mc-stan.org/cmdstanr
- CmdStan path set to: /home/user/.cmdstanr/cmdstan-2.28.2
- Use set_cmdstan_path() to change the path
> library(foreach)
> library(doParallel)
Loading required package: iterators
Loading required package: parallel
> library(doRNG)
Loading required package: rngtools
> 
> ### Simulation parameters
> ncores <- 5
> n_sim <- 5
> 
> file <- file.path("/hpc/home/user/test/", "test.stan")
> mod <- cmdstan_model(file)
> 
> registerDoParallel(ncores)
> 
> set.seed(1234)
> 
>                      data_list <- list()
>                      
>                      fit <- mod$sample(
+                        data = data_list, 
+                        seed = 123, 
+                        chains = 4, 
+                        parallel_chains = 4,
+                        refresh = 500
+                      )
Running MCMC with 4 parallel chains...

Chain 1 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./test)
Chain 1 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `CXXABI_1.3.13' not found (required by /hpc/home/user/.cmdstanr/cmdstan-2.28.2/stan/lib/stan_math/lib/tbb/libtbb.so.2)
Chain 2 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./test)
Chain 2 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `CXXABI_1.3.13' not found (required by /hpc/home/user/.cmdstanr/cmdstan-2.28.2/stan/lib/stan_math/lib/tbb/libtbb.so.2)
Chain 3 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./test)
Chain 3 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `CXXABI_1.3.13' not found (required by /hpc/home/user/.cmdstanr/cmdstan-2.28.2/stan/lib/stan_math/lib/tbb/libtbb.so.2)
Chain 4 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./test)
Chain 4 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `CXXABI_1.3.13' not found (required by /hpc/home/user/.cmdstanr/cmdstan-2.28.2/stan/lib/stan_math/lib/tbb/libtbb.so.2)
Warning: Chain 1 finished unexpectedly!

Warning: Chain 2 finished unexpectedly!

Warning: Chain 3 finished unexpectedly!

Warning: Chain 4 finished unexpectedly!

Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Warning messages:
1: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
 
2: No chains finished successfully. Unable to retrieve the fit. 
>                      
>                      fit$draws() 
Error: No chains finished successfully. Unable to retrieve the draws.
Execution halted

So it looks R is having trouble finding the system headers and libraries after the parallel workers have been initialised. Let’s try using different worker types to see if that will help.

SOCK

cl <- makeCluster(ncores, type = "PSOCK")
registerDoParallel(cl)

FORK

cl <- makeCluster(ncores, type = "FORK")
registerDoParallel(cl)

Can you see if either of those approaches to initialising workers gives you a change in behaviour?

Realised I had a typo in the registerDoParallel calls above, note that they’ve been changed sorry!

No worries at all. I tried the two approaches, but I got the same error messages.

Alright, so one last thing before we have to dive into system libraries and paths and such, can you try using the future backend for foreach? Their method of initialising the workers might be more robust.

Restart R, and replace your current doParallel load and initialise with:

install.packages("doFuture")

library(doFuture)
registerDoFuture()
plan(multisession)

Same error. It works if I open R in Linux and run my code not in the batch mode.
Copying it below to ensure that I used doFuture correctly.

> ### Libraries
> library(cmdstanr)
This is cmdstanr version 0.4.0
- Online documentation and vignettes at mc-stan.org/cmdstanr
- CmdStan path set to: /home/user/.cmdstanr/cmdstan-2.28.2
- Use set_cmdstan_path() to change the path
> library(foreach)
> library(doFuture)
Loading required package: future
> library(doRNG)
Loading required package: rngtools
> registerDoFuture()
> plan(multisession)
> 
> 
> ### Simulation parameters
> file <- file.path("/hpc/home/user/test/", "test.stan")
> mod <- cmdstan_model(file)
> 
> set.seed(1234)
> 
>                      data_list <- list()
>                      
>                      fit <- mod$sample(
+                        data = data_list, 
+                        seed = 123, 
+                        chains = 4, 
+                        parallel_chains = 4,
+                        refresh = 500
+                      )
Running MCMC with 4 parallel chains...

Chain 1 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./test)
Chain 1 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `CXXABI_1.3.13' not found (required by /hpc/home/user/.cmdstanr/cmdstan-2.28.2/stan/lib/stan_math/lib/tbb/libtbb.so.2)
Chain 2 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./test)
Chain 2 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `CXXABI_1.3.13' not found (required by /hpc/home/user/.cmdstanr/cmdstan-2.28.2/stan/lib/stan_math/lib/tbb/libtbb.so.2)
Chain 3 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./test)
Chain 3 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `CXXABI_1.3.13' not found (required by /hpc/home/user/.cmdstanr/cmdstan-2.28.2/stan/lib/stan_math/lib/tbb/libtbb.so.2)
Chain 4 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./test)
Chain 4 ./test: /hpc/apps/gcc/9.1.0/lib64/libstdc++.so.6: version `CXXABI_1.3.13' not found (required by /hpc/home/user/.cmdstanr/cmdstan-2.28.2/stan/lib/stan_math/lib/tbb/libtbb.so.2)
Warning: Chain 1 finished unexpectedly!

Warning: Chain 2 finished unexpectedly!

Warning: Chain 3 finished unexpectedly!

Warning: Chain 4 finished unexpectedly!

Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Warning messages:
1: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
 
2: No chains finished successfully. Unable to retrieve the fit. 
>                      
>                      fit$draws() 
Error: No chains finished successfully. Unable to retrieve the draws.
Execution halted

Let’s test if you can compile any c++ while the parallelism is setup. Can you try running:

registerDoParallel(ncores)

Rcpp::sourceCpp(code='
  #include <Rcpp.h>

  // [[Rcpp::export]]
  int fibonacci(const int x) {
    if (x == 0) return(0);
    if (x == 1) return(1);
    return (fibonacci(x - 1)) + fibonacci(x - 2);
  }'
)

Ouch. Not even that. I tried to reinstall Rcpp, and I still got the same error message:

> ### Libraries
> library(cmdstanr)
This is cmdstanr version 0.4.0
- Online documentation and vignettes at mc-stan.org/cmdstanr
- CmdStan path set to: /home/dinizma/.cmdstanr/cmdstan-2.28.2
- Use set_cmdstan_path() to change the path
> library(foreach)
> library(doParallel)
Loading required package: iterators
Loading required package: parallel
> library(doRNG)
Loading required package: rngtools
> 
> ### Simulation parameters
> ncores <- 5
> n_sim <- 5
> 
> 
> 
> registerDoParallel(ncores)
> 
> Rcpp::sourceCpp(code='
+   #include <Rcpp.h>
+ 
+   // [[Rcpp::export]]
+   int fibonacci(const int x) {
+     if (x == 0) return(0);
+     if (x == 1) return(1);
+     return (fibonacci(x - 1)) + fibonacci(x - 2);
+   }'
+ )
In file included from /hpc/apps/gcc/9.1.0/include/c++/9.1.0/x86_64-pc-linux-gnu/bits/c++config.h:524,
                 from /hpc/apps/gcc/9.1.0/include/c++/9.1.0/cmath:41,
                 from /hpc/apps/R/4.1.2/lib64/R/library/Rcpp/include/Rcpp/platform/compiler.h:100,
                 from /hpc/apps/R/4.1.2/lib64/R/library/Rcpp/include/Rcpp/r/headers.h:66,
                 from /hpc/apps/R/4.1.2/lib64/R/library/Rcpp/include/RcppCommon.h:30,
                 from /hpc/apps/R/4.1.2/lib64/R/library/Rcpp/include/Rcpp.h:27,
                 from file13ad766a66624.cpp:2:
/hpc/apps/gcc/9.1.0/include/c++/9.1.0/x86_64-pc-linux-gnu/bits/os_defines.h:39:10: fatal error: features.h: No such file or directory
   39 | #include <features.h>
      |          ^~~~~~~~~~~~
compilation terminated.
make: *** [file13ad766a66624.o] Error 1
g++ -std=gnu++14 -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I"/hpc/apps/R/4.1.2/lib64/R/library/Rcpp/include" -I"/localscratch/250944.1.ragatkolab.q/RtmpssxXmA/sourceCpp-x86_64-pc-linux-gnu-1.0.7" -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -c file13ad766a66624.cpp -o file13ad766a66624.o
Error in Rcpp::sourceCpp(code = "\n  #include <Rcpp.h>\n\n  // [[Rcpp::export]]\n  int fibonacci(const int x) {\n    if (x == 0) return(0);\n    if (x == 1) return(1);\n    return (fibonacci(x - 1)) + fibonacci(x - 2);\n  }") : 
  Error 1 occurred building shared library.

WARNING: The tools required to build C++ code for R were not found.

Please install GNU development tools including a C++ compiler.

Execution halted

Also, it works if I do not run the code in batch mode.

Can you run the Rcpp code again for both the batch and non-batch, but this time add verbose=T? Then we can see if/how the compilation arguments are differing

Not in batch mode:

Generated R functions
-------------------------------------------------------

`.sourceCpp_1_DLLInfo` <- dyn.load('/tmp/RtmpDNL6of/sourceCpp-x86_64-pc-linux-gnu-1.0.7/sourcecpp_178b21d435d91/sourceCpp_4.so')

fibonacci <- Rcpp:::sourceCppFunction(function(x) {}, FALSE, `.sourceCpp_1_DLLInfo`, 'sourceCpp_1_fibonacci')

rm(`.sourceCpp_1_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /tmp/RtmpDNL6of/sourceCpp-x86_64-pc-linux-gnu-1.0.7/sourcecpp_178b21d435d91

/hpc/apps/R/4.1.2/lib64/R/bin/R CMD SHLIB --preclean -o 'sourceCpp_4.so' 'file178b25628f5ea.cpp'
g++ -std=gnu++14 -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I"/hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include" -I"/tmp/RtmpDNL6of/sourceCpp-x86_64-pc-linux-gnu-1.0.7" -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -c file178b25628f5ea.cpp -o file178b25628f5ea.o
g++ -std=gnu++14 -shared -L/hpc/apps/R/4.1.2/lib64/R/lib -L/hpc/apps/zlib/1.2.8/lib -L/hpc/apps/pcre/8.38/lib -L/hpc/apps/bzip2/1.0.6/lib -L/hpc/apps/curl/7.54.1/lib -L/hpc/apps/cairo/1.14.12/lib -o sourceCpp_4.so file178b25628f5ea.o -L/hpc/apps/R/4.1.2/lib64/R/lib -lR

In batch mode:

Generated R functions 
-------------------------------------------------------

`.sourceCpp_1_DLLInfo` <- dyn.load('/localscratch/250948.1.ragatkolab.q/RtmpDlK5Sy/sourceCpp-x86_64-pc-linux-gnu-1.0.7/sourcecpp_142bd68d660f1/sourceCpp_2.so')

fibonacci <- Rcpp:::sourceCppFunction(function(x) {}, FALSE, `.sourceCpp_1_DLLInfo`, 'sourceCpp_1_fibonacci')

rm(`.sourceCpp_1_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /localscratch/250948.1.mylab.q/RtmpDlK5Sy/sourceCpp-x86_64-pc-linux-gnu-1.0.7/sourcecpp_142bd68d660f1

/hpc/apps/R/4.1.2/lib64/R/bin/R CMD SHLIB -o 'sourceCpp_2.so' 'file142bd64ebe902.cpp' 
g++ -std=gnu++14 -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I"/hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include" -I"/localscratch/250948.1.ragatkolab.q/RtmpDlK5Sy/sourceCpp-x86_64-pc-linux-gnu-1.0.7" -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -c file142bd64ebe902.cpp -o file142bd64ebe902.o
In file included from /hpc/apps/gcc/9.1.0/include/c++/9.1.0/x86_64-pc-linux-gnu/bits/c++config.h:524,
                 from /hpc/apps/gcc/9.1.0/include/c++/9.1.0/cmath:41,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp/platform/compiler.h:100,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp/r/headers.h:66,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/RcppCommon.h:30,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp.h:27,
                 from file142bd64ebe902.cpp:2:
/hpc/apps/gcc/9.1.0/include/c++/9.1.0/x86_64-pc-linux-gnu/bits/os_defines.h:39:10: fatal error: features.h: No such file or directory
   39 | #include <features.h>
      |          ^~~~~~~~~~~~
compilation terminated.
make: *** [file142bd64ebe902.o] Error 1
Error in Rcpp::sourceCpp(code = "\n  #include <Rcpp.h>\n\n  // [[Rcpp::export]]\n  int fibonacci(const int x) {\n    if (x == 0) return(0);\n    if (x == 1) return(1);\n    return (fibonacci(x - 1)) + fibonacci(x - 2);\n  }",  : 
  Error 1 occurred building shared library.

WARNING: The tools required to build C++ code for R were not found.

Please install GNU development tools including a C++ compiler.

Execution halted

Nothing obvious there. The next step is to find where that features.h file is located and and add the directory to the include path. Try this command in R to search for it:

system('find /hpc/apps -name "features.h"')

Please see below:

> system('find /hpc/apps -name "features.h"')

/hpc/apps/gcc/7.2.0/include/c++/7.2.0/parallel/features.h
/hpc/apps/gcc/11.1.0/include/c++/11.1.0/parallel/features.h
/hpc/apps/gcc/4.9.3/include/c++/4.9.3/parallel/features.h
/hpc/apps/gcc/5.4.0.old/include/c++/5.4.0/parallel/features.h
/hpc/apps/gcc/5.4.0.old/5.4.0/include/c++/5.4.0/parallel/features.h
/hpc/apps/gcc/5.4.0/include/c++/5.4.0/parallel/features.h
/hpc/apps/gcc/8.5.0/include/c++/8.5.0/parallel/features.h
/hpc/apps/gcc/8.5.0/src/gcc-8.5.0/x86_64-pc-linux-gnu/libstdc++-v3/include/parallel/features.h
/hpc/apps/gcc/8.5.0/src/gcc-8.5.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/include/parallel/features.h
/hpc/apps/gcc/8.5.0/src/gcc-8.5.0/libstdc++-v3/include/parallel/features.h
/hpc/apps/gcc/8.5.0/src/gcc-8.5.0/fixincludes/tests/base/features.h
/hpc/apps/gcc/8.5.0/src/gcc-8.5.0/prev-x86_64-pc-linux-gnu/libstdc++-v3/include/parallel/features.h
/hpc/apps/gcc/5.2.0/include/c++/5.2.0/parallel/features.h
/hpc/apps/gcc/9.1.0/include/c++/9.1.0/parallel/features.h
/hpc/apps/miniconda3/3.8/lib/python3.8/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/miniconda3/3.8/pkgs/pycparser-2.20-pyh9f0ad1d_2/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/miniconda3/3.8/pkgs/pycparser-2.20-py_2/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/miniconda3/3.8/envs/eukdetect/lib/python3.6/site-packages/pycparser/utils/fake_libc_include/features.h
find: ‘/hpc/apps/admin’: Permission denied
find: ‘/hpc/apps/bcl2fastq2/bcl2fastq/build/libxslt-1.1.26’: Permission denied
find: ‘/hpc/apps/bcl2fastq2/bcl2fastq/build/libxml2-2.7.8’: Permission denied
/hpc/apps/anaconda/4.5.4/lib/python3.8/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/anaconda/4.5.4/pkgs/pycparser-2.20-py_2/site-packages/pycparser/utils/fake_libc_include/features.h
find: ‘/hpc/apps/anaconda/anaconda2/2019.10/pkgs/libgfortran-ng-7.3.0-hdf63c60_0bk8e_dtp’: Permission denied
/hpc/apps/anaconda/anaconda2/2019.10/pkgs/pycparser-2.20-py_2/site-packages/pycparser/utils/fake_libc_include/features.h
find: ‘/hpc/apps/samtools/0.1.19/src/samtools-0.1.19’: Permission denied
/hpc/apps/matlab/r2013a/polyspace/verifier/cxx/include/include-libc/features.h
/hpc/apps/fsl/6.0.3-no-fsleyes/fslpython/pkgs/jsoncpp-1.8.3-h3a67955_0/include/json/features.h
/hpc/apps/fsl/6.0.3-no-fsleyes/fslpython/pkgs/wxpython-4.0.6-py37h8f66242_1/include/wx-3.0/wx/features.h
/hpc/apps/fsl/6.0.3-no-fsleyes/fslpython/envs/fslpython/include/json/features.h
/hpc/apps/fsl/6.0.3-no-fsleyes/fslpython/envs/fslpython/include/wx-3.0/wx/features.h
/hpc/apps/fsl/6.0.3/fslpython/pkgs/jsoncpp-1.8.3-h3a67955_0/include/json/features.h
/hpc/apps/fsl/6.0.3/fslpython/pkgs/wxpython-4.0.6-py37h8f66242_1/include/wx-3.0/wx/features.h
/hpc/apps/fsl/6.0.3/fslpython/envs/fslpython/include/json/features.h
/hpc/apps/fsl/6.0.3/fslpython/envs/fslpython/include/wx-3.0/wx/features.h
find: ‘/hpc/apps/smartpeak/1.12.0/root’: Permission denied
/hpc/apps/smartpeak/1.12.0/usr/local/cmake-3.8.2/Utilities/cmjsoncpp/include/json/features.h
/hpc/apps/smartpeak/1.12.0/usr/include/features.h
/hpc/apps/smartpeak/1.12.0/usr/include/c++/6.4.0/parallel/features.h
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/etc/ssl/private’: Permission denied
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/var/lib/private’: Permission denied
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/var/lib/apt/lists/partial’: Permission denied
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/var/cache/private’: Permission denied
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/var/cache/ldconfig’: Permission denied
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/var/cache/apt/archives/partial’: Permission denied
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/var/log/apache2’: Permission denied
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/var/log/private’: Permission denied
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/root’: Permission denied
/hpc/apps/smartpeak/1.12.0_ubuntu/usr/include/features.h
/hpc/apps/smartpeak/1.12.0_ubuntu/usr/include/c++/9/parallel/features.h
find: ‘/hpc/apps/smartpeak/1.12.0_ubuntu/opt/contrib-build/src/kissfft’: Permission denied
find: ‘/hpc/apps/octave/3.8.2/build/SuiteSparse.4.4.1/GPUQREngine’: Permission denied
find: ‘/hpc/apps/octave/3.8.2/build/SuiteSparse.4.4.1/SPQR’: Permission denied
find: ‘/hpc/apps/octave/3.8.2/build/SuiteSparse.4.4.1/SuiteSparse_GPURuntime’: Permission denied
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/viz/common/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/security_state/core/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/ntp_snippets/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/content_settings/core/common/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/browsing_data/core/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/printing/browser/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/search_provider_logos/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/download/public/background_service/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/suggestions/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/payments/core/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/favicon/core/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/browser_watcher/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/certificate_transparency/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/safe_browsing/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/device/base/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/services/network/public/cpp/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/services/service_manager/sandbox/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/ui/message_center/public/cpp/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/third_party/blink/public/common/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/third_party/blink/renderer/platform/scheduler/child/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/third_party/angle/src/libANGLE/features.h
/hpc/apps/qt5/5.12.2/qt-everywhere-src-5.12.2/qtbase/src/3rdparty/angle/src/libANGLE/features.h
find: ‘/hpc/apps/cellranger/dodgec/take3/_STARtmp’: Permission denied
/hpc/apps/miniconda/2/pkgs/gcc_impl_linux-64-7.3.0-habb00fd_2/x86_64-conda_cos6-linux-gnu/sysroot/usr/include/features.h
/hpc/apps/miniconda/2/pkgs/gxx_impl_linux-64-7.3.0-hdf63c60_2/x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/parallel/features.h
/hpc/apps/miniconda/3/pkgs/gcc_impl_linux-64-7.3.0-habb00fd_2/x86_64-conda_cos6-linux-gnu/sysroot/usr/include/features.h
/hpc/apps/miniconda/3/pkgs/gxx_impl_linux-64-7.3.0-hdf63c60_2/x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/parallel/features.h
/hpc/apps/miniconda/3/envs/picrust2/x86_64-conda_cos6-linux-gnu/sysroot/usr/include/features.h
/hpc/apps/miniconda/3/envs/picrust2/x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/parallel/features.h
/hpc/apps/miniconda/3/envs/qiime2-2019.7/x86_64-conda_cos6-linux-gnu/sysroot/usr/include/features.h
/hpc/apps/miniconda/3/envs/qiime2-2019.7/x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/parallel/features.h
/hpc/apps/miniconda/3/envs/qiime2-2019.10/x86_64-conda_cos6-linux-gnu/sysroot/usr/include/features.h
/hpc/apps/miniconda/3/envs/qiime2-2019.10/x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/parallel/features.h
/hpc/apps/miniconda/3/envs/picrust1/x86_64-conda_cos6-linux-gnu/sysroot/usr/include/features.h
/hpc/apps/miniconda/3/envs/picrust1/x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/parallel/features.h
find: ‘/hpc/apps/perl-XML-Parser/2.44/src/XML-Parser-2.44’: Permission denied
/hpc/apps/croo/0.6.0/lib/python3.8/site-packages/pycparser/utils/fake_libc_include/features.h
find: ‘/hpc/apps/breakdancer/build/vendor/samtools’: Permission denied
/hpc/apps/caper/1.6.3/lib/python3.8/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/mmvec/1.0.5/lib/python3.7/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/mmvec/1.0.5/lib/python3.7/site-packages/tensorflow_core/include/include/json/features.h
/hpc/apps/eukdetect/1.0.1/lib/python3.6/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/eukdetect/1.0.1/pycparser-2.20-pyh9f0ad1d_2/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/eukdetect.old/1.0.1/lib/python3.8/site-packages/pycparser/utils/fake_libc_include/features.h
find: ‘/hpc/apps/eukdetect.old/1.0.1/pkgs/cryptography-3.1.1-py38h1ba5d50_0ljk_kzqn’: Permission denied
/hpc/apps/eukdetect.old/1.0.1/pkgs/pycparser-2.20-pyh9f0ad1d_2/site-packages/pycparser/utils/fake_libc_include/features.h
find: ‘/hpc/apps/eukdetect.old/1.0.1/pkgs/bleach-3.2.1-py_04baivls7’: Permission denied
/hpc/apps/eukdetect.old/1.0.1/pkgs/pycparser-2.20-py_2/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/eukdetect.old/1.0.1/envs/eukdetect/lib/python3.6/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/eukdetect.old/1.0.1/pycparser-2.20-pyh9f0ad1d_2/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/freesurfer/7.1.1/centos6/MCRv84/polyspace/verifier/cxx/include/include-libc/features.h
/hpc/apps/freesurfer/7.1.1/centos7/MCRv84/polyspace/verifier/cxx/include/include-libc/features.h
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/rh/cortex/fwhm25’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/rh/cortex/fwhm15’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/rh/cortex/fwhm19’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/rh/cortex/fwhm17’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/rh/cortex/fwhm04’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/rh/cortex/fwhm23’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/rh/cortex/fwhm06’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/rh/cortex/fwhm01’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage/lh’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/average/mult-comp-cor/fsaverage_sym’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/fsfast’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/data’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/docs’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/fsafd’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/tktools’: Permission denied
find: ‘/hpc/apps/freesurfer/5.3.0-nifti/diffusion’: Permission denied
find: ‘/hpc/apps/freesurfer/7.0.0-beta/centos6/subjects/cvs_avg35_inMNI152’: Permission denied
find: ‘/hpc/apps/freesurfer/7.0.0-beta/centos6/subjects/fsaverage6’: Permission denied
find: ‘/hpc/apps/freesurfer/7.0.0-beta/centos6/subjects/fsaverage4’: Permission denied
find: ‘/hpc/apps/freesurfer/7.0.0-beta/centos6/subjects/fsaverage3’: Permission denied
/hpc/apps/freesurfer/7.0.0-beta/centos6/MCRv84/polyspace/verifier/cxx/include/include-libc/features.h
find: ‘/hpc/apps/freesurfer/7.0.0-beta/centos7/subjects/cvs_avg35_inMNI152’: Permission denied
find: ‘/hpc/apps/freesurfer/7.0.0-beta/centos7/subjects/fsaverage6’: Permission denied
find: ‘/hpc/apps/freesurfer/7.0.0-beta/centos7/subjects/fsaverage4’: Permission denied
find: ‘/hpc/apps/freesurfer/7.0.0-beta/centos7/subjects/fsaverage3’: Permission denied
/hpc/apps/freesurfer/7.0.0-beta/centos7/MCRv84/polyspace/verifier/cxx/include/include-libc/features.h
find: ‘/hpc/apps/ants/2.1.0-devel/src/TestData’: Permission denied
find: ‘/hpc/apps/ants/2.1.0-devel/build/ANTS-build/ExternalData/TestData’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/lapack-3.1.1’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/exports’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/interface’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/test’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/benchmark’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/driver’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/kernel’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/ctest’: Permission denied
find: ‘/hpc/apps/benchmarks/hpl/gotoblas/lapack’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/CG’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/BT’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/common’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/FT’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/MPI_dummy’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/DT’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/bin’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/EP’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/sys’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/config’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/IS’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/LU’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/MG’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-MPI/SP’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-OMP’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/CG’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/BT’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/common’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/FT’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/bin’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/EP’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/sys’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/DC’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/config’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/IS’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/UA’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/LU’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/MG’: Permission denied
find: ‘/hpc/apps/benchmarks/npb/NPB3.3-SER/SP’: Permission denied
find: ‘/hpc/apps/benchmarks/pmb-io/RUN’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/lapack-3.1.1’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/exports’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/interface’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/test’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/benchmark’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/driver’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/kernel’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/ctest’: Permission denied
find: ‘/hpc/apps/benchmarks/libs/gotoblas/lapack’: Permission denied
find: ‘/hpc/apps/openms/2.4.0/src/contrib-build/src/kissfft’: Permission denied
find: ‘/hpc/apps/openms/2.0.0/src/contrib-build/src/boost_1_52_0/bin.v2/config.log’: Permission denied
find: ‘/hpc/apps/openms/2.0.0/src/contrib-build/src/boost_1_52_0/bin.v2/libs’: Permission denied
find: ‘/hpc/apps/boost/1.56/src/boost_1_56_0’: Permission denied
find: ‘/hpc/apps/boost/1.59/src/boost_1_59_0’: Permission denied
/hpc/apps/metaphlan/3.0.7/lib/python3.6/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/metaphlan/3.0.13_new/lib/python3.7/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/viz/common/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/security_state/core/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/ntp_snippets/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/content_settings/core/common/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/browsing_data/core/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/printing/browser/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/search_provider_logos/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/download/public/background_service/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/suggestions/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/payments/core/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/favicon/core/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/browser_watcher/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/certificate_transparency/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/components/safe_browsing/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/device/base/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/services/network/public/cpp/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/services/service_manager/sandbox/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/ui/message_center/public/cpp/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/third_party/blink/public/common/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/third_party/blink/renderer/platform/scheduler/child/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtwebengine/src/3rdparty/chromium/third_party/angle/src/libANGLE/features.h
/hpc/apps/supplemental/qt5/qt-everywhere-src-5.12.2/qtbase/src/3rdparty/angle/src/libANGLE/features.h
find: ‘/hpc/apps/supplemental/z3/4.7.1/src/z3rel’: Permission denied
/hpc/apps/supplemental/glibc/2.29/src/glibc-2.29/include/features.h
find: ‘/hpc/apps/bowtie/2.2.5/bin’: Permission denied
/hpc/apps/cmake/3.12.0/src/cmake-3.12.0-rc1/Utilities/cmjsoncpp/include/json/features.h
/hpc/apps/snakemake/lib/python3.8/site-packages/pycparser/utils/fake_libc_include/features.h
/hpc/apps/iris/1.0.0/lib/python3.8/site-packages/pycparser/utils/fake_libc_include/features.h

Alright, so now lets try manually adding that include path to the list that R looks in. Can you edit your Makevars file to add the lines:

CXXFLAGS += -I"/hpc/apps/gcc/9.1.0/include/c++/9.1.0/parallel/"
CXX14FLAGS += -I"/hpc/apps/gcc/9.1.0/include/c++/9.1.0/parallel/"

THen try again?

I was finally able to try your suggestion. I am not quite sure if I edited the file Makevars file correctly though. I used the function usethis::edit_r_makevars(), the file was empty, I copied the suggested lines and save it. The error changed. Please see below:

Generated extern "C" functions 
--------------------------------------------------------


#include <Rcpp.h>
#ifdef RCPP_USE_GLOBAL_ROSTREAM
Rcpp::Rostream<true>&  Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// fibonacci
int fibonacci(const int x);
RcppExport SEXP sourceCpp_1_fibonacci(SEXP xSEXP) {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< const int >::type x(xSEXP);
    rcpp_result_gen = Rcpp::wrap(fibonacci(x));
    return rcpp_result_gen;
END_RCPP
}

Generated R functions 
-------------------------------------------------------

`.sourceCpp_1_DLLInfo` <- dyn.load('/localscratch/251578.1.mylablab.q/RtmpCHyEAd/sourceCpp-x86_64-pc-linux-gnu-1.0.7/sourcecpp_1e50364a03315/sourceCpp_2.so')

fibonacci <- Rcpp:::sourceCppFunction(function(x) {}, FALSE, `.sourceCpp_1_DLLInfo`, 'sourceCpp_1_fibonacci')

rm(`.sourceCpp_1_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /localscratch/251578.1.mylablab.q/RtmpCHyEAd/sourceCpp-x86_64-pc-linux-gnu-1.0.7/sourcecpp_1e50364a03315

/hpc/apps/R/4.1.2/lib64/R/bin/R CMD SHLIB -o 'sourceCpp_2.so' 'file1e50325e7686.cpp' 
g++ -std=gnu++14 -I"/hpc/apps/R/4.1.2/lib64/R/include" -DNDEBUG   -I"/hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include" -I"/localscratch/251578.1.mylablab.q/RtmpCHyEAd/sourceCpp-x86_64-pc-linux-gnu-1.0.7" -I/hpc/apps/zlib/1.2.8/include -I/hpc/apps/pcre/8.38/include -I/hpc/apps/bzip2/1.0.6/include -I/hpc/apps/curl/7.54.1/include -I/hpc/apps/cairo/1.14.12/include   -fpic  -g -O2  -I"/hpc/apps/gcc/9.1.0/include/c++/9.1.0/parallel/" -c file1e50325e7686.cpp -o file1e50325e7686.o
In file included from /hpc/apps/gcc/11.1.0/include/c++/11.1.0/x86_64-pc-linux-gnu/bits/c++config.h:571,
                 from /hpc/apps/gcc/11.1.0/include/c++/11.1.0/cmath:41,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp/platform/compiler.h:100,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp/r/headers.h:66,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/RcppCommon.h:30,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp.h:27,
                 from file1e50325e7686.cpp:2:
/hpc/apps/gcc/11.1.0/include/c++/11.1.0/x86_64-pc-linux-gnu/bits/os_defines.h:44:19: error: missing binary operator before token "("
   44 | #if __GLIBC_PREREQ(2,15) && defined(_GNU_SOURCE)
      |                   ^
/hpc/apps/gcc/11.1.0/include/c++/11.1.0/x86_64-pc-linux-gnu/bits/os_defines.h:52:19: error: missing binary operator before token "("
   52 | #if __GLIBC_PREREQ(2, 27)
      |                   ^
In file included from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp/platform/compiler.h:100,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp/r/headers.h:66,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/RcppCommon.h:30,
                 from /hpc/home/user/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp.h:27,
                 from file1e50325e7686.cpp:2:
/hpc/apps/gcc/11.1.0/include/c++/11.1.0/cmath:45:15: fatal error: math.h: No such file or directory
   45 | #include_next <math.h>
      |               ^~~~~~~~
compilation terminated.
make: *** [file1e50325e7686.o] Error 1
Error in Rcpp::sourceCpp(code = "\n  #include <Rcpp.h>\n\n  // [[Rcpp::export]]\n  int fibonacci(const int x) {\n    if (x == 0) return(0);\n    if (x == 1) return(1);\n    return (fibonacci(x - 1)) + fibonacci(x - 2);\n  }",  : 
  Error 1 occurred building shared library.

WARNING: The tools required to build C++ code for R were not found.

Please install GNU development tools including a C++ compiler.

Execution halted