Procedure for any type of array in Fortran

前端 未结 2 1959
孤街浪徒
孤街浪徒 2021-01-22 05:59

Basis: I want to write a procedure which make some operation for any input array type: integer, real(4), real(8), etc. The only idea i read on Stac

相关标签:
2条回答
  • 2021-01-22 06:15

    This is the same situation as with the MPI procedures. The key is (at least in Fortran 2008 and older) to use implicit interface to the routine. In that case no argument checking takes place and you can pass whatever you want. You have the implicit interface if the procedure is external.This is the same situation as with the MPI procedures. The key is (at least in Fortran 2008 and older) to use implicit interface to the routine. In that case no argument checking takes place and you can pass whatever you want. You have the implicit interface if the procedure is external.

    Example:

         subroutine store(A,count,sizeof)
             use iso_fortran_env, only: int8
             integer, intent(in) :: count, sizeof
             integer(int8) :: A(count*sizeof)
             integer :: u
    
             !here you can treat A just as an array of count*sizeof bytes
           open(newunit=u,file=out.bin,access=stream)
            write(u)  A
            close(u)
    
         end subroutine
    
         program p
           real :: A(5,6)
           call store(A, size(A), storage_size(A(1,1))/8)
    
         end program
    

    When you need the explicit interface, you could (untested) do something like (not supported by many compilers, beyond Fortran 2008):

        subroutine typebound(self, A, count, sizeof)
          class(my_class) :: self
          TYPE(*),DIMENSION(..) :: A
          integer, intent(in) :: count, sizeof
    
          !subroutine
          call store(A, count, sizeof)
    

    or use the directives which a few compilers support:

        !GCC$ attributes no_arg_check::A
        !DEC$ attributes no_arg_check::A
    

    this disables the check of the arguments.

    0 讨论(0)
  • 2021-01-22 06:32

    To complement the last part of Vladimir's answer: The mpi_f08 module defines most routines with the following unportable part, which seems to suit most compilers (but most likely, not all):

    !DEC$ ATTRIBUTES NO_ARG_CHECK :: sendbuf, recvbuf
    !GCC$ ATTRIBUTES NO_ARG_CHECK :: sendbuf, recvbuf
    !$PRAGMA IGNORE_TKR sendbuf, recvbuf
    !DIR$ IGNORE_TKR sendbuf, recvbuf
    !IBM* IGNORE_TKR sendbuf, recvbuf
    

    (Adjust the variable names according to your needs.)

    0 讨论(0)
提交回复
热议问题