• When should you use an internal procedure?

    From Beliavsky@21:1/5 to All on Mon Feb 28 20:38:18 2022
    I will tweet https://twitter.com/fortrantip about the block construct, a neat feature of Fortran 2008. A similar feature to discuss is the internal procedure. I don't use internal procedures often, since with host association it is not clear what the
    inputs and outputs are, and since they don't foster modularity when they cannot be called except from the enclosing unit.

    What are the benefits of internal procedures? What are some common use cases?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Van Buskirk@21:1/5 to All on Mon Feb 28 22:05:47 2022
    "Beliavsky" wrote in message news:5c3eb8e9-e3b6-41d8-81e2-b353296381f7n@googlegroups.com...

    I will tweet https://twitter.com/fortrantip about the block construct, a
    neat
    feature of Fortran 2008. A similar feature to discuss is the internal procedure.
    I don't use internal procedures often, since with host association it is
    not
    clear what the inputs and outputs are, and since they don't foster
    modularity when they cannot be called except from the enclosing unit.

    What are the benefits of internal procedures? What are some common use
    cases?

    A common use case for me is to perform integration of a parameterized integrand. The enclosing subprogram sets the parameters in its local
    variables and passes a procedure pointer to the internal function as an
    actual argument to the integration procedure. When that procedure
    invokes the internal procedure it gets the values of the parameters via
    host association. This works in parallel code as well, provided the local variables don’t have the SAVE attribute.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Beliavsky@21:1/5 to James Van Buskirk on Mon Feb 28 21:18:37 2022
    On Tuesday, March 1, 2022 at 12:06:14 AM UTC-5, James Van Buskirk wrote:
    "Beliavsky" wrote in message

    What are the benefits of internal procedures? What are some common use cases?
    A common use case for me is to perform integration of a parameterized integrand. The enclosing subprogram sets the parameters in its local variables and passes a procedure pointer to the internal function as an actual argument to the integration procedure. When that procedure
    invokes the internal procedure it gets the values of the parameters via
    host association. This works in parallel code as well, provided the local variables don’t have the SAVE attribute.

    Thanks. It will be fun to learn about procedure pointers and demonstrate
    what you wrote :).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Koenig@21:1/5 to James Van Buskirk on Tue Mar 1 06:45:30 2022
    James Van Buskirk <not_valid@comcast.net> schrieb:
    "Beliavsky" wrote in message news:5c3eb8e9-e3b6-41d8-81e2-b353296381f7n@googlegroups.com...

    I will tweet https://twitter.com/fortrantip about the block construct, a
    neat
    feature of Fortran 2008. A similar feature to discuss is the internal
    procedure.
    I don't use internal procedures often, since with host association it is
    not
    clear what the inputs and outputs are, and since they don't foster
    modularity when they cannot be called except from the enclosing unit.

    What are the benefits of internal procedures? What are some common use
    cases?

    A common use case for me is to perform integration of a parameterized integrand. The enclosing subprogram sets the parameters in its local variables and passes a procedure pointer to the internal function as an actual argument to the integration procedure. When that procedure
    invokes the internal procedure it gets the values of the parameters via
    host association. This works in parallel code as well, provided the local variables don’t have the SAVE attribute.

    One problem is that this does not work from a normal module procedure.

    For passing along info to integration routines, zero finders and so
    on, my usual style is something like

    module x
    implicit none
    type zero_type
    real :: a, b, c
    end type zero_type
    contains
    recursive function to_zero(x,parm) result(r)
    real, intent(in) :: x
    class(*), intent(in) :: parm
    real :: r
    select type(parm)
    type is(zero_type)
    r = parm%a + x*(parm%b+x*parm%c)
    class default
    error stop
    end select
    end function to_zero
    end module x

    If I'm worried about inefficiencies, LTO to the rescue :-)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gah4@21:1/5 to Beliavsky on Mon Feb 28 23:24:32 2022
    On Monday, February 28, 2022 at 8:38:20 PM UTC-8, Beliavsky wrote:

    (snip)

    What are the benefits of internal procedures? What are some common use cases?

    Sometimes it is nice to have them near, even while not using or needing
    host association. That is especially true when they are small, and forcing someone to search through a large project to find one isn't needed.

    It isn't so obvious that they are more or less efficient than external procedures.

    But yes, host association is nice sometimes.

    Using internal procedures for actual arguments is a relatively new addition
    to the standard, as internal procedures have been there much longer.

    Before internal procedures, COMMON was used to get parameters into
    routines passed as actual arguments. Well, until they were allowed as
    actual arguments.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Van Buskirk@21:1/5 to Thomas Koenig on Tue Mar 1 09:02:09 2022
    "Thomas Koenig" wrote in message news:svkfea$p06$1@newsreader4.netcologne.de...

    James Van Buskirk <not_valid@comcast.net> schrieb:

    A common use case for me is to perform integration of a parameterized integrand. The enclosing subprogram sets the parameters in its local variables and passes a procedure pointer to the internal function as an actual argument to the integration procedure. When that procedure
    invokes the internal procedure it gets the values of the parameters via host association. This works in parallel code as well, provided the
    local
    variables don’t have the SAVE attribute.

    One problem is that this does not work from a normal module procedure.

    Unless you are allowed the use of thread local storage...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Koenig@21:1/5 to James Van Buskirk on Tue Mar 1 17:27:12 2022
    James Van Buskirk <not_valid@comcast.net> schrieb:
    "Thomas Koenig" wrote in message news:svkfea$p06$1@newsreader4.netcologne.de...

    James Van Buskirk <not_valid@comcast.net> schrieb:

    A common use case for me is to perform integration of a parameterized
    integrand. The enclosing subprogram sets the parameters in its local
    variables and passes a procedure pointer to the internal function as an
    actual argument to the integration procedure. When that procedure
    invokes the internal procedure it gets the values of the parameters via
    host association. This works in parallel code as well, provided the
    local
    variables don’t have the SAVE attribute.

    One problem is that this does not work from a normal module procedure.

    Unless you are allowed the use of thread local storage...

    I'm not quite sure what use case you have in mind. TLS implies
    multithreading, which standard Fortran does not have. Fortran
    has either coarrays or, as an extension, OpenMP or OpenACC.

    For the use case of recursively calling an integration routine
    along several dimensions, for example, using TLS will not help.

    What do you have in mind?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)