I have an algorithm that requires the construction of an NxN matrix inside a function that will return the product of this matrix with an Nx1 vector that's also built on the fly. (N is usually 8 or 9, but must be generalized for values greater than that).
I'm using the Eigen library for performing algebraic operations that are even more complex (least squares and several other constrained problems), so switching it isn't an option.
I've benchmarked the functions, and there's a huge bottleneck due to the intensive memory allocations. I aim to build a thread safe application, so, for some cases, I replaced these matrices and vectors with references to elements from a global vector that serves as a provider for objects that cannot be stored on the stack. This avoids calling the constructors/destructors of the Eigen matrices and vectors, but it's not an elegant solution and it can lead to huge problems if considerable care is not taken.
As such, does Eigen either offer a workaround because I don't see the option of passing an allocator as a template argument for these objects, OR is there a more obvious thing to do?
N
change between the calls? If yes, then how did you prepare this "global object" when you do not know whatN
will be? – luk32static
within your function. Otherwise the global one is hard to avoid IMO. It should not be very hard I think. You should get away with something simple, i.e. with out garbage collection, or any clean ups. – luk32static
or not. Of course if your app spams allocations it will get serialized on those calls. But it do not think it's a real problem in a normal use-case. Also I don't thinkeigen
exposes it's memory allocations. They do some magic to get memory alignment in order to use SSE. So best bet is to wrap theeigen
objects themselves in some kind of resource manager like you did. – luk32