Armadillo:堆上的高效矩阵分配
我正在使用犰狳来操作从 CSV 文件读取的 C++ 大型矩阵。
mat X;
X.load("myfile.csv",csv_ascii);
colvec x1 = X(span::all,0);
colvec x2 = X(span::all,1);
//etc.
所以 x1,...,xk(例如 k=20
)是 X 的列。X 通常具有从 2000 到 16000 的行。我的问题是:
如何分配(并随后释放)X到堆上(免费存储)?
本节 的Armadillo 文档解释了垫子的辅助内存分配。这与堆分配相同吗?它需要事先了解矩阵维度,直到从 csv 读取 X 后我才知道:
mat(aux_mem*, n_rows, n_cols, copy_aux_mem = true, strict = true)
任何建议将不胜感激。 (我使用的是 g++-4.2.1;我当前的程序在我的 Macbook Pro 上本地运行良好,但是当我在大学的计算集群 (Linux g++-4.1.2) 上运行它时,我遇到了分段错误。该程序太大,无法发布)。
编辑:我最终这样做了:
arma::u32 Z_rows = 10000;
arma::u32 Z_cols = 20;
double* aux_mem = new double[Z_rows*Z_cols];
mat Z(aux_mem,Z_rows,Z_cols,false,true);
Z = randn(Z_rows, Z_cols);
首先在堆上分配内存,然后告诉矩阵 Z 使用它。
I'm using Armadillo to manipulate large matrices in C++ read from a CSV-file.
mat X;
X.load("myfile.csv",csv_ascii);
colvec x1 = X(span::all,0);
colvec x2 = X(span::all,1);
//etc.
So x1,...,xk (for k=20
say) are the columns of X. X will typically have rows ranging from 2000 to 16000. My question is:
How can I allocate (and subsequently deallocate) X onto the heap (free store)?
This section of Armadillo docs explains auxiliary memory allocation of a mat. Is this the same as heap allocation? It requires prior knowledge of matrix dimensions, which I won't know until X is read from csv:
mat(aux_mem*, n_rows, n_cols, copy_aux_mem = true, strict = true)
Any suggestions would be greatly appreciated. (I'm using g++-4.2.1; my current program runs fine locally on my Macbook Pro, but when I run it on my university's computing cluster (Linux g++-4.1.2), I run into a segmentation fault. The program is too large to post).
Edit: I ended up doing this:
arma::u32 Z_rows = 10000;
arma::u32 Z_cols = 20;
double* aux_mem = new double[Z_rows*Z_cols];
mat Z(aux_mem,Z_rows,Z_cols,false,true);
Z = randn(Z_rows, Z_cols);
which first allocates memory on the heap and then tells the matrix Z to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过查看源代码,Armadillo 已经在堆上分配了大矩阵。
要减少所需的内存量,您可能需要使用 fmat 而不是 mat。这将以降低精度为代价。
fmat 使用 float,而 mat 使用 double:请参阅 http://arma.sourceforge.net/docs.html#Mat。
Linux 计算集群的系统管理员也可能对其启用了限制(例如,每个用户只能分配一定数量的最大内存)。例如,请参见 http://www.linuxhowtos.org/Tips%20and%20Tricks /ulimit.htm。
By looking at the source code, Armadillo already allocates large matrices on the heap.
To reduce the amount of memory required, you may want to use fmat instead of mat. This will come with the trade-off of reduced precision.
fmat uses float, while mat uses double: see http://arma.sourceforge.net/docs.html#Mat.
It's also possible that the system administrator of the linux computing cluster has enabled limits on it (eg. each user can allocate only upto a certain amount of maximum memory). For example, see http://www.linuxhowtos.org/Tips%20and%20Tricks/ulimit.htm.