What is the right way to work with Matlab coder dynamic and static allocations?
조회 수: 10 (최근 30일)
이전 댓글 표시
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:
- How to use consecutive memory allocations between different iteration of the algorithm. (How to avoid allocation and de-allocation every iteration)
- What is a proper stack size I should set for static memory allocation (Do 1mb for static allocation is a reasonable value?)
- I thought about using global memory for this case. Would it work?
- Any suggestions how to work things out?
Thanks
댓글 수: 0
채택된 답변
David Fink
2019년 3월 19일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!