c++ - How to map opencv to eigen with alignment support without allocate the memory? -
i use opencv read big data data set(28*28 rows, 200000 cols) , want map matrix of eigen alignment support without allocate big buffer.
cv::mat big_data(28*28, 200000, cv_64f); //...read data , preprocess ematrix map_big_data; //cv2eigen allocate new, big buffer cv::cv2eigen(big_data, map_big_data);
is possible map matrix without allocate big memory?it okay resize cv::mat, want avoid 2 big buffer exist @ same time(may throw bad_alloc)
cv::mat big_data(28*28, 200000, cv_64f); //read data , preprocess.... using ematrix = eigen::matrix<double, eigen::dynamic, eigen::dynamic, eigen::rowmajor>; using emap = eigen::map<ematrix, eigen::aligned>; //resize cv::mat meet alignment requirement(16bytes aligned),is possible?how align_cols? big_data.resize(big_data.rows, align_cols); //this may or may not crash emap map_big_data(reinterpret_cast<double*>(big_data.data), big_data.rows, big_data.cols);
edit : use eigen::aligned_allocator allocate align memory
eigen::aligned_allocator<double> alloc; int const rows = 28*28; int const cols = 200000; auto buffer = alloc.allocate(rows*cols); cv::mat big_data(rows, cols, cv_64f, buffer, cols * sizeof(double)); //read data set , preprocess.... using ematrix = eigen::matrix<double, eigen::dynamic, eigen::dynamic, eigen::rowmajor>; using emap = eigen::map<ematrix, eigen::aligned>; emap map_big_data(reinterpret_cast<double*>(big_data.data), big_data.rows, big_data.cols); //some jobs..... alloc.deallocate(buffer, 0);
is ok?
why use eigen allocator directly @ all? use eigen::matrixxd (or similar), , pass pointer data cvmat constructor. used way, cvmat "wraps" data without copying (same eigen::map).
int rows = 28*28; int cols = 200000; eigen::matrix<double, eigen::dynamic, eigen::dynamic, eigen::rowmajor> eigenmat(rows, cols); cv::mat big_data(rows, cols, cv_64f, eigenmat.data(), cols * sizeof(double)); //read data set , preprocess....
the thing shouldn't try , resize big_data
, rather eigenmat
if necessary.
from opencv documentation:
data
pointer user data. matrix constructors take data , step parameters not allocate matrix data. instead, initialize matrix header points specified data, means no data copied. operation efficient , can used process external data using opencv functions. external data not automatically deallocated, should take care of it.
this way there no maps, no trying determine if there's padding, , no need remember deallocate. while still gaining advantages of eigen matrix.
Comments
Post a Comment