a case for source level transformations in matlab
Download
Skip this Video
Download Presentation
A Case for Source-Level Transformations in MATLAB

Loading in 2 Seconds...

play fullscreen
1 / 42

A Case for Source-Level Transformations in MATLAB - PowerPoint PPT Presentation


  • 62 Views
  • Uploaded on

A Case for Source-Level Transformations in MATLAB. Vijay Menon and Keshav Pingali Cornell University. The MaJic Project at Illinois/Cornell. George Almasi Luiz De Rose David Padua. MATLAB. High-Level Interpreted Language for Numerical Computing Matrix is 1st class type

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'A Case for Source-Level Transformations in MATLAB' - breena


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
a case for source level transformations in matlab

A Case for Source-Level Transformations in MATLAB

Vijay Menon and Keshav Pingali

Cornell University

The MaJic Project

at Illinois/Cornell

George Almasi

Luiz De Rose

David Padua

matlab
MATLAB
  • High-Level Interpreted Language for Numerical Computing
    • Matrix is 1st class type
    • Library of numerical functions
  • Application Domains
    • Image Processing
    • Structural Mechanics
    • Computational Finance
the problem
The Problem
  • Development is fast...
    • ~10X as concise as C/Fortran
  • Performance is slow!
    • ~10X as slow as C/Fortran
  • Conventional Approach:
    • Rewrite
    • Compile
our approach source level optimization
Our Approach: Source-Level Optimization
  • Apply high-level transformations directly on MATLAB codes
  • Significant performance benefit for:
    • interpreted code
    • compiled code
outline
Outline
  • Overheads in MATLAB
  • Conventional Compilation
  • Source-Level Optimization
  • Comparison
  • Implementation Status
outline1
Outline
  • Overheads in MATLAB
    • Type/Shape Checking
    • Memory Management
    • Array Bounds Checking
  • Conventional Compilation
  • Source-Level Optimization
  • Comparison
  • Implementation Status
type shape checking
MATLAB has no type/shape declarations

Consider:

A * B

Interpreter checks to perform multiply (*)

Shape

Scalar*Scalar

Scalar*Matrix

Matrix*Matrix

Type/Shape Checking
  • Type
    • Real*Real
    • Real*Complex
    • Complex*Complex
type shape checking1
Consider:

for i = 1:n

y = y + a * x(i)

end

Loops

perform redundant checks

magnify interpreter overhead

Type/Shape Checking
memory management dynamic resizing
Memory Management: Dynamic Resizing
  • Consider:

x(10) = 10;

  • C/Fortran: x must have >= 10 elements
  • MATLAB: x is resized if needed
    • Memory reallocated
    • Data copied
memory management dynamic resizing1
Memory Management: Dynamic Resizing
  • MATLAB dynamically grows arrays:

for i = 1 : 1000

x(i) = i;

end

  • Every iteration triggers resize!
    • 1,000 memory allocations
    • ~500,000 elements copied
  • Execution Time:
    • x is undefined: 14.2 seconds
    • x is already defined: 0.37 seconds
array bounds checking
Array Bounds Checking
  • Consider array indexing:

x(i) = y(i);

  • Failed Bounds Check on
    • x(i) can trigger resize
    • y(i) can trigger error
array bounds checking1
Array Bounds Checking
  • In a loop:

for i = 3:100

x(i) = x(i-1) + x(i-2);

end

  • Interpreter performance redundant checks
  • Compiler work:
    • Nonresizable arrays: Gupta PLDI’90
    • Resizable arrays: more difficult
common theme
Common Theme
  • Loops magnify overheads
    • every iteration: redundant checks, resizes, …
  • MATLAB interprets naively
    • computes as is
    • no reorganization to optimize
outline2
Outline
  • Overheads in MATLAB
  • Conventional Compilation
    • Compile to C/Fortran
    • Rely on C/Fortran compiler for optimization
  • Source-Level Optimization
  • Comparison
  • Implementation Status
matlab compilers
MATLAB Compilers
  • Compile to C/C++/Fortran
    • MCC -> C (The MathWorks)
    • MATCOM -> C++ (Mathtools)
    • FALCON -> F90 (U of Illinois)
  • Native compiler generates executable code:
    • Link back into MATLAB environment
    • Run as stand-alone program
the mcc compiler
The MCC Compiler
  • Safe Optimization:
    • Type Inference - no declarations in MATLAB
    • Eliminate Type Checks / Reduce Storage
    • Specialize for real input variables
    • Always legal!
  • Unsafe Optimization:
    • Assume all data is real
    • Eliminate all bounds checks - disallow resizing
    • User must ensure legality!
falcon benchmarks
Falcon Benchmarks
  • Collected by DeRose from MATLAB users at Illinois/NCSA
    • Element/Loop Intensive
      • CN - Crank-Nicholson PDE Solver
      • Di - Dirichlet PDE Solver
      • FD - Finite Difference PDE Solver
      • Ga - Galerkin PDE Solver
      • IC - Incomplete Cholesky Factorization
    • Memory Intensive
      • AQ - Adaptive Quadrature w/ Simpson’s Rule
      • EC - Euler-Cromer 2 body problem
      • RK - Runga Kutta 2 body problem
    • Library Intensive
      • CG - Conjugate Gradients Iterative Solver
      • Mei - 3D surface Generation
      • QMR - Quasi-Minimal Residual
      • SOR - Successive Over-Relaxation AQ
mcc unsafe optimizations
MCC: Unsafe Optimizations

Note: User must ensure legality!

outline3
Outline
  • Overheads in MATLAB
  • Conventional Compilation
  • Source-Level Optimization
    • Vectorization
    • Preallocation
    • Expression Optimization
  • Comparison
  • Implementation Status
vectorization
Vectorization
  • Loops are expensive
    • Overheads are magnified
  • Idea: Eliminate Loops
    • Map loops to higher-level matrix operations
    • Interpreter uses efficient libraries
      • BLAS
      • LINPACK/EISPACK
example of vectorization
Example of Vectorization
  • In Galerkin, 98% of execution spent in:

for i = 1:N

for j = 1:N

phi(k) += a(i,j)*x(i)*y(i);

end

end

vectorized code
Vectorized Code
  • In Optimized Galerkin:

phi(k) += x*a*y’;

  • Fragment Speedup: 260
  • Program Speedup: 110
  • Note: Not always possible!
preallocation
Preallocation
  • Eliminate Dynamic Resizing
    • Try to predict eventual size of array
    • Insert early allocation when possible:
      • x = zeros(1000,1);
    • Resizing will not be triggered
example of preallocation
Example of Preallocation
  • In Euler-Cromer, 87% of time spent in:

for i = 1:N

r(i) = …

th(i) = …

t(i) = …

k(i) = …

p(i) = …

end

preallocated code
Preallocated Code
  • In Optimized Euler-Cromer:

r = zeros(1,N);

...

for i = 1:N

r(i) = …

end

  • Fragment Speedup: 7
  • Program Speedup: 4
expression optimization
Expression Optimization
  • MATLAB interprets expressions naïvely in left to right order
  • Simple restructuring may significantly effects execution time, e.g.:
    • A*B*x : O(n3) flops
    • A*(B*x) : O(n2) flops
example of expression optimization
Example of Expression Optimization
  • In QMR, 70% of execution spent in:

w = A’*q;

  • A : 420x420 matrix
  • q, w : 420x1 vectors
  • A’ = transpose(A)
expression optimized code
Expression Optimized Code
  • In Optimized QMR: A’*q == (q’*A)’

w = (q’*A)’;

  • Transpose 2 vectors instead 1 matrix
  • Fragment Speedup: 20
  • Program Speedup: 3
point 1
Point #1:
  • Source optimizations can outperform MCC
point 2
Point #2:
  • Source optimizations complement MCC
benefits of source level optimizations
Benefits of Source-Level Optimizations
  • Vectorization
    • Directly eliminates loop overhead
    • Move work to hand-optimized BLAS
  • Preallocation
    • Eliminates resizing overhead
    • Enables MCC array bounds elimination
  • Expression Optimization
    • Uses algebraic info unavailable in C/Fortran
implementation status
Implementation Status
  • Illinois/Cornell MaJic system
    • Just-in-time MATLAB interpreter/compiler
    • Incorporates Source-Level Transformation
      • Semantic Optimization (Menon/Pingali ICS’99)
        • Vectorization/BLAS call generation
        • Expression Optimization
      • Preallocation/Bounds Check Optimization (Work in progress)
conclusion
Conclusion
  • Source Level Optimizations are important for enhancing performance of MATLAB whether code is just interpreted or later compiled
unsafe type check removal
Unsafe Type Check Removal
  • Correct on 11/12 Codes
unsafe bounds check removal
Unsafe Bounds Check Removal
  • Correct on 7/12 Codes
ad