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

c/c++/Fortran efficient indexing

Register Blogs Community New Posts Updated Threads Search

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

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 25, 2011, 03:54
Default c/c++/Fortran efficient indexing
  #1
Senior Member
 
Ford Prefect's Avatar
 
Join Date: Mar 2009
Posts: 151
Rep Power: 17
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, 04:49
Default
  #2
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 74
Blog Entries: 5
Rep Power: 14
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, 05:10
Default
  #3
Senior Member
 
Ford Prefect's Avatar
 
Join Date: Mar 2009
Posts: 151
Rep Power: 17
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, 05:23
Default
  #4
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 74
Blog Entries: 5
Rep Power: 14
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, 05:40
Default
  #5
Senior Member
 
Ford Prefect's Avatar
 
Join Date: Mar 2009
Posts: 151
Rep Power: 17
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, 05:57
Default
  #6
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 74
Blog Entries: 5
Rep Power: 14
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, 19:36
Default
  #7
Senior Member
 
Martin Hegedus
Join Date: Feb 2011
Posts: 500
Rep Power: 19
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, 21:10
Default
  #8
Senior Member
 
Arjun
Join Date: Mar 2009
Location: Nurenberg, Germany
Posts: 1,273
Rep Power: 34
arjun will become famous soon enougharjun will become famous soon enough
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 online now   Reply With Quote

Old   November 28, 2011, 00:57
Default
  #9
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 74
Blog Entries: 5
Rep Power: 14
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, 01:04
Default
  #10
Member
 
SergeAS's Avatar
 
Serge A. Suchkov
Join Date: Oct 2011
Location: Moscow, Russia
Posts: 74
Blog Entries: 5
Rep Power: 14
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, 04:39
Default
  #11
Member
 
Join Date: Nov 2011
Location: Czech Republic
Posts: 97
Rep Power: 14
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


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 Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:11.