Embedded Systems Design Europe - February 2008 - (Page 34) feature down because it increases memory use. In these cases, you may want to turn your functions or loops into MEX-files. MEX-files, like all MATLAB core built-in functions, are compiled files. They can therefore be extremely fast. Once developed, functions implemented as MEX-files are also very easy to use. They can be called directly from MATLAB, just like any other function. Within MATLAB, you can create MEX-files from C or Fortran source code by using the mex command. The C or Fortran source code has to be manually written using the MEX application programming interface (MEX API). MEX API functions are directly available to read input arguments from MATLAB, write outputs arguments back to MATLAB, or access global variables. The MEX API also provides hooks that let you call back to MATLAB to make use of visualization functions or leverage computation functions that do not need to be compiled. The MEX-file guide from The Mathworks provides more information on how to write your own MEX-files. Needless to say, the process of writing these MEX-files by hand is both time-consuming and error-prone. Several tasks must happen when translating MATLAB to C or Fortran. First, function cost = costFuncMAD(currentBlk,refBlk, n) % Non-vectorized code % err = 0; % for i = 1:n % for j = 1:n % err = err + abs((currentBlk(i,j) - refBlk(i,j))); % end %end % Vectorized code err = sum(abs(currentBlk(:) - refBlk(:))); cost = err / (n*n); you must implement the algorithm functionality, which includes MATLAB functions, by writing new code or calling pre-existing functions and libraries. While doing this, you need to explicitly allocate storage by declaring internal variables and making proper use of memory management routines. Finally you need to interface your code with the rest of your MATLAB model using the MEX API. Debugging MEX-files can also be difficult as none of the MATLAB analysis and visualization functions are available from the C or Fortran debugger. The C and Fortran languages are also less forgiving than the MATLAB language. Out-of-bound array accesses or failure to properly allocate or free memory may have catastrophic consequences on your application, including unexpected results, segmentation violations, or “out of memory” errors. A more convenient path is to automatically generate MEX-files directly from MATLAB. This allows you to fully utilize the MATLAB environment to develop your algorithm and optimize it for speed. Then, at any time, you may compile your code into a fast MEX-file. Code changes applied to your algorithm are automatically incorporated in the MEX-file, creating a fast, reliable, and reproducible path from MATLAB to compiled code. Using advanced optimizing compiler technology, Agility tools can automatically compile your MATLAB code into fast MEX-files. Internally, your MATLAB code is translated into an intermediary C code. The ability to generate efficient C code from MATLAB may be used to create not only MEXfiles for acceleration but also standalone platform-independent ANSI C models by the push of a button. On the block-matching algorithm presented above, generating MEX-files gives by far the biggest speed-up. Block-matching algorithm Run time MATLAB Original 46s MATLAB Vectorized 26s MATLAB with Agility MEX-file 2.7s The screen shot left shows how an automatically-generated MEX-file sped up the block-matching algorithm by almost 10x. Luc Semeria (luc@agilityds.com) is product manager at Agility Design Solutions (formally Catalytic Inc.) 34 JANUARY – FEBRUARY 2008 | embedded systems design europe | www.embedded.com/europe 032-033-034_ESDE.indd 34 6/02/08 16:53:47 http://www.embedded.com/europe
Table of Contents Feed for the Digital Edition of Embedded Systems Design Europe - February 2008 Embedded Systems Design Europe - February 2008 Contents ARTEMIS and ENIAC Get Parlimentary Approval Product Teardown Videos Come On Screen Esterel and Abslnt Integrate Products Microsoft Opens Windows to Networked Embedded Applications Trango Embeds Virtualization Tool in Cavium's Multicore CPUs MindTree - ADI Develop Security DVR Platform NXP Extends Deal with ARM to Cover MCUs Automotive and Embedded to Dominate DATE 08 ZigBee Spec Gets Smart On Energy Updated Card Spec Provides for Power-On Boot The Basics of Embedded Multitasking On a PIC Cover Feature: The Art of FPGA Construction Is Symmetric Multiprocessing For You? Accelerating MATLAB Using MEX-Files ARM Provides the Microcontroller Solution Embedded World Advertising Contracts Embedded Systems Design Europe - February 2008 Embedded Systems Design Europe - February 2008 - Embedded Systems Design Europe - February 2008 (Page Cover1) Embedded Systems Design Europe - February 2008 - Embedded Systems Design Europe - February 2008 (Page Cover2) Embedded Systems Design Europe - February 2008 - Contents (Page 3) Embedded Systems Design Europe - February 2008 - Contents (Page 4) Embedded Systems Design Europe - February 2008 - Contents (Page 5) Embedded Systems Design Europe - February 2008 - Esterel and Abslnt Integrate Products (Page 6) Embedded Systems Design Europe - February 2008 - Esterel and Abslnt Integrate Products (Page 7) Embedded Systems Design Europe - February 2008 - Microsoft Opens Windows to Networked Embedded Applications (Page 8) Embedded Systems Design Europe - February 2008 - Microsoft Opens Windows to Networked Embedded Applications (Page 9) Embedded Systems Design Europe - February 2008 - NXP Extends Deal with ARM to Cover MCUs (Page 10) Embedded Systems Design Europe - February 2008 - NXP Extends Deal with ARM to Cover MCUs (Page 11) Embedded Systems Design Europe - February 2008 - Updated Card Spec Provides for Power-On Boot (Page 12) Embedded Systems Design Europe - February 2008 - The Basics of Embedded Multitasking On a PIC (Page 13) Embedded Systems Design Europe - February 2008 - The Basics of Embedded Multitasking On a PIC (Page 14) Embedded Systems Design Europe - February 2008 - The Basics of Embedded Multitasking On a PIC (Page 15) Embedded Systems Design Europe - February 2008 - The Basics of Embedded Multitasking On a PIC (Page 16) Embedded Systems Design Europe - February 2008 - The Basics of Embedded Multitasking On a PIC (Page 17) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 18) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 19) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 20) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 21) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 22) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 23) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 24) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 25) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 26) Embedded Systems Design Europe - February 2008 - Cover Feature: The Art of FPGA Construction (Page 27) Embedded Systems Design Europe - February 2008 - Is Symmetric Multiprocessing For You? (Page 28) Embedded Systems Design Europe - February 2008 - Is Symmetric Multiprocessing For You? (Page 29) Embedded Systems Design Europe - February 2008 - Is Symmetric Multiprocessing For You? (Page 30) Embedded Systems Design Europe - February 2008 - Is Symmetric Multiprocessing For You? (Page 31) Embedded Systems Design Europe - February 2008 - Accelerating MATLAB Using MEX-Files (Page 32) Embedded Systems Design Europe - February 2008 - Accelerating MATLAB Using MEX-Files (Page 33) Embedded Systems Design Europe - February 2008 - Accelerating MATLAB Using MEX-Files (Page 34) Embedded Systems Design Europe - February 2008 - ARM Provides the Microcontroller Solution (Page 35) Embedded Systems Design Europe - February 2008 - ARM Provides the Microcontroller Solution (Page 36) Embedded Systems Design Europe - February 2008 - ARM Provides the Microcontroller Solution (Page 37) Embedded Systems Design Europe - February 2008 - Embedded World (Page 38) Embedded Systems Design Europe - February 2008 - Embedded World (Page 39) Embedded Systems Design Europe - February 2008 - Embedded World (Page 40) Embedded Systems Design Europe - February 2008 - Embedded World (Page 41) Embedded Systems Design Europe - February 2008 - Embedded World (Page 42) Embedded Systems Design Europe - February 2008 - Advertising Contracts (Page 43) Embedded Systems Design Europe - February 2008 - Advertising Contracts (Page Cover4)
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.