首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用RcppParallel实现堆栈不平衡

用RcppParallel实现堆栈不平衡
EN

Stack Overflow用户
提问于 2014-12-17 10:57:43
回答 1查看 829关注 0票数 3

我编写了以下代码来训练自己如何使用RcppParallel。这只是一个玩具的例子。

代码语言:javascript
复制
// [[Rcpp::depends(RcppParallel)]]
#include <Rcpp.h>
#include <RcppParallel.h>
#include <iostream>
using namespace Rcpp;
using namespace RcppParallel;


struct Lapin : public Worker {
  // input pars
  const NumericVector input;
  const size_t dim;

  // outputs a matrix
  NumericMatrix output;

  // two constructors
  Lapin(const NumericVector input, const int dim) : input(input), dim(dim), output(NumericMatrix(dim,dim)) {}

  Lapin(const Lapin & jeannot, Split) : input(jeannot.input), dim(jeannot.dim), output(NumericMatrix(dim,dim)) {}

  // the working operator
  void operator()(size_t begin, size_t end) {
    for(size_t k = begin; k < end; k++) {
      for(size_t i = 0; i < dim; i++) {
        for(size_t j = 0; j < dim; j++) {
          output(i,j) += input(k)+i+j;
        }
      }
    }
  }

  // the join
  void join(const Lapin & peter) {
    output += peter.output;
  }
};

// [[Rcpp::export]]
NumericMatrix f(NumericVector A, size_t dim) {
  Lapin groumf(A, dim);
  parallelReduce(0, A.length(), groumf);
  return groumf.output;
}

以下是R中发生的情况,在对其进行源化之后:

代码语言:javascript
复制
> f(rep(1,1100), 5)
     [,1] [,2] [,3] [,4] [,5]
[1,] 1100 2200 3300 4400 5500
[2,] 2200 3300 4400 5500 6600
[3,] 3300 4400 5500 6600 7700
[4,] 4400 5500 6600 7700 8800
[5,] 5500 6600 7700 8800 9900
> sourceCpp("parallel-matrix-reduce.cpp")
> f(rep(1,1100), 5)
Warning: stack imbalance in '.Call', 6 then 11
     [,1] [,2] [,3] [,4] [,5]
[1,] 1100 2200 3300 4400 5500
[2,] 2200 3300 4400 5500 6600
[3,] 3300 4400 5500 6600 7700
[4,] 4400 5500 6600 7700 8800
[5,] 5500 6600 7700 8800 9900

请注意,这种行为是静态的:有时,我根本没有任何警告,有时是在第一次运行时.我想我的会话信息在这里是有用的:

代码语言:javascript
复制
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] Rcpp_0.11.3

loaded via a namespace (and not attached):
[1] RcppParallel_4.3.3 tools_3.1.2       

我预先感谢大家的答复和评论。

正如Dirk在下面解释的那样,编辑,这是由于在工作人员中使用了R类型,这会混淆垃圾收集器。我用Armadillo矩阵来解决这个问题(我被RMatrix搞糊涂了)。以下是修正后的代码:

代码语言:javascript
复制
// [[Rcpp::depends(RcppParallel)]]
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include <iostream>
using namespace Rcpp;
using namespace RcppParallel;


struct Lapin : public Worker {
  // input pars
  const arma::vec input;
  const size_t dim;

  // outputs a matrix
  arma::mat output;

  // two constructors
  Lapin(const arma::vec input, const int dim) : input(input), dim(dim), output(arma::mat(dim,dim)) {
    output.zeros();
  }

  Lapin(const Lapin & jeannot, Split) : input(jeannot.input), dim(jeannot.dim), output(arma::mat(dim,dim)) {
    output.zeros();
  }

  // the working operator
  void operator()(size_t begin, size_t end) {
    for(size_t k = begin; k < end; k++) { 
      for(size_t i = 0; i < dim; i++) {
        for(size_t j = 0; j < dim; j++) {
          output(i,j) += input(k)+i+j;
        } 
      }
    }
  }
  // the join
  void join(const Lapin & peter) {
    output += peter.output;
  }
};

// [[Rcpp::export]]
arma::mat f(arma::vec & A, size_t dim) {
  Lapin groumf(A, dim);
  parallelReduce(0, A.size(), groumf);
  return groumf.output;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-18 12:40:15

更仔细地看一看来自Rcpp画廊的示例,例如并行距离。

它不使用NumericMatrix,而是使用RMatrix<double>。我在这里也会这样做,并且一般都主张在运行并行段时不要依赖与R类型的接触。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27523979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档