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

Custom allocator for sparse and dynamic-sized vecs/matrices?

Tags: None
(comma "," separated)
CantankerousBlowhard
Registered Member
Posts
1
Karma
0
Actual Questions:
What exactly does Eigen use for dynamically allocating memory for large vectors/matrices/etc.? I'm guessing it uses an Eigen-specific aligned allocator, which probably uses malloc as the underlying allocator. Is this correct? (I'd look at the code myself, but it's way outside of my comfort zone. Eigen's code is up there with Boost in terms of being a wall of opaque template voodoo, and I'm too scared to try wading through it. ;))

Is there any way for clients to supply their own custom allocators instead (assuming they follow certain requirements, e.g. alignment), or are there any future plans for this? If not, is malloc the underlying allocator, and would hooking/redefining/wrapping/etc. malloc/free be sufficient for intercepting Eigen's memory allocation?

If nothing else, does Eigen offer any way to measure its memory usage?

Context, in case it matters:
I'm writing a video game engine, and I'd like to tightly track and manage memory with custom allocators. I'll be using fixed-size vectors and matrices for the most part, so there aren't any worries there.

However, I'd also like to experiment with dynamic fluids (especially water) in my engine. There are a lot of fluid simulation approaches, and I haven't settled on one yet, but one major approach involves solving a large set of linear equations with a preconditioned conjugate gradient method (using a modified incomplete Cholesky preconditioner with no fill-ins)...which I barely [kind of, sort of] understand. I'm actually pretty out of my depth, but I'm trying. ;)

Anyway, if I were to pick that approach, I'd have to manipulate some large vectors and a very large sparse matrix. I could write an inefficient and hacky implementation myself (and I might have to for the preconditioner), but I figure that it would probably be a lot smarter and more efficient to leverage Eigen for as much legwork as possible. The only problem is, using Eigen for the large vectors and sparse matrix might cramp my style from a memory management point of view.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
CantankerousBlowhard wrote:Actual Questions:
What exactly does Eigen use for dynamically allocating memory for large vectors/matrices/etc.? I'm guessing it uses an Eigen-specific aligned allocator, which probably uses malloc as the underlying allocator. Is this correct? (I'd look at the code myself, but it's way outside of my comfort zone. Eigen's code is up there with Boost in terms of being a wall of opaque template voodoo, and I'm too scared to try wading through it. ;))


Behind the wall of template voodoo, there is this file:
Eigen/src/Core/util/Memory.h
which contains all the memory allocation functions we're using, and it's not too templated, it's mostly plain C. Read it! See especially the implementation of ei_aligned_malloc.

We try hard to use system-specific aligned malloc functions (such as posix_memalign on POSIX systems) but if we fail to, as a fallback we use what you describe above --- malloc a bigger buffer and align it.

Is there any way for clients to supply their own custom allocators instead (assuming they follow certain requirements, e.g. alignment), or are there any future plans for this?


Unfortunately we don't support this at the moment, but I certainly agree that that could be useful. It's not clear however at what level to do it. The biggest use case we've heard of was to use distributed storage for huge matrices, and that required a separate Matrix class anyway to be efficient, so we have made sure our architecture is extensible in this respect.

If not, is malloc the underlying allocator, and would hooking/redefining/wrapping/etc. malloc/free be sufficient for intercepting Eigen's memory allocation?


Nope, it's not safe to assume that Eigen uses malloc. Right now, if you want to override Eigen's memory allocation, you have to edit Memory.h (mentioned above). Fortunately, everything goes through ei_aligned_malloc (or standard malloc / operator new) so you only have to override that.

If nothing else, does Eigen offer any way to measure its memory usage?


Same answer: unfortunately no, but since all non-standard allocs go through ei_aligned_malloc / ei_aligned_free, you only have to hook into that.

Oh and on linux we do some temporaries on the stack with alloca(), but you can get rid of that by defining
#define EIGEN_STACK_ALLOCATION_LIMIT 0
that's in the same file.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
jslee
Registered Member
Posts
1
Karma
0
Hello,

I'm also interested in using a custom allocator for the same reason as the original post. I wonder if Eigen still doesn't provide a way for this.

What I expect is something like how the STL containers allow the user to pass in the custom allocator. For example,

Code: Select all
// std::vector uses the custom allocated internally without needing to change the public API of std::vector
std::vector<int, CustomAllocator<int>> vec;

// similarly, Eigen matrix could be something like
Eigen::Matrix<double, ..., CustomAllocator<...>> matrix;


Thanks,
Jeongseok


Bookmarks



Who is online

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