CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

c/c++/Fortran efficient indexing

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree3Likes
  • 1 Post By arjun
  • 1 Post By SergeAS
  • 1 Post By Sixkillers

Reply
 
LinkBack Thread Tools Display Modes
Old   November 25, 2011, 04:54
Default c/c++/Fortran efficient indexing
  #1
Member
 
Ford Prefect's Avatar
 
Join Date: Mar 2009
Posts: 72
Rep Power: 8
Ford Prefect is on a distinguished road
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!
__________________
"Trying is the first step to failure." - Homer Simpson
Ford Prefect is offline   Reply With Quote

Old   November 25, 2011, 05:49
Default
  #2
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 73
Blog Entries: 5
Rep Power: 5
SergeAS is on a distinguished road
Send a message via Skype™ to SergeAS
Quote:
Originally Posted by Ford Prefect View Post
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
SergeAS is offline   Reply With Quote

Old   November 25, 2011, 06:10
Default
  #3
Member
 
Ford Prefect's Avatar
 
Join Date: Mar 2009
Posts: 72
Rep Power: 8
Ford Prefect is on a distinguished road
Quote:
Originally Posted by SergeAS View Post
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
__________________
"Trying is the first step to failure." - Homer Simpson
Ford Prefect is offline   Reply With Quote

Old   November 25, 2011, 06:23
Default
  #4
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 73
Blog Entries: 5
Rep Power: 5
SergeAS is on a distinguished road
Send a message via Skype™ to SergeAS
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
SergeAS is offline   Reply With Quote

Old   November 25, 2011, 06:40
Default
  #5
Member
 
Ford Prefect's Avatar
 
Join Date: Mar 2009
Posts: 72
Rep Power: 8
Ford Prefect is on a distinguished road
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
__________________
"Trying is the first step to failure." - Homer Simpson
Ford Prefect is offline   Reply With Quote

Old   November 25, 2011, 06:57
Default
  #6
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 73
Blog Entries: 5
Rep Power: 5
SergeAS is on a distinguished road
Send a message via Skype™ to SergeAS
The truth is that modern optimizing compilers have become so "clever" that "helping" them to optimize their code manually, you just disturb them
SergeAS is offline   Reply With Quote

Old   November 27, 2011, 20:36
Default
  #7
Senior Member
 
Martin Hegedus
Join Date: Feb 2011
Posts: 467
Rep Power: 9
Martin Hegedus is on a distinguished road
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;
Martin Hegedus is offline   Reply With Quote

Old   November 27, 2011, 22:10
Default
  #8
Senior Member
 
Arjun
Join Date: Mar 2009
Location: Nurenberg, Germany
Posts: 373
Rep Power: 10
arjun is on a distinguished road
Quote:
Originally Posted by SergeAS View Post
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.
lakeat likes this.
arjun is offline   Reply With Quote

Old   November 28, 2011, 01:57
Default
  #9
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 73
Blog Entries: 5
Rep Power: 5
SergeAS is on a distinguished road
Send a message via Skype™ to SergeAS
Quote:
Originally Posted by Martin Hegedus View Post
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 ?
lakeat likes this.
SergeAS is offline   Reply With Quote

Old   November 28, 2011, 02:04
Default
  #10
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 73
Blog Entries: 5
Rep Power: 5
SergeAS is on a distinguished road
Send a message via Skype™ to SergeAS
Fully agree, we must focus on algorithm optimization and leave code optimizations to compiler
SergeAS is offline   Reply With Quote

Old   November 29, 2011, 05:39
Default
  #11
Member
 
Join Date: Nov 2011
Location: Czech Republic
Posts: 95
Rep Power: 5
Sixkillers is on a distinguished road
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.
arjun likes this.
Sixkillers is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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