Debugging and performance tuning of Matlab Codes

Debugging and performance tuning of Matlab Codes Shuxia Zhang Supercomputing Institute For Advanced Computational Research e-mail: [email protected] ...
Author: Clara Golden
2 downloads 0 Views 129KB Size
Debugging and performance tuning of Matlab Codes Shuxia Zhang Supercomputing Institute For Advanced Computational Research e-mail: [email protected] or [email protected] Tel: 612-624-8858 (direct) , 612-626-0802(help)


Outline Matlab Debugging Debugging from the GUI Debugging at the command prompt Performance tuning Analyzing your code Manually modify your code Hands-on exercises


Introduction •  •  •  • 

MATLAB - One of the most practical software used for numerical calculation and system design. Flaws (or bugs) may not be avoidable It is very easy in Matlab to write a program that runs really slowly. Objective - Better use of the computing resources by developing more efficient MATLAB codes better memory access, and shorter execution time.


Matlab debugging Three main types of bugs • 

typo, or typographic error: cod(pi) - ??? Undefined function or variable ‘cod’ •  A syntax error : Use of functions Arrays dimension does not match •  An algorithmic error: The result you receive is not what you expect. •  Combination of the above three


Procedure for graphical debugging

Matlab debugging

1.  Open your program at the command line, i.e., >> edit your.m %your matlab code is shown in a separate window 1.  Do the rest on the new window where the src is shown 1.  Set breakpoint and run 2.  Open selection 3.  Evaluate selction 4.  Help on selection 5.  Continue the run (f5 key) 6.  Fix the bugs 3. Close all the selected windows by the close icon under File, not the Exit Matlab one


Matlab debugging Debugging from the Command Prompt Many of the techniques discussed in Editor/Debugger have their counterparts at the command prompt. •  •  •  •  •  •  •  • 

dbstop in my.m at 10 dbstop in my at funtion1 dbstop if error dbstop if warning dbstop if naninf or dbstop if infnan dbclear all in my.m dbclear all dbstatus - List all breakpoints 6

Performance tuning Useful Matlab tools for understanding your code mlint - Check M-files for possible problems >> mlint lengthofline.m % Display to command line >> info = mlint('lengthofline') % Store to struct tic/toc - accurate timing of each operation/function >> tic; a=rand(1000); toc; Matlab Profiler - find where the bottle neck is >> profile on >> calcultion; >> profile off >> profile report 7

Performance tuning Useful Matlab tools for understanding the computer Multiple threadings – Many functions now support multithreaded computation: linear algebra operations that call the BLAS library and element-wise numerical operations >> [threads Operations] = runAndTimeOps Time a number of operations and return the times plus their names. For example: >> maxNumCompThreads >> maxNumCompThreads(1); >> [threads Operations] = runAndTimeOps >> maxNumCompThreads(2); >> [threads Operations] = runAndTimeOps 8

Performance tuning Multiple threadings to increase performance without modifying your code How many threads can your job use? How many threads should your job use? Why does my matlab job run much slower? maxNumCompThreads - Control maximum number of computational threads - Syntax N = maxNumCompThreads M = maxNumCompThreads(2) matlab -nodisplay -r "maxNumCompThreads(1)" < my.m On elmo, it speeds up the calculations. 9

JIT Acceleration - without modifying your code >> feature accel off >> tic; >> JITexam; >> toc; >> feature accel on >> tic; >> JITexam; >> toc;


Use of Just-In-Time (JIT) Accelerator Matlab 6.5 (R13) and later feature the Just-In-Time (JIT) Accelerator for improving the speed. As of Matlab R2008b, only a subset of the Matlab language is supported : Data types: double (both real and complex), logical, char, int8-32, uint8-32. Some struct, cell, classdef, and function handle usage. Sparse arrays - not Array shapes: Array shapes of any size with 3 or fewer dimensions Function calls: Calls to built-in functions and M-functions are supported. Calling MEX functions and Java interrupts acceleration. Conditionals and loops: if, elseif, and simple switch statements are supported if the conditional expression evaluates to a scalar. Loops of the form for k=a:b, for k=a:b:c, and while loops are accelerated if all code within the loop is supported. 11

Performance tuning General rules for improving Matlab codes •  •  •  •  •  • 

Improve the algorithm before attempting to optimize the code. Benefit from MATRIX abilities and built-in functions. Avoid using for-loop and if-statements Break down the code into separate m-file functions and reuse functions. Use comments as much as possible to document the code. Manually tune the code

Bottomline: Correct results are always essential!


Performance tuning Manually tuning the code - MATLAB is very fast on vector and matrix operations • 

Use built-in function predefined MATLAB constants are built-in functions >> type i pi j inf nan % to check


Try to void for-loop and if-statements Take a look at the helps for the following MATLAB functions:

meshgrid, repmat, reshape, find, any, all Use them as much as possible Bottom line: Correct results are essential.


Manually tuning the code • 


Pre-allocate memory using zeros, ones, eye, etc for initializing matrix: e.g., A= zeros(n);

tic; N=1000; for i=1:N; for j=1:N x(i,j)=i+j; end end takes 5.13 For a toc cell% array, the sfollowing

tic; N=1000; x=zeros(N); for i=1:N, for j=1:N x(i,j)=i+j; end end toc % takes 0.1 s

>> C = cell(m, n, p,...) will creates an m-by-n-by-p-... cell array of empty matrices. Arguments m, n, p,... must be scalars. 14

Manually tuning the code • 

Store matrices in column order, Array index and subscript: tic; N=1000; x=zeros(N); for i=1:N, for j=1:N x(i,j)=i+j; end end toc % takes 0.104 s


tic; N=1000; x=zeros(N); for j=1:N, for i=1:N x(i,j)=i+j; end end toc % takes 0.09 s

Avoid creating unnecessary variables or in place operation function x = myfuncIP(x) x = sin(2*x.^2+3*x+4); 15


Performance tuning Manually tuning code

Given an=n and bn=1000−n for n=1, 2,...,1000 Calculate s = ∑ (an* bn ) >> a = 1:1000; >> b = 1000 - a; >> ssum=0; >> for n=1:1000 ssum = ssum +a(n)*b(n); end Recognizing that the sum is the inner product of the vectors a and b >> ssum = a*b' %Vectorized, better 16

Performance tuning Manually tuning the code - Vectorization Calculation of two-dimensional functions such as F(x, y) = x*exp(−x2−y2) >> x = (−2:2); >> y = (−1.5:.5:1.5); >> % first method. Not efficient! >> F = zeros(length(x),length(y)); >> for i = 1:length(x), >> for j = 1:length(y), >> F(i,j) = x(i)*exp(−x(i)ˆ2−y(j)ˆ2); >> end >> end

>> % second method. Better! >> [X Y] = meshgrid(x,y); >> F = X.*exp(−X.ˆ2−Y.ˆ2); >> % Efficient! >> F = (x'.*exp(−x'.ˆ2)) * exp(−y.ˆ2);


Hands-on Exercise module load matlab matlab Exercise 1: How many threads are available? >> maxNumCompThreads Exercise 2: Can your code take advantage of multi-cores? >> maxNumCompThreads(1) >> [threads Operations] = runAndTimeOps >> run_job

>> maxNumCompThreads(2) >> [threads Operations] = runAndTimeOps >> run_job 18

Matlab debugging and Performance tuning

Exercise 3: Matlab Debugging Download the example files: gunzip debugging.tar.gz tar debugging.tar.gz find and fix the bugs in the example cases,

Exercise 4: Performance Tuning Download the example files: tar -xvf tune.tar 1. Use of mlint and profiler 2. Improve the performance of slow1.m, slow2.m 3. Use of JIT accelerator


Reference: Writing fast matlab code, Pascal Getreuer, February 2009 Writing Efficient MATLAB® Codes, Reza Sameni, 2006 Need help? Send e-mail to: [email protected] [email protected] Or call at 612-624-8858 612-626-0802