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

Post on 05-Jan-2016

213 views 0 download

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

ITC Research Computing SupportUsing 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

Matlab Overview

Matlab Overview

Matlab Overview

Matlab Overview

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

Matlab Programming

Matlab M-files

Matlab Function M-files

Matlab Function M-files

Matlab Function M-files

Maximizing MATLAB Performance

• Techniques for Improving Performance

• Performance Acceleration

• Sample Accelerated Program

• Measuring Performance

• Making Efficient Use of Memory

Techniques for Improving Performance

• Vectorizing loops

Vectorization (cont.)

• Logical indexing of arrays

Vectorization (cont.)

Preallocating Arrays

• Avoids overhead of dynamic resizing

• Reduces memory fragmentation

• Use array appropriate preallocation

Preallocation (cont.)

• Preallocating a Nondouble Matrix

• Use repmat to enlarge arrays

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

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

What MATLAB Accelerates

• Performance enhancement supported on subset of Matlab

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

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

What MATLAB Doesn’t Accelerate

• Changing data types or variable shapes

• Using complex constants in scripts

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

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

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

Matlab Profiler

• GUI interface to profile function

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

• Process for Improving Performance

Matlab Profiler (cont.)

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

Making Efficient Use of Memory

• Memory management functions

• Memory Management Functions (cont.)

Ways to Conserve Memory

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

Ways to Conserve Memory

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

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

Unix-Specific Memory Topics

• Reusing Heap Memory

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

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

Matlab Compiler

Matlab Compiler

Matlab Compiler

Unix-Specific Compiler Topics

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

Running Matlab in Batch Mode

• On a Unix platform

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