Sie sind auf Seite 1von 3

ESL DESIGN

Allocate memory efficiently in


Matlab-to-C code
By Robert Yu In stack allocation, the pro-
Applications Engineer cessor allocates a fixed amount
Agility Design Solutions Inc. of memory when a function is
E-mail: robert.yu@agilityds.com invoked. The memory for the
currently executing function
When developing algorithms for is maintained on a stack, and
communication, signal process- when the function exits the
ing and controls, Matlab is the memory on the stack is released.
de facto standard development This method uses memory more
environment. Matlab’s syntax and efficiently than static allocation,
toolbox functions facilitate algo- because memory is allocated Listing 1: The memory for variable “array” is allocated statically.
rithm exploration and refinement. and released as needed by each
At some point, however, a function. However, the stack is
Matlab algorithm must be ported limited to a finite area of memory.
to another implementation, It can be difficult to predict how
typically C. Embedded systems, in large the overall stack needs to
which many algorithms eventu- be, and stack overflow can be
ally end up as software, can im- difficult to detect. Like static
pose additional requirements on allocation, the size of variables
memory usage. This article looks on the stack must be specified
at how data storage in Matlab at compile time. Listing 2 illus-
translates to memory allocation in trates stack allocation for vari-
C for embedded systems. able “array.”
In dynamic allocation, you Listing 2: Shown is a stack allocation for variable “array.”
Methods manually allocate memory at
Translating Matlab code to C is not runtime. The memory is drawn
a straightforward process. Several from the heap, using C’s malloc
articles have tackled translation and free commands. Because
techniques, and tools now exist you have complete control over
for automatic Matlab-to-C trans- memory usage, dynamic alloca-
lation. In embedded software, tion allows more efficient use
one of the biggest concerns is of memory than either static
memory usage. or stack allocation. Listing 3
In C, the developer has the shows an example of dynamic
choice of allocating memory in allocation.
static memory, on the stack or
dynamically (on the heap). Each Be wary of its trade-offs
allocation method has its advan- The flexibility of dynamic memory
tages and disadvantages. allocation, however, comes at a
Static allocation, in which price: Listing 3: Shown is an example of dynamic allocation.
the compiler sets aside a fixed • Manual memory manage-
amount for each variable, is the ment is error-prone, and heap- • Custom memory allocators— projects forbid the use of dynamic
simplest method and works related software problems (i.e. such as memory pool manag- memory allocation entirely.
even on processors without stack memory leaks) are particularly ers—avoid these problems, In Matlab, you don’t need to
support. However, this method difficult to debug. but introduce more complex- think about memory allocation.
inefficiently allocates all storage • Searching the heap for an ap- ity and possible defects into The Matlab interpreter handles
memory for the lifetime of the propriate-sized block takes an the system. details of array memory and
program, so more memory is al- indeterminate amount of time, data types, leaving the algorithm
located than is actually in use. In which is unacceptable for Given these issues, embedded developer free to concentrate
addition, the amount of memory “hard” real-time systems. software developers traditionally on the algorithm itself. In fact,
is fixed and must be specified at • Repeatedly allocating and frown upon dynamic memory al- typical Matlab users don’t even
compile time. In Listing 1, the freeing memory blocks of dif- location, especially in software for worry about array sizes as Matlab
memory for variable “array” is al- ferent sizes eventually leads to mission-critical systems and avi- will automatically resize arrays as
located statically. fragmentation. onics. Many embedded software required. For this article, however,

EE Times-Asia | October 1-15, 2008 | eetasia.com 


Listing 7: One can allocate LUT in static memory.

Listing 4: In Matlab, you don’t need to think about memory allocation. Fixed-
and variable-size arrays in Matlab are shown.

Listing 5: Shown is a Matlab function that maps one integer to another via a
simple LUT.

Listing 8: This example initializes x with 10 random numbers between 0 and


1.0, then returns the indices of elements in x that are greater.

Listing 6: Shown is a Matlab function with LUT allocated on the stack.

we’ll identify two types of Matlab cate memory for your data. If the
arrays: fixed-size and variable-size. Matlab code uses a fixed-size array,
Fixed-size arrays are Matlab arrays the translation is a simple matter
for which the dimensions remain of allocating an equivalent array in
fixed during runtime. Variable-size C. Because the array size is known
arrays change in size or shape at compile time, you can allocate
during runtime. Some Matlab ex- the array statically or on the stack,
amples are shown in Listing 4. and there’s no need to resort to Listing 9: Shown is the C translation of findrand with fixed-size arrays.
Variable-size arrays are an ex- dynamic allocation. For example,
tremely useful feature of Matlab. the Matlab function in Listing 5 tion tool uses the size of the array location, you have several solutions
Most non-trivial Matlab programs maps one integer to another via a to determine whether to allocate for handling the variable-size array:
make use of them; sometimes simple look-up table (LUT), which the array on the stack, statically or • Rewrite the algorithm to elimi-
you simply don’t know how is a vector of fixed size. dynamically. However, you have nate all variable-size arrays—
large an array (such as an image The equivalent C code, with complete control over this behav- You can emulate variable-size
or resampled buffer) will be until LUT allocated on the stack, is ior and, for example, can direct arrays by pre-allocating a maxi-
runtime. Although the Matlab shown in Listing 6. Similarly, you the translator to avoid all dynamic mum, fixed-size array, then use
language doesn’t explicitly distin- can allocate LUT in static memory, memory allocation. extra “bookkeeping” variables
guish between fixed-size and vari- as shown in Listing 7. and logic to track the actual
able-size arrays, you’ll see that this A tool we developed for Variable-size arrays array size during runtime. This
distinction becomes critical when Matlab-to-C translation takes the If your Matlab code uses variable- method can be done in C or
translating to C. same approach to fixed-size ar- size arrays, how do you translate even Matlab, although this
rays. When the size of a Matlab them to C? The natural solution goes against the grain of nor-
Fixed-size arrays array is known ahead of time, it is to allocate an equivalent array mal Matlab coding style.
When translating Matlab code to simply allocates the equivalent dynamically. But in an environment • Use a custom memory alloca-
C, you must choose how to allo- array in C. By default, the transla- that prohibits dynamic memory al- tor in C such as a pool manag-

 eetasia.com | October 1-15, 2008 | EE Times-Asia


er to allow dynamic memory of ind cannot be determined until To avoid dynamic memory encourages the use of variable-
allocation without the associ- runtime. Find is a commonly used allocation, you can recognize size arrays. On the other hand,
ated problems—This solution function in Matlab, but the vari- that the length of ind will never many algorithms developed in
sidesteps the problems with able length of the output makes exceed the length of x (=10). This Matlab are eventually ported
malloc and free, but introduces translation to C problematic, es- gives you the maximum possible to C on an embedded system,
extra complexity. pecially when dynamic memory size for ind, which you allocate on where memory, time and hard-
allocation is prohibited. the stack. You can also introduce ware constraints limit how the
As an example of variable- The example in Listing 8 ini- a separate variable, ind_dim1, to C implementation allocates
size arrays in Matlab, consider tializes x with 10 random numbers track the actual size of ind. The memory. In some cases, embed-
the Matlab find function: Given between 0 and 1.0, then returns resulting C translation of findrand ded projects prohibit the use of
a vector x, the Matlab statement the indices of elements in x that is shown in Listing 9. dynamic memory allocation en-
“ind=find(x)” returns a vector ind are greater than 0.5. The length of The Matlab language frees tirely. It’s no wonder that trans-
of indices corresponding to non- ind isn’t known until after the ran- you from having to think about lating from Matlab to embedded
zero entries of x. The exact length dom numbers are generated. memory allocation and actively C is a complicated task.

EE Times-Asia | October 1-15, 2008 | eetasia.com 

Das könnte Ihnen auch gefallen