Re: [eigen] Custom memory allocation for SparseMatrix?

[ Thread Index | Date Index | More Archives ]

Hi, sorry I had to abandon this topic for a little while.

2016-04-15 16:27 GMT+02:00 Christoph Hertzberg <chtz@xxxxxxxxxxxxxxxxxxxxxxxx>:
C++ allows to globally overwrite new/delete like this:

  void* operator new (size_t size) {
    void *p=your_custom_malloc(size);
    // throw if malloc failed:
    if (p==0)  throw std::bad_alloc();
    return p;
  void operator delete (void *p) {

But that would affect every new/delete.

If you don't want that, I guess there is no easy solution at the moment without changing the CompressedStorage class. An overly complicated alternative would be to work on custom-allocated Map<SparseMatrix>.

Would you like to change all SparseMatrix, or do you need to decide for each matrix where it shall be allocated (i.e., via a template parameter, such as in the std-containers)?

Furthermore, do you want the same for dynamic dense matrices? Then this BZ entry is related:

I really try to completely avoid use of new and delete on the embedded devices. The memory available is limited and as I'm worried about memory fragmentation and what might happen after running the device for a long time. I prefer allocating everything I need during the boot process. 

For the sparse matrix, I think it would be really helpful to have a sparse matrix class that behaves like the fixed-size dense matrix class. It should simply have an internal array large enough to contain the whole matrix even if all elements were non-zero (absolute worst-case). Then I could use such a matrix on the stack, or during static init. 

I did a small experiment here by just doing something like that in the CompressedStorage class (removed use of new/delete, and made fixed size arrays instead). It seems to work but I'm not familiar with the internals yet, I didn't test what would happen once I start modifying the sparse matrix. 

For dynamic matrices, something similar could be useful where it should be possible to just define an absolute maximum size for the matrix. Resizing should be possible below that maximum size. 

This way of solving this makes things very easy to use. There is no need to use custom allocators or overloading new/delete. It's just the same behavior as with the fixed-size dense matrices. 

Maybe a start would be some way of passing a custom "CompressedStorage" class to the sparse matrix? 

Best regards,

Mail converted by MHonArc 2.6.19+