Title: ITC Research Computing Support Using Matlab Effectively
1ITC Research Computing SupportUsing Matlab
Effectively
- By Ed Hall
- Research Computing Support Center
- Phone 243-8800 F Fax 243-8765
-
- E-Mail Res-Consult_at_Virginia.EDU
- http//www.itc.Virginia.edu/researchers
2Matlab Overview
3Matlab Overview
4Matlab Overview
5Matlab Overview
6Matlab Overview
- UVA Matlab Resource webpage
- www.itc.virginia.edu/research/matlab/
- Matlab tutorial Getting Started
- www.mathworks.com/access/helpdesk/help/techdoc/l
earn_matlab/learn_matlab.shtml
7Matlab Programming
8Matlab M-files
9Matlab Function M-files
10Matlab Function M-files
11Matlab Function M-files
12Maximizing MATLAB Performance
- Techniques for Improving Performance
- Performance Acceleration
- Sample Accelerated Program
- Measuring Performance
- Making Efficient Use of Memory
13Techniques for Improving Performance
14Vectorization (cont.)
- Logical indexing of arrays
15Vectorization (cont.)
16Preallocating Arrays
- Avoids overhead of dynamic resizing
- Reduces memory fragmentation
- Use array appropriate preallocation
17Preallocation (cont.)
- Preallocating a Nondouble Matrix
- Use repmat to enlarge arrays
18Other Ways to Speed Performance
- Coding loops in a MEX-file for speed using Matlab
compiler - Functions M-files are faster than scripts
- Load and Save are faster than File I/O Functions
- Avoid large background processes
19Performance Acceleration
- JIT-Accelerator, new in Matlab 6.5
- Make MATLAB as fast as C and Fortran
- Most improvement in self-contained loops
- As fast as vectorized code
- Improvement may vary depending on code
20What MATLAB Accelerates
- Performance enhancement supported on subset of
Matlab
21What Matlab Accelerates (cont.)
- Arrays shapes of lt 3 dimensions
- for loops faster as long as
- Conditional statements (if, elseif, while,
switch) that evaluate to a scalar - Overhead of manipulating small arrays
22What MATLAB Doesnt Accelerate
- Some data types and array shapes
- Calls to other functions (M-File of MEX)
- More than one operation on a line
- First instruction disqualifies entire line
23What MATLAB Doesnt Accelerate
- Changing data types or variable shapes
- Using complex constants in scripts
-
24Perfomance Acceleration (cont.)
- How Vectorization and Preallocation fit in
- Acceleration functionally equivalent to
vectorization - Choose method most understandable
- Preallocation still important
- Avoid running in debug mode
- Aviod command echoing
25Measuring Performance
- Profiling measure where program spends the most
time - Create simple implementation dealing with
obvious performance issues - Use profiler to identify performance bottlenecks
(80/20 rule) - Identify time-costly functions and where they are
called
26Measuring Performance (cont.)
- Problems identified by profiler solved by
- Avoiding unnecessary computation
- Changing algorithm to avoid time-costly
functions - Avoiding recomputation by storing results for
future use - Want most time spent on calls to small number of
builtin functions
27Matlab Profiler
- GUI interface to profile function
- See Matlab News Notes example
- www.mathworks.com/company/newsletter/may03/pr
ofiler.shtml - Process for Improving Performance
28Matlab Profiler (cont.)
29Matlab Profile Function
- Command line profiler
- gtgtprofile on detail builtin history
- gtgt(Execute your M-file)
- gtgtprofile report
- gtgtprofile off
- Some features not in GUI Profiler
30Making Efficient Use of Memory
- Memory management functions
31- Memory Management Functions (cont.)
32Ways to Conserve Memory
33Ways to Conserve Memory
- Compressing data in memory
- Memory fragmentaion prevents contiguous memory
block to store new large variable - Out of Memory message
- Pack function compresses data in memory,
freeing large contiguous blocks -
34Ways to Conserve Memory
35Unix-Specific Memory Topics
- Freeing clear memory on Unix
- Matlab does not free up cleared memory in Unix
until it Matlab exits - Save workspace variables
- Exit Matlab
- Startup Matlab
- Load variables back in new workspace
-
36Unix-Specific Memory Topics
- Additional Memory used to execute external
commands - Out of Memory error when executing Unix
command from within Matlab (using shell escape
(!) operator) - Unix allocates as much memory to subprocess as
parent process
37Unix-Specific Memory Topics
- Reusing Heap Memory
- Matlab request memory from Unix when not
enough available in Matlab heap
38Matlab Compiler
- Converts function M-files to C or C source code
- Uses native compiler to create executables
- Creates MEX-file from function M-file, e.g.
function.m - gtgtmcc x function
- Creates stand-alone executable from function
M-file - gtgtmcc m function
39Matlab Compiler
40Matlab Compiler
41Matlab Compiler
42Unix-Specific Compiler Topics
- Setting LD_LIBRARY_PATH (e.g. in .kshrc) to
locate shared libraries for stand-alone
executables
43Running Matlab in Batch Mode
44Further Resources
- Using Matlab Programming and Data Types
- www.mathworks.com/access/helpdesk/help/techdoc/mat
lab_prog/matlab_prog.shtml - Code Vectorization Guide
- www.mathworks.com/support/tech-notes/v5/1100/11
09.shtml - Matlab Compiler Users Guide
- www.mathworks.com/access/helpdesk/help/pdf_doc/co
mpiler/compiler3.pdf