将列表用作C ++代码的输入,并使用Rcpp进行调用(列表输入非常慢)

问题描述 投票:0回答:1

我正在尝试使用列表(R对象)作为C ++函数的输入,后来使用R中的Rcpp对其进行调用。此列表包含大量矩阵。我提供的代码是一个玩具示例。我已经编写了一个非常复杂的代码,但是效率很低。在下面的代码中,我想知道是否存在从列表中提取矩阵的有效方法。

以下是我尝试过的代码。它可以工作,但也告诉我下标的值不是数组,指针或向量。我正在使用R studio编写此代码。当我编译代码时,它可以工作,但是当我将鼠标光标放在编辑器中时,我也看到红叉显示“下标的值不是数组,指针或向量”。

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

// [[Rcpp::export]]
Rcpp::List tt( 
    Rcpp::List&  ht,
    int n){
  List A(n);
  for(int i=0;i<n;++i){
    arma::mat htt = ht[i];// this is where I see subscripted value is not an array, pointer or vector
    arma::mat x = htt * htt.t();
    A[i] = x;//this is where I see subscripted value is not an array, pointer or vector
  }
  List res(1);
  res[0] = A;//this is where I see subscripted value is not an array, pointer or vector
  return(res);
}

同样,这是一个玩具示例,可以很容易地在R中完成。我想对如何有效地完成操作有一些了解。假设我希望将列表的每个矩阵乘以它的转置。任何帮助,将不胜感激?以下是我的实际问题。

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

// [[Rcpp::export]]
List se_4a( 
     Rcpp::List& ht,
     const int& n, 
    const int& p,
    const int& pc,
    NumericMatrix& S1byS0_,
    NumericMatrix& S1byS0c_,
     NumericMatrix& za_,
     NumericMatrix& zb_,
     NumericMatrix& wd_,
     NumericMatrix& I_,
     NumericVector& S0c_,
     NumericVector& gammah_){

  List A(ht.length());

    arma::mat S1byS0hat(S1byS0_.begin(),S1byS0_.nrow(),S1byS0_.ncol(),false);
    arma::mat S1byS0hatc(S1byS0c_.begin(),S1byS0c_.nrow(),S1byS0c_.ncol(),false); 
    arma::mat z(za_.begin(),za_.nrow(),za_.ncol(),false);
    arma::mat zc(zb_.begin(),zb_.nrow(),zb_.ncol(),false);
    arma::mat wdM(wd_.begin(),wd_.nrow(),wd_.ncol(),false);
    arma::mat Ic(I_.begin(),I_.nrow(),I_.ncol(),false);
    arma::vec S0hatc(S0c_.begin(),S0c_.size(),false);
    arma::vec gammahat(gammah_.begin(),gammah_.size(),false);

   Rcpp::List q1hat(n);
   Rcpp::List q2hat(n);



  for(int i=0; i < n;++i){
    arma::mat q11hat(p,n);
    q11hat.zeros();
    arma::mat q21hat(p,n);
    q21hat.zeros();

    // arma::mat q11hat(q11hata.begin(),q11hata.nrow(),q11hata.ncol(),false);
    // arma::mat q21hat(q21hata.begin(),q21hata.nrow(),q21hata.ncol(),false);
    for(int u = 0;u < n; ++u){
      // arma::mat q(qa.begin(),qa.nrow(),qa.ncol(),false);
      // arma::mat qq(qqa.begin(),qqa.nrow(),qqa.ncol(),false);

      arma::mat q(p,1);
      q.zeros();
      arma::mat qq(p,1);
      qq.zeros();

      for(int j=0;j <n;++j){
        if(j < n){
          for(int k = j; k <n;++k){
               //NumericMatrix httt = as<NumericMatrix>(ht[k]);
               Rcpp::NumericMatrix htt_R = ht[k];
                arma::mat htt(htt_R.begin(), htt_R.rows(), htt_R.cols(), false, true);
                //arma::vec y = httt(_,j);
                arma::colvec y = htt.col(j);
               arma::rowvec yy = y.t() * Ic * (zc.row(i).t() - S1byS0hatc.col(u));
             double zz = yy(0,0);
              q += (z.row(j).t() - S1byS0hat.col(k)) *
                zz * wdM(j,k);
            if (u <= k){
              qq += (z.row(j).t() - S1byS0hat.col(k)) *
                exp(arma::as_scalar(gammahat.t()*zc.row(j).t()))*wdM(j,k) / (S0hatc(u)/n);
            }
          }
        }
      }
      q11hat.cols(u,u) = -1 * q;
      q21hat.cols(u,u) = -1 * qq;
    }
     q1hat[i] = q11hat/n;
     q2hat[i] = q21hat/n;
  }
    return List::create(Named("A")=q1hat,
                        Named("B")=q2hat);


}

现在,让我们从R调用上面的函数。

#Calling from R
ht <- list()
for(i in 1 : 100){
  ht[[i]] <-matrix(runif(10*100), 10, 100)
}
n <- 100
p <- 10
pc <- 10
S1byS0 <- matrix(rnorm(10*100),10,100)
S1byS0c <- S1byS0
za <- matrix(rnorm(100*10),100,10)
zb <- za
wd <- matrix(rnorm(100*100),100,100)
I <- matrix(rnorm(100),10,10)
S0c <- c(rnorm(100))
gammah <- matrix(rnorm(10),1,10)

#Calling se_4a function

pp=bench::mark(se_4a(ht,n,p,pc, S1byS0,S1byS0c,za,zb,wd,I,S0c,gammah))

bench::mark(se_4a(ht,n,p,pc, S1byS0,S1byS0c,za,zb,wd,I,S0c,gammah))

# A tibble: 1 x 13
  expression                                                         min median `itr/sec`
  <bch:expr>                                                       <bch> <bch:>     <dbl>
1 se_4a(ht, n, p, pc, S1byS0, S1byS0c, za, zb, wd, I, S0c, gammah) 20.9s  20.9s    0.0479
# … with 9 more variables: mem_alloc <bch:byt>, `gc/sec` <dbl>, n_itr <int>, n_gc <dbl>,
#   total_time <bch:tm>, result <list>, memory <list>, time <list>, gc <list>
Warning message:
Some expressions had a GC in every iteration; so filtering is disabled. 

使用矩阵作为参数而不是列表。

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

// [[Rcpp::export]]
List se_4c( 
    const arma::mat& ht,
    const int& n, 
    const int& p,
    const int& pc,
    const arma::mat& S1byS0hat,
    const arma::mat& S1byS0hatc,
    const arma::mat& z,
    const arma::mat& zc,
    const arma::mat& wdM,
    const arma::mat& Ic,
    const arma::vec& S0hatc,
    const arma::vec& gammahat){

  Rcpp::List q1hat(n);
  Rcpp::List q2hat(n);

  arma::mat q11hat(p,n);
  arma::mat q21hat(p,n);
  arma::mat q(p,1);
  arma::mat qq(p,1);

   std::vector<arma::mat> htt_vec(n);
   for(int i = 0; i < n; ++i) {
  //   Rcpp::NumericMatrix htt_R = ht[i];
  //   arma::mat htt(htt_R.begin(), htt_R.rows(), htt_R.cols(), false, true);
  //   htt_vec[i] = htt;
  htt_vec[i] = ht.rows(i,i+(p-1));
   }



  for(int i=0; i < n;++i){
    for(int u = 0;u < n; ++u){

      q.zeros();
      qq.zeros();

      arma::mat bar = Ic * (zc.row(i).t() - S1byS0hatc.col(u));

      for(int j=0;j <n;++j){
        if(j < n){
          double foo = exp(arma::as_scalar(gammahat.t()*zc.row(j).t())) / (S0hatc(u)/n);
          for(int k = j; k <n;++k){
            //arma::mat htt_vec = ht.rows(k,k+(p-1));
            arma::colvec y = htt_vec[k].col(j);
            arma::rowvec yy = y.t() * bar;
            double zz = yy(0,0);
            arma::mat baz = (z.row(j).t() - S1byS0hat.col(k)) * wdM(j,k);
            q +=  zz * baz;
            if (u <= k){
              qq += foo * baz;
            }
          }
        }
      }
      q11hat.col(u) = -q;
      q21hat.col(u) = -qq;
    }
    q1hat[i] = q11hat/n;
    q2hat[i] = q21hat/n;
  }
  return List::create(Named("A")=q1hat,
                      Named("B")=q2hat);


}

以下是简单的代码,大约需要4秒钟。期望更快。

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

// [[Rcpp::export]]
List test( 
    List& ht,
    const int& n,
    const int& p){

  Rcpp::List q1hat(n);
  Rcpp::List q2hat(n);



  for(int i=0; i < n;++i){
    arma::mat q11hat(p,n);
    q11hat.zeros();
    arma::mat q21hat(p,n);
    q21hat.zeros();
    for(int u = 0;u < n; ++u){
      arma::mat q(p,1);
      q.zeros();
      arma::mat qq(p,1);
      qq.zeros();

      for(int j=0;j <n;++j){
        if(j < ht.length()){
          for(int k = j; k <n;++k){
            Rcpp::NumericMatrix htt_R = ht[k];
            arma::mat htt(htt_R.begin(), htt_R.rows(), htt_R.cols(), false, true);

          }
        }
      }
      q11hat.cols(u,u) = -1 * q;
      q21hat.cols(u,u) = -1 * qq;
    }
    q1hat[i] = q11hat/n;
    q2hat[i] = q21hat/n;
  }
  return List::create(Named("A")=q1hat,
                      Named("B")=q2hat);
}

我试图实现的公式如下:enter image description here

c++ rstudio rcpp
1个回答
3
投票

几条评论:

  • RcppArmadillo可以直接将Armadillo的高级构造函数用于函数参数。我不确定确切的条件,但是const引用会像这样处理。

  • Rcpp::NumericMatrix中提取Rcpp::List似乎很昂贵。因此,一开始就处理一次矩阵是有意义的。

  • 计算的几个部分,独立于内部循环变量。在循环之前一次计算它们是有意义的(请参见下面的foobarbaz)。

  • 我也做了一些清理。

替代功能:

// [[Rcpp::export]]
List se_4b( 
        Rcpp::List& ht,
        const int& n, 
        const int& p,
        const int& pc,
        const arma::mat& S1byS0hat,
        const arma::mat& S1byS0hatc,
        const arma::mat& z,
        const arma::mat& zc,
        const arma::mat& wdM,
        const arma::mat& Ic,
        const arma::vec& S0hatc,
        const arma::vec& gammahat){

    Rcpp::List q1hat(n);
    Rcpp::List q2hat(n);

    arma::mat q11hat(p,n);
    arma::mat q21hat(p,n);
    arma::mat q(p,1);
    arma::mat qq(p,1);

    std::vector<arma::mat> htt_vec(n);
    for(int i = 0; i < n; ++i) {
        Rcpp::NumericMatrix htt_R = ht[i];
        arma::mat htt(htt_R.begin(), htt_R.rows(), htt_R.cols(), false, true);
        htt_vec[i] = htt;        
    }

    for(int i=0; i < n;++i){
        for(int u = 0;u < n; ++u){

            q.zeros();
            qq.zeros();

            arma::mat bar = Ic * (zc.row(i).t() - S1byS0hatc.col(u));

            for(int j=0;j <n;++j){
                if(j < n){
                    double foo = exp(arma::as_scalar(gammahat.t()*zc.row(j).t())) / (S0hatc(u)/n);
                    for(int k = j; k <n;++k){
                        arma::colvec y = htt_vec[k].col(j);
                        arma::rowvec yy = y.t() * bar;
                        double zz = yy(0,0);
                        arma::mat baz = (z.row(j).t() - S1byS0hat.col(k)) * wdM(j,k);
                        q +=  zz * baz;
                        if (u <= k){
                            qq += foo * baz;
                        }
                    }
                }
            }
            q11hat.col(u) = -q;
            q21hat.col(u) = -qq;
        }
        q1hat[i] = q11hat/n;
        q2hat[i] = q21hat/n;
    }
    return List::create(Named("A")=q1hat,
                        Named("B")=q2hat);


}

基准测试结果与您的功能进行比较:

> bench::mark(se_4a = se_4a(ht,n,p,pc, S1byS0,S1byS0c,za,zb,wd,I,S0c,gammah),
+             se_4b = se_4b(ht,n,p,pc, S1by .... [TRUNCATED] 
# A tibble: 2 x 13
  expression    min median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result
  <bch:expr> <bch:> <bch:>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>
1 se_4a      21.97s 21.97s    0.0455    1.54MB     7.97     1   175     21.97s <list…
2 se_4b       4.84s  4.84s    0.206     1.54MB     0        1     0      4.84s <list…
# … with 3 more variables: memory <list>, time <list>, gc <list>
© www.soinside.com 2019 - 2024. All rights reserved.