Writing Fast Matlab Code #4: JIT Acceleration

October 21, 2009 by Pascal Getreuer · Leave a Comment
Filed under: Code Optimization, Tutorials 
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 6.0/10 (2 votes cast)

Writing Fast Matlab code

Matlab 6.5 (R13)  and  later  feature  the  Just-In-Time (JIT) Accelerator  for improving  the  speed  of M-functions,  particularly with loops.  By knowing a few things  about  the accelerator, you can improve its performance.

The  JIT  Accelerator  is enabled  by default.   To disable  it,  type  “feature accel off” in the  console, and “feature accel on” to enable it again.

As of Matlab R2008b,  only a subset  of the  Matlab language  is supported for acceleration.  Upon encountering an unsupported feature,  acceleration  processing falls back to non-accelerated evaluation. Acceleration  is most effective when significant contiguous  portions  of code are supported.

  • Data types: Code  must  use  supported data   types  for  acceleration:  double (both  real  and complex),  logical,  char, int8–32,  uint8–32. Some  struct,  cell, classdef,  and  function handle  usage is supported. Sparse arrays  are not accelerated.
  • Array shapes: Array shapes of any size with 3 or fewer dimensions are supported. Changing  the shape  or data  type of an array  interrupts acceleration. A few limited  situations with  4D arrays are accelerated.
  • Function calls: Calls to built-in  functions  and M-functions  are accelerated. Calling MEX func- tions and Java  interrupts acceleration. (See also page 14 on inlining simple functions.)
  • Conditionals and loops: The conditional statements 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.

In-place computation

Introduced in Matlab 7.3 (R2006b),  the element-wise operators (+, .*, etc.)  and some other  functions can be computed  in-place.  That is, a computation like

x  =  5*sqrt(x.ˆ2  +  1);

is handled  internally without needing  temporary storage  for accumulating the  result.   An M-function can also be computed in-place if its output argument matches  one of the input  arguments.

x  =  myfun(x);
 
function x  =  myfun(x) x = 5*sqrt(x.ˆ2 + 1); return;

To enable  in-place computation, the  in-place  operation  must  be within  an M-function  (and  for an in- place function,  the function  itself must  be called within  an M-function). Currently, there  is no support for in-place computation with MEX-functions.

Multithreaded Computation

Matlab 7.4 (R2007a)  introduced multithreaded computation for multicore  and  multiprocessor  com- puters.  Multithreaded computation accelerates  some per-element functions when applied to large arrays (for example, .^, sin, exp) and certain  linear algebra functions in the BLAS library.  To enable it, select File → Preferences  → General → Multithreading and select “Enable  multithreaded computation.” Fur- ther  control  over parallel  computation is possible with the Parallel  Computing Toolbox  using parfor and spmd.

JIT-Accelerated Example

For example,  the following loop-heavy  code is supported for acceleration:

function B  =  bilateral(A,sd,sr,R)
 
%   The   bilateral image denoising  filter
 
B  =  zeros(size(A));
 
for i1:size(A,1)
 
for j1:size(A,2)
 
zsum   =  0;
 
for m   = -R:R
 
if i+m  >= 1  &&   i+m  <= size(A,1)
 
for n  = -R:R
 
if j+n >= 1  &&   j+n <= size(A,2)
 
z =  exp(-(A(i+m,j+n)-A(i,j))^2/(2*sd^2))...
 
*exp(-(m^2  +  n^2)/(2*sr^2));
 
zsum   =  zsum   +  z;
 
B(i,j) =  B(i,j) +  z*A(i+m,j+n);
 
end
 
end
 
end
 
end
 
end
 
end
 
B(i,j) =  B(i,j)/zsum;

For  a 128 × 128 input  image  and  R = 3, the  run  time  is 53.3 seconds  without acceleration  and  0.68 seconds with acceleration.

Download Full Pdf Version

function B = bilateral(A,sd,sr,R)

% The bilateral image denoising filter

B = zeros(size(A));

for i = 1:size(A,1)

for j = 1:size(A,2)

zsum = 0;

for m = -R:R

if i+m >= 1 && i+m <= size(A,1)

for n = -R:R

if j+n >= 1 && j+n <= size(A,2)

z = exp(-(A(i+m,j+n)-A(i,j))^2/(2*sd^2))

*exp(-(m^2 + n^2)/(2*sr^2));

zsum = zsum + z;

B(i,j) = B(i,j) + z*A(i+m,j+n);

end

end

end

end

end

end

B(i,j) = B(i,j)/zsum;

VN:F [1.8.8_1072]
Rating: 6.0/10 (2 votes cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS