Skip to content
Extraits de code Groupes Projets
slides.md 26,8 ko
Newer Older
  • Learn to ignore specific revisions
  •    implicit none
    
       integer, intent (in) :: i
       logical, intent (in) :: control
    
       integer, save :: j = 0
       integer :: k
    
       j = j + i
       if ( control ) k = 0
       k = k + i
    
       write (*, *) 'i, j, k=', i, j, k
    
    end subroutine asub
    
    end module subs
    
    program test_saves
    
       use subs
       implicit none
    
       call asub ( 3, .TRUE. )
       call asub ( 4, .FALSE. )
    
    end program test_saves
    ```
    
    Local variable k of the subroutine is intentionally misused -- in this program it is initialized in the first call since control is TRUE, but on the second call control is FALSE, so k is not redefined. But without the save attribute k is undefined, so the using its value is illegal.
    
    ```fortran
     i, j, k=           3           3           3
     i, j, k=           4           7           7
    ```
    
    Compiling the program with ifort and aggressive optimization options, k lost its value:
    
    ```fortran
     i, j, k=           3           3           3
     i, j, k=           4           7           4
    ```
    -->
    
    ---
    
    # `internal` subprogams
    
    ```fortran
    program main
      implicit none
      integer N
      real X(20)
      ...
      write(*,*), 'Processing x...', process()
      ...
    contains
      logical function process()
        ! in this function N and X can be accessed directly (scope of main)
        ! Please not that this method is not recommended:
        ! it would be better to pass X as an argument of process
        implicit none
        if (sum(x) > 5.) then
           process = .FALSE.
        else
           process = .TRUE.
        endif
      end function process
    end program
    ```
    
    <!-- _footer: "" -->
    
    ---
    
    # `external` subprogams
    
    - `external` subprogams are defined in a separate program unit
    - to use them in another program unit, refer with the `EXTERNAL` statement
    - compiled separately and linked
    
    **!!! DO NOT USE THEM**: modules are much easier and more robust :exclamation:
    
    They are only needed when subprogams are written with different programming language or when using external libraries (such as BLAS)
    
    > It's **highly** recommended to construct `INTERFACE` blocks for any external subprogams used
    
    ---
    
    # `interface` statement
    
    ```fortran
    SUBROUTINE nag_rand(table)
      INTERFACE 
        SUBROUTINE g05faf(a,b,n,x)
          REAL, INTENT(IN)    :: a, b
          INTEGER, INTENT(IN) :: n
          REAL, INTENT(OUT)   :: x(n)
        END SUBROUTINE g05faf
      END INTERFACE
      !
      REAL, DIMENSION(:), INTENT(OUT) :: table
      !
      call g05faf(-1.0,-1.0, SIZE(table), table)
    END SUBROUTINE nag_rand
    ```
    
    <!-- _footer: "" -->
    
    ---
    
    # Fortran Compiler and libraries
    
    Examples:
    
    ```bash
    module load netCDF-Fortran/4.5.3-gompi-2021b
    gfortran -ffree-line-length-none \
    -o OceanGrideChange.exe 07_OceanGrideChange.f90 \
    -I${EBROOTNETCDFMINFORTRAN}/include -L${EBROOTNETCDFMINFORTRAN}/lib -lnetcdff
    ```
    
    ```bash
    module load netCDF-Fortran/4.5.3-iimpi-2021b
    ifort -O3 \
    -o OceanGrideChange.exe 07_OceanGrideChange.f90 \
    -I${EBROOTNETCDFMINFORTRAN}/include -L${EBROOTNETCDFMINFORTRAN}/lib -lnetcdff
    ```
    
    
    > Fortran 90 source code [09_OceanGrideChange.f90](https://forge.uclouvain.be/barriat/learning-fortran/-/blob/master/src/09_OceanGrideChange.f90) with the input file [09_input.nc](https://forge.uclouvain.be/barriat/learning-fortran/-/blob/master/src/09_input.nc)
    
    
    ---
    
    # Conclusions
    
    - Fortran in all its standard versions and vendor-specific dialects is a rich but confusing language
    - Fortran is a modern language that continues to evolve
    
    - Fortran is still ideally suited for numerical computations in engineering and science
      - most new language features have been added since F95
      - "High Performance Fortran" includes capabilities designed for parallel processing