visual studio 2015 的 r 工具中的 Rcpp 编译器错误

Rcpp Compiler error in r tools for visual studio 2015

我是 RcppOpenMP 的新用户。
我写了一个代码来乘以 2 个数组。
我也想使用 omp 来检查它在大矩阵上的性能。 我在 visual studio 2015 中使用 R 工具来工作 运行 此代码
当我在 r 工具中为 visual studio 2015 编译它时,我得到了这个错误

c:/Rtools/mingw_64/bin/g++ -m64 -I"C:/PROGRA~1/MIE74D~1/ROPEN~1/R-35~1.1/include" -DNDEBUG   -I"C:/Users/amaa11/Documents/R/win-library/3.5/Rcpp/include" -I"C:/Users/amaa11/DOCUME~1/VISUAL~1/Projects/RPROJE~1/RPROJE~1"   -I"C:/swarm/workspace/External-R-3.5.1/vendor/extsoft/include"     -O2 -Wall  -mtune=core2 -c mul_mat.cpp -o mul_mat.o
mul_mat.cpp:10:4: error: stray '#' in program
/ /#pragma omp parallel for
^
mul_mat.cpp:4:1: error: expected unqualified-id before '/' token
/ / [[Rcpp::export(mul_mat)]]<br>
^
make: *** [mul_mat.o] Error 1

知道如何解决吗?

简单快捷的答案是:删除评论正斜杠之间的 space。例如,更改

/ /#pragma omp parallel for

//#pragma omp parallel for

不过我还有其他comments/tips以后可以在这里提问:

  • 请不要post你的代码图片。将其复制并粘贴到您的问题中。然后回答者可以很容易地复制和粘贴你的代码来测试它。在这种情况下,我手动重新输入了您的代码以对其进行测试(所以我可以肯定地说这个答案会解决您的错误),因为我只是无聊地坐着喝早上的咖啡,但在其他情况下,老实说可能没有花时间重新打字。
  • 如果您想要的是矩阵乘法,请选择 RcppArmadillo! As several answers to other questions on here have pointed out, and
  • 请注意,您不必将 mult 的元素初始化为 0;例如,如 Rcpp Quick Reference Guide 中所述,以 Rcpp::NumericMatrix x(n, m) 等方式构造的 NumericMatrix 会创建一个已填充 0.
  • 的 n x m 矩阵

更新

所以,为了完整起见,这是为我编译好的固定代码:

#include <Rcpp.h>
#include <omp.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix mul_mat(int n, NumericMatrix A, NumericMatrix B) {
    int i, j, k;
    NumericMatrix mult(n, n);
    for ( i = 0; i < n; ++i ) {
        //#pragma omp parallel for
        for ( j = 0; j < n; ++j ) {
            mult(i, j) = 0;
        }
    }
    for ( i = 0; i < n; ++i ) {
        for ( j = 0; j < n; ++j ) {
            for ( k = 0; k < n; ++k ) {
                mult(i, j) += A(i, k) * B(k, j);
            }
        }
    }
    return mult;
}

我们可以看到它将函数导出到 R(添加这部分是为了响应现在已删除的关于将函数导出到 R 的正确方法的评论):

> Rcpp::sourceCpp("so-answer.cpp")
> ls()
[1] "mul_mat"

您最初在 #pragma omp parallel for 之前有一个(n 个错误输入的)注释分隔符;如果您真的想使用它,请删除注释分隔符并添加 // [[Rcpp::plugins(openmp)]];这在 Rcpp Gallery post:

中(在其他地方)进行了讨论

Additionally, we can make use of the OpenMP library to use multiple cores. For the OpenMP implementation, we need to enable OpenMP support. One way of doing so is by adding the required compiler and linker flags as follows:

Sys.setenv("PKG_CXXFLAGS"="-fopenmp")
Sys.setenv("PKG_LIBS"="-fopenmp")

Rcpp version 0.10.5 and later will also provide a plugin to set these variables for us:

// [[Rcpp::plugins(openmp)]]

所以,尝试:

#include <Rcpp.h>
#include <omp.h>

// [[Rcpp::plugins(openmp)]]

using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix mul_mat(int n, NumericMatrix A, NumericMatrix B) {
    int i, j, k;
    NumericMatrix mult(n, n);
    for ( i = 0; i < n; ++i ) {
        omp_set_num_threads(2); // For example
        #pragma omp parallel for
        for ( j = 0; j < n; ++j ) {
            mult(i, j) = 0;
        }
    }
    for ( i = 0; i < n; ++i ) {
        for ( j = 0; j < n; ++j ) {
            for ( k = 0; k < n; ++k ) {
                mult(i, j) += A(i, k) * B(k, j);
            }
        }
    }
    return mult;
}

在我的机器上也编译得很好。