c++ expression templates in an embedded, parallel, real-time signal processing … ·...

15
C++ Expression Templates in an Embedded, Parallel, Real-Time Signal Processing Library Edward M. Rutledge MIT Lincoln Laboratory Abstract 1.0 INTRODUCTION In order to facilitate a smooth transition from an algorithm's linear algebra specification to its software implementation, a high performance signal processing library should provide software constructs that allow linear algebra to be easily translated into high performance code. Currently, C is the prevalent language for high performance signal processing libraries, largely because of concerns about the efficiency of C++. If it were not for these concerns, C++ would be a better choice, partly because of its expressiveness. Using C++, we can define vector and matrix classes and overloaded operators that allow linear algebra to be more easily and intuitively translated into code. However, C++ operator overloading is the source of much of the concern about the efficiency of C++. Traditional techniques of operator overloading incur added overhead that may be unacceptable in an embedded real-time system. C++ "expression templates," which are currently gaining popularity in the scientific computing arena, provide a solution to this problem. In this presentation, we explore the impact of C++ expression templates on the simplicity and performance of an experimental, embedded, parallel, real-time signal processing library. We demonstrate that expression templates can be as beneficial in the rapid development of embedded, parallel, real-time signal processing applications as they have proven to be in the rapid development of high performance scientific simulations. Our examples and experiments focus on architectures, problem sizes, and kernels relevant to radar and other array-sensor processing applications. 2.0 OVERVIEW OF C++ EXPRESSION TEMPLATES AND PETE C++ overloaded operators typically return temporary objects containing the results of the operation. This technique incurs the overhead of creating and destroying the temporary objects, which include additional memory use, copy overhead, and possible overhead of allocating and freeing dynamic memory in the objects. Alternatively, C++ expression templates can be used to transform an arbitrary expression of objects such as vectors or matrices into a parse tree representation of that expression. This is done at compile time, and the parse tree object itself takes a small amount of memory, so little overhead is incurred at run time. With an expression's entire parse tree represented in a single object, the expression can be evaluated as a whole instead of being evaluated one operation at a time. This eliminates the need to produce temporary objects to hold intermediate results, creating a profound positive impact on the suitability of C++ operator overloading for embedded real-time systems, where efficient use of system resources is essential. The Portable Expression Template Engine (PETE) is a free software package, developed at Los Alamos National Laboratory's Advanced Computing Laboratory, that allows programmers to add expression template capability to their C++ programs. PETE furnishes a utility for defining C++ operators that transform expressions of class objects into parse trees, and PETE furnishes functions that help in navigating these expression parse trees and help in efficiently evaluating expressions of element-wise operations. We use PETE to add expression template capability to the experimental C++ signal processing library referred to in this presentation.

Upload: others

Post on 08-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

C++ Expression Templates in an Embedded, Parallel, Real-Time Signal ProcessingLibrary

Edward M. RutledgeMIT Lincoln Laboratory

Abstract

1.0 INTRODUCTION

In order to facilitate a smooth transition from an algorithm's linear algebra specification to its softwareimplementation, a high performance signal processing library should provide software constructs thatallow linear algebra to be easily translated into high performance code. Currently, C is the prevalentlanguage for high performance signal processing libraries, largely because of concerns about theefficiency of C++. If it were not for these concerns, C++ would be a better choice, partly because of itsexpressiveness. Using C++, we can define vector and matrix classes and overloaded operators that allowlinear algebra to be more easily and intuitively translated into code. However, C++ operator overloading isthe source of much of the concern about the efficiency of C++. Traditional techniques of operatoroverloading incur added overhead that may be unacceptable in an embedded real-time system. C++"expression templates," which are currently gaining popularity in the scientific computing arena, provide asolution to this problem.

In this presentation, we explore the impact of C++ expression templates on the simplicity andperformance of an experimental, embedded, parallel, real-time signal processing library.

We demonstrate that expression templates can be as beneficial in the rapid development of embedded,parallel, real-time signal processing applications as they have proven to be in the rapid development ofhigh performance scientific simulations. Our examples and experiments focus on architectures, problemsizes, and kernels relevant to radar and other array-sensor processing applications.

2.0 OVERVIEW OF C++ EXPRESSION TEMPLATES AND PETE

C++ overloaded operators typically return temporary objects containing the results of the operation. Thistechnique incurs the overhead of creating and destroying the temporary objects, which include additionalmemory use, copy overhead, and possible overhead of allocating and freeing dynamic memory in theobjects. Alternatively, C++ expression templates can be used to transform an arbitrary expression ofobjects such as vectors or matrices into a parse tree representation of that expression. This is done atcompile time, and the parse tree object itself takes a small amount of memory, so little overhead isincurred at run time. With an expression's entire parse tree represented in a single object, the expressioncan be evaluated as a whole instead of being evaluated one operation at a time. This eliminates the needto produce temporary objects to hold intermediate results, creating a profound positive impact on thesuitability of C++ operator overloading for embedded real-time systems, where efficient use of systemresources is essential.

The Portable Expression Template Engine (PETE) is a free software package, developed at Los AlamosNational Laboratory's Advanced Computing Laboratory, that allows programmers to add expressiontemplate capability to their C++ programs. PETE furnishes a utility for defining C++ operators thattransform expressions of class objects into parse trees, and PETE furnishes functions that help innavigating these expression parse trees and help in efficiently evaluating expressions of element-wiseoperations. We use PETE to add expression template capability to the experimental C++ signalprocessing library referred to in this presentation.

3.0 IMPACT OF PETE ON CODE SIMPLICITY AND PERFORMANCE

Through the following examples and experiments, we show that PETE and C++ can combine to form anefficient and easy to use signal processing programming capability.

3.1 Simplicity

To show the impact of C++ and operator overloading on the simplicity and readability of signal processingcode, we compare implementations of linear algebra expressions using our experimental C++ signalprocessing library with operator overloading to implementations using a C signal processing library.

3.2 Performance

To show the performance impact of PETE in our experimental signal processing library, we measure andcompare the execution time of a linear algebra expression implemented in three different ways:

− using an optimized native C library,

− using our experimental C++ library with PETE operators and vector and matrix classes thatevaluate the expression using an optimized native C library, and

− using our experimental C++ library with PETE operators and vector and matrix classes thatevaluate the expression using PETE methods of element-wise evaluation.

The linear algebra expression and problem size are extracted from an existing radar signal processingapplication.

The first two approaches are compared to show the overhead introduced by the C++/PETE approach.The second two approaches are compared to show the possible performance advantage of PETEelement-wise expression evaluation over optimized native C library call expression evaluation, especiallyfor evaluating expressions where many element-wise operations are chained together.

The experiments are run on both serial and distributed memory message passing architectures. In thelatter case, we examine both data mappings that require inter-processor communication and those thatdo not.

4.0 FUTURE OPTIMIZATIONS

We also discuss some possibilities for further optimization of mathematical expressions, includingdetermining the optimal order for performing a series of chained operations in an expression with respectto operation count and with respect to communication overhead. These optimizations would not bepossible using traditional C++ operator overloading.

MIT Lincoln Laboratory000801-er-1KAM 4/27/01

C++ Expression Templates in an Embedded,Parallel, Real-Time Signal Processing

Library

Eddie Rutledge

*This work is sponsored by the US Navy, under Air Force Contract F19628-00-C-0002. Opinions, interpretations, conclusions, andrecommendations are those of the author and not necessarily endorsed by the United States Air Force.

MIT Lincoln Laboratory000801-er-2KAM 4/27/01

Evolution of Parallel ProcessingLibraries

Parallel Processing

Library

Parallel Communications

Single processor Library

1988 20009998979695949392919089

Applicability

= Scientific (non-real-time) computing

= Real-time signal processing

LAPACK = Linear algebra package

MPI = Message-passing interface

MPI/RT = MPI real-time

ScaLAPACK = Scalable LAPACK

VSIPL = Vector, Signal, and Image Processing Library

STAPL = Space-Time Adaptive Proc. Library

PETE= Portable Expression Template Engine

VSIPL

MPI/RT

LAPACK

MPI

ScaLAPACK

STAPL

PVL

PVL• Collaboration between Lincoln and Lockheed Martin• Component of AEGIS Common Signal Processor (CSP)

Software Application Program Interface (API)• Combines VSIPL API & parallel constructs from STAPL• Portable, high performance, standardized signal processing

library for real-time array signal processing

PVL• Collaboration between Lincoln and Lockheed Martin• Component of AEGIS Common Signal Processor (CSP)

Software Application Program Interface (API)• Combines VSIPL API & parallel constructs from STAPL• Portable, high performance, standardized signal processing

library for real-time array signal processing

• Fortran• Object-based

• C• Object-based

• Fortran

• C• Object-based

• C• Object-based

• C• Object-based

• C++• Object- oriented

PETE • C++• Object-oriented

MIT Lincoln Laboratory000801-er-3KAM 4/27/01

C Vs. C++ Code Simplicity

for (i=0;I<rows;I++)for (j=0;j<cols;j++)

A[i][j]=B[i][j]+C[i][j];vsip_vadd_f(&B,&C,&A); A=B+C

We examine C++ expression templates as a way to increase code performance

C (Functional Approach) C (Object Based) C++ (Object Oriented)

MIT Lincoln Laboratory000801-er-4KAM 4/27/01

ApplicationSpace

LibrarySpace

VSIPL

MPI PVL

C vs. C++ Performance

We compare the performance of the object-oriented C++ approach (PVL)with the performance of the object-based C approach (VSIPL/MPI)

We compare the performance of the object-oriented C++ approach (PVL)with the performance of the object-based C approach (VSIPL/MPI)

ApplicationSpace

LibrarySpace

VSIPL

MPI PVL

Focus: Compare performance

FunctionalApproach

FunctionalApproach

Object-BasedC Library

Object-orientedC++ Library

Object-BasedC Library

Object-orientedC++ Library

Expression Templates

Co

de

Co

mp

lexi

tyO

pti

miz

atio

nP

ote

nti

al

MIT Lincoln Laboratory000801-er-5KAM 4/27/01

Typical C++ Operator Overloading-Overhead

temp

B+C temp

temp copy A

Main

Operator +

Operator =

1. Pass B and Creferences tooperator +

2. Create temporaryresult vector

3. Calculate results,store in temporary

4.Return copy of temporary

5. Pass results referenceto operator=

6. Perform assignment

temp copy

temp copy &

Example: A=B+C vector addExample: A=B+C vector add

B&,

C&

Additional Memory Use

Additional Execution Time

• Static memory• Dynamic memory

(also affectsexecution time)

• Time to create anew vector

• Time to create a copy of a vector

• Time to destructboth temporaries

2 temporary vectors created2 temporary vectors created

MIT Lincoln Laboratory000801-er-6KAM 4/27/01

C++ Expression Templates and PETE

Parse trees, not vectors, createdParse trees, not vectors, created

Reduced Memory Use

Reduced Execution Time

• Parse tree only contains references

• Parse tree created at compile time

PETE: http://www.acl.lanl.gov/pete

• PETE, the Portable Expression Template Engine, is available from theAdvanced Computing Laboratory at Los Alamos National Laboratory

• PETE provides:– Expression template capability– Facilities to help navigate and evaluating parse trees

A=B+C*DA=B+C*D

BinaryNode<OpAssign, Vector,BinaryNode<OpAdd, VectorBinaryNode<OpMultiply, Vector,

Vector >>>

ExpressionTemplates

Expression

Expression TypeParse Tree

B+C A

Main

Operator +

Operator =

+B& C&

1. Pass B and Creferences tooperator +

4. Pass expression treereference to operator

2. Create expressionparse tree

3. Return expressionparse tree

5. Calculate result andperform assignment

copy &

copy

B&,

C&

Parse trees, not vectors, createdParse trees, not vectors, created

MIT Lincoln Laboratory000801-er-7KAM 4/27/01

Experiments

Measure Execution TimeExperiment 3: Varying distributions

Experiment 2: Identical distributions

Experiment 1: Single Node

SoftwareTechnology

ExpressionComplexity

ParallelComplexity

C C++/VSIPL C++/PETEC C++ C++MPI PVL PVLVSIPL - MPI - MPI

- VSIPL - PETE- PETE - Expression

templates- Expression templates - Evaluation

Software Technology

MIT Lincoln Laboratory000801-er-8KAM 4/27/01

Experiment 1: Single Node

• Platform: Linux PC• Element-wise multiply and divide

Relative overhead of C++/VSIPL vs. C is small when expression templates are used

Relative overhead of C++/VSIPL vs. C is small when expression templates are used

For longer chained expressions of element-wise operations, C++ with PETE element-wise evaluation outperforms other implementations

For longer chained expressions of element-wise operations, C++ with PETE element-wise evaluation outperforms other implementations

A=B+C A=B+C*D A=B+C*D/E+fft(F)

CC++/VSIPLC++/PETE

CC++/VSIPLC++/PETE

Vector Length Vector Length

0.6

0.8

0.9

1

1.2

0.7

1.1

0.8

0.9

1.1

1

CC++/VSIPLC++/PETE

0.9

1

1.1

1.2

1.3

32 128512

2048

Vector Length

Rel

ativ

e E

xecu

tio

n T

ime

819232768

1310728

Rel

ativ

e E

xecu

tio

n T

ime

Rel

ativ

e E

xecu

tio

n T

ime

1.2

32 128512

20488192

327681310728 32 128

5122048

819232768

1310728

MIT Lincoln Laboratory000801-er-9KAM 4/27/01

Experiment 2: Identical Distributions

• Platform: 4 node Linux cluster• Element-wise multiply and divide

Similar performance to single node caseSimilar performance to single node case

Relative differences in A=B+C*D/E+fft(F) are smaller because ofcommunication required in (unoptimized) FFT

Relative differences in A=B+C*D/E+fft(F) are smaller because ofcommunication required in (unoptimized) FFT

A=B+C A=B+C*D A=B+C*D/E+fft(F)

CC++/VSIPL

C++/PETE

CC++/VSIPLC++/PETE

CC++/VSIPLC++/PETE

Vector Length

0.9

1

1.2

1.3

1.4

1.1

1.5

0.6

0.8

0.7

1.2

1.3

0.9

1.4

1.1

1

0.9

1

1.1

Rel

ativ

e E

xecu

tio

n T

ime

Rel

ativ

e E

xecu

tio

n T

ime

Rel

ativ

e E

xecu

tio

n T

ime

32 128512

20488192

327681310728

Vector Length

32 128512

20488192

327681310728

Vector Length

32 128512

20488192

327681310728

MIT Lincoln Laboratory000801-er-10KAM 4/27/01

Experiment 3: Different Distributions

• Platform: 4 node Linux cluster• Element-wise multiply and divide• A distributed on 2 nodes, B, C, D, E and F distributed on 4 nodes

Relative differences between the 3 approaches are much smallerbecause of communication required in all cases

Relative differences between the 3 approaches are much smallerbecause of communication required in all cases

A=B+C A=B+C*D A=B+C*D/E+fft(F)

CC++/VSIPLC++/PETE

CC++/VSIPLC++/PETE

CC++/VSIPLC++/PETE

1. 1

1

0.9

1

1.1

0.9

0.8 0.9

1

1.1

Rel

ativ

e E

xecu

tio

n T

ime

Rel

ativ

e E

xecu

tio

n T

ime

Rel

ativ

e E

xecu

tio

n T

ime

Vector Length

32 128512

20488192

327681310728

1

Vector Length

32 128512

20488192

327681310728

Vector Length

32 128512

20488192

327681310728

MIT Lincoln Laboratory000801-er-11KAM 4/27/01

Results Summary

Vector Length=8 Vector Length=131072

1.3

1.2

1

0.9C++/VSIPL C++/PETE

Vector Length=8 Vector Length=131072

C++/VSIPL C++/PETE

• C++/VSIPL- Smalloverhead compared to C

• C++/PETE- Significantadvantage for chainedexpressions

• Relative differenceslargest where nocommunication isrequired

Single Node: A=B+C Single Node : A=B+C*D Single Node : A=B+C*D/E+fft(F)

Identical Distributions: A=B+C*D/E+fft(F) Different Distributions: A=B+C*D/E+fft(F)

1.1

1.4Vector Length=8 Vector Length=131072

1.11

0.70.6

C++/VSIPL C++/PETE

0.8

1.2

0.9

1.3

0.9

1

1.1

Vector Length=8 Vector Length=131072

1

C++/VSIPL C++/PETE0.9

1.1

Execution time relative to C/VSIPL implementation

1.2

Vector Length=8 Vector Length=131072

1

0.9C++/VSIPL C++/PETE

1.1

MIT Lincoln Laboratory000801-er-12KAM 4/27/01

Future Optimizations

2 optimizations made possible by expression templatesnot possible with typical C++ operator overloading

Minimize Op Count Minimize Communication

Example: matrix-matrix multiply Example: A=B+C

B-30x35C-35x15D-15x5E-5x10F-10x20

A=BxCxDxExF

• A and C are on processor 1• B is on processor 2

Both optimizations require knowledge of the entire expression

Expression templatesExpression templates

Default ordering: 25,500 scalar mulitpliesA=(((BxC)xD)xE)xF

Default ordering: 25,500 scalar mulitpliesA=(((BxC)xD)xE)xF

Optimal ordering: 11,875 scalar multipliesA=(Bx(CxD))x(ExF)

Optimal ordering: 11,875 scalar multipliesA=(Bx(CxD))x(ExF)

Typical C++ operator overloading solutioncould involve unnecessary communicationof C to processor 2, then results toprocessor 1

Typical C++ operator overloading solutioncould involve unnecessary communicationof C to processor 2, then results toprocessor 1

Optimal solution: Move B’s data toprocessor 1, then evaluate

Optimal solution: Move B’s data toprocessor 1, then evaluate

MIT Lincoln Laboratory000801-er-13KAM 4/27/01

Conclusions

• C++ is preferable to C for high--performance signalprocessing– More direct translation from linear algebra to code– Use of C++ expression templates results in similar

performance• PETE element-wise evaluation is preferable in some cases

to mathematical libraries such as VSIPL which necessitatethe creation of temporary objects. Example: A=B+C*D

• Expression templates allow optimizations not possible withtypical C++ operator overloading– Minimize operation count. Example: A=BxCxDxExF– Minimize communication. Example: A=B+C

• PETE was obtained from Los Alamos National Laboratory’sAdvanced Computing LaboratoryLockheed Martin MIT Lincoln Laboratory

Acknowledgements

Phil Barile Jane KentNathan Doss Mike LontocMary Frances Caravaglio Rathin PutatundaMichael Iaquinto Roelan Teachey

Jim Daly Jan MatlisJim Demers Patrick RichardsonDan Drake Eddie RutledgeHank Hoffmann Glenn SchraderJeremy Kepner Ben SadoskiJames Lebak