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.

}<br />

}<br />

// q= A ∗ p;<br />

<strong>for</strong> (i= 0; i < size; i++)<br />

q[i]= 0;<br />

<strong>for</strong> (i= 0; i < nnz; i++)<br />

q[aip[i]]+= avp[i] ∗ p[ajp[i]];<br />

alpha= rho / dot(size, p, q);<br />

// x+= alpa ∗ p; r−= alpha ∗ q;<br />

<strong>for</strong> (i= 0; i < size; i++) {<br />

x[i]+= alpha ∗ p[i];<br />

r[i]−= alpha ∗ q[i];<br />

}<br />

iter++;<br />

}<br />

free(q); free(p); free(r); free(z);<br />

return iter;<br />

void ic 0(int size, double∗ out, double∗ in) { /∗ .. ∗/ }<br />

int main (int argc, char∗ argv[])<br />

{<br />

int nnz, size;<br />

}<br />

// set nnz and size<br />

int ∗aip= (int∗) malloc(nnz ∗ sizeof(double));<br />

int ∗ajp= (int∗) malloc(nnz ∗ sizeof(double));<br />

double ∗avp= (double∗) malloc(nnz ∗ sizeof(double));<br />

double ∗x= (double∗) malloc(size ∗ sizeof(double));<br />

double ∗b= (double∗) malloc(size ∗ sizeof(double));<br />

// set A and b<br />

cg(size, nnz, aip, ajp, avp, x, b, ilu, 1e−9);<br />

return 0 ;<br />

Listing 1.1: Low Abstraction Implementation of CG<br />

As said be<strong>for</strong>e the details do not matter here but only the principal approach. The good thing<br />

about this code is that it is self-contained. But this is about the only advantage. The problem<br />

with this implemenation is its low abstraction level. This creates three major disadvantages:<br />

• Bad readability;<br />

• No flexibility; and<br />

• High error-proneness.<br />

The bad readability manifests in the fact that almost every operation is implemented in one<br />

or multiple loops. For instance, would we have found the matrix vector multiplication q = Ap<br />

13

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

Saved successfully!

Ooh no, something went wrong!