Segmentation error when passing functions as arguments

Hi,

I’m having problems with pgf90 when running a code that often passes functions as arguments. The code compiles correctly but has a run-time error (sample code attached below). The same code works correctly when compiled with ifort.

module mod_util
 implicit none
 contains

   function do_something(n,func)
     implicit none
     integer,intent(in) :: n
     real               :: do_something(n+1)
     interface
       function func(x)
         implicit none
         real,intent(in) :: x(:)
         real            :: func(size(x))
       end function func
     end interface
     real                :: t(n+1)
     
     t = 1.0/(n+1)
     do_something = func(cos(t))/(n+1)
   end function do_something   
   
 end module mod_util

module module1
 implicit none
 contains
 
  function get_Am(Rn,N) result(Am_in)
    use mod_util,      only : do_something
    real,intent(in)          :: Rn(:)
    integer,intent(in)       :: N
    real                     :: Am_in(0:N)
    Am_in = 0.0
    Am_in = do_something(N,foo)
    
   contains
    
    function foo(x)
      real,intent(in)   :: x(:)
      real              :: foo(size(x))
      foo = 0.5*x*sum(Rn)
    end function foo 
         
  end function  get_Am  

end module module1


program test
  use module1,    only : get_Am
  implicit none
  integer,parameter     :: N=5,NRn=2
  real                  :: Rn(0:NRn),Am(0:N)
  
  Rn = 0.1
  Am = get_Am(Rn,N)
  print *,' Am = ',Am

end program test

The problem appears to be that Rn isn’t defined in foo, for reasons unknown to me. It seems to me that it is written according to the standard. If the sum(Rn) is commented out from foo, the program works. Any help on this matter would be greatly appreciated.

Thanks,
grgur
[/code]

Hi grgur,

In F2003, Internal procedures may only be called by their host. Hence it’s illegal to pass “foo” to “do_something”.

I came across the following post (http://software.intel.com/en-us/blogs/2009/09/02/doctor-fortran-in-think-thank-thunk/) where it appears that Intel added an extension which allow it.

Hope this helps,
Mat

Hi Mat,

Thanks a lot for your hint. I wasn’t aware that pgf90 would compile under F2003 standard. Was passing internal functions allowed under F95? If so, can some flags be specified to ensure f95 being used?

Thanks,
Grgur

Was passing internal functions allowed under F95?

Nope, the same restriction is in F95. I do believe that the restriction will be lifted in the next Fortran standard though.

  • Mat

Thanks Mat,

I better rewrite the code then. Thanks for all the info.

Grgur