CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   c/c++/Fortran efficient indexing (http://www.cfd-online.com/Forums/main/94782-c-c-fortran-efficient-indexing.html)

Ford Prefect November 25, 2011 04:54

c/c++/Fortran efficient indexing
 
Hey,

If I want to access an integer array several times in a loop is is more efficient to use a variable to hold the integer value, e.g.:

int array[];
int k;

for(i)
{
array[i]=k;
(k+5)/k+k^4;
}

rather than

for(i)
{
(array[i]+5)/array[i] + array[i]^4;
}

?

Thank you!

SergeAS November 25, 2011 05:49

Quote:

Originally Posted by Ford Prefect (Post 333534)
Hey,

If I want to access an integer array several times in a loop is is more efficient to use a variable to hold the integer value, e.g.:

int array[];
int k;

for(i)
{
array[i]=k;
(k+5)/k+k^4;
}

rather than

for(i)
{
(array[i]+5)/array[i] + array[i]^4;
}

?

Thank you!

A strange code ...

Modern high optimizing compilers easier to vectorize the code that does not use intermediate variables to store values​​, in addition with the best opportunity to use prefetch

But in any case for specific code need tests and profiling

Ford Prefect November 25, 2011 06:10

Quote:

Originally Posted by SergeAS (Post 333537)
A strange code ...

Modern high optimizing compilers easier to vectorize the code that does not use intermediate variables to store values​​, in addition with the best opportunity to use prefetch

But in any case for specific code need tests and profiling

Thank you. But if you set aside the strangeness of the code (which is just an example and not used in any way), is your answer that you don't know if example 1 or 2 is faster if you can't run a test, but in theory example 2 should be faster?

Regards

SergeAS November 25, 2011 06:23

I would say that the 2nd coding style will ultimately generate more efficient code (in my experience)

The rate of a particular code will depend on many factors, such as for small data sets are significant caсhe-effects

In any case, I would have tested the performance of several versions of the code

PS: It should be noted that the first coding style allows you to write more understandable and readable code that can ultimately will save considerably more time than the modification will be lost at a slower from its execution. The truth is usually somewhere in the middle

Ford Prefect November 25, 2011 06:40

Ok!

I have written lots of code over the years, but I have never had any reason/time to make it more efficient, nor have I had any formal training in writing optimized code. However it just seemed inefficient to repeat several operations during each cycle. A common way to write indexing in CFD is using one array, i.e.

u[i+j*iMax], u[(i+1)+j*iMax], u[i+(j+1)*iMax]...

This index repeats several times in the difference equations to be solved and writing

k=i+j*iMax;

u[k], u[k+1], u[k+iMax],...

seemed more simple.

Anyways, if I really need to optimize the code in the future I will definitely hire some with more knowledge than me in this field ;)

SergeAS November 25, 2011 06:57

The truth is that modern optimizing compilers have become so "clever" that "helping" them to optimize their code manually, you just disturb them:)

Martin Hegedus November 27, 2011 20:36

In general, as was mentioned above, I would let the optimizer handle things. Also, in general for a CFD code, you'll see the biggest bang for the buck by minimizing memory bandwidth.

That being said, the code that you've given can maybe made more efficient by writing

a2 = array[i]*array[i];
(array[i]+5)/array[i] + a2*a2;

instead of

(array[i]+5)/array[i] + array[i]^4;

arjun November 27, 2011 22:10

Quote:

Originally Posted by SergeAS (Post 333548)
The truth is that modern optimizing compilers have become so "clever" that "helping" them to optimize their code manually, you just disturb them:)


It all depends on the algorithm. Compiler can not optimize all the algorithms. It can however , many times , optimize loops much better than user might do.

Few months ago I wrote polygonal polygonal interaction algorithm for GGI. after rewriting the code for performance, the final code could do the same thing in 40% of time of what compiler optimized could manage.

My general advise is first go for accuracy and when it is accurate slowly change the code if performance is very important.

Faster codes are often difficult to read and understand, so it can make life difficult in future. For this reason it is always useful to keep old code commented out so that it could be helpful in understanding what is going on.

SergeAS November 28, 2011 01:57

Quote:

Originally Posted by Martin Hegedus (Post 333731)
In general, as was mentioned above, I would let the optimizer handle things. Also, in general for a CFD code, you'll see the biggest bang for the buck by minimizing memory bandwidth.

That being said, the code that you've given can maybe made more efficient by writing

a2 = array[i]*array[i];
(array[i]+5)/array[i] + a2*a2;

instead of

(array[i]+5)/array[i] + array[i]^4;

this is a bad example in general true paradigm :)

both code have same memory bandwith (will be used 3 memory cells)
but first code will be on one multiply instruction shorter

Just in this case, pice of code are too short to put in a difficult situation compiler


Let's tests ? :)

SergeAS November 28, 2011 02:04

Fully agree, we must focus on algorithm optimization and leave code optimizations to compiler

Sixkillers November 29, 2011 05:39

Anyway if you are interested in software optimization I would you recommend you this well known series of PDFs http://www.agner.org/optimize/#manuals.


All times are GMT -4. The time now is 10:15.