【发布时间】:2018-06-30 19:48:08
【问题描述】:
我在一个项目中使用 2D Eigen::Arrays,我喜欢在大型 2D 数组的情况下继续使用它们。
为了避免内存问题,我想使用内存映射文件来管理(读取/修改/写入)这些数组,但我找不到工作示例。
我发现的最接近的例子是基于boost::interprocess 的this,但它使用共享内存(虽然我更喜欢持久存储)。
缺乏示例让我担心是否有更好的主流替代解决方案来解决我的问题。是这样吗?一个最小的例子会很方便。
编辑:
这是一个解释我在 cmets 中的用例的最小示例:
#include <Eigen/Dense>
int main()
{
// Order of magnitude of the required arrays
Eigen::Index rows = 50000;
Eigen::Index cols = 40000;
{
// Array creation (this is where the memory mapped file should be created)
Eigen::ArrayXXf arr1 = Eigen::ArrayXXf::Zero( rows, cols );
// Some operations on the array
for(Eigen::Index i = 0; i < rows; ++i)
{
for(Eigen::Index j = 0; j < cols; ++j)
{
arr1( i, j ) = float(i * j);
}
}
// The array goes out of scope, but the data are persistently stored in the file
}
{
// This should actually use the data stored in the file
Eigen::ArrayXXf arr2 = Eigen::ArrayXXf::Zero( rows, cols );
// Manipulation of the array data
for(Eigen::Index i = 0; i < rows; ++i)
{
for(Eigen::Index j = 0; j < cols; ++j)
{
arr2( i, j ) += 1.0f;
}
}
// The array goes out of scope, but the data are persistently stored in the file
}
}
【问题讨论】:
-
您可以创建巨大的交换文件并根据需要拥有操作系统交换页面
-
@Darklighter,您能否发表更多评论?
-
您可以简单地通过页面/交换文件扩展您的虚拟内存,而不是尝试将文件用作一块内存。这允许您使用大于物理内存的矩阵。不过,它可能只在 SSD 上运行得相当好。
-
正如您自己所说的,minimal reproducible example 您实际打算做的事情会非常方便。或者至少是一些伪代码。您是否存储了想要线性遍历/随机访问的现有数组?或者您是否在运行时生成了太大而无法放入 RAM 的巨大数组?您使用的数量级是多少?
-
您应该能够想出一些东西,使用boost::interprocess 将文件映射为内存缓冲区,然后 (Eigen::Map)[eigen.tuxfamily.org/dox/group__TutorialMapClass.html] 来查看它并将其作为
ArrayXXf.
标签: c++ multidimensional-array eigen memory-mapped-files eigen3