犰狳:堆上有效的矩阵分配 [英] Armadillo: efficient matrix allocation on the heap

查看:112
本文介绍了犰狳:堆上有效的矩阵分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Armadillo来处理从CSV文件读取的C ++中的大型矩阵.

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.

所以x1,...,xk(例如k=20)是X的列.X通常具有从2000到16000的行.我的问题是:

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:

我如何在堆(免费存储)中分配(然后取消分配)X?

本节解释了垫子的辅助内存分配.这和堆分配一样吗?它需要矩阵尺寸的先验知识,直到从csv读取X时我才知道:

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) 

任何建议将不胜感激. (我使用的是g ++-4.2.1;我当前的程序在Macbook Pro上可以在本地正常运行,但是当我在大学的计算集群(Linux g ++-4.1.2)上运行它时,我遇到了分段错误.该程序太大而无法发布).

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).

我最终这样做:

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使用它.

which first allocates memory on the heap and then tells the matrix Z to use it.

推荐答案

通过查看源代码,犰狳已经在堆上分配了大型矩阵.

By looking at the source code, Armadillo already allocates large matrices on the heap.

为减少所需的内存量,您可能要使用 fmat 而不是 mat .这将以降低精度为代价.

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 使用浮点数,而 mat 使用双精度点:请参见 http://arma.sourceforge.net/docs.html#Mat .

fmat uses float, while mat uses double: see http://arma.sourceforge.net/docs.html#Mat.

Linux计算群集的系统管理员也可能对其启用了限制(例如,每个用户最多只能分配一定数量的最大内存).例如,请参见 http://www.linuxhowtos.org/Tips%20and%20Tricks/ulimit.htm .

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.

这篇关于犰狳:堆上有效的矩阵分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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