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.

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

}<br />

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

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

<strong>for</strong> (unsigned i= 0; i < sb; i+= BSize)<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 drawback is that we cannot use the symbol ‘=’ naturally as infix operator but must write:<br />

u.operator=(v + v + w);<br />

This has in fact a certain geeky charm and one could also argue that people did (and still do)<br />

more painful things <strong>for</strong> per<strong>for</strong>mance. Nonetheless, it does not meet our ideals of intuitiveness<br />

and readability.<br />

Alternative notations are:<br />

or<br />

unroll(u= v + v + w);<br />

unroll(u)= v + v + w;<br />

Both version are implementable and provide comparable intuitiveness. The <strong>for</strong>mer expresses<br />

more correctly what we are doing while the latter is easier to implement and the structure of the<br />

computed expression remains better visibility. There<strong>for</strong>e we show the realization of the second<br />

<strong>for</strong>m.<br />

The function unroll is simple to implement: it just returns an object with a reference to the<br />

vector and a type in<strong>for</strong>mation <strong>for</strong> the unroll size:<br />

template <br />

unroll vector inline unroll(Vector& v)<br />

{<br />

return unroll vector(v);<br />

}<br />

The class unroll vector is not complicated either. It only needs to take a reference to the target<br />

vector and an assignment operator:<br />

template <br />

class unroll vector<br />

{<br />

public:<br />

unroll vector(V& ref) : ref(ref) {}<br />

template <br />

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

{<br />

assert(size(ref) == size(that));<br />

unsigned s= size(ref), sb= s / BSize ∗ BSize;

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

Saved successfully!

Ooh no, something went wrong!