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

[Fortran] DO loop bounds calculation

Register Blogs Community New Posts Updated Threads Search

Like Tree8Likes
  • 1 Post By FMDenaro
  • 1 Post By flotus1
  • 1 Post By sbaffini
  • 1 Post By sbaffini
  • 1 Post By sbaffini
  • 1 Post By Gerry Kan
  • 1 Post By Simbelmynė
  • 1 Post By Simbelmynė

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 17, 2021, 13:35
Default [Fortran] DO loop bounds calculation
  #1
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11
Gerry Kan is on a distinguished road
Howdy Folks:

I have a block of code in a do loop that is abbreviated as follows:
Code:
integer :: k0, k1, k, kN

k0 = 0
kN = 100
k1 = k0 + 1

do k = k1,kN
   ! do something
end do
Some guy tells me that the do loop has to be done like this:

Code:
do k = k0+1,kN
Is there any hidden reason that makes the second approach superior? Also, wouldn't k0+1 run a risk of being needlessly evaluated in every iteration of the do loop?

Your comments would be quite welcomed.

Gerry.
Gerry Kan is offline   Reply With Quote

Old   August 17, 2021, 13:51
Default
  #2
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,849
Rep Power: 73
FMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura about



that is strange ...
Gerry Kan likes this.
FMDenaro is offline   Reply With Quote

Old   August 17, 2021, 14:01
Default
  #3
Super Moderator
 
flotus1's Avatar
 
Alex
Join Date: Jun 2012
Location: Germany
Posts: 3,427
Rep Power: 49
flotus1 has a spectacular aura aboutflotus1 has a spectacular aura about
Are you sure your example retains all of the original context if this issue?
Did "that guy" give any reason for his recommendation?
Gerry Kan likes this.
flotus1 is offline   Reply With Quote

Old   August 17, 2021, 14:13
Default
  #4
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11
Gerry Kan is on a distinguished road
Howdy:

This is also very strange to me. "This guy" is the code reviewer, so my first reaction was, WTF. All he said was something along the line of do this or I don't get his blessings.

Having said that the example is the absolute minimum that I can contain within a single block. To be more specific, k0 and kN are global variables already defined in another module. The variable k1 (and counter k) are locally declared.

I don't know if this would affect your perception for this. If k0 were declared as a parameter then the compiler would be smart enough to calculate k0+1 before the do loop. However k0 is not a parameter so what the compiler would do is kind of up in the air. Calculating k0+1 beforehand should make this behavior more clear, I would think.

Whadayathink, Gerry.
Gerry Kan is offline   Reply With Quote

Old   August 17, 2021, 15:49
Default
  #5
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,190
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
I can see few reasons why, but honestly none of them is reasonably linked to performances, given the piece of code we have seen.

First of all, as there is an official reviewer for the code, I also expect code styles to be in place (despite the horrible decision of having a global variable... and naming it k0!!!!), so you might be simply using k0 in a way that doesn't conform to the rest of the code.

A second reason might be linked to be able to catch meaningful pieces of code when searching for k0. Say, a grep from command line, would return, in your case, an obscure line and you would have no idea what's going on without looking at the whole source code.

Finally, it may sound silly, but you are actually using one more line of code than is actually needed. Loose control on this and you end up with thousands more lines of code in a project.

However, if there is a performance issue (which we can't spot from here), then I bet my soul that this is not the actual solution to the actual problem
Gerry Kan likes this.
sbaffini is offline   Reply With Quote

Old   August 17, 2021, 18:19
Default
  #6
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11
Gerry Kan is on a distinguished road
Thanks Paolo.

Of course, these global variables wouldn't be called k0 and kN in the actual code. I renamed them here for illustration, and also to protect my identity from the few of those who might know the code.

And if we have to code accordingly to its greppability we will end up something that look quite ugly in no time.

However I might have to checck their punch card era coding standard to see if it has to be done like that. Last time I checked there was no such entry but you'll never know.

Quote:
However, if there is a performance issue (which we can't spot from here), then I bet my soul that this is not the actual solution to the actual problem
I don't quite know what you meant by that. Do you mean I shouldn't have defined k1 to pre-calulate k0+1 in the first place?

Gerry.
Gerry Kan is offline   Reply With Quote

Old   August 17, 2021, 18:59
Default
  #7
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,190
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
Quote:
Originally Posted by Gerry Kan View Post
Thanks Paolo.

Of course, these global variables wouldn't be called k0 and kN in the actual code. I renamed them here for illustration, and also to protect my identity from the few of those who might know the code.

And if we have to code accordingly to its greppability we will end up something that look quite ugly in no time.

However I might have to checck their punch card era coding standard to see if it has to be done like that. Last time I checked there was no such entry but you'll never know.



I don't quite know what you meant by that. Do you mean I shouldn't have defined k1 to pre-calulate k0+1 in the first place?

Gerry.
No, I simply meant that if, by any chance, your code was corrected for performance reasons, it is a mistake on their side. Even if, literally, your code causes indeed troubles, it is a larger issue to be looked elsewhere, certainly not a single line such this one.
Gerry Kan likes this.
sbaffini is offline   Reply With Quote

Old   August 18, 2021, 01:32
Default
  #8
Senior Member
 
piu58's Avatar
 
Uwe Pilz
Join Date: Feb 2017
Location: Leipzig, Germany
Posts: 744
Rep Power: 15
piu58 is on a distinguished road
I k1 is not needed elsewhere it is useless to have a variable only for the purpose of the loop. I find the second version more clear and better readable. I don't believe that is faster or less memory consuming.
__________________
Uwe Pilz
--
Die der Hauptbewegung überlagerte Schwankungsbewegung ist in ihren Einzelheiten so hoffnungslos kompliziert, daß ihre theoretische Berechnung aussichtslos erscheint. (Hermann Schlichting, 1950)
piu58 is offline   Reply With Quote

Old   August 18, 2021, 02:02
Default
  #9
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11
Gerry Kan is on a distinguished road
So it basically comes down to that he is used to writing do loops this way, and I the other way. Correct? Again I need to check the standards to see whether it is codified.

Gerry.
Gerry Kan is offline   Reply With Quote

Old   August 18, 2021, 07:42
Default
  #10
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11
Gerry Kan is on a distinguished road
Quote:
Originally Posted by sbaffini View Post
First of all, as there is an official reviewer for the code, I also expect code styles to be in place.
Okay, I checked their coding standards, and there was no mention of needing to use k0+1 in the do loop.

Yay!
Gerry Kan is offline   Reply With Quote

Old   August 18, 2021, 07:58
Default
  #11
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,190
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
Quote:
Originally Posted by Gerry Kan View Post
Okay, I checked their coding standards, and there was no mention of needing to use k0+1 in the do loop.

Yay!
I was more like referencing to the fact that, maybe, there are several such loops in the code, and they are all written the same way. Yet, I would expect to be explicitly told so if there isn't an official document mentioning it.

So, if it isn't so, I honestly have no idea why it might be so important at all.
Gerry Kan likes this.
sbaffini is offline   Reply With Quote

Old   August 18, 2021, 09:19
Default
  #12
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11
Gerry Kan is on a distinguished road
Quote:
Originally Posted by sbaffini View Post
So, if it isn't so, I honestly have no idea why it might be so important at all.
Paolo:

I got an inkling that he just wanted to show me how much he disliked my approach because he disagreed with it from the get go. He left five full pages of comments and cc'd his boss, who has the last word, even stressing that the whole thing be rewritten to his approach. But of all his criticisms, this one was ironically the most difficult to defend.

Gerry.
sbaffini likes this.
Gerry Kan is offline   Reply With Quote

Old   August 24, 2021, 02:27
Default
  #13
Senior Member
 
Simbelmynė's Avatar
 
Join Date: May 2012
Posts: 551
Rep Power: 16
Simbelmynė is on a distinguished road
From the number of replies here (with guesses) it seems that the only way you might get "closure" in this case is to actually ask the code reviewer about this. It is either this or just perform the changes and leave it.


By the way, if you have several loops beginning with k1 then I think it is more clean to write it like you did. Similar to using kN. If it is only one loop then I guess kN would be bad to use as well if this was some "save space" argument so this may not be the reason.
Gerry Kan likes this.
Simbelmynė is offline   Reply With Quote

Old   August 24, 2021, 03:42
Default
  #14
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11
Gerry Kan is on a distinguished road
Quote:
Originally Posted by Simbelmynė View Post
By the way, if you have several loops beginning with k1 then I think it is more clean to write it like you did. Similar to using kN. If it is only one loop then I guess kN would be bad to use as well if this was some "save space" argument so this may not be the reason.
I was taught you should never introduce expressions of any kind in the loop bound as you don't know whether the limits will be calculated beforehand or are evaluated at each iteration. In this case the impact is not so heavy, since you are only adding 1, and the bounds (supposedly) don't change throughout the loop. But to me this is still a bad habit.

If one wants to work with floating bounds, a while loop or internal break mechanisms would be more effective.

Gerry.
Gerry Kan is offline   Reply With Quote

Old   August 24, 2021, 04:12
Default
  #15
Senior Member
 
Simbelmynė's Avatar
 
Join Date: May 2012
Posts: 551
Rep Power: 16
Simbelmynė is on a distinguished road
I do not have sufficient coding, or rather compiler, experience to say anything about this. I have a hard time believing that this has any measurable impact on performance. It should be fairly easy to benchmark though.



I was just pointing out that the reviewer is inconsistent if the reason would have been "to save space". However, this is probably not the reason so I think you can disregard my last point
Gerry Kan likes this.
Simbelmynė 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transfer output data as input for second calculation mannobot Main CFD Forum 3 December 7, 2018 10:37
Determining the calculation sequence of the regions in multe regions calculation peterhess OpenFOAM Running, Solving & CFD 4 March 9, 2016 04:07
Quick Question on Calculation Time Davitt COMSOL 0 January 14, 2016 15:16
Understanding the PISO loop of icoFoam ooo OpenFOAM Programming & Development 0 March 14, 2014 10:28
Defining output as input for second calculation mannobot FLUENT 1 June 2, 2010 05:20


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