27
The MATISSE MA TLAB Compi ler 1 The MATISSE MATLAB Compiler A MATrix(MATLAB)-aware compiler InfraStructure for embedded computing SystEms INDIN 2013 Bochum, Germany   July 29-31 João Bispo, Pedro Pinto, Ricar do Nobre, Tiago Carvalho, João M. P. Cardoso, Pedro C. Diniz {jbispo, pmsp, ricardo.nobre, tiago.diogo.carvalho, jmpc}@fe.up.pt , [email protected]

2013 INDIN v6.7-jb

Embed Size (px)

Citation preview

Page 1: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 1/27

The MATISSE MATLAB Compiler 1

The MATISSE MATLAB CompilerA MATrix(MATLAB)-aware compiler InfraStructure for embedded computing SystEms

INDIN 2013Bochum, Germany  – July 29-31 

João Bispo, Pedro Pinto, Ricardo Nobre, Tiago Carvalho, João M. P. Cardoso, Pedro C. Diniz

{jbispo, pmsp, ricardo.nobre, tiago.diogo.carvalho, jmpc}@fe.up.pt, [email protected]

Page 2: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 2/27

The MATISSE MATLAB Compiler 2

Outline

Motivation

MATISSE Architecture

LARA Language

Examples

Results

Conclusions and Future Work

Page 3: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 3/27

The MATISSE MATLAB Compiler 3

Motivation – MATLAB Language

High-level matrix-oriented language, adequate for

prototyping, modeling and simulation

Fast learning curve

Rich set of libraries (toolboxes)

(De facto) Standard Prototype Development

Language for System Engineering

Page 4: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 4/27

The MATISSE MATLAB Compiler 4

Motivation – MATLAB in

Embedded Systems

MATLAB is translated to C

MATLAB characteristics make compilation complexand more difficult to achieve efficient solutions

On-the-fly interpretation of code

Dynamic types and shapes

Page 5: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 5/27

The MATISSE MATLAB Compiler 5

Motivation – Related Work FALCON [DeRose, Padua]

MATLAB to Fortran90

[Joisha, Banerjee]

Focus on type and shape inference

[Navak , Banerjee]

Annotations

C targeting HW MathWorks

Real-Time Workshop

MATLAB Coder

Page 6: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 6/27

The MATISSE MATLAB Compiler 6

Motivation – MATISSE Approach

Objectives:

Generate optimized C code

Code transformation, e.g.: instrumentation and monitoring

General several versions from the same MATLAB code

Guide compilation using user/domain information

Provide fine-grained control through LARA aspects

Define type of any variable

Behavior of induction variable (e.g., monotonicallyincreasing)

Indicate if matrix shape does not change

Static vs Dynamic arrays

Specialize functions for certain input values (e.g., image size)

Page 7: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 7/27The MATISSE MATLAB Compiler 7

MATISSE Architecture

MATISSE:

MATLAB Parser

MATLAB Weaver

MATLAB to MATLAB Code Instrumentation

Type specialization

Transformationspropagate to C

MATLAB to C

MATLAB Parser 

MATLAB Weaver 

MIR

MATLAB Code

Generator 

MATLAB Code LARA Aspects

MATLAB Code

MATISSE

MATLAB to C

C Code

CIR

 Aspect Data

MIR

C Code Generator 

Page 8: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 8/27The MATISSE MATLAB Compiler 8

LARA Language

Select-Apply-Condition block

Modularity (aspects can call other aspects)

Javascript for arbitrary computation

aspectdef <aspect_name>

select <point_of_interest> end  

apply 

<actions>

end  

[condition <boolean_expression> end ] 

end  

Page 9: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 9/27The MATISSE MATLAB Compiler 9

Example – FIR Dynamic

function output=fir(input, coef) 

NTAPS = length(coef); 

N = length(input);

output = zeros(1, N);

for i = NTAPS:1:N

sum = 0.0; 

for j = 0:1:NTAPS-1 

sum = sum + input(i-j) * 

coef (j+1); 

end  

output(i) = sum; 

end  

end  

aspectdef firSingle

// Define array implementation select file end  apply

def array_implementation = "dynamic"; end

var typeDef = {input: "single[]",coef: "single[]",sum: "single", output: "single" }; 

// Define types call defineTypes("fir", typeDef);

end

MATLAB Code LARA Code (Aspect)

Page 10: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 10/27The MATISSE MATLAB Compiler 10

Example – FIR Dynamic

function output=fir(input, coef) 

NTAPS = length(coef); 

N = length(input);

output = zeros(1, N);

for i = NTAPS:1:N

sum = 0.0; 

for j = 0:1:NTAPS-1 

sum = sum + input(i-j) * 

coef (j+1); 

end  

output(i) = sum; 

end  

end  

aspectdef defineTypes 

input functionName, typeDef end  

select function.var end  

apply 

def type = typeDef[$var.name]; end  

condition $var.name in typeDef && 

$function.name == functionName 

end

end  

MATLAB Code LARA Code (Aspect)

Page 11: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 11/27The MATISSE MATLAB Compiler 11

Example – FIR Dynamictensor_f** fir (tensor_f* input, tensor_f* coef, tensor_f** output) { 

int NTAPS; int N; int i; float sum; int j; 

NTAPS = length_alloc_f(coef); 

N = length_alloc_f(input); 

zeros_f2(1, N, output); 

for(i = NTAPS; i <=N; i = i+1) { sum = 0.0f; 

for(j = 0; j <=(NTAPS-1); j = j+1) { 

sum = sum+(input-> data[(i-j)-1]*coef-> data[(j+1)-1]); 

(*output)-> data[i-1] = sum; 

return output; 

C Code

Page 12: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 12/27The MATISSE MATLAB Compiler 12

Example – FIR Dynamic

function output=fir(input, coef) 

NTAPS = length(coef); 

N = length(input);

output = zeros(1, N);

for i = NTAPS:1:N

sum = 0.0; 

for j = 0:1:NTAPS-1 

sum = sum + input(i-j) * 

coef (j+1); 

end  

output(i) = sum; 

end  

end  

aspectdef firSingle

// Define array implementation select file end  apply

def array_implementation = "static"; end

var typeDef = {input: "single[1][1024]",coef: "single[1][32]",sum: "single", output: "single" };

// Define types call defineTypes("fir", typeDef);

end

MATLAB Code LARA Code (Aspect)

Page 13: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 13/27The MATISSE MATLAB Compiler 13

Example – FIR Staticfloat* fir(float* input, float* coef, float* output) { 

int NTAPS; int N; int i; float sum; int j; 

NTAPS = 32; 

N = 1024; 

zeros_f1x1024(output); 

for(i = NTAPS; i <=N; i = i+1) { sum = 0.0f; 

for(j = 0; j <=(NTAPS-1); j = j+1) { 

sum = sum+(input[(i-j)-1]*coef[(j+1)-1]); 

output[i-1] = sum; 

return output; 

C Code

Page 14: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 14/27The MATISSE MATLAB Compiler 14

Example – Grid Iteratefunction v_old = grid_iterate(obstacle, v, iter_max, nx, ny, nz) 

v_0 = 0; v_end = 1; c = 1/6; 

for iter = 1:iter_max

for i=2:nx-1 

for j=2:ny-1 for k=2:nz-1 

if(obstacle(i,j,k)==1) 

v(i,j,k) = v_0; 

elseif(obstacle(i,j,k)==-1) v(i,j,k) = v_end; 

else 

temp = v(i-1,j,k) + v(i+1,j,k) + v(i,j-1,k) +v(i,j+1,k) + v(i,j,k-1) + v(i,j,k+1); 

v(i,j,k) = temp * c; end  

endend  end  

end  

v_old = v; 

end  

MATLAB Code

Page 15: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 15/27The MATISSE MATLAB Compiler 15

Example – Grid Iteratefunction v_old = grid_iterate(obstacle, v, iter_max, nx, ny, nz) 

v_0 = 0; v_end = 1; c = 1/6; 

for iter = 1:iter_max

for i=2:nx-1 

for j=2:ny-1 for k=2:nz-1 

if(obstacle(i,j,k)==1) 

v(i,j,k) = v_0; 

elseif(obstacle(i,j,k)==-1) v(i,j,k) = v_end; 

else 

temp = v(i-1,j,k) + v(i+1,j,k) + v(i,j-1,k) +v(i,j+1,k) + v(i,j,k-1) + v(i,j,k+1); 

v(i,j,k) = temp * c; end  

endend  end  

end  

v_old = v; 

end  

MATLAB Code

Page 16: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 16/27The MATISSE MATLAB Compiler 16

Example – Grid Iterate

temp = v-> data[((i-1)-1)+(j-1)*1*v-> shape[0]... 

int v_shape0 = v-> shape[0];

... 

temp = v-> data[((i-1)-1)+(j-1)*1*v_shape0... 

Aspect: “shape of „v‟ does not change” 

Performance increase: 8%

Page 17: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 17/27The MATISSE MATLAB Compiler 17

Experimental Setup

Set of kernels from embedded computing

GridIterate, FIR, FilterSubband and FFT2D

C Hand-coded and MATLAB versions

Two targets:

(PPC-O2) Cycle-acc. simulation, PowerPC 604,

gcc 4.3.3 (MB-O2) Cycle-acc. simulation, 3-stages MicroBlaze,

gcc 4.1.2

Page 18: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 18/27The MATISSE MATLAB Compiler 18

Results – C code performance

Performance of MATISSE competitive to hand-made code

Different versions of the code specified through aspects

0.95   0.99   0.98   0.97

1.16

1.111.07

1.32

0.50

0.75

1.00

1.25

gridIt fir fsubband fft2D

   P   e   r    f   o   r   m   a   n   c   e    (   m   a

   n   u   a    l    /   M   A   T   I   S   S   E    )

PPC-O2 MB-O2

Page 19: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 19/27The MATISSE MATLAB Compiler 19

Results – fsubband

MATISSE-generated code suited for HW implementation

1.36

1.02

0.991.00

1.04

1.001.001.02

1.06

1.09

1.30

1.33

0.95

1.00

1.05

1.10

1.15

1.20

1.25

1.30

1.35

1.40

single double

Manual / MATISSE

SW 0

SW 1

SW 2

HW 0

HW 1

HW 2

Page 20: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 20/27The MATISSE MATLAB Compiler 20

Conclusions

Aspect-oriented approach

Separation of code and additional information

Fine-grained control over generated code

MATLAB-to-MATLAB transformations

E.g., code monitoring and instrumentation

Generation of efficient C code from MATLAB

Performance competitive to manual code

Code prepared for hardware generation

Page 21: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 21/27The MATISSE MATLAB Compiler 21

Future Work

Support for fixed-point data types and morefunctions

Continue evaluation and experiments

Improve code optimization and specialization with

other kinds of information

Page 22: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 22/27

The MATISSE MATLAB Compiler 22

THANK YOU!

Questions?

http://specs.fe.up.pt/tools/matisse/ 

Check MATISSE at the DEMO NIGHT!

Page 23: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 23/27

The MATISSE MATLAB Compiler 23

Example – Grid Iteratefunction v_old = grid_iterate(obstacle, v, iter_max, nx, ny, nz) 

v_0 = 0; v_end = 1; c = 1/6; 

for iter = 1:iter_max

for i=2:nx-1 

for j=2:ny-1 for k=2:nz-1 

if(obstacle(i,j,k)==1) 

v(i,j,k) = v_0; 

elseif(obstacle(i,j,k)==-1) v(i,j,k) = v_end; 

else 

temp = v(i-1,j,k) + v(i+1,j,k) + v(i,j-1,k) +v(i,j+1,k) + v(i,j,k-1) + v(i,j,k+1); 

v(i,j,k) = temp * c; end  

endend  end  

end  

v_old = v; 

end  

MATLAB Code

Page 24: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 24/27

The MATISSE MATLAB Compiler 24

Example – Grid Iterate

Run C code on a MicroBlaze

Single precision implementation

No HW support for double precision

C = 1/6 -> Inferred as a double

Statement alone is not enough to infer single

LARA aspect -> c: "single“ 

Performance increase: 6.3x

Page 25: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 25/27

The MATISSE MATLAB Compiler 25

Example – FFT 2D

theta = isign*(2.0*(scalar_division_f(PI, temp_div))); ...

wp_x = wp_x*(((-2.0))*wp_x); wp_y = sinf(theta);

theta = isign*(2.0f*(scalar_division_f(PI, temp_div))); ...

wp_x = wp_x*(((-2.0f))*wp_x); wp_y = sinf(theta); 

Automatically performed by MATISSE

Performance increase: 13%

Page 26: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 26/27

The MATISSE MATLAB Compiler 26

Motivation – LARA Toolchain

Generation of C code

according to different

non-functional

requirements

LARA-based toolchain:

C Optimized for

Embedded Systems

HW/SW Partitioning

DSE

Profiling   H  a  r   d  w  a  r  e

   /   S  o   f   t  w  a  r  e   F   l  o  w

 Application

(C)

C Front-End

 Aspects and Design

Patterns (LARA)

VHDL-RTL

Back-End (code

generators)

Optimizer

(Software/Hardware)

Harmonic

 Application

(C)

CDFG-IR

Kernels for Swand HwComponents (C) +

 Annotations

 Aspect-IR

Design-Space

Exploration

(DSE)

LARA Front-End

Best Practices

CDFG-IR

Hardware/Softwar 

e Templates

CoSy

 Assembly

weaving

Page 27: 2013 INDIN v6.7-jb

8/14/2019 2013 INDIN v6.7-jb

http://slidepdf.com/reader/full/2013-indin-v67-jb 27/27

Motivation  However, it is cumbersome to apply certain

transformations directly in C code

Too many implementations decisions already in the

code

Higher level language for description of the

problem