RcppParallel:代码不并行运行

时间:2015-11-07 00:32:40

标签: c++ r concurrency rcpp

我有这段代码,我基本上写的是基于:http://gallery.rcpp.org/articles/parallel-distance-matrix/

然而,我的修改打破了并发性。代码编译并给出正确的结果。 (基本上是两个矩阵之间的查找,顺便说一句,如果有人有更快的代码,我会很感激它)

我只想了解什么是打破它。

#include <Rcpp.h>
using namespace Rcpp;

#include <cmath>
#include <algorithm>

// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
using namespace RcppParallel;

struct JsDistance : public Worker {

  const RMatrix<double> mat;
  const RMatrix<double> mat2;

  RMatrix<double> rmat;
  JsDistance(const NumericMatrix mat,const NumericMatrix mat2, NumericMatrix rmat)
    : mat(mat), mat2(mat2), rmat(rmat) {}

  void operator()(std::size_t begin, std::size_t end) {
    for (std::size_t i = begin; i < end; i++) {

      RMatrix<double>::Column col1 = mat.column(i);
      RMatrix<double>::Column col2 = mat2.column(i);

      for (std::size_t j = 0; j < mat.nrow(); j++) {
        rmat(j,i) = col2[col1[j]];
      }

    }
  }
};


// [[Rcpp::export]]
NumericMatrix DIST(NumericMatrix mat,NumericMatrix mat2) {

  NumericMatrix rmat(mat.nrow(), mat.ncol());

  JsDistance jsDistance(mat, mat2, rmat);

  parallelFor(0, mat.ncol(), jsDistance);

  return rmat;
}

编辑: 会话信息:

> sessionInfo()
  R version 3.2.2 (2015-08-14)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows 8 x64 (build 9200)

  locale:
  [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

  other attached packages:
  [1] foreach_1.4.3       RcppParallel_4.3.14 Rcpp_0.12.1         checkpoint_0.3.15  

loaded via a namespace (and not attached):
  [1] tools_3.2.2      codetools_0.2-14 iterators_1.0.8 

0 个答案:

没有答案