ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support...

44
ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support Center Phone: 243-8800 Φ Fax: 243-8765 E-Mail: [email protected] http://www.itc.Virginia.edu/researcher s

Transcript of ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support...

Page 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.

ITC Research Computing SupportUsing Matlab Effectively

By: Ed Hall

Research Computing Support Center

Phone: 243-8800 Φ Fax: 243-8765

E-Mail: [email protected]

http://www.itc.Virginia.edu/researchers

Page 2: 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.

Matlab Overview

Page 3: 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.

Matlab Overview

Page 4: 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.

Matlab Overview

Page 5: 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.

Matlab Overview

Page 6: 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.

Matlab Overview

• UVA Matlab Resource webpage www.itc.virginia.edu/research/matlab/

• Matlab tutorial: Getting Started

www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/learn_matlab.shtml

Page 7: 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.

Matlab Programming

Page 8: 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.

Matlab M-files

Page 9: 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.

Matlab Function M-files

Page 10: 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.

Matlab Function M-files

Page 11: 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.

Matlab Function M-files

Page 12: 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.

Maximizing MATLAB Performance

• Techniques for Improving Performance

• Performance Acceleration

• Sample Accelerated Program

• Measuring Performance

• Making Efficient Use of Memory

Page 13: 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.

Techniques for Improving Performance

• Vectorizing loops

Page 14: 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.

Vectorization (cont.)

• Logical indexing of arrays

Page 15: 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.

Vectorization (cont.)

Page 16: 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.

Preallocating Arrays

• Avoids overhead of dynamic resizing

• Reduces memory fragmentation

• Use array appropriate preallocation

Page 17: 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.

Preallocation (cont.)

• Preallocating a Nondouble Matrix

• Use repmat to enlarge arrays

Page 18: 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.

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

Page 19: 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.

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

Page 20: 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.

What MATLAB Accelerates

• Performance enhancement supported on subset of Matlab

Page 21: 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.

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

Page 22: 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.

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

Page 23: 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.

What MATLAB Doesn’t Accelerate

• Changing data types or variable shapes

• Using complex constants in scripts

Page 24: 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.

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

Page 25: 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.

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

Page 26: 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.

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

Page 27: 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.

Matlab Profiler

• GUI interface to profile function

• See “Matlab News & Notes” example www.mathworks.com/company/newsletter/may03/profiler.shtml

• Process for Improving Performance

Page 28: 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.

Matlab Profiler (cont.)

Page 29: 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.

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

Page 30: 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.

Making Efficient Use of Memory

• Memory management functions

Page 31: 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.

• Memory Management Functions (cont.)

Page 32: 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.

Ways to Conserve Memory

Page 33: 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.

Ways 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

Page 34: 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.

Ways to Conserve Memory

Page 35: 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.

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

Page 36: 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.

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

Page 37: 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.

Unix-Specific Memory Topics

• Reusing Heap Memory

Matlab request memory from Unix when not enough available in Matlab heap

Page 38: 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.

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

Page 39: 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.

Matlab Compiler

Page 40: 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.

Matlab Compiler

Page 41: 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.

Matlab Compiler

Page 42: 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.

Unix-Specific Compiler Topics

• Setting LD_LIBRARY_PATH (e.g. in .kshrc) to locate shared libraries for stand-alone executables

Page 43: 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.

Running Matlab in Batch Mode

• On a Unix platform

Page 44: 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.

Further Resources

• Using Matlab: Programming and Data Typeswww.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/matlab_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/compiler3.pdf