代码之家  ›  专栏  ›  技术社区  ›  Kamat

无法在Windows上使用RppParallel进行编译

  •  0
  • Kamat  · 技术社区  · 1 年前

    我已经安装了Rcpp、RcppAllegal,并测试了用.cpp文件编写的简单功能。

    #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;
    }
    

    然后

    sourceCpp("UntitledCPP.cpp")
    

    它给了我:

    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.
    

    虽然我指定了RcppAllegal的完整路径,但它却询问了tinythread。

    我有R和Rtools的新版本。我的会话信息:

    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      
    

    我也重新安装了所有的rcpp包。 你有什么想法吗?

    亲切问候,

    马特乌斯

    1 回复  |  直到 1 年前
        1
  •  1
  •   Ada Lovelace    1 年前

    你没说 sourceCpp() 你的代码依赖于 RcppParallel 因此,由于不包含相应的标头,它无法编译。(如果不链接,则无法构建。)

    修复很简单,例如解释 in this Rcpp Gallery post about RcppParallel :您需要添加一行

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

    到您的源文件,之后它应该可以工作(如果没有其他问题)。

    你可能想尝试其他更简单的案例,比如Rcpp-Galltery帖子中的案例,以确保你实际上可以构建 Rcpp并行 .

    推荐文章