45
1 Introduction Template Metaprogramming TMP in Armadillo Conclusion Armadillo: Template Metaprogramming for Linear Algebra Leo Antunes and Markus Kruber Seminar on Automation, Compilers, and Code-Generation HPAC June 2014 Leo Antunes and Markus Kruber Armadillo

Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

1

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

Armadillo: Template Metaprogramming forLinear Algebra

Leo Antunes and Markus Kruber

Seminar on Automation, Compilers, and Code-GenerationHPAC

June 2014

Leo Antunes and Markus Kruber Armadillo

Page 2: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

2

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

What is Armadillo?Code example

What is Armadillo?

. C++ Library for Linear Algebra

. Aims for ease of use

. Still wants performance

. Abstract interface to BLAS and LAPACK

. Pure template library (only headers)

Leo Antunes and Markus Kruber Armadillo

Page 3: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

2

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

What is Armadillo?Code example

What is Armadillo?

. C++ Library for Linear Algebra

. Aims for ease of use

. Still wants performance

. Abstract interface to BLAS and LAPACK

. Pure template library (only headers)

Leo Antunes and Markus Kruber Armadillo

Page 4: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

2

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

What is Armadillo?Code example

What is Armadillo?

. C++ Library for Linear Algebra

. Aims for ease of use

. Still wants performance

. Abstract interface to BLAS and LAPACK

. Pure template library (only headers)

Leo Antunes and Markus Kruber Armadillo

Page 5: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

2

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

What is Armadillo?Code example

What is Armadillo?

. C++ Library for Linear Algebra

. Aims for ease of use

. Still wants performance

. Abstract interface to BLAS and LAPACK

. Pure template library (only headers)

Leo Antunes and Markus Kruber Armadillo

Page 6: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

3

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

What is Armadillo?Code example

Code example

Calculating A−1BvvT :

mat A = << 3.0 << 2.5 << endr

<< 2.0 << 1.0 << endr

mat B = randu (2,5);

colvec v = randu (5);

mat result = inv(A)*B*v*trans(v);

cout << "Result:" << endl << result.row (0) << endl;

Leo Antunes and Markus Kruber Armadillo

Page 7: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

4

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Refresher: C++ Templates

. Allow type-agnostic development (e.g.: class Array<type>)”generics” in Java, C#; ”parametric polymorphism” in Haskell, Scala

. Realized at compile-time → code generation

. Generate different class for each type used in code(thus: ”template”)

. Therefore: no dynamic typing

. Allows for further optimizations by compiler

Leo Antunes and Markus Kruber Armadillo

Page 8: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

4

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Refresher: C++ Templates

. Allow type-agnostic development (e.g.: class Array<type>)”generics” in Java, C#; ”parametric polymorphism” in Haskell, Scala

. Realized at compile-time → code generation

. Generate different class for each type used in code(thus: ”template”)

. Therefore: no dynamic typing

. Allows for further optimizations by compiler

Leo Antunes and Markus Kruber Armadillo

Page 9: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

4

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Refresher: C++ Templates

. Allow type-agnostic development (e.g.: class Array<type>)”generics” in Java, C#; ”parametric polymorphism” in Haskell, Scala

. Realized at compile-time → code generation

. Generate different class for each type used in code(thus: ”template”)

. Therefore: no dynamic typing

. Allows for further optimizations by compiler

Leo Antunes and Markus Kruber Armadillo

Page 10: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

4

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Refresher: C++ Templates

. Allow type-agnostic development (e.g.: class Array<type>)”generics” in Java, C#; ”parametric polymorphism” in Haskell, Scala

. Realized at compile-time → code generation

. Generate different class for each type used in code(thus: ”template”)

. Therefore: no dynamic typing

. Allows for further optimizations by compiler

Leo Antunes and Markus Kruber Armadillo

Page 11: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

4

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Refresher: C++ Templates

. Allow type-agnostic development (e.g.: class Array<type>)”generics” in Java, C#; ”parametric polymorphism” in Haskell, Scala

. Realized at compile-time → code generation

. Generate different class for each type used in code(thus: ”template”)

. Therefore: no dynamic typing

. Allows for further optimizations by compiler

Leo Antunes and Markus Kruber Armadillo

Page 12: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

5

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

C++ Template example

Type-dependent:

class Adder{

int base;

public:

Adder(int x):base(x){}

int doit(int y){

return base+y;

}

};

Type-agnostic:

template <typename TYPE >

class Adder{

TYPE base;

public:

Adder(TYPE x):base(x){}

TYPE doit(TYPE y){

return base+y;

}

};

Leo Antunes and Markus Kruber Armadillo

Page 13: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

6

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Template Metaprogramming

. Use C++ templates as a language in itself

. Type specification as pattern matching → Branching(independently of C++’s control structures)

. Template used inside template → Recursion

. Branching and Recursion → Turing complete

. Allows offloading of computation to compile-time

Leo Antunes and Markus Kruber Armadillo

Page 14: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

6

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Template Metaprogramming

. Use C++ templates as a language in itself

. Type specification as pattern matching → Branching(independently of C++’s control structures)

. Template used inside template → Recursion

. Branching and Recursion → Turing complete

. Allows offloading of computation to compile-time

Leo Antunes and Markus Kruber Armadillo

Page 15: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

6

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Template Metaprogramming

. Use C++ templates as a language in itself

. Type specification as pattern matching → Branching(independently of C++’s control structures)

. Template used inside template → Recursion

. Branching and Recursion → Turing complete

. Allows offloading of computation to compile-time

Leo Antunes and Markus Kruber Armadillo

Page 16: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

6

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Template Metaprogramming

. Use C++ templates as a language in itself

. Type specification as pattern matching → Branching(independently of C++’s control structures)

. Template used inside template → Recursion

. Branching and Recursion → Turing complete

. Allows offloading of computation to compile-time

Leo Antunes and Markus Kruber Armadillo

Page 17: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

6

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Template Metaprogramming

. Use C++ templates as a language in itself

. Type specification as pattern matching → Branching(independently of C++’s control structures)

. Template used inside template → Recursion

. Branching and Recursion → Turing complete

. Allows offloading of computation to compile-time

Leo Antunes and Markus Kruber Armadillo

Page 18: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

7

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Factorial: normal C++

int factorial(int n) {

if(n < 1){

return 1;

}

else {

return n*factorial(n-1);

}

}

Leo Antunes and Markus Kruber Armadillo

Page 19: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

8

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

C++ TemplatesOverviewExamples

Factorial: Template metaprogramming

template <int n>

struct factorial {

const static int result =

n * factorial <n - 1>:: result;

};

template <>

struct factorial <0> {

const static int result = 1 ;

};

. Types used for pattern matching.

. Structs used as functions.

. Most ”specific” template chosen first. (cf. Haskell)

Leo Antunes and Markus Kruber Armadillo

Page 20: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

9

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

TMP in Armadillo

. Classes for common LA objects: matrix, vector, etc

. Literal expression in code converted to template tree, e.g.:A*inv(B) → Glue<A, Op<B, op inv>, op times>

. Infers information on arguments via pattern matching

. User can provide information about objects.e.g.: diagmat(A) or trimatu(A)

. Actual calculation delayed until runtime request for results

Leo Antunes and Markus Kruber Armadillo

Page 21: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

9

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

TMP in Armadillo

. Classes for common LA objects: matrix, vector, etc

. Literal expression in code converted to template tree, e.g.:A*inv(B) → Glue<A, Op<B, op inv>, op times>

. Infers information on arguments via pattern matching

. User can provide information about objects.e.g.: diagmat(A) or trimatu(A)

. Actual calculation delayed until runtime request for results

Leo Antunes and Markus Kruber Armadillo

Page 22: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

9

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

TMP in Armadillo

. Classes for common LA objects: matrix, vector, etc

. Literal expression in code converted to template tree, e.g.:A*inv(B) → Glue<A, Op<B, op inv>, op times>

. Infers information on arguments via pattern matching

. User can provide information about objects.e.g.: diagmat(A) or trimatu(A)

. Actual calculation delayed until runtime request for results

Leo Antunes and Markus Kruber Armadillo

Page 23: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

9

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

TMP in Armadillo

. Classes for common LA objects: matrix, vector, etc

. Literal expression in code converted to template tree, e.g.:A*inv(B) → Glue<A, Op<B, op inv>, op times>

. Infers information on arguments via pattern matching

. User can provide information about objects.e.g.: diagmat(A) or trimatu(A)

. Actual calculation delayed until runtime request for results

Leo Antunes and Markus Kruber Armadillo

Page 24: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

9

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

TMP in Armadillo

. Classes for common LA objects: matrix, vector, etc

. Literal expression in code converted to template tree, e.g.:A*inv(B) → Glue<A, Op<B, op inv>, op times>

. Infers information on arguments via pattern matching

. User can provide information about objects.e.g.: diagmat(A) or trimatu(A)

. Actual calculation delayed until runtime request for results

Leo Antunes and Markus Kruber Armadillo

Page 25: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

10

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Simple example: inv(inv(A))

. Rationale: should not compute anything; just return A

. Inner call to inv() returns a Op<A, op inv> object.

. Outter call matches the definition of inv() withOp<A, op inv> argument. Returns A.

. Advantages:

. Obvious: no actual inversion.

. Not obvious: multiple-dispatch without the runtime overhead.

. Allows compiler to get rid of unneeded function calls.

Leo Antunes and Markus Kruber Armadillo

Page 26: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

10

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Simple example: inv(inv(A))

. Rationale: should not compute anything; just return A

. Inner call to inv() returns a Op<A, op inv> object.

. Outter call matches the definition of inv() withOp<A, op inv> argument. Returns A.

. Advantages:

. Obvious: no actual inversion.

. Not obvious: multiple-dispatch without the runtime overhead.

. Allows compiler to get rid of unneeded function calls.

Leo Antunes and Markus Kruber Armadillo

Page 27: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

11

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Matrix Chain Multiplication

. Matrix multiplication is associative.(ABC )D = A(BCD) = . . .

. Order affects the number of operations.

. A ∈ R60×5, B ∈ R5×30, C ∈ R30×10

. (AB)C = (60 · 5 · 30) + (60 · 30 · 10) = 27.000

. A(BC ) = (5 · 30 · 10) + (60 · 5 · 10) = 4.500

. E.g. Matlab: naive left to right ordering

. Complexity: O(n3) with dynamic programmingSmarter approach: O(n · log(n))

Leo Antunes and Markus Kruber Armadillo

Page 28: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

11

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Matrix Chain Multiplication

. Matrix multiplication is associative.(ABC )D = A(BCD) = . . .

. Order affects the number of operations.

. A ∈ R60×5, B ∈ R5×30, C ∈ R30×10

. (AB)C = (60 · 5 · 30) + (60 · 30 · 10) = 27.000

. A(BC ) = (5 · 30 · 10) + (60 · 5 · 10) = 4.500

. E.g. Matlab: naive left to right ordering

. Complexity: O(n3) with dynamic programmingSmarter approach: O(n · log(n))

Leo Antunes and Markus Kruber Armadillo

Page 29: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

11

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Matrix Chain Multiplication

. Matrix multiplication is associative.(ABC )D = A(BCD) = . . .

. Order affects the number of operations.

. A ∈ R60×5, B ∈ R5×30, C ∈ R30×10

. (AB)C = (60 · 5 · 30) + (60 · 30 · 10) = 27.000

. A(BC ) = (5 · 30 · 10) + (60 · 5 · 10) = 4.500

. E.g. Matlab: naive left to right ordering

. Complexity: O(n3) with dynamic programmingSmarter approach: O(n · log(n))

Leo Antunes and Markus Kruber Armadillo

Page 30: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

12

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Complex example: A*B*C*D

. Instantiated as:Glue<Glue<Glue<A,B,op times>,C,op times>,D,op times>

. Determines ”depth” of expression: N = 3(amount of operators with same precedence)

. Pattern matches specialized function for N = 3 which:

. Decide (ABC )D vs. A(BCD)

. Heuristic: cost(ABC ) := columns(A) · rows(C )

. Recursively calls reordering function with N = 2

. Thus: linear (but sub-optimal) reordering of operations

. Remember: all this at compile-time!

Leo Antunes and Markus Kruber Armadillo

Page 31: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

12

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Complex example: A*B*C*D

. Instantiated as:Glue<Glue<Glue<A,B,op times>,C,op times>,D,op times>

. Determines ”depth” of expression: N = 3(amount of operators with same precedence)

. Pattern matches specialized function for N = 3 which:

. Decide (ABC )D vs. A(BCD)

. Heuristic: cost(ABC ) := columns(A) · rows(C )

. Recursively calls reordering function with N = 2

. Thus: linear (but sub-optimal) reordering of operations

. Remember: all this at compile-time!

Leo Antunes and Markus Kruber Armadillo

Page 32: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

12

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Complex example: A*B*C*D

. Instantiated as:Glue<Glue<Glue<A,B,op times>,C,op times>,D,op times>

. Determines ”depth” of expression: N = 3(amount of operators with same precedence)

. Pattern matches specialized function for N = 3 which:

. Decide (ABC )D vs. A(BCD)

. Heuristic: cost(ABC ) := columns(A) · rows(C )

. Recursively calls reordering function with N = 2

. Thus: linear (but sub-optimal) reordering of operations

. Remember: all this at compile-time!

Leo Antunes and Markus Kruber Armadillo

Page 33: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

13

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Complex example: A*B*C*D (cont.)

. ((AB)C )D 3

⇒ O(n3)

. (A(BC ))D 3

⇒ O(n3)

. A((BC )D) 3

⇒ O(n3)

. A(B(CD)) 3

⇒ O(n3)

. (AB)(CD) 7

⇒ O(n2)

n Armadillo All

3 2 24 4 55 8 146 16 427 32 132

n

n

A

1

n

B

n1

C

n

n

D

Leo Antunes and Markus Kruber Armadillo

Page 34: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

13

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Complex example: A*B*C*D (cont.)

. ((AB)C )D 3

⇒ O(n3)

. (A(BC ))D 3

⇒ O(n3)

. A((BC )D) 3

⇒ O(n3)

. A(B(CD)) 3

⇒ O(n3)

. (AB)(CD) 7

⇒ O(n2)

n Armadillo All

3 2 24 4 55 8 146 16 427 32 132

n

n

A

1

n

B

n1

C

n

n

D

Leo Antunes and Markus Kruber Armadillo

Page 35: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

13

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Complex example: A*B*C*D (cont.)

. ((AB)C )D 3 ⇒ O(n3)

. (A(BC ))D 3 ⇒ O(n3)

. A((BC )D) 3 ⇒ O(n3)

. A(B(CD)) 3 ⇒ O(n3)

. (AB)(CD) 7 ⇒ O(n2)

n Armadillo All

3 2 24 4 55 8 146 16 427 32 132

n

n

A

1

n

B

n1

C

n

n

D

Leo Antunes and Markus Kruber Armadillo

Page 36: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

13

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Complex example: A*B*C*D (cont.)

. ((AB)C )D 3 ⇒ O(n3)

. (A(BC ))D 3 ⇒ O(n3)

. A((BC )D) 3 ⇒ O(n3)

. A(B(CD)) 3 ⇒ O(n3)

. (AB)(CD) 7 ⇒ O(n2)

n Armadillo All

3 2 24 4 55 8 146 16 427 32 132

n

n

A

1

n

B

n1

C

n

n

D

Leo Antunes and Markus Kruber Armadillo

Page 37: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

14

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Other improvements

. A = A1 + A2 + . . . + An

No temporary matrix besides output

. y = A−1x → y = solve(A, x)Faster and more accurate

. inv(diag(A))Elementwise inverse

. as scalar(r ∗ X ∗ q) with r ∈ R1×n,X ∈ Rn×n, q ∈ Rn×1

Result of computation is 1× 1 matrix

. Lots more!

Leo Antunes and Markus Kruber Armadillo

Page 38: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

14

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Other improvements

. A = A1 + A2 + . . . + An

No temporary matrix besides output

. y = A−1x → y = solve(A, x)Faster and more accurate

. inv(diag(A))Elementwise inverse

. as scalar(r ∗ X ∗ q) with r ∈ R1×n,X ∈ Rn×n, q ∈ Rn×1

Result of computation is 1× 1 matrix

. Lots more!

Leo Antunes and Markus Kruber Armadillo

Page 39: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

14

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Other improvements

. A = A1 + A2 + . . . + An

No temporary matrix besides output

. y = A−1x → y = solve(A, x)Faster and more accurate

. inv(diag(A))Elementwise inverse

. as scalar(r ∗ X ∗ q) with r ∈ R1×n,X ∈ Rn×n, q ∈ Rn×1

Result of computation is 1× 1 matrix

. Lots more!

Leo Antunes and Markus Kruber Armadillo

Page 40: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

14

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Other improvements

. A = A1 + A2 + . . . + An

No temporary matrix besides output

. y = A−1x → y = solve(A, x)Faster and more accurate

. inv(diag(A))Elementwise inverse

. as scalar(r ∗ X ∗ q) with r ∈ R1×n,X ∈ Rn×n, q ∈ Rn×1

Result of computation is 1× 1 matrix

. Lots more!

Leo Antunes and Markus Kruber Armadillo

Page 41: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

14

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

OverviewExamples

Other improvements

. A = A1 + A2 + . . . + An

No temporary matrix besides output

. y = A−1x → y = solve(A, x)Faster and more accurate

. inv(diag(A))Elementwise inverse

. as scalar(r ∗ X ∗ q) with r ∈ R1×n,X ∈ Rn×n, q ∈ Rn×1

Result of computation is 1× 1 matrix

. Lots more!

Leo Antunes and Markus Kruber Armadillo

Page 42: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

15

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

Conclusion

3 Easy to use for non-LA-experts

3 Some optimization comes built-in

7 Cannot optimize all cases

7 Debugging for more complex code can be a problem (C++)

. Our recommendation: Use it! Don’t try to understand it.

Leo Antunes and Markus Kruber Armadillo

Page 43: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

15

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

Conclusion

3 Easy to use for non-LA-experts

3 Some optimization comes built-in

7 Cannot optimize all cases

7 Debugging for more complex code can be a problem (C++)

. Our recommendation: Use it! Don’t try to understand it.

Leo Antunes and Markus Kruber Armadillo

Page 44: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

15

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

Conclusion

3 Easy to use for non-LA-experts

3 Some optimization comes built-in

7 Cannot optimize all cases

7 Debugging for more complex code can be a problem (C++)

. Our recommendation: Use it! Don’t try to understand it.

Leo Antunes and Markus Kruber Armadillo

Page 45: Armadillo: Template Metaprogramming for Linear Algebrahpac.rwth-aachen.de/teaching/sem-accg-14/Armadillo.pdf · C++ Templates Overview Examples Template Metaprogramming. Use C++ templates

16

IntroductionTemplate Metaprogramming

TMP in ArmadilloConclusion

Thanks! Questions?

Sources:

i Conrad Sanderson, Armadillo: An Open Source C++ Linear AlgebraLibrary for Fast Prototyping and Computationally Intensive Experiments,Technical Report, NICTA, 2010.

i Armadillo source code.

i C++ Programming, chapter Template Meta-Programming, Wikibooks

i Matrix chain multiplication, Wikipedia

Leo Antunes and Markus Kruber Armadillo