使用 Rcpp 进行高效的矩阵子集化
Efficient Matrix subsetting with Rcpp
我正在尝试找到一种有效的方法来为 非连续 行和列集使用 Rcpp 对矩阵进行子集化:
m <- matrix(1:20000000, nrow=5000)
rows <- sample(1:5000, 100)
cols <- sample(1:4000, 100)
在 R 中,可以使用 rows
和 cols
向量直接对矩阵进行子集化:
matrix_subsetting <- function(m, rows, cols){
return(m[rows, cols])
}
m[rows, cols]
# or
matrix_subsetting(m, rows, cols)
最快的 Rcpp 方法,到目前为止我能找到的是:
Rcpp::cppFunction("
NumericMatrix cpp_matrix_subsetting(NumericMatrix m, NumericVector rows, NumericVector cols){
int rl = rows.length();
int cl = cols.length();
NumericMatrix out(rl, cl);
for (int i=0; i<cl; i++){
NumericMatrix::Column org_c = m(_, cols[i]-1);
NumericMatrix::Column new_c = out(_, i);
for (int j=0; j<rl; j++){
new_c[j] = org_c[rows[j]-1];
}
}
return(out);
}
")
但相比之下,Rcpp 版本明显较慢:
> microbenchmark::microbenchmark(matrix_subsetting(m, rows, cols), cpp_matrix_subsetting(m, rows, cols), times=500)
Unit: microseconds
expr min lq mean median uq max neval
matrix_subsetting(m, rows, cols) 23.269 90.127 107.8273 130.347 135.3285 605.235 500
cpp_matrix_subsetting(m, rows, cols) 69191.784 75254.277 88484.9328 90477.448 95611.9090 178903.973 500
任何想法,至少获得与 Rcpp 相当的速度?
我已经尝试了 RcppArmadillo
arma::mat::submat
功能,但它比我的版本慢。
解法:
使用 IntegerMatrix
而不是 NumericMatrix
实现 cpp_matrix_subsetting
函数。
新基准:
> microbenchmark::microbenchmark(matrix_subsetting(m, rows, cols), cpp_matrix_subsetting(m, rows, cols), times=1e4)
Unit: microseconds
expr min lq mean median uq max neval
matrix_subsetting(m, rows, cols) 41.110 60.261 66.88845 61.730 63.8900 14723.52 10000
cpp_matrix_subsetting(m, rows, cols) 43.703 61.936 71.56733 63.362 65.8445 27314.11 10000
这是因为您有一个 integer
类型的矩阵 m
(不是 NumericMatrix
所期望的 double
),所以这会复制整个矩阵(这需要很多时间)。
例如,尝试使用 m <- matrix(1:20000000 + 0, nrow=5000)
。
我正在尝试找到一种有效的方法来为 非连续 行和列集使用 Rcpp 对矩阵进行子集化:
m <- matrix(1:20000000, nrow=5000)
rows <- sample(1:5000, 100)
cols <- sample(1:4000, 100)
在 R 中,可以使用 rows
和 cols
向量直接对矩阵进行子集化:
matrix_subsetting <- function(m, rows, cols){
return(m[rows, cols])
}
m[rows, cols]
# or
matrix_subsetting(m, rows, cols)
最快的 Rcpp 方法,到目前为止我能找到的是:
Rcpp::cppFunction("
NumericMatrix cpp_matrix_subsetting(NumericMatrix m, NumericVector rows, NumericVector cols){
int rl = rows.length();
int cl = cols.length();
NumericMatrix out(rl, cl);
for (int i=0; i<cl; i++){
NumericMatrix::Column org_c = m(_, cols[i]-1);
NumericMatrix::Column new_c = out(_, i);
for (int j=0; j<rl; j++){
new_c[j] = org_c[rows[j]-1];
}
}
return(out);
}
")
但相比之下,Rcpp 版本明显较慢:
> microbenchmark::microbenchmark(matrix_subsetting(m, rows, cols), cpp_matrix_subsetting(m, rows, cols), times=500)
Unit: microseconds
expr min lq mean median uq max neval
matrix_subsetting(m, rows, cols) 23.269 90.127 107.8273 130.347 135.3285 605.235 500
cpp_matrix_subsetting(m, rows, cols) 69191.784 75254.277 88484.9328 90477.448 95611.9090 178903.973 500
任何想法,至少获得与 Rcpp 相当的速度?
我已经尝试了 RcppArmadillo
arma::mat::submat
功能,但它比我的版本慢。
解法:
使用 IntegerMatrix
而不是 NumericMatrix
实现 cpp_matrix_subsetting
函数。
新基准:
> microbenchmark::microbenchmark(matrix_subsetting(m, rows, cols), cpp_matrix_subsetting(m, rows, cols), times=1e4)
Unit: microseconds
expr min lq mean median uq max neval
matrix_subsetting(m, rows, cols) 41.110 60.261 66.88845 61.730 63.8900 14723.52 10000
cpp_matrix_subsetting(m, rows, cols) 43.703 61.936 71.56733 63.362 65.8445 27314.11 10000
这是因为您有一个 integer
类型的矩阵 m
(不是 NumericMatrix
所期望的 double
),所以这会复制整个矩阵(这需要很多时间)。
例如,尝试使用 m <- matrix(1:20000000 + 0, nrow=5000)
。