03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

170 CHAPTER 5. META-PROGRAMMING<br />

only a sum but in theory it could be tons of them) only return objects with references. The<br />

loop operator= is split into the unrolled at the beginning and the one-by-one completion at the<br />

end:<br />

template <br />

class vector<br />

{<br />

template <br />

vector& operator=(const Src& that)<br />

{<br />

check size(size(that));<br />

unsigned s= my size, sb= s / 4 ∗ 4;<br />

};<br />

}<br />

<strong>for</strong> (unsigned i= 0; i < sb; i+= 4)<br />

assign()(∗this, that, i);<br />

<strong>for</strong> (unsigned i= sb; i < s; i++)<br />

data[i]= that[i];<br />

return ∗this;<br />

The assign functor is realized analogous to my axpy ftor:<br />

template <br />

struct assign<br />

{<br />

template <br />

void operator()(U& u, const V& v, unsigned i)<br />

{<br />

u[i+Offset]= v[i+Offset];<br />

assign()(u, v, i);<br />

}<br />

};<br />

template <br />

struct assign<br />

{<br />

template <br />

void operator()(U& u, const V& v, unsigned i) {}<br />

};<br />

Computing the expression above we yield:<br />

Compute time is 1.37 µs.<br />

With this rather simple modification we now accelerated ALL vector expression templates.<br />

In comparison with the previous implementation we lost however the flexibility to costumize<br />

the loop unrolling. The functor assign has two arguments thus allowing <strong>for</strong> customization. The<br />

problem is the assignment operator. In principle we can define an explicit template argument<br />

there:<br />

template <br />

vector& operator=(const Src& that)<br />

{

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!