Embedded Systems Design Europe - June/July 2008 - (Page 21) memory Listing 7 Look up table (C, static allocation). int lut_static(int ind) { int N=1025; int n; static int lut[N]; // static allocation dynamic memory allocation without the associated problems: this solution sidesteps the problems with malloc and free but introduces extra complexity. As an example of variable-size arrays in MATLAB, consider the MATLAB find function: given a vector x, the MATLAB statement “ind=find(x)” returns a vector ind of indices corresponding to nonzero entries of x. The exact length of ind cannot be determined until run time. Find is a commonly used function in MATLAB, but the variable length of the output makes translation to C problematic, especially when dynamic memory allocation is prohibited. The example in Listing 8 initializes x with ten random numbers between 0 and 1.0, then returns the indices of elements in x that are greater than 0.5. The length of ind isn’t known until after the random numbers are generated. To avoid dynamic memory allocation, you can recognize that the length of ind will never exceed the length of x (=10). This gives you the maximum possible size for ind, which you allocate on the stack. You can also introduce a separate variable, ind_dim1, to track the actual size of ind. The resulting C translation of findrand is shown in Listing 9. The MATLAB language frees you from having to think about memory allocation and actively encourages the use of variable-size arrays. On the other hand, many algorithms developed in MATLAB are eventually ported to C on an embedded system, where memory, time, and hardware constraints limit how the C implementation allocates memory. In some cases, embedded projects prohibit the use of dynamic memory allocation entirely. It’s no wonder that translating from MATLAB to embedded C is a complicated task. Robert Yu (robert.yu@agilityds.com) is an applications engineer at Agility Design Solutions involved in the development and use of Agility MCS for automatic MATLAB-to-C translation tool in embedded applications. 21 Listing 8 Find and variable-size array. function ind=findrand() % % fill x with random values % between 0 and 1.0 x=rand(10,1); % % length of ind unknown a priori ind=find(x>0.5); Listing 9 C Translation of findrand with fixed-size arrays. void _findrand(void) { double x[10]; int32 x_dim1; int32 ind[10]; int32 ind_dim1; // track actual size of ind int32 n; // fill x with 10 random numbers my_rand(x, &x_dim1); // ind=find(x>0.5) ind_dim1=0; for (n=0;n 0.5) { ind[ind_dim1]=n; // store 0-based index ind_dim1++; } } print_vector(“ind”, ind, NULL, 9, 2, 1, ind_dim1); return; } tion. If your MATLAB code uses variable-size arrays, how do you translate them to C? The natural solution is to allocate an equivalent array dynamically, but in an environment that prohibits dynamic memory allocation, you have several solutions for handling the variable-size array: • Rewrite the algorithm to eliminate all variable-size arrays: you can • emulate variable-size arrays by preallocating a maximum, fixed-size array, then use extra “bookkeeping” variables and logic to track the actual array size during runtime. This method can be done in C or even MATLAB, although this goes against the grain of normal MATLAB coding style. Use a custom memory allocator in C, such as a pool manager, to allow www.embedded.com/europe | embedded systems design europe | JUNE – JULY 2008 http://www.embedded.com/europe
Table of Contents Feed for the Digital Edition of Embedded Systems Design Europe - June/July 2008 Embedded Systems Design Europe - June 2008 Contents Work in Progress to Define Compact PCI Plus Power.org Demonstrates New Tools Project Supports Multi-core System Programming Altium Links Electronic to Mechanical Design PLDs Look to Cut Power Budget and Costs Project to Provide Coverage Analysis Tool Microsoft Details Windows Embedded Update Cover Feature: Leveraging Virtual Hardware Platforms for Software Allocating Memory in MATLAB-to-C Code MDD & IDEs: Making the Twain Meet in Embedded System Designs Debugging Mixed Signal Designs for Infrequent & Random Events Why Open Source is the Natural Choice for High-security Systems Bringing the Benefits of Low Power CPUs to Modular Design New Products Advertising Contacts Embedded Systems Design Europe - June/July 2008 Embedded Systems Design Europe - June/July 2008 - Embedded Systems Design Europe - June 2008 (Page 1) Embedded Systems Design Europe - June/July 2008 - Embedded Systems Design Europe - June 2008 (Page 2) Embedded Systems Design Europe - June/July 2008 - Contents (Page 3) Embedded Systems Design Europe - June/July 2008 - Contents (Page 4) Embedded Systems Design Europe - June/July 2008 - Contents (Page 5) Embedded Systems Design Europe - June/July 2008 - Project Supports Multi-core System Programming (Page 6) Embedded Systems Design Europe - June/July 2008 - Project Supports Multi-core System Programming (Page 7) Embedded Systems Design Europe - June/July 2008 - Altium Links Electronic to Mechanical Design (Page 8) Embedded Systems Design Europe - June/July 2008 - Altium Links Electronic to Mechanical Design (Page 9) Embedded Systems Design Europe - June/July 2008 - PLDs Look to Cut Power Budget and Costs (Page 10) Embedded Systems Design Europe - June/July 2008 - PLDs Look to Cut Power Budget and Costs (Page 11) Embedded Systems Design Europe - June/July 2008 - Microsoft Details Windows Embedded Update (Page 12) Embedded Systems Design Europe - June/July 2008 - Microsoft Details Windows Embedded Update (Page 13) Embedded Systems Design Europe - June/July 2008 - Cover Feature: Leveraging Virtual Hardware Platforms for Software (Page 14) Embedded Systems Design Europe - June/July 2008 - Cover Feature: Leveraging Virtual Hardware Platforms for Software (Page 15) Embedded Systems Design Europe - June/July 2008 - Cover Feature: Leveraging Virtual Hardware Platforms for Software (Page 16) Embedded Systems Design Europe - June/July 2008 - Cover Feature: Leveraging Virtual Hardware Platforms for Software (Page 17) Embedded Systems Design Europe - June/July 2008 - Allocating Memory in MATLAB-to-C Code (Page 18) Embedded Systems Design Europe - June/July 2008 - Allocating Memory in MATLAB-to-C Code (Page 19) Embedded Systems Design Europe - June/July 2008 - Allocating Memory in MATLAB-to-C Code (Page 20) Embedded Systems Design Europe - June/July 2008 - Allocating Memory in MATLAB-to-C Code (Page 21) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 22) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 23) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 24) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 25) Embedded Systems Design Europe - June/July 2008 - MDD & IDEs: Making the Twain Meet in Embedded System Designs (Page 26) Embedded Systems Design Europe - June/July 2008 - Debugging Mixed Signal Designs for Infrequent & Random Events (Page 27) Embedded Systems Design Europe - June/July 2008 - Debugging Mixed Signal Designs for Infrequent & Random Events (Page 28) Embedded Systems Design Europe - June/July 2008 - Debugging Mixed Signal Designs for Infrequent & Random Events (Page 29) Embedded Systems Design Europe - June/July 2008 - Debugging Mixed Signal Designs for Infrequent & Random Events (Page 30) Embedded Systems Design Europe - June/July 2008 - Why Open Source is the Natural Choice for High-security Systems (Page 31) Embedded Systems Design Europe - June/July 2008 - Why Open Source is the Natural Choice for High-security Systems (Page 32) Embedded Systems Design Europe - June/July 2008 - Why Open Source is the Natural Choice for High-security Systems (Page 33) Embedded Systems Design Europe - June/July 2008 - Bringing the Benefits of Low Power CPUs to Modular Design (Page 34) Embedded Systems Design Europe - June/July 2008 - Bringing the Benefits of Low Power CPUs to Modular Design (Page 35) Embedded Systems Design Europe - June/July 2008 - Bringing the Benefits of Low Power CPUs to Modular Design (Page 36) Embedded Systems Design Europe - June/July 2008 - New Products (Page 37) Embedded Systems Design Europe - June/July 2008 - New Products (Page 38) Embedded Systems Design Europe - June/July 2008 - New Products (Page 39) Embedded Systems Design Europe - June/July 2008 - New Products (Page 40) Embedded Systems Design Europe - June/July 2008 - New Products (Page 41) Embedded Systems Design Europe - June/July 2008 - New Products (Page 42) Embedded Systems Design Europe - June/July 2008 - Advertising Contacts (Page 43) Embedded Systems Design Europe - June/July 2008 - Advertising Contacts (Page 44)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.