1 / 27

MATLAB Building and Running Faster Matlab

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.

jersey
Download Presentation

MATLAB Building and Running Faster Matlab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB Building and Running Faster Matlab UNIVERSITY OF SHEFFIELD CiCS DEPARTMENT Deniz Savas & Mike Griffiths JULY.2008

  2. PART 3TOPICS • Improving Performance and The Profiler • Running Matlab on Iceberg • Matlab Compiler

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

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

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

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

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

  8. Improving Performance of Matlab • Using profiler – to check for bottlenecks • Built in functions • Vectorisation • Pre loading • Multi core

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

  10. 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)

  11. Vectorisation Example 1: for i = 1:100 for j = 1:100 r(i,j) = sqrt(i^2+j^2); end end Vectorised thisbecomes [i,j]=meshgrid(1:100,1:100); r = sqrt(i.^2+j.^2);

  12. 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 thisbecomes n=1000; x(1)=1; j=1:n-1; x(j+1) = n - j; cumsum(x);

  13. 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 • cache (default), compact, fast and debug ( • Specify using –memmgr switch on matlab command line • E.g. matlab –memmgr debug • matlab –memmgr fast • Monitor memory utilisation within matlab by using • feature memstats • When submitting task to iceberg specify memory requirements e.g. • 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

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

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

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

  17. Using MEX to Build Faster Applications • Motivation • Matlab excellent for prototyping • For raw speed not as good • Set up MEX • mex -setup • Compile c routines using mex • Simple examples • Basics of Mex programming • Need to be a C programmer

  18. 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)

  19. 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)

  20. Example demo.cpp • Take as input two matrices • 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 • Compile • mex demo.cpp • Run the demo • v1=rand(2,2) • v2=rand(3,3) • [vo1,vo2]=demo(v1,v2)

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

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

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

  24. Overview Your Matlab Script (.m) The Matlab Compiler Stand Alone executable (.exe) Shared Library ( .dll or .so )

  25. How does the Matlab Compiler Work

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

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

More Related