Comparison of Expression Templates fast Expression Templates - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Comparison of Expression Templates fast Expression Templates

Description:

We can implement the same template technique just like the Sum class. ... Just think nx*ny=10,000 ,and the size of our A matrix is 1e 8,but the useful ... – PowerPoint PPT presentation

Number of Views:154
Avg rating:3.0/5.0
Slides: 17
Provided by: www10Info
Category:

less

Transcript and Presenter's Notes

Title: Comparison of Expression Templates fast Expression Templates


1
Comparison of Expression Templates fast
Expression Templates
  • CE
  • ZHOU LONG
  • 12/01/2009

2
List of content
  • How is the template works
  • How to improve this template

3
The first part
  • How is the template works

4
Comparing the simple operator overloading and ET
  • Operator overloading always creat temporary
    vector
  • That is lead to a memory allocation, a copy
    operation, and a memory deallocation
  • Do not need creat new vector
  • Without any memory allocation and copy operation
  • It allows the compiler to perform significant
    optimizations such as inlining

5
Comparing the simple operator overloading and ET
  • inline
  • const Vector
  • operator( const Vector lhs, const Vector rhs )
  • assert( lhs.size() rhs.size() )
  • Vector tmp( lhs.size() )
  • for( size_t i0 iltlhs.size() i )
  • tmpi lhsi rhsi
  • return tmp
  • 1) creat a temporary vector tmp
  • 2) invoke the copy fuction
  • 3) delete the tmp
  • For a lang vector ,it will be very time consuming
  • templatelt typename L, typename R gt
  • class Sum
  • private
  • const L lhs_
  • const R rhs_
  • public
  • inline Sum( const L lhs, const R rhs )
  • lhs_( lhs ), rhs_( rhs )
  • inline double operator( size_t index ) const
  • return lhs_index rhs_index
  • templatelt typename L, typename R gt
  • inline
  • const SumltL,Rgt operator( const L lhs, const R
    rhs )
  • return SumltL,Rgt( lhs, rhs )

6
Where to apply ET
  • For twe vectors addition and substraction
  • For multiplication of a vector and a scalar
  • For negate a vector
  • We can implement the same template technique just
    like the Sum class.

7
Whats the problem of this ET
  • We want to solve f A b
  • Although we can use the normal ET for the
    multiplication of matrix A and vector b, it is
    still time consuming because of A is band matrix.

8
The second part
  • How to improve this template

9
A is band matrix
10
A is band matrix
  • Which is means the useless 0 occupy the most
    space of the whole matrix.
  • In our problem, the maximum number of useful
    values in each row is 5.
  • If we multiply A with a vector,we spend a lot of
    time to locate 0 and multiply them and then sum
    them with the vector.
  • Just think nxny10,000 ,and the size of our A
    matrix is 1e8,but the useful values are ltlt 50,000

11
Compact the A matrix
  • Since there are at most 5 useful members each
    row, I compact the matrix A with only the useful
    information.
  • Information represented by the compact A matrix
  • 1)The maximum 5 useful memmbers in each row of
    A.
  • 2)The corresponding position in vector b of these
    5 members should multiply with.
  • Namely ,the compact A matrix are not only store A
    values but also store the corresponding position
    in vector b.

12
For A matrix to compact A matrix
13
A(i,1)bA(i,0)
A(i,3)bA(i,2)
?
?
nb0
mbny
The corresponding position in compact A matrix
14
  • To get the fi,we need to sum them up
  • fi A(i,1)bA(i,0) A(i,3)bA(i,2)A(i,4)b
    A(i,2)1A(i,5)bA(i,2)2A(i,7)bA(i,6)
  • The multiplication now can be evaluted by
    overloading the template of assignment operator
    and it only need one for loop.

15
The modified multiply class
  • templatelt typename L,typename Rgt
  • class MatMultiply public VExprlt
    MatMultiplyltL,Rgt gt
  • private
  • const L A_
  • const R rhs_
  • public
  • inline MatMultiply(const L A,const R rhs)
    A_(A),rhs_(rhs)
  • assert ( A_.colum() rhs.size() )
  • inline double operator(size_t index) const
  • double sum0.0
  • int mA_(index,2)2
  • if (mgtA_.colum()) m0
  • sum A_(index,1)rhs_A_(index,0)A_(index,3)
    rhs_A_(index,2)A_(index,4)rhs_A_(index,2)1
    A_(index,5)rhs_mA_(index,7)rhs_A_(index,6)
  • return sum
  • inline size_t size() const return
    rhs_.size()

16
The end
Write a Comment
User Comments (0)
About PowerShow.com