有没有办法创建 std::vector来自 arma::mat 矩阵而不创建矩阵的副本?
我是c ++
的新手。对于统计方法,我计算大型矩阵,例如 a 和b
。它们是nx n
,因此对于大型样本量n
,它们变得非常大。如果它们是double
和n = 70k
,我认为它可能是按30GB
的顺序? 由于所需的矩阵数量可能会有所不同,因此我实现了算法以使用矩阵向量并在其上迭代某些操作。例如,
arma::mat A;
arma::mat B;
std::vector<arma::mat> matrices;
matrices = {A, B};
有没有一种方法可以在不复制矩阵的情况下创建此std :: vector
?
我试图通过这样做来检查内存是否相同:
logger->info("Memory address for A: {}.", (void *)&A);
logger->info("Memory address for matrices.at(0): {}.", (void *)&matrices.at(0));
它显示了不同的地址,因此我认为它正在创建副本,但我不确定。
我尝试使用
std::vector<arma::mat> matrices;
matrices.push_back(A);
内存地址仍然不同。 由于矩阵是空的,因此使用
std::vector<arma::mat> matrices;
matrices.push_back(std::move(A));
算法不再起作用。
I am new to C++
. For a statistical method, I compute large matrices, e.g. A
and B
. They are n x n
so for large sample sizes n
, they become very large. If they are double
and n = 70k
, I think it might be on the order of 30GB
?
Because the number of matrices needed can vary, I implemented the algorithm to use a vector of matrices and iterate over it for some operations. E.g.
arma::mat A;
arma::mat B;
std::vector<arma::mat> matrices;
matrices = {A, B};
Is there a way to create this std::vector
without copying the matrices?
I tried to check whether the memory is the same by doing this:
logger->info("Memory address for A: {}.", (void *)&A);
logger->info("Memory address for matrices.at(0): {}.", (void *)&matrices.at(0));
And it showed different addresses so I assume it is creating a copy but I am not sure.
I tried to use
std::vector<arma::mat> matrices;
matrices.push_back(A);
The memory addresses differed still. With
std::vector<arma::mat> matrices;
matrices.push_back(std::move(A));
the algorithm no longer worked because the matrices were empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要逆转逻辑:让
std :: vector
分配内存并创建矩阵。然后,您直接与向量中的元素合作。例如:注意:重要的是要注意
a
和b
的引用,如果以后将更多元素添加到向量,即如果向量增长,则将变得无效。 。但是,由于这些矩阵太大,因此无论如何,您都希望不惜一切代价避免这种情况。如果容器需要增长,您可能需要看一下EG
std :: List
。You need to reverse the logic: Let the
std::vector
allocate the memory and create the matrices. Then you work directly with the elements in the vector. For example:Note: It is important to note here that the references to
A
andB
will become invalid if you add more elements afterwards to the vector, i.e. if the vector grows. But since these matrices are so large, you want to avoid this anyway at all costs.If the container needs to grow, you might want to take a look at e.g.
std::list
.首先,提前申请内存。
二、使用高级构造函数:
mat(ptr_aux_mem, n_rows, n_cols, copy_aux_mem = false, strict = false)
First, apply for memory in advance.
Second, use advanced constructors:
mat(ptr_aux_mem, n_rows, n_cols, copy_aux_mem = false, strict = false)