将大矩阵传递给 RcppArmadillo 函数而不创建副本(高级构造函数) [英] Passing large matrices to RcppArmadillo function without creating copy (advanced constructors)

查看:56
本文介绍了将大矩阵传递给 RcppArmadillo 函数而不创建副本(高级构造函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个大矩阵传递给 RcppArmadillo 函数(大约 30,000*30,000),并且感觉仅此传递就耗尽了所有的性能提升.这个问题也在这里 建议使用带有 copy_aux_mem = false 参数的高级构造函数的解决方案.这似乎也是一个很好的解决方案,因为我只需要从矩阵中读取行而无需更改任何内容.不过,我在正确实施解决方案时遇到了问题.这可能只是一个简单的语法问题.

I want to pass a large matrix to a RcppArmadillo function (about 30,000*30,000) and have the feeling that this passing alone eats up all the performance gains. The question was also raised here with the suggested to solution to use advanced constructors with the copy_aux_mem = false argument. This seems to be a good solution also because I only need to read rows from the matrix without changing anything. I am having problems implementing the solution correctly though. This is probably just a simply syntax question.

这是我当前的函数调用设置(当然是简化的):

Here is my current set-up of the function call (simplified, of course):

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::vec test(arma::mat M) {
    return(M.row(0))
}

这对于大矩阵 M 非常慢(例如 M=matrix(rnorm(30000*30000), nrow=30000, ncol=30000).所以我想使用高级构造函数如此处所述.语法是mat(aux_mem*, n_rows, n_cols,copy_aux_mem = true,strict = true)copy_aux_mem 应该设置为 false 到'pass-by-reference'.我只是不确定中的语法函数定义.我如何在 arma::vec test(arma::mat M) { 中使用它?

this is pretty slow with large a matrix M (e.g. M=matrix(rnorm(30000*30000), nrow=30000, ncol=30000). So I would like to use an advanced constructor as documented here. The syntax is mat(aux_mem*, n_rows, n_cols, copy_aux_mem = true, strict = true) and copy_aux_mem should be set to false to 'pass-by-reference'. I just not sure about the syntax in the function definition. How do I use this in arma::vec test(arma::mat M) {?

推荐答案

这已在 Rcpp 邮件列表中进行了广泛讨论.请参阅此线程.RcppArmadillo 中已经实现的解决方案是通过引用传递arma::mat.在内部,这将为您调用高级构造函数.

This has been discussed extensively in the Rcpp mailing list. See this thread. The solution that has been implemented in RcppArmadillo is to pass the arma::mat by reference. Internally this will call the advanced constructor for you.

所以在这个版本中,你会做这样的事情:

So with this version, you would do something like this:

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::vec test(const arma::mat& M) {
    // do whatever with M
    ...
}

R 矩阵中的数据不是复制而是借用的.线程中的更多详细信息.

And the data from the R matrix is not copied but rather borrowed. More details in the thread.

以下是比较复制或通过引用传递所需时间的一些基准:

Here are some benchmarks comparing the time it takes to copy or pass by reference:

                 expr      min        lq    median        uq      max neval
    arma_test_value(m) 3540.369 3554.4105 3572.3305 3592.5795 4168.671   100
      arma_test_ref(m)    4.046    4.3205    4.7770   15.5855   16.671   100
arma_test_const_ref(m)    3.994    4.3660    5.5125   15.7355   34.874   100

具有这些功能:

#include <RcppArmadillo.h>
using namespace Rcpp ;

// [[Rcpp::depends("RcppArmadillo")]]

// [[Rcpp::export]]
void arma_test_value( arma::mat x){}

// [[Rcpp::export]]
void arma_test_ref( arma::mat& x){}

// [[Rcpp::export]]
void arma_test_const_ref( const arma::mat& x){}

这篇关于将大矩阵传递给 RcppArmadillo 函数而不创建副本(高级构造函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆