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 163<br />

w[1]+= A[1][1] ∗ v[1];<br />

w[2]+= A[2][1] ∗ v[1];<br />

w[3]+= A[3][1] ∗ v[1];<br />

w[0]+= A[0][2] ∗ v[2];<br />

w[1]+= A[1][2] ∗ v[2];<br />

w[2]+= A[2][2] ∗ v[2];<br />

w[3]+= A[3][2] ∗ v[2];<br />

w[0]+= A[0][3] ∗ v[3];<br />

w[1]+= A[1][3] ∗ v[3];<br />

w[2]+= A[2][3] ∗ v[3];<br />

w[3]+= A[3][3] ∗ v[3];<br />

We only need to reorganize our functor. The general template reads now:<br />

template <br />

struct fsize mat vec mult cm<br />

{<br />

template <br />

void operator()(const Matrix& A, const VecIn& v in, VecOut& v out)<br />

{<br />

fsize mat vec mult cm()(A, v in, v out);<br />

v out[Rows]+= A[Rows][Cols] ∗ v in[Cols];<br />

}<br />

};<br />

Now, we need a partial specialization <strong>for</strong> row 0 to go the next column:<br />

template <br />

struct fsize mat vec mult cm<br />

{<br />

template <br />

void operator()(const Matrix& A, const VecIn& v in, VecOut& v out)<br />

{<br />

fsize mat vec mult cm()(A, v in, v out);<br />

v out[0]+= A[0][Cols] ∗ v in[Cols];<br />

}<br />

};<br />

The partial specialization <strong>for</strong> column 0 is also needed to initialize the entry of the output vector:<br />

template <br />

struct fsize mat vec mult cm<br />

{<br />

template <br />

void operator()(const Matrix& A, const VecIn& v in, VecOut& v out)<br />

{<br />

fsize mat vec mult cm()(A, v in, v out);<br />

v out[Rows]= A[Rows][0] ∗ v in[0];<br />

}<br />

};<br />

Finally, we still need a specialization <strong>for</strong> row and column 0 to terminate the recursion. This<br />

can be reused from the previous functor:<br />

template <br />

struct fsize mat vec mult cm<br />

: fsize mat vec mult {};

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

Saved successfully!

Ooh no, something went wrong!