How to use command line arguments in Fortran?

If you want to get the arguments fed to your program on the command line, use the (since Fortran 2003) standard intrinsic subroutine GET_COMMAND_ARGUMENT. Something like this might work PROGRAM MAIN REAL(8) :: A,B integer :: num_args, ix character(len=12), dimension(:), allocatable :: args num_args = command_argument_count() allocate(args(num_args)) ! I’ve omitted checking the return status of … Read more

Where to put `implicit none` in Fortran

The implicit statement (including implicit none) applies to a scoping unit. Such a thing is defined as BLOCK construct, derived-type definition, interface body, program unit, or subprogram, excluding all nested scoping units in it This “excluding all nested scoping units in it” suggests that it may be necessary/desirable to have implicit none in each function … Read more

Fortran OpenMP program shows no speedup of CPU_TIME()

cpu_time() takes the time on the CPU, not the walltime. In parallel applications these are not the same. See here for details. Using system_clock() solves this problem: PROGRAM MAIN use omp_lib implicit none REAL*8 Times1,Times2 INTEGER I,J, iTimes1,iTimes2, rate real, allocatable, dimension(:) :: a allocate(a(1000)) CALL system_clock(count_rate=rate) DO J = 1, 1000 a(j)=j ENDDO ! … Read more

Template in Fortran?

Fortran does not have templates, but you can put the code common to the functions handling different types in an include file as a kludge to simulate templates, as shown in this code: ! file “cumul.inc” ! function cumul(xx) result(yy) ! return in yy(:) the cumulative sum of xx(:) ! type, intent(in) :: xx(:) ! … Read more

Sending 2D arrays in Fortran with MPI_Gather

The following a literal Fortran translation of this answer. I had thought this was unnecessary, but the multiple differences in array indexing and memory layout might mean that it is worth doing a Fortran version. Let me start by saying that you generally don’t really want to do this – scatter and gather huge chunks … Read more

Why should I use interfaces?

As Alexander Vogt says, interface blocks can be useful for providing generic identifiers and allowing certain compiler checks. If you’re using interface blocks to create generic identifiers you’re probably doing so within modules, so the main reason to write such blocks when modules aren’t about is for the explicit interfaces that they create in the … Read more