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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.4. META-TUNING: WRITE YOUR OWN COMPILER OPTIMIZATION 175<br />

{<br />

};<br />

typedef multi tmp sub type;<br />

multi tmp(const Value& v) : value(v), sub(v) {}<br />

Value value;<br />

sub type sub;<br />

template <br />

struct multi tmp<br />

{<br />

multi tmp(const Value& v) {}<br />

};<br />

An object of this type can be recursively initialized so that we do not need a loop as <strong>for</strong> the<br />

array. A functor can operate on the value member and pass a reference to the sub member to<br />

its successor. This leads us to the implementation of our functor:<br />

template <br />

struct one norm ftor<br />

{<br />

template <br />

void operator()(S& sum, const V& v, unsigned i)<br />

{<br />

using std::abs;<br />

sum.value+= abs(v[i+Offset]);<br />

one norm ftor()(sum.sub, v, i);<br />

}<br />

};<br />

template <br />

struct one norm ftor<br />

{<br />

template <br />

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

};<br />

The unrolled function that uses this functor reads:<br />

template <br />

typename Vector::value type<br />

inline one norm(const Vector& v)<br />

{<br />

using std::abs;<br />

typedef typename Vector::value type value type;<br />

multi tmp multi sum(0);<br />

unsigned s= size(v), sb= s / BSize ∗ BSize;<br />

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

one norm ftor()(multi sum, v, i);<br />

value type sum= multi sum.sum();<br />

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

sum+= abs(v[i]);

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

Saved successfully!

Ooh no, something went wrong!