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.

104 CHAPTER 4. GENERIC PROGRAMMING<br />

{<br />

}<br />

...<br />

double inline power(double x, double y)<br />

{<br />

return std::pow(x, y);<br />

}<br />

Speaking of plat<strong>for</strong>m-specific assembler hacks, maybe we are eager to contribute a code that<br />

explores SSE units by per<strong>for</strong>ming two computations in parallel:<br />

template <br />

Base inline power(const Base& x, const Exponent) { ... }<br />

#ifdef SSE FOR TRYPTICHON WQ OMICRON LXXXVI SUPPORTED<br />

std::pair inline power(const std::pair& x, double y)<br />

{<br />

asm {<br />

# Yo, I’m the greatestest geek under the sun!<br />

}<br />

return whatever;<br />

}<br />

#endif<br />

#ifdef ... more hacks ...<br />

What is to say about this snippet? If you do not like to write such specializations, we will<br />

not blame you. If you do, always put such hacks in conditional compilation. You have<br />

to make sure as well that your build system only enables the macro when it is definitely a<br />

plat<strong>for</strong>m that supports the hack. For the case that it does not, we must guarentee that the<br />

generic implementation or another overload can deal with pairs of double. Last but not least,<br />

you have to rewrite your applications <strong>for</strong> using this function. Convincing others to use such<br />

special implementation could be even more work than getting the assembler hack producing<br />

plausible numbers. More importantly, such special signatures undermines the ideal of a clear<br />

and intuitive programming. However, if power functions are computed on entire vectors and<br />

matrices, one could per<strong>for</strong>m the calculation pairwise internally without affecting the interface<br />

or the user application.<br />

You might also think that SSEs were yesterday and today we have GPUs and GPGPUs but<br />

programming generically still takes a lot of tricks (at least in the beginning of 2010). But this is<br />

another story and we digress. Resuming: programming <strong>for</strong> highest per<strong>for</strong>mance can be tricky<br />

but at least there often ways to explore unportable feature (where available) without sacrificing<br />

portability at the application level. 10<br />

In the previous examples, we specialized all arguments of the function. It is also possible to<br />

specialize some argument(s) and leave the remaining argument(s) as template(s):<br />

template <br />

Base inline power(const Base& x, const Exponent& y);<br />

template <br />

10 TODO: Is this comprehensible?

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

Saved successfully!

Ooh no, something went wrong!