RcppArmadillo:内存使用问题

mas*_*oon 5 c++ memory r rcpp

我已经开始使用Rcpp了.我很喜欢.我对编程很新.我有一个关于内存使用的问题.以下是一个可重现的问题:

library(RcppArmadillo)
library(inline)
code <- "
  Rcpp::NumericVector input_(input);
  arma::cube disturb(input_.begin(), 2, 2, 50000000, false);
  return wrap(2);
"
Test <- cxxfunction(signature(input = "numeric"), plugin = "RcppArmadillo", body = code)
input <- array(rnorm(2 * 2 * 50000000), dim = c(2, 2, 50000000))
Test(input)
Run Code Online (Sandbox Code Playgroud)

我的理解是,在上面的问题中,唯一的内存使用是当我将数组分配给R中的变量输入时.所以我应该只使用大约1.6 gb(2*2*50*8 = 1600).当我去Rcpp时,我使用SEXP对象初始化变量input_,这是一个指针.所以这不应该使用任何额外的内存.然后当我初始化变量扰乱时,我也使用指针并设置copy_aux = FALSE.所以我不应该使用任何记忆.因此,如果我的理解是正确的,那么在运行代码时我应该只使用1.6 GB.它是否正确?

但是,当我运行代码时,内存使用情况(通过查看Ubuntu中的系统监视器判断)跳到10 gb以上(从大约1 gb),然后再下降到4 gb左右.我不明白发生了什么.我是否错误地使用了Rcpp?

非常感谢您的帮助.非常感谢.

Nic*_*edy 5

新版犰狳 (5.300) 后编辑

在 StackOverflow 上的这个初步问答之后,Conrad Sanderson 和我就这个问题进行了一些电子邮件讨论。根据设计,arma::cube对象arma::matcube. 这是在创建 期间完成的cube,即使数据是从现有内存中复制的(如原始问题中所示)。由于这并不总是需要,我建议应该有一个选项来禁用切片的矩阵预分配。从当前版本的犰狳 (5.300.4) 开始,现在有了。这可以从 CRAN 安装。

示例代码:

library(RcppArmadillo)
library(inline)
code <- "
  Rcpp::NumericVector input_(input);
  arma::cube disturb(input_.begin(), 2, 2, 50000000, false, true, false);
  return wrap(2);
"
Test <- cxxfunction(signature(input = "numeric"), plugin = "RcppArmadillo", body = code)
input <- array(rnorm(2 * 2 * 50000000), dim = c(2, 2, 50000000))
Test(input)
Run Code Online (Sandbox Code Playgroud)

这里的关键cube是现在使用调用构造函数arma::cube disturb(input.begin(), 2, 2, 50000000, false, true, false);false这里的最后一个是prealloc_mat决定是否预先分配矩阵的新参数。该slice方法在cube没有预先分配的矩阵上仍然可以正常工作- 矩阵将按需分配。但是,如果您直接访问 a 的mat_ptrs成员,cube它将被NULL指针填充。帮助也已更新。

非常感谢 Conrad Sanderson 如此迅速地提供这个附加选项,并感谢 Dirk Eddelbuettel 在 Rcpp 和 RcppArmadillo 上所做的所有工作!

原答案

这是一个有点奇怪的。我尝试了一系列不同的数组大小,问题只发生在第 3 维比其他 2 维大得多的数组中。这是一个可重现的例子:

library("RcppArmadillo")
library("inline")
code <- "
Rcpp::NumericVector input_(input);
IntegerVector dim = input_.attr(\"dim\");
arma::cube disturb(input_.begin(), dim[0], dim[1], dim[2], false);
disturb[0, 0, 0] = 45;
return wrap(2);
"
Test <- cxxfunction(signature(input = "numeric"), plugin = "RcppArmadillo", body = code)
input <- array(0, c(1e7, 2, 2))
Test(input)
# no change in memory usage

dim(input) <- c(2, 1e7, 2)
gc()
Test(input)
# no change in memory usage

dim(input) <- c(2, 2, 1e7)
gc()
Test(input)
# spike in memory usage

dim(input) <- c(20, 2, 1e6)
gc()
Test(input)
# no change in memory usage
Run Code Online (Sandbox Code Playgroud)

这表明它与Aramadillo库的实现方式有关(或可能RcppArmadillo)。这当然似乎不是你做错了什么。

注意我已经包含了一些修改来代替数据(将第一个元素设置为 45),您可以确认在每种情况下数据都修改了,这表明没有复制正在进行。

现在,我建议如果可能的话,组织你的 3d 数组,这样最大的维度不是第三个维度。

编辑做一些更多的挖掘之后,它看起来好像有创作的过程中的RAM分配arma::cube。在Cube_meat.hppcreate_mat方法中,有如下代码:

library(RcppArmadillo)
library(inline)
code <- "
  Rcpp::NumericVector input_(input);
  arma::cube disturb(input_.begin(), 2, 2, 50000000, false, true, false);
  return wrap(2);
"
Test <- cxxfunction(signature(input = "numeric"), plugin = "RcppArmadillo", body = code)
input <- array(rnorm(2 * 2 * 50000000), dim = c(2, 2, 50000000))
Test(input)
Run Code Online (Sandbox Code Playgroud)

Cube_prealloc::mat_ptrs_size 似乎是 4,所以对于任何超过 4 个切片的数组来说,这实际上是一个问题。

在 github 上发布了一个问题

EDIT2但是,这绝对是底层犰狳代码的问题。这是一个完全不使用 Rcpp 的可重现示例。这是 linux-only - 它使用来自How to get memory usage at run time in c++? 拉出正在运行的进程的当前内存使用情况。

#include <iostream>
#include <armadillo>
#include <unistd.h>
#include <ios>
#include <fstream>
#include <string>

//////////////////////////////////////////////////////////////////////////////
//
// process_mem_usage(double &, double &) - takes two doubles by reference,
// attempts to read the system-dependent data for a process' virtual memory
// size and resident set size, and return the results in KB.
//
// On failure, returns 0.0, 0.0

void process_mem_usage(double& vm_usage, double& resident_set)
{
   using std::ios_base;
   using std::ifstream;
   using std::string;

   vm_usage     = 0.0;
   resident_set = 0.0;

   // 'file' stat seems to give the most reliable results
   //
   ifstream stat_stream("/proc/self/stat",ios_base::in);

   // dummy vars for leading entries in stat that we don't care about
   //
   string pid, comm, state, ppid, pgrp, session, tty_nr;
   string tpgid, flags, minflt, cminflt, majflt, cmajflt;
   string utime, stime, cutime, cstime, priority, nice;
   string O, itrealvalue, starttime;

   // the two fields we want
   //
   unsigned long vsize;
   long rss;

   stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
               >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
               >> utime >> stime >> cutime >> cstime >> priority >> nice
               >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest

   stat_stream.close();

   long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
   vm_usage     = vsize / 1024.0;
   resident_set = rss * page_size_kb;
}

using namespace std;
using namespace arma;

void test_cube(double* numvec, int dim1, int dim2, int dim3) {
  double vm, rss;

  cout << "Press enter to continue";
  cin.get();

  process_mem_usage(vm, rss);
  cout << "Before:- VM: " << vm << "; RSS: " << rss << endl;

  cout << "cube c1(numvec, " << dim1 << ", " << dim2 << ", " << dim3 << ", false)" << endl;
  cube c1(numvec, dim1, dim2, dim3, false);

  process_mem_usage(vm, rss);
  cout << "After:-  VM: " << vm << "; RSS: " << rss << endl << endl;
}

int
main(int argc, char** argv)
  {
  double* numvec = new double[40000000];

  test_cube(numvec, 10000000, 2, 2);
  test_cube(numvec, 2, 10000000, 2);
  test_cube(numvec, 2, 2, 1000000);
  test_cube(numvec, 2, 2, 2000000);
  test_cube(numvec, 4, 2, 2000000);
  test_cube(numvec, 2, 4, 2000000);
  test_cube(numvec, 4, 4, 2000000);
  test_cube(numvec, 2, 2, 10000000);

  cout << "Press enter to finish";
  cin.get();

  return 0;
  }
Run Code Online (Sandbox Code Playgroud)

编辑 3根据create_mat上面的代码,arma::mat为立方体的每个切片创建一个。在我的 64 位机器上,这会导致每个切片产生 184 字节的开销。对于具有 5e7 个切片的多维数据集,这相当于 8.6 GiB 的开销,即使基础数字数据仅占用 1.5 GiB。我已经给康拉德·桑德森发了电子邮件,询问这是否是犰狳工作方式的基础或可以改变,但现在看来,slice如果可能的话,您肯定希望您的维度(第三个维度)是三个维度中最小的一个. 还值得注意的是,这适用于所有 cubes,而不仅仅是那些从现有内存创建的。使用arma::cube(dim1, dim2, dim3)构造函数会导致相同的内存使用。

  • 不过,这是一个公平的假设。我通常一开始就认为这是我的错。如果你和我结婚的时间一样长,那就是这样;-) (3认同)