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.

174 CHAPTER 5. META-PROGRAMMING<br />

}<br />

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

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

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

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

sum[0]+= sum[i];<br />

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

sum[0]+= abs(v[i]);<br />

return sum[0];<br />

The according functor must refer the right element in the sum array:<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[Offset]+= abs(v[i+Offset]);<br />

one norm ftor()(sum, 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 />

On the test machine this took:<br />

Compute time one_norm(v) is 7.33 µs.<br />

Compute time one_norm(v) is 5.15 µs.<br />

Compute time one_norm(v) is 2 µs.<br />

Compute time one_norm(v) is 1.4 µs.<br />

Compute time one_norm(v) is 1.16 µs.<br />

This is even a bit slower than the version with one variable. Maybe an array is more expensive<br />

to pass as argument even in an inline function. Let us try something else.<br />

Reducing on a Nested Class Object<br />

⇒ reduction unroll nesting example.cpp<br />

To avoid arrays, we can define a class <strong>for</strong> n temporary variables where n is a template argument.<br />

Such a class is designed more consistently with the recursive scheme of the functors:<br />

template <br />

struct multi tmp

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

Saved successfully!

Ooh no, something went wrong!