ITC Research Computing Support Using Matlab Effectively - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

ITC Research Computing Support Using Matlab Effectively

Description:

Uses native compiler to create executables ... Setting LD_LIBRARY_PATH (e.g. in .kshrc) to locate shared libraries for stand-alone executables ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 45
Provided by: rcsc8
Category:

less

Transcript and Presenter's Notes

Title: ITC Research Computing Support Using Matlab Effectively


1
ITC 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

2
Matlab Overview
3
Matlab Overview
4
Matlab Overview
5
Matlab Overview
6
Matlab 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

7
Matlab Programming
8
Matlab M-files
9
Matlab Function M-files
10
Matlab Function M-files
11
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 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

22
What 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

23
What MATLAB Doesnt 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/pr
    ofiler.shtml
  • Process for Improving Performance

28
Matlab Profiler (cont.)
29
Matlab 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

30
Making Efficient Use of Memory
  • Memory management functions

31
  • Memory Management Functions (cont.)

32
Ways to Conserve Memory
33
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

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
  • gtgtmcc x function
  • Creates stand-alone executable from function
    M-file
  • gtgtmcc m function

39
Matlab Compiler
40
Matlab Compiler
41
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/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
Write a Comment
User Comments (0)
About PowerShow.com