What is the right way to work with Matlab coder dynamic and static allocations?

14 visualizaciones (últimos 30 días)
Hi
I recently started working with Matlab coder for building computer vision algorithms. I'm working on a point cloud classification problem which means that my algorithm input is a set of 3D points (x,y,z) and my output is a variable array with classified 3D points.
For this project I decided to use Matlab coder. Coder requires the developer to specify an upper bound for the memory allocation. Well here comes the problem – the set of input 3D points can be up to 250k points, and the output is ~50k points for each class. In addition to that my implementation allocates 3D grid in size up to: 600x600x120 (uint8) and few more auxilary memory that I work with. My point is that I'm working with large memory allocations.
I tried to use static allocation; however it feels like Matlab haven't build their static memory allocation for cases where the each memory allocation is around 1mb.
I set the upper memory limit to large value, and then I used Coder to generate C++ code and compiled it. The first time I run the algorithm I had a stack overflow, Then I had to enlarge the stack size to 30mb (inside visual studio), which is think is absurd.
On the other hand, I could use dynamic memory allocation. However in this case almost everything would be dynamically allocated, which is also a pain the neck due to a large allocation and de-allocation time consuming.
I wish from Matlab to write a code that would hold inside its implementation the upper bounded memory that will not change between several iterations of the algorithms. (Just like a member in C++ classes)
I'll more specific: In case my algorithm uses a grid of 600x600x120 and several images with size 600x600, I would like that Matlab Coder will generate code that would allocate these memories only once, and I'd use them on different algorithmic iterations.
I have few questions:
  1. How to use consecutive memory allocations between different iteration of the algorithm. (How to avoid allocation and de-allocation every iteration)
  2. What is a proper stack size I should set for static memory allocation (Do 1mb for static allocation is a reasonable value?)
  3. I thought about using global memory for this case. Would it work?
  4. Any suggestions how to work things out?
Thanks

Respuesta aceptada

David Fink
David Fink el 19 de Mzo. de 2019
There are two recommended ways to avoid large stack usage and dynamic allocation in multiple iterations of the algorithm.
For both, set the Stack Space Usage small enough to prevent the local variables from fitting on the stack.
Option A: Use the stack space setting. This will force those variables to have the "static" storage class. These are allocated once when the program starts, and the same memory is reused on each call to the algorithm.
Option B. In addition to the stack space setting, turn on MultiInstanceCode. Instead of static local variables, this creates a type containing all of the would-be-local variables too large for the stack. Before calling the algorithm, allocate that space once, and pass it to the generated C++ function each time the algorithm is called.
  1 comentario
TripleSSSS
TripleSSSS el 20 de Mzo. de 2019
Thank you so much.
I ended up using the default stack limit, I expended the Dynamic memory allocation threshold (In coder app: Memory --> Dynamic memory allocation threshold) to a large limit (~50MB).
Then I set the generate re-entrant code (In coder app: Memory --> generate re-entrant code)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by