|
[Sponsors] |
August 17, 2021, 13:35 |
[Fortran] DO loop bounds calculation
|
#1 |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11 |
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 Code:
do k = k0+1,kN Your comments would be quite welcomed. Gerry. |
|
August 17, 2021, 13:51 |
|
#2 |
Senior Member
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,849
Rep Power: 73 |
that is strange ... |
|
August 17, 2021, 14:01 |
|
#3 |
Super Moderator
Alex
Join Date: Jun 2012
Location: Germany
Posts: 3,427
Rep Power: 49 |
Are you sure your example retains all of the original context if this issue?
Did "that guy" give any reason for his recommendation? |
|
August 17, 2021, 14:13 |
|
#4 |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11 |
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. |
|
August 17, 2021, 15:49 |
|
#5 |
Senior Member
|
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 |
|
August 17, 2021, 18:19 |
|
#6 | |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11 |
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:
Gerry. |
||
August 17, 2021, 18:59 |
|
#7 | |
Senior Member
|
Quote:
|
||
August 18, 2021, 01:32 |
|
#8 |
Senior Member
Uwe Pilz
Join Date: Feb 2017
Location: Leipzig, Germany
Posts: 744
Rep Power: 15 |
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) |
|
August 18, 2021, 02:02 |
|
#9 |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11 |
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. |
|
August 18, 2021, 07:42 |
|
#10 |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11 |
||
August 18, 2021, 07:58 |
|
#11 | |
Senior Member
|
Quote:
So, if it isn't so, I honestly have no idea why it might be so important at all. |
||
August 18, 2021, 09:19 |
|
#12 | |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11 |
Quote:
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. |
||
August 24, 2021, 02:27 |
|
#13 |
Senior Member
Join Date: May 2012
Posts: 551
Rep Power: 16 |
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. |
|
August 24, 2021, 03:42 |
|
#14 | |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 362
Rep Power: 11 |
Quote:
If one wants to work with floating bounds, a while loop or internal break mechanisms would be more effective. Gerry. |
||
August 24, 2021, 04:12 |
|
#15 |
Senior Member
Join Date: May 2012
Posts: 551
Rep Power: 16 |
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 |
|
|
|
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 |