This forum has been archived. All content is frozen. Please use KDE Discuss instead.

does Eigen guarantee to not realocate memory used by Map

Tags: None
(comma "," separated)
smajewski
Registered Member
Posts
6
Karma
0
Hello

I have lately started to develop my own ML library using Eigen. One of the problems I had was to load a dynamic sized matrix from file to a Eigen MatrixXd object. The functions I wrote so far do what I intended them to do, but I am not sure if they will always do, I'll explain in a moment why. Here is the code:

template <typename Derived>
void loadDynamicMatrixCSV(std::string filename, Eigen::MatrixBase<Derived> const & m_, bool addBias)
{
int rowSize;
int lastRowSize = 0;
int colSize = 0;
std::vector<double> v;
std::ifstream ifs(filename.c_str(), std::ifstream::in);
if (ifs.fail()) {
std::cout << "Failed to open the file\n";
throw;
}
if (ifs.eof()) {
std::cout << "File is empty\n";
throw;
}
else {
rowSize = readLineOfValues<double>(ifs, v, addBias);
lastRowSize = rowSize;
colSize += 1;
}
while (!ifs.eof()) {
try {
rowSize = readLineOfValues<double>(ifs, v, addBias);
colSize += 1;
if (lastRowSize != rowSize) {
throw;
}
}
catch (int x){
break;
}
catch (char c) {
std::cout << c << "\n";
}
}

Eigen::MatrixBase<Derived> & m = const_cast<Eigen::MatrixBase<Derived>& > (m_);

if (addBias) {
rowSize += 1;
}

m = Eigen::Map<typename Eigen::MatrixXd, Eigen::Unaligned, Eigen::Stride<1, Eigen::Dynamic> >
(&v[0], colSize, rowSize, Eigen::Stride<1, Eigen::Dynamic>(1,rowSize));
}

The contents of function readLineOfValues is irrelevant. My problem is that I am not sure that the memory used by the MatrixXd m object won't be realocated.

In short the procedure does the following:
1. Read the contents of the file to a STL vector, checking if the values make sense
2. Use the map constructor to create an Eigen::MatrixXd object, using the same part of memory as v.

Now here is what I don't understand: if v is an STL object that is used in this function but nowhere else, it should be destroyed after the function ends. But won't it mean the memory used by v, which is now also used by m, will be freed, and could be later realocated? If no, then why, and how does Eigen guarantee it won't happen. If yes, then what can I do about it (I will probably load big files, so I don't want to make as little copies and temporaries of the loaded matrix as possible). Or maybe I am wrong and the memory isn't really reused in this example?

Sidenote: the Eigen project could really use a few functions to load matrices from files, written as class methods of some matrix class. It's easy to do, it's a functionality widely used, and making it a class method saves a bunch of trouble with memory and resizing.

I thank you in advance for any help.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Hi, when you do m = Eigen::Map<...>(....) then the data mapped by the Map<> object is *copied* into the MatrixXd objects. So you can delete your STL object safely.

One the other hand, if you really want to map a buffer without reallocation you have to use a named Map<> object:

Map<...> m(...);
m = 2*m;
...

but in your case I recommend to copy into a MatrixXd as you currently do.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]