Author Message
viroxa

Joined: 28 Jul 2017
Posts: 78 Posted: Thu Sep 14, 2017 1:35 pm    Post subject: Named Constructs - why is the layout as it is? Hello, everyone,

as I'm diving deeper into Fortran, learning more each day (thanks to all of you!), my code becomes more complex.

http://forums.silverfrost.com/viewtopic.php?t=3586&postdays=0&postorder=asc&start=0

was quite interesting to follow, and I've adapted some of it that was new to me, which I liked more than what I was doing before.

Basically, my style is what John-Silver gave as second example in the linked thread:

 Code: ! Fortran Style Guide (max(?) 4 spaces version) (10)       some code here       some more code here       do j = 1,ncb           c(:,j) = 0           do k = 1,nca               c(1:nra,j) = c(1:nra,j) + a(1:nra,k) * b(k,j)           end do ! k       end do ! j       almost last code here       last code here

But instead of using the DO_variable as comments like it is done here, I wanted to use the possibility of naming DOs, IFs, etc. like so:

 Code: some code here       some more code here       loopj:do j = 1,ncb           c(:,j) = 0           loopk:do k = 1,nca               c(1:nra,j) = c(1:nra,j) + a(1:nra,k) * b(k,j)           loopk:end do       loopj:end do       almost last code here       last code here

BUT it only works like so:
 Code: some code here       some more code here       loopj:do j = 1,ncb           c(:,j) = 0           loopk:do k = 1,nca               c(1:nra,j) = c(1:nra,j) + a(1:nra,k) * b(k,j)           end do loopk       end do  loopj       almost last code here       last code here

Which, in my opinion is
1. extremely unsatisfying in terms of symmetry
2. makes it harder to grasp the structure than without using any naming (at least for me it does).

Now I'm curious: Is there a reason, why the name is at the beginning when starting, but has to be at the end when finishing?   mecej4

Joined: 31 Oct 2006
Posts: 1202 Posted: Thu Sep 14, 2017 3:25 pm    Post subject: Construct names are used for more purposes than the single one in your example code. DO constructs can contain CYCLE and EXIT statements followed by the DO construct name. DO statements can be nested, and it is with such nesting that the construct name following CYCLE and EXIT becomes more useful. In such cases, your label: prefix would appear in more places than the starting and final statements of the construct. It is not easy (or even possible, in some instances) to find explanations for why certain aspects of Fortran are the way they are. Usually, the explanation is that "Section x.y.z of the Fortran Standard NN says so".   JohnCampbell

Joined: 16 Feb 2006
Posts: 2144
Location: Sydney Posted: Fri Sep 15, 2017 1:30 am    Post subject: You could consider an alternative style for placing the names (there are no style rules)
 Code: some code here        some more code here loopj: do j = 1,ncb            c(:,j) = 0 loopk:     do k = 1,nca                c(1:nra,j) = c(1:nra,j) + a(1:nra,k) * b(k,j)            end do loopk        end do  loopj        almost last code here        last code here

I don't like the Fortran name rules, as when you have multiple DO j loops in the same routine, you need to be inventive with selection of names. I would prefer to just use j as a name. The name has no meaning outside the DO loop.   mecej4

Joined: 31 Oct 2006
Posts: 1202 Posted: Fri Sep 15, 2017 1:50 am    Post subject: If you have a compiler that supports F2008, you can use the BLOCK construct. You can reuse labels as long as labels within each block are distinct.

For example:
 Code: program xyz integer :: i,j,x(8) ! blk_A: block    loop_i:   do i=1,5       x(i)=i       loop_j:      do j=1,5          x(i)=x(i)+j       enddo loop_j    enddo loop_i end block blk_A blk_B: block    loop_j:   do j=1,5       x(j)=j       loop_i:      do i=1,5          x(j)=x(j)+i       enddo loop_i    enddo loop_j end block blk_B end   viroxa

Joined: 28 Jul 2017
Posts: 78 Posted: Fri Sep 15, 2017 9:20 am    Post subject: @mecej4: Thanks for pointing out the additional purposes of construct names. @johncampbell: Thanks for this suggestion. I'll stick to that, because (for me) it's easier to read than what I have now.   DanRRight Joined: 10 Mar 2008
Posts: 2108
Location: South Pole, Antarctica Posted: Fri Sep 15, 2017 8:18 pm    Post subject: Currently for DOs and IFs i do this way (for larger size blocks, no exclusions, for failing - punishment)

 Code: some code here      some more code here        do j = 1,ncb          c(:,j) = 0          do k = 1,nca            c(1:nra,j) = c(1:nra,j) + a(1:nra,k) * b(k,j)          end do ! do k = 1,nca        end do ! do j = 1,ncb      almost last code here      last code here

But combining that with John's suggestion that probably would be the best

 Code: some code here      some more code here j:     do j = 1,ncb          c(:,j) = 0 k:       do k = 1,nca            c(1:nra,j) = c(1:nra,j) + a(1:nra,k) * b(k,j) k::      end do ! do k = 1,nca j::    end do ! do j = 1,ncb      almost last code here      last code here

And i encourage all you to support my suggestion to implement in SDBG to show the starting DO or IF statement in popping up bubble help when you place your mouse on ENDDO, ELSE or END IF same way like this bubble help shows values of regular variables. There are a lot of legacy code written without these tricks and beautifications which is literally incomprehensible   DanRRight Joined: 10 Mar 2008
Posts: 2108
Location: South Pole, Antarctica Posted: Fri Sep 22, 2017 12:34 am    Post subject: Forgot one more good trick about naming DO and IFs.

In the case of multiple DOs and IFs I found very helpful do capital-lettering them. The first marked as DO/endDO, second Do/endDo, third dO/enddO, fourth do/enddo. Same with IF

 Code: DO j = 1,ncb           c(:,j) = 0           Do k = 1,nca             dO m = 1,nca               do n = 1,nca                 c(1:nra,j) = c(1:nra,j) + a(1:nra,k) * b(n,m,k,j)               enddo             enddO           endDo         endDO

Noticed I do not use letter l for indexing? Same for files. Easy to mix it with number 1 or capital I. Capital L is ok.

Again please support the proposal of implementing of showing in SDBG matching DO and IF when you hover on ENDDO, ELSE or ENDIF. The structure of DO and IFs can be very tricky after 10, 20, 30 years of using and reusing your own codes and that little improvement will save you few last hairs then.   viroxa

Joined: 28 Jul 2017
Posts: 78 Posted: Fri Sep 22, 2017 10:28 am    Post subject: Thanks for your input. It's interesting to see the different styles used by everyone. Personally, I find your last suggestion of using capital letters very confusing. Sometimes it would indeed be helpful, if DOs, IFs, and SELECTs behaved like () so that matching pairs would be highlighted.   DanRRight Joined: 10 Mar 2008
Posts: 2108
Location: South Pole, Antarctica Posted: Fri Sep 22, 2017 7:57 pm    Post subject: Confusing? That what is very confusing. In such cases you will beg gods for any hint to find what is what
 Code: DO j = 1,NCA SOME CODE HERE C(1:M,J) = 0 SONE CODE HERE DO K = 1,NCA SOME CODE HERE DO M= 1,NCB SOME CODE HERE DO N= 1,NCC C(1:NCA,J) = C(1:NRA,J) + A(1:NRA,K) * B(N,M,K,J) SOME CODE HERE ENDDO SOME CODE HERE ENDDO SOME CODE HERE ENDDO SOME CODE HERE ENDDO   John-Silver Joined: 30 Jul 2013
Posts: 1227
Location: Aerospace Valley Posted: Fri Sep 22, 2017 10:25 pm    Post subject: swings or roundabouts ? , that 'tis the question    Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 Hour Page 1 of 1