#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阵列所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)