Download presentation
Presentation is loading. Please wait.
Published byRegina Pitts Modified over 9 years ago
1
ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support Center Phone: 243-8800 Φ Fax: 243-8765 E-Mail: Res-Consult@Virginia.EDU http://www.itc.Virginia.edu/researchers
2
Matlab Overview
6
UVA Matlab Resource webpage www.itc.virginia.edu/research/matlab/ Matlab tutorial: Getting Started www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/le arn_matlab.shtml www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/le arn_matlab.shtml
7
Matlab Programming
8
Matlab M-files
9
Matlab Function M-files
12
Maximizing MATLAB Performance Techniques for Improving Performance Performance Acceleration Sample Accelerated Program Measuring Performance Making Efficient Use of Memory
13
Techniques for Improving Performance Vectorizing loops
14
Vectorization (cont.) Logical indexing of arrays
15
Vectorization (cont.)
16
Preallocating Arrays Avoids overhead of dynamic resizing Reduces memory fragmentation Use array appropriate preallocation
17
Preallocation (cont.) Preallocating a Nondouble Matrix Use repmat to enlarge arrays
18
Other 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
19
Performance 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
20
What MATLAB Accelerates Performance enhancement supported on subset of Matlab
21
What Matlab Accelerates (cont.) Arrays shapes of <= 3 dimensions for loops faster as long as: Conditional statements (if, elseif, while, switch) that evaluate to a scalar Overhead of manipulating small arrays
22
What MATLAB Doesn’t 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
23
What MATLAB Doesn’t Accelerate Changing data types or variable shapes Using complex constants in scripts
24
Perfomance 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
25
Measuring 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
26
Measuring 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
27
Matlab Profiler GUI interface to profile function See “Matlab News & Notes” example www.mathworks.com/company/newsletter/may03/profiler.shtml Process for Improving Performance
28
Matlab Profiler (cont.)
29
Matlab Profile Function Command line profiler >>profile on –detail builtin –history >>(Execute your M-file) >>profile report >>profile off Some features not in GUI Profiler
30
Making Efficient Use of Memory Memory management functions
31
Memory Management Functions (cont.)
32
Ways to Conserve Memory
33
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
34
Ways to Conserve Memory
35
Unix-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
36
Unix-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
37
Unix-Specific Memory Topics Reusing Heap Memory Matlab request memory from Unix when not enough available in Matlab heap
38
Matlab 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 >>mcc –x function Creates stand-alone executable from function M-file >>mcc –m function
39
Matlab Compiler
42
Unix-Specific Compiler Topics Setting LD_LIBRARY_PATH (e.g. in.kshrc) to locate shared libraries for stand-alone executables
43
Running Matlab in Batch Mode On a Unix platform
44
Further Resources Using Matlab: Programming and Data Types www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/matla b_prog.shtml Code Vectorization Guide www.mathworks.com/support/tech-notes/v5/1100/1109.shtml Matlab Compiler User’s Guide www.mathworks.com/access/helpdesk/help/pdf_doc/compiler/compi ler3.pdf www.mathworks.com/access/helpdesk/help/pdf_doc/compiler/compi ler3.pdf
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.