c – 特征和巨大的密集2D阵列

c – 特征和巨大的密集2D阵列,第1张

概述我正在为项目使用2D Eigen :: Arrays,我喜欢在巨大的2D数组的情况下继续使用它们. 为了避免内存问题,我想使用内存映射文件来管理(读/修改/写入)这些数组,但我找不到工作示例. 我发现的最接近的例子是基于boost :: interprocess的this,但它使用共享内存(而我更喜欢持久存储). 缺乏示例让我担心是否有更好的主流替代解决方案来解决我的问题.是这样的吗?一个最小的例 我正在为项目使用2D Eigen :: Arrays,我喜欢在巨大的2D数组的情况下继续使用它们. @H_301_2@为了避免内存问题,我想使用内存映射文件来管理(读/修改/写入)这些数组,但我找不到工作示例.

@H_301_2@我发现的最接近的例子是基于boost :: interprocess的this,但它使用共享内存(而我更喜欢持久存储).

@H_301_2@缺乏示例让我担心是否有更好的主流替代解决方案来解决我的问题.是这样的吗?一个最小的例子非常方便.

@H_301_2@编辑:

@H_301_2@这是在评论中解释我的用例的最小示例:

#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    }}
解决方法 所以我用Google搜索
@H_301_2@boost memory mapped file

@H_301_2@在第一个结果中出现了boost::iostreams::mapped_file.

@H_301_2@结合从this comment到Eigen::Map的链接,我测试了以下内容:

#include <boost/iostreams/device/mapped_file.hpp>#include <Eigen/Dense>
boost::iostreams::mapped_file file("foo.bin");const std::size_t rows = 163840;const std::size_t columns = 163840;if (rows * columns * sizeof(float) > file.size()) {    throw std::runtime_error("file of size " + std::to_string(file.size()) + " Couldn’t fit float Matrix of " + std::to_string(rows) + "×"  + std::to_string(columns));}Eigen::Map<Eigen::MatrixXf> matrix(reinterpret_cast<float*>(file.data()),rows,columns);std::cout << matrix(0,0) << ' ' << matrix(rows - 1,columns - 1) << std::endl;matrix(0,0) = 0.5;matrix(rows - 1,columns - 1) = 0.5;
@H_301_2@使用cmake

find_package(Boost required COMPONENTS iostreams)find_package(Eigen3 required)target_link_librarIEs(${PROJECT_name} Boost::iostreams Eigen3::Eigen)
@H_301_2@然后我用Google搜索

@H_301_2@windows create dummy file

@H_301_2@而first result给了我

fsutil file createnew foo.bin 107374182400
@H_301_2@两次运行程序给出:

@H_301_2@0 0

@[email protected] 0.5

@H_301_2@没有炸毁内存使用情况.

@H_301_2@所以它就像一个魅力.

总结

以上是内存溢出为你收集整理的c – 特征和巨大的密集2D阵列全部内容,希望文章能够帮你解决c – 特征和巨大的密集2D阵列所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1241130.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存