feldspar - functional programming at ericsson baseband research · 2010-10-22 · feldspar...

23
Feldspar Functional programming @ Ericsson Baseband Research DAVID ENGDAL, ERICSSON Baseband Research TEAM, Goteborg EMIL AXELSSON, CHALMERS

Upload: others

Post on 10-Mar-2020

42 views

Category:

Documents


0 download

TRANSCRIPT

Feldspar

Functional programming @ Ericsson Baseband Research

DAVID ENGDAL, ERICSSON Baseband Research TEAM, GoteborgEMIL AXELSSON, CHALMERS

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 2

Baseband Signal Processing

BS

UE

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 3

)()()( 00 nvtnshny +−=

Channel Model

UE BS

)(ns )(ny

)(nv

)())()()(()( 111100 nvetnshtnshtnshny njLL

offset +−++−+−= −−ω

Algorithms developed with Matlab, later implemented in c, run on “DSPs”

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 4

Typical Signal Processing Codecase MOD_64QAM:

{

int temp_real, temp_imag; int ind = 0;

for ( i = 0; i < n; i++) { for ( k = 0; k < 6; k++ ) { L = k*4096; temp_real = (int)(((L - 1) - ((in[i].re * scaling_factor)>>8) + (L - 1)) / 2.0); temp_imag = (int)(((L - 1) - ((in[i].im * scaling_factor)>>8) + (L - 1)) / 2.0); if (temp_real < 0)

temp_real = 0; else if (temp_real > (L - 1))

temp_real = (L - 1); if (temp_imag < 0)

temp_imag = 0; else if (temp_imag > (L - 1))

temp_imag = (L - 1); out[ind++] = temp_real; out[ind++] = temp_imag; } }

} break;

n typically >100

Inner loop with some arithmetics,typically > 80% complex multiplications

and accumulations a.k.a CMACs

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 5

Typical Hardware (Freescale example)

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 6

What we are facing

› Baseband signal processing increasingly demanding– Expected bitrate in 2015 is ~10 times higher than today– This requires ~100 times more computational power– Compare to Moore's law: ~10 times speed increase over the same

period

› Utilization of upcoming generations of multicore platforms– Hide (separate) deployment from algorithm design: “

Separation of concerns”– SW survives HW

› Current signal processing code mostly written in C– Highly optimized for specific hardware– Switching hardware causes major code rewrite – very costly!– C not close to mathematical formulations often used in algorithms

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 7

Use Functional Programming!› No side effects

– No variables, no states› No unnecessary “sequencing” of events

– “map” instead of for loop.› Functional programming is suitable for programming mathematical

functions

› Small example: map vs. for

But: functional programming implementations usually resource requiring (clock cycles & memory) and uses large run-time libraries. Not good for embedded systems!

Feldspar…

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 8

Map vs. For:

map :: (a -> b) -> [a] -> [b]

map f [x1, x2, ...] == [f x1, f x2, ...]

for(counter = 0; counter < length ; counter ++) { out = foo(counter, … ); }

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 9

Use Functional Programming!› No side effects

– No variables, no states› No unnecessary “sequencing” of events

– “map” instead of for loop.› Functional programming is suitable for programming mathematical

functions

› Small example: map vs. for

› But: functional programming implementations usually resource requiring (clock cycles & memory) and uses large run-time libraries. Not good for embedded systems!

› Feldspar…

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 10

What kind of name is that?

Feldspar: Functional Embedded

Language for DSP and Parallelism

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 11

› Cooperation between Ericsson, Chalmers and ELTE University (Budapest)

› Domain-specific language targeting digital signal processing

› Aims to be a high-level language for digital signal processing

– …to gain portability and maintainability– …suitable for implementation of mathematical equations– …ideally without sacrificing speed– …allows execution and functional verification.

Feldspar – Goal and Project

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 12

Feldspar – Technically speaking

› Embedded in the functional language Haskell – Implemented as a Haskell library

› “Feels” like a restricted version of Haskell– No recursion– Only static allocation– Etc.

› Open source: http://feldspar.inf.elte.hu/feldspar/

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 13

Feldspar – Build steps

Feldsparsourcecode

Compiler -> C

Executable

C-codeLegacy & Glue

“missing functions”

CCompiler

VendorSpecific

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 14

Feldspar:– First real application

› To - try Feldspar in a realistic scenario - give feedback to Chalmers and ELTE

› 4G- test-base station

› Two programmers with domain knowledge, but without functional language knowledge

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 15

– Suitable for baseband? Feldspar example 1, ”Reorder”: › Reordering of complex vector:

› Math:

› Matlab: function x = reorder ( y ) x = zeros(size(y)); x(1:2:(end-1)) = y(1:(end/2)); x(2:2:end) = y(end:-1:(end/2+1)); end

› Feldspar: reorder :: DVector a -> DVector a reorder = permute ( \len k -> (rem k 2 == 0) ? (div k 2 , len - (div (k + 1) 2)))

−=+−−=

=1...3,1)2/)1((

2...2,0)2/()(

NkkNy

Nkkykx

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 16

“REORDER” Execution time

› Feldspar generated c-code: 478µs› Plain handwritten c-code: 107µs

› Gain 371µs <=> 78%

› Why?– The feldspar language has no construct to express that you want every

even element of a vector treated in one way and every odd element in another way. Instead feldspar tests whether an element is odd or even.

– The feldspar compiler gives c-code that does pre-calculations for both branches in a later if-then-else clause.

– The feldspar compiler does not recognize that a division by 2 should be compiled into a right shift.

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 17

Example 2, ”prechest”: – Does it perform well?

Handwritten C-code:

preChEst(…){

func_1(…);func_2(…);func_3(…);

}

func_1(…)for( i = 0; i<nSC; i++)c[i]=complex_mul(a[i],b[i]);

func_2(…)for( i = 0; i<nSC; i++)c[i]=complex_scale(r, b[i]);

func_3(…)for( i = 0; i<nSC; i++)c[i]=complex_mul(a[i],b[i]);

Part of Channel Estimation

Handwritten Feldspar-code:

func_1 = zipWith (*) a b

func_2 = map (*r) b

func_3 = zipWith (*) a b

preChEst = func_3… func_2… func_1

Feldspar generated C-code:

preChEst(…){ for( i = 0; i<nSC; i++)

v1[i]=complex_mult(v2[i], complex_scale(r, complex_mult(v3[i],v4[i])));

}12644 cc

9942 cc

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 18

Feldspar Example 3, ”RS generation”:

› Demodulation reference symbol generation– 3GPP 36.211– One 3GPP equation or table <=> One Feldspar function– Verifiable individually in interpreter, still fused into efficient c-code at

compilation

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 19

Feldspar: – First user experiences

› Interpreter:

› Performance:

› Functional programming:

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 20

Conclusions

› Feldspar fits the signal processing domain better than C

› Feldspar is still an infant, but growing fast

› It works!

- Try it Yourself: Feldspar

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 22

Further reading

› Feldspar: A domain specific language for DSP › Paul Hudak: Conception, evolution, and application of functional programming languages› From MATLAB to Embedded C› Experience report: Haskell and mathematics› The LLVM Compiler Infrastructure › SequenceL: Taking Parnas's

Principles to the Next Level: Declarative Language Design › C++ with templates, example: Blitz++› Haskell Tutorial for C Programmers› Compiling Haskell To LLVM

Feldspar - Functional Programming at Ericsson Baseband Research | Public | © Ericsson AB 2010 | 2010-10-05 | Page 23

Functional programming

› Define program as composition of sub-functions– “Functions” in the mathematical sense (side-effect free)– No state– Sequencing only due to data dependencies

› Absense of side-effects is good!– Parallelizable functions– Optimizing transformations

› Higher-order functions drastically increase expressiveness– Double each element in a vector: map (*2) vector

– Sum the elements of a vector: fold (+) 0 vector

– Less need for special syntax!

› Often combined with strong typing› Further reading: A gentle introduction to Haskell