Docsity

Optimizing Numerical Code for Signal Processing: Overcoming the Challenges of Moore's Law , Study notes of Computer Science

Partial preview of the text

Download Optimizing Numerical Code for Signal Processing: Overcoming the Challenges of Moore's Law and more Study notes Computer Science in PDF only on Docsity!

Algorithms and Computation in

Signal Processing

Slides by Markus Pueschel

CS 498: Program Optimization, Fall 2006

The Problem Again reasonable implementation (Numerical recipes. GNU scientific library) Intel vendor library (hand-optimized assembly code) but also FFTW, SPIRAL generated code 10 x DFT size Writing fast numerical code is a tough problem

Moore’s Law

! Moore’s Law: exponential (x2 in ~18 months) increase

number of transistors/chip

But everything has its price …

source: Scientific American, Nov 2004, p. 98 Moore’s Law: Consequences

! Computers are very complex

! multilevel memory hierarchy

! special instruction sets beyond standard C programming model

! undocumented hardware optimizations

! Consequences:

! Runtime depends only roughly on the operations

! Runtime behavior is hard to understand

! Compiler development can hardly keep track

! The best code (and algorithm) is platform-dependent

! It is very difficult to write really fast code

! Computers evolve fast

! Highly tuned code becomes obsolete almost as fast as it as written

! It’ll get rather worse: Multicoresystems

Implementation Level

Present

Algorithm Level

Future

automation

a u t o m a t i o n

Common Philosophy a new breed of domain-aware approaches/tools push automation beyond what is currently possible applies for software and hardware design alike

human

one algorithm and program SPIRAL www.spiral.net Team: James C. Hoe (ECE, CMU) Jeremy Johnson (CS, Drexel) José M. F. Moura (ECE, CMU) David Padua (CS, UIUC) Markus Püschel (ECE, CMU) Manuela Veloso (CS, CMU) Robert W. Johnson (Quarry Comp. Inc.) Christoph Überhuber (Math, TU Wien Sponsors: DARPA NSF ACR- 0234293 NSF ITR/NGS- 0325687 and: Cylab, CMU Austrian Science Fund Intel ITRI, Taiwan ENSCO, Inc. Students/PostDocs: Bryan W. Singer (CS, CMU) Jianxin Xiong (CS, UIUC) Srinivas Chellappa (ECE, CMU) Franz Franchetti (ECE, CMU, before TU Vienna) Aca Gacic (ECE, CMU) Yevgen Voronenko (ECE, CMU) Anthony Breitzman (CS, Drexel) Kang Chen (CS, Drexel) Pinit Kumhom (ECE, Drexel) Adam Zelinski (ECE, CMU) Peter Tummeltshammer (CS, TU Vienna) … ~ 40 Publications Overview paper: Markus Püschel, José M. F. Moura, Jeremy Johnson, David Padua, Manuela Veloso, Bryan Singer, Jianxin Xiong, Franz Franchetti, Aca Gacic, Yevgen Voronenko, Kang Chen, Robert W. Johnson, and Nick Rizzolo, SPIRAL: Code Generation for DSP Transforms, Proceedings of the IEEE

Spiral

! Code generation from scratch for linear digital signal

processing (DSP) transforms (DFT, DCT, DWT, filters, ….)

! Automatic optimization and platform-tuning at the algorithmic

level and the code level

! Different code types supported (scalar, vector, FMA, fixed-

point, multiplierless, …)

Goal: A flexible, extensible code generation framework

that can survive time (to whatever extent possible)

for an entire domain of algorithms

Research question: To what extent is it possible to abolish

handcoding and handoptimization?

Code Generation and Tuning as Optimization Problem a DSP transform to be implemented the target platform set of possible implementations of T on P cost of implementation I of T on P The implementation of T that is tuned to P is given by: Problems:

  • How to characterize and generate the set of implementations?
  • How to efficiently minimize C? Spiral exploits the domain-specific structure to implement a solver for this optimization problem

DSP Algorithms: Example 4 - point DFT Cooley/Tukey FFT (size 4 ): ! mathematical notation exhibits structure: SPL (signal processing language) ! Suitable for computer representation ! contains all information to generate code

i i i

i i

4 2 2 2 4 DFT 4 (^) =( DFT 2 " I 2 )! T 2 !( I " DFT )! L Fourier transform Identity Permutation Diagonal matrix (twiddles) Kronecker product SPL: Definition (BNF)

! Description language for linear DSP algorithms

! Definition (BNF):

Some Definitions:

DSP Algorithms: Spiral Terminology

Transform

Rule

Formula

DFT (^) nm #^ ( D^ FTn " Im )!^ D !( I^ n " DFTm )^! P DFT 8 = ( F 2 " I 4 )! D !( I 2 "( I 2 " F 2 L))! P parameterized matrix

  • a breakdown strategy
  • product of sparse matrices
    • recursive application of rules
    • uniquely defines an algorithm
    • efficient representation
    • easy manipulation DFT n

Ruletree DFT^8

DFT 2 DFT 4 DFT 2 DFT 2

  • few constructs and primitives
  • uniquely defines an algorithm
  • can be translated into code Some Transforms Spiral currently contains 36 transforms

Formula- (Algorithm) generation

Ruletree:

Formula:

Transform:

(fast)

C Code:

Remaining task (many possibilities) Set of Algorithms

! Given a transform:

! Apply breakdown rules recursively until all occurring transforms are

expanded

! Choice of rules at each step yields (usually) exponentially large algorithms

space:

! about equal in operations count

! differ in data flow

**k 1 2 3 4 5 6 7 8 9

DFTs, size 2k

1 6 40 296 27744 162570361280 ~1.01 • 10^ ~2.31 • 10^ ~2.86 • 10^

DCT IV, size 2k

1 10 126 31242 1924443362 7343815121631354242 ~1.07 • 10^ ~2.30 • 10^ ~1.06 • 10^**

From Algorithm (Formula) to Optimized Algorithm Input: Fast algorithm as formula Output: Optimized formula Domain Knowledge II: Optimizing an algorithm Motivation: Loop Fusion **void I 4 xF 2 _L 84 (double *y, double x) { double t[ 8 ]; for (int i= 0 ; i< 8 ; i++) t[i== 7? 7 : (i 4 )% 7 ] = x[i]; for (int i= 0 ; i< 4 ; i++){ y[ 2 *i] = t[ 2 *i] + t[ 2 *i+ 1 ]; y[ 2 *i+ 1 ] = t[ 2 *i] - t[ 2 *i+ 1 ]; } } void I 4 xF 2 _L 84 (double *y, double *x) { for (int j= 0 ; j< 4 ; j++){ y[ 2 j] = x[j] + x[j+ 4 ]; y[ 2 j+ 1 ] = x[j] - x[j+ 4 ]; } } direct code generation no compiler does that Solution: !-SPL and Formula manipulation

Loop Fusion Beyond Cooley-Tukey Main DFT recursion (breakdown rules): rather cheap expensive very expensive

How to fuse permutations from different combinations of rules?

Example

! Consider the DFT formula fragment

! In! - SPL:

Cooley-Tukey Rader Good-Thomas

Complicated

array access

Example (cont’d)

! After index function simplification:

Simplified

array access

Example (cont’d)

! Generated Code

Implementation of Formula Generation and Manipulation

! Implementation using a computer algebra system (GAP)

! SPL/! -SPL implemented as recursive data types

! Exact representation of sin(), cos(), etc.

! Symbolic computation enables exact verification of rules

From Optimized Algorithm (Formula) to Code Input: Optimized formula Output: Intermediate Code

Straightforward

From Code to Optimized Code Input: Intermediate Code Output: Optimized C code Code Level Optimizations

! Precomputation of constants

! Loop unrolling (controlled by search module)

! Constant inlining

! SSA code, scalar replacement, algebraic simplifications, CSE

! Code reordering for locality (optional)

! Conversion to FMA code (optional)

! Conversion to fixed point code (optional)

! Conversion to multiplierless code (optional)

! Finally: Unparsing to C (or Fortran)

Search (Learning) for the Best Input: Performance Number Output: Controls Formula Generation Controls Implementation Options Search Methods ! Search over: ! Algorithmic degrees of freedom (choice of breakdown rules) ! Implementation degrees of freedom (degree of unrolling) ! Operates with the ruletree representation of an algorithm ! transform independent ! efficient ! Search Methods ! Exhaustive Search ! Dynamic Programming (DP) ! Random Search ! Hill Climbing ! STEER (an evolutionary algorithm)

STEER: Evolutionary Search Population n: Population n+1: …… …… Mutation Cross-Breeding expand differently swap expansions Survival of Fittest Learning

! Procedure:

! Generate a set of ( 1000 say) algorithms and their runtimes (one transform,

one size); represent algorithms by features

! From this data (pairs of features and runtimes), learn a set of algorithm

design rules

! From this set, generate best algorithms (theory of Markov decision

processes)

! Evaluation:

! Tested for WHT and DFT

! From data generated for one size ( 2 ^ 15 ) could construct best algorithms

across sizes ( 2 ^ 12 - 2 ^ 18 ) Bryan Singer and Manuela Veloso Learning to Construct Fast Signal Processing Implementations Journal of Machine Learning Research, 2002 , Vol. 3 , pp. 887 - 919