Menu

#11 Improper indentation of OpenMP conditional compilation specification lines

1.0
closed
nobody
None
2022-01-28
2022-01-14
No

According to the OpenMP API specifications [v5.2, §3.3.2 Free Source Form Conditional Compilation Sentinel] !$ is the conditional compilation sentinel in free form source files and can be used as:

!$ iam = omp_get_thread_num()

But the current version of findent (4.1.3) does not indent these lines correctly.

Example:

[input]

if (condition)
!$omp parallel
!$ num_threads = omp_get_num_threads()
!$omp end parallel
end if

[current behaviour]

if (condition)
 !$omp parallel
!$ num_threads = omp_get_num_threads()
 !$omp end parallel
end if

[desired output]

if (condition)
 !$omp parallel
 !$ num_threads = omp_get_num_threads()
 !$omp end parallel
end if

Discussion

  • Willem Vermin

    Willem Vermin - 2022-01-15

    You forgot the THEN after the first line, but adding that does not change the output.

    In the following I run findent with no flags.

    When I run your example, (with added THEN), I get this:

    if (condition) then
    !$omp parallel
    !$ num_threads = omp_get_num_threads()
    !$omp end parallel
    end if
    

    The 'num_threads line was already correctly indented, so we see no change there. I get no leading space in lines 2 and 4. Strange.

    Findent indents after the !$ sentinel, see the following example:

    program p
    if(.true.) then
    !$ if(.true.) then
    !$ print *,'OMP'
    !$ end if
    end if
    end program
    

    Findent indents this thusly:

    program p
       if(.true.) then
    !$    if(.true.) then
    !$       print *,'OMP'
    !$    end if
       end if
    end program
    

    Does this answer your question?

     
    • Meisam Tabriz

      Meisam Tabriz - 2022-01-15

      Thanks for your quick response. I guess my confusion arises from the fact that the indentation is done after the !$ sentinel but before the !$omp. So my code turns into something like:

          !$omp parallel shared(num_threads)
      !$  num_thread = omp_get_num_threads()
          !$omp end parallel
      

      Is there any way to make this consistent (to have indentation before both)?

       
  • Willem Vermin

    Willem Vermin - 2022-01-15

    Personally, I find the way findent handles !$ lines results in a better readable code than adding indent before !$. And I like to have the !$omp lines starting in column 1: these lines are not indented by findent.

    Your suggestion would entail the following to be done by findent:

    change a line like

      !$    continue
    

    into

    !$ continue

    and then, indent this line as a whole, also indent !$omp lines, whether they start in column 1 or not. This is not a trivial change, practice learned me that there always nasty issues with continuation lines and such. Nevertheless, I will investigate the matter and let you know.

     
  • Willem Vermin

    Willem Vermin - 2022-01-17

    I have a solution for you that does not involve changing findent itself:

    Create a script "myfindent":

    1
    2
    #!/bin/sh
    findent "$@" | sed 's/!\$\([ &]\)\( *\)/\2!$\1/'
    

    Given the following Fortran source:

    program q
    if(.true.) then
    !$ if(.true.) then
    !$ print *,'OMP', &
    !$  'omp'
    !$ end if
    end if
    end program
    

    the following output is produced by "myfindent":

    program q
       if(.true.) then
       !$ if(.true.) then
          !$ print *,'OMP', &
             !$ 'omp'
       !$ end if
       end if
    end program
    

    (one could say that the handling of continuation lines could be better...)
    while findent produces:

    program q
       if(.true.) then
    !$    if(.true.) then
    !$       print *,'OMP', &
    !$          'omp'
    !$    end if
       end if
    end program
    
     
  • Meisam Tabriz

    Meisam Tabriz - 2022-01-28

    Thanks for your helps. I think I should be able to modify it to align the !$ and add space afterwards.

     
  • Willem Vermin

    Willem Vermin - 2022-01-28
    • status: open --> closed
     

Log in to post a comment.