matlabmatlab-coder

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


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


Solution

  • Whether or not local variables are allocated on the stack can be controlled via the Stack Space Usage setting, which sets a limit on the stack size.

    If not on the stack, variables will normally be "spilled" as static local variables, which are allocated once on program initialization. However, enabling the Re-entrant Code setting instead allows for one-time dynamic allocation of the large memory (rather than static local variables).

    For additional details, please see my answer on the corresponding MATLAB Answers post:

    https://www.mathworks.com/matlabcentral/answers/450732-what-is-the-right-way-to-work-with-matlab-coder-dynamic-and-static-allocations