[FR] Allow SparseMatrix to accept user defined allocator
Describe the feature you would like to be implemented.
For a SparseMatrix
there are a few points where the impl calls std::malloc()
directly such as here and here like
// turn the matrix into non-compressed mode
m_innerNonZeros = static_cast<StorageIndex*>(std::malloc(m_outerSize * sizeof(StorageIndex)));
if(!m_innerNonZeros) internal::throw_std_bad_alloc();
It would be nice to have a new template parameter Allocator
for sparse matrices which would allow users to pass their own allocator when constructing sparse matrices. I think this could be done not too painfully with something like
template<typename Scalar_, int Options_, typename StorageIndex_, typename Allocator = Eigen::internal::aligned_allocator<Scalar_>>
class SparseMatrix
: public SparseCompressedBase<SparseMatrix<Scalar_, Options_, StorageIndex_, Eigen::internal::aligned_allocator<Scalar_>> >
Would such a feature be useful for other users? Why?
This would be nice for folks who use their own custom allocators. For example, Stan math has a global memory arena we use to keep track of memory needed for the reverse pass of automatic differentiation. It would be nice if we could use our allocator for storage of all the dynamic memory in sparse matrices as our ad is mostly used for MCMC things which require the same sized computations over and over so just making one big memory pool on the first iteration and re-using it is very nice.
Any hints on how to implement the requested feature?
If this is deemed too "not backwards compatible" then I'm fine with not doing it, but if there's appetite I think it's mostly just plugging in the allocator and changing the current mallocs to use the allocator instead of raw malloc. I'd be happy to do the feature if folks think it could be good.