matlab_3 - The University of Sheffield High Performance and

advertisement
MATLAB
Building and Running Faster Matlab
UNIVERSITY OF SHEFFIELD
CiCS DEPARTMENT
Deniz Savas & Mike Griffiths
JULY.2008
PART 3TOPICS
• Improving Performance and The
Profiler
• Running Matlab on Iceberg
• Matlab Compiler
Making Matlab Use More Compute Resource
• Compute Cluster e.g. Iceberg
• Uses a scheduler e.g. Sun Grid Engine
• Break a matlab job in to many independent tasks
– E.g. cycle of a for loop might run independently
• Array of matlab tasks running on compute cluster
• Can run 1000’s of tasks
• Good for
– parametric sampling
– Genetic algorithms
– Model ensembles
Submitting a matlab job to Sun Grid Engine
on Iceberg
• qsub mymatlabjob.sh
• Where mymatlabjob.sh is:
#!/bin/sh
#$ -cwd
#$ -l h_rt=hh:mm:ss
#$ -l mem=1G
/usr/local/bin/matlab -nojvm -nosplash -nodisplay < matlabscriptfile > outputfile
Access to More Compute Resources
• Grids
– White Rose Grid/N8
• Benefits
– Access to data
– More compute resources
– Share applications with collaboration partners
• Example lensless microscopy project at Sheffield
Final Remarks
• Task arrays are a powerful method utilising large
pools of compute resources e.g. clusters
• Mainly use batch submission mode not interactive
use of matlab
• Challenging to utilise compute resource outside
adminstrative domain
– Difficulty alleviated using geodise toolkit
• Parallel computing toolbox enables researchers to
develop interactive matlab applications able to
exploit a pool of compute resources
Practice session 9
• Open the folder iceberg_sgearray
• Submit the beats job as an SGE task array
• Open an interactive session using qsh and use the
command
– qsub beats.sh
Improving Performance of Matlab
•
•
•
•
•
Using profiler – to check for bottlenecks
Built in functions
Vectorisation
Pre loading
Multi core
Built In Functions
•
Some matlab routines are *.m files, others are
compiled into matlab (built-in) and are much faster.
Try to use built-ins where possible. Use type
functionname to see if a function is a built-in.
Vectorisation
•
•
Matlab scripts can be written using fortran/C-style
"for" loops, but to make the most of matlab's abilities
you should try whenever possible to treat matrices
as a single entity rather than a bundle of elements.
Vectorisation functions
▬
▬
▬
arrayfun
repmat
bsxfun (Binary Singleton eXpansion FUNction)
Vectorisation Example 1:
for i = 1:100
for j = 1:100
r(i,j) = sqrt(i^2+j^2);
end
end
Vectorised this becomes
[i,j]=meshgrid(1:100,1:100);
r = sqrt(i.^2+j.^2);
Vectorisation Example 2:
•
Vectorise the following, where elements depend on
previous ones
n=1000; x(1)=1;
for j=1:n-1,
x(j+1) = x(j) + n - j;
end
Vectorised this becomes
n=1000; x(1)=1;
j=1:n-1;
x(j+1) = n - j;
cumsum(x);
Monitoring Memory Usage with Matlab
•
•
•
•
•
•
Matlab has the ability to increase the size of a matrix on demand.
If you know the maximum size of a matrix beforehand, it's worth
creating the matrix with this size initially rather than making the matrix
grow incrementally. Using cell and zeros function
Speed-up factors of 500 are possible using this method.
Common problems include deframgementation of matlab memory
Use pack reorganises memory so minimal space used
Try different memory managers
▬
▬
▬
•
•
▬
Monitor memory utilisation within matlab by using
▬
feature memstats
When submitting task to iceberg specify memory requirements e.g.
▬
▬
•
cache (default), compact, fast and debug (
Specify using –memmgr switch on matlab command line
E.g. matlab –memmgr debug
matlab –memmgr fast
qsh –l mem=2G etc…
qsub –l mem=2G etc… (or make sure #$ -l mem=2G is at the head of the
script file)
pload function in parallel matlab loads data onto nodes
Using Multicore
•
•
•
•
•
Can yield improved performance
Select File Preferences
From preferences menu select
▬
General->Multithreading
Tick enable multithreaded computation
Use maxNumCompThreads to setthe number of threads
▬
N = maxNumCompThreads returns the current maximum number
of computational threads N.
▬
LASTN = maxNumCompThreads(N) sets the maximum number of
computational threads to N, and returns the previous maximum
number of computational threads, LASTN.
The profiler
•
•
•
•
This facility is enhanced recently at version 6.x, which enables
the user to investigate the time take taken to run a matlab task
or a script. The profiling reports help us to improve the efficiency
of the code by pin-pointing the parts of the code that takes most
of the time.
Type profiler to start the profiler up and
In the run this code field enter the name of your script file, or
keep that field clear and just click on Start profiling followed by
running your task as usual from the Matlab Command Window
and when finish click on the stop profiling icon.
Clicking on the profiling summary icon will display a profile
summary which can be expanded by clicking on the routine
names on the list.
Practice Session 9
•
Profiling
▬
▬
▬
▬
In the folder vectorize run the scripts genar.m and
genarray.m. Compare the performance and compare the
scripts.
See the file named findrt.m in the examples directory - sub
directory: root
Study this script and run it under the control of Matlab
debugger .
Run the scripts animpeaks1.m and animpeaks2.m and
compare using the profiler
Using MEX to Build Faster Applications
•
Motivation
▬
▬
•
Set up MEX
▬
•
mex -setup
Compile c routines using mex
▬
•
Matlab excellent for prototyping
For raw speed not as good
Simple examples
Basics of Mex programming
▬
Need to be a C programmer
Example - timestwo
•
•
Example to multiply a scalar by 2, timestwo.c
Compile the mex file
▬
•
mex timestwo.c
Run the mex file takes as input one scalar
▬
timestwo(3)
Example xtimesy
•
•
Example multiply a matrix by a scalar
Compile
▬
•
mex xtimesy.c
Run the example the matlab function takes as input
a scalar and a matrix
▬
▬
v1=rand(2,2)
v2=xtimesy(5.0,v1)
Example demo.cpp
•
Take as input two matrices
▬
▬
▬
•
Compile
▬
•
Add a scalar fixed scalar to one (not input)
Take the square of the second matrix
Display each of the elements of the first matrix
mex demo.cpp
Run the demo
▬
▬
▬
v1=rand(2,2)
v2=rand(3,3)
[vo1,vo2]=demo(v1,v2)
What does Matlab Compiler do ?
•
•
Compiles ‘builds’ a Matlab application into a stand alone software.
It can be used to ;
▬
▬
▬
▬
Package Matlab applications as a stand-alone executable (.exe) or
dynamic link library (.dll or .so )
Distribute these applications as independent ( free to use) software
that does not require installing Matlab at the destination computer.
Incorporate Matlab developed code into programs written in other
languages.
Internally, Matlab Compiler makes use of the MEX ‘external-interface’
features of Matlab that are used for linking external Fortran and C
programs with Matlab.
How do I use the Matlab Compiler ?
•
•
•
•
•
•
•
COMMAND LINE: Matlab compiler can be accessed by using
the mcc ( Matlab Compiler Command ) from the Matlab
command line.
GUI : It can also be used in a GUI environment by using the
deployment tool. Via Desktop  Deployment Tool
or
by typing deploytool at the command line.
GUI method is recommended as it is intuitive and easier to use.
The GUI method, invokes the mcc command-line methods in the
background as and when it is needed.
Compiler Deployment
•
•
•
•
•
•
Terminology:
Matlab Compiler (mcc) : Matlab utility that automatically converts a
Matlab script (.m file ) into a C/C++ source file ( .c / .c++ / .cpp )
Compiler : A platform dependent C/C++ compiler compatible with
Matlab, such as Lahey, g++, Intel or Microsoft compilers.
Building an Application: The act of generating a stand-alone (.exe) or
Dynamic Link Library (.dll or .so ) from one or more .m or .c or .c++
source files.
Deployment : Running the built application on another machine that
uses the same platform ( ie. Windows/Linux/Mac 64bit/32bit )
MCR : Matlab Compiler Runtime. Software components needed to run
mcc generated software. This is independent of your own software but
dependent on platform.
Overview
•
Your Matlab
Script (.m)
The Matlab
Compiler
Stand Alone
executable (.exe)
Shared Library
( .dll or .so )
How does the Matlab Compiler Work
•
Tasks performed internally by the Matlab Compiler
•
•
•
•
•
•
•
•
Parse the command line
Performs dependency analysis to create a CTF ( Component Technology
File Archive )
Check to ensure all dependencies are satisfied
Auto generates C/C++ code by translating the users Matlab script
file(s).
Auto generates target specific wrapper code files
Passes all files that needs to be compiled ‘including any MEX files that
are provided’ to the native compiler.
Generates the required software item ( i.e. .exe , .dll , .so , .jar file )
Note that most of the internal Matlab functions are not translated but
simply referenced from the Matlab Compiler Runtime Library (MCR) (in
the form of .DLL or .so) . That is why MCR is needed.
Practice – using the matlab compiler
•
Using mex compile and run the example c programs
▬
▬
▬
•
•
xtimesy.c
demo.cpp
timestwo.c
The examples are in the mex folder of the
matlab_examples
Adapt the program timestwo.c so that it adds two
scalar values
Download