rrcpprcppparallel

Cannot compile with RcppParallel on Windows


I have installed Rcpp, RcppParallel and I was testing simple function, written in .cpp file.

#include <RcppParallel.h>
#include <Rcpp.h>
#include <vector>

using namespace RcppParallel;
using namespace Rcpp;

struct Sum : public Worker {
  // Source vector
  const std::vector<int> &input;

  // Accumulated value
  int value;

  // Constructor
  Sum(const std::vector<int> &input) : input(input), value(0) {}

  // Split constructor
  Sum(const Sum &sum, Split) : input(sum.input), value(0) {}

  // Accumulate values in the given range
  void operator()(std::size_t begin, std::size_t end) {
    value += std::accumulate(input.begin() + begin, input.begin() + end, 0);
  }

  // Join the accumulated values
  void join(const Sum &rhs) {
    value += rhs.value;
  }
};

// [[Rcpp::export]]
int parallelSum(const std::vector<int> &x) {
  Sum sum(x);

  // Call parallelReduce to start the work
  parallelReduce(0, x.size(), sum);

  return sum.value;
}

then:

sourceCpp("UntitledCPP.cpp")

It gave me:

sourceCpp("UntitledCPP.cpp")
using C++ compiler: 'G__~1.EXE (GCC) 13.2.0'
g++ -std=gnu++17  -I"C:/PROGRA~1/R/R-44~1.1/include" -DNDEBUG   -I"C:/Users/callimae/AppData/Local/R/win-library/4.3/Rcpp/include" -I"C:/Users/callimae/Documents/Projects/NewEM_conjugate_base"   -I"C:/rtools44/x86_64-w64-mingw32.static.posix/include"     -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c UntitledCPP.cpp -o UntitledCPP.o
UntitledCPP.cpp:1:10: fatal error: RcppParallel.h: No such file or directory
    1 | #include <RcppParallel.h>
      |          ^~~~~~~~~~~~~~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-44~1.1/etc/x64/Makeconf:296: UntitledCPP.o] Error 1
Error in sourceCpp("UntitledCPP.cpp") : 
  Error 1 occurred building shared library.

While i specified full path to RcppParallel, it asked about tinythread instead.

I have new version of R and Rtools. My session info:

sessionInfo()
R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default


locale:
[1] LC_COLLATE=Polish_Poland.utf8  LC_CTYPE=Polish_Poland.utf8    LC_MONETARY=Polish_Poland.utf8 LC_NUMERIC=C                  
[5] LC_TIME=Polish_Poland.utf8    

time zone: Europe/Warsaw
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RcppArmadillo_14.0.0-1 RcppParallel_5.1.8     Rcpp_1.0.12           

loaded via a namespace (and not attached):
 [1] compiler_4.4.1    magrittr_2.0.3    cli_3.6.3         tools_4.4.1       fs_1.6.4          glue_1.7.0        rstudioapi_0.16.0 vctrs_0.6.5      
 [9] usethis_2.2.3     lifecycle_1.0.4   rlang_1.1.4       purrr_1.0.2      

I have reinstalled all rcpp packages as well. Do you have any ideas?

Kind regards,

Mateusz


Solution

  • You are not telling sourceCpp() that your code has a dependency on RcppParallel and so it fails to compile by not including the corresponding headers. (And would then fail to build by not linking.)

    The fix is straightforward and for example explained in this Rcpp Gallery post about RcppParallel: you need to add one line

     // [[Rcpp::depends(RcppParallel)]]
    

    to your source file after which it should work (if it doesn't have other issues).

    You may want to try other, simpler cases such as the one in the Rcpp Galltery post to make sure you can in fact build against RcppParallel.