concept name in category fortran

appears as: name, names, names, name, The name
Modern Fortran: Building efficient parallel applications

This is an excerpt from Manning's book Modern Fortran: Building efficient parallel applications.

Coarrays are the main data structure for native parallel programming in Fortran. Originally developed by Robert Numrich and John Reid in the 1990s as an extension for the Cray Fortran compiler, coarrays have been introduced into the standard starting with the 2008 release. Coarrays are much like arrays, as the name implies, except that their elements are distributed along the axis of parallel processes (cores or threads). As such, they provide an intuitive way to copy data between remote processes.

Finally, we get to our derived type, ipaddr. The opening line of the type definition has a new attribute, bind(). This attribute states that this derived type is meant to bind to an existing C struct, whose name is specified as a keyword argument, name. If this argument is omitted, the compiler will use the name of the derived type (in all lowercase) as the name of the matching C struct. Like its C counterpart, this derived type has only one component, an array of 32 characters. This is where we use the c_char as a type kind parameter to declare the component in the Fortran implementation.

Recall that the original C struct has a component named _ (underscore). Ideally we’d match C variable names exactly in our Fortran interface, for clarity. Although Fortran allows underscores in variable, procedure, and module names, it doesn’t allow using one as the first or the only character in a name. To work around this restriction, we can name our component something meaningful. I chose address, as this component will internally store the address and port components of an IP address.

Listing 11.2. Defining a Fortran interface to the ipaddr_local C function
language="fortran" linenumbering="unnumbered">module mod_dill

  use iso_c_binding, only: c_char, c_int #1
  implicit none

  private
  public :: ipaddr, ipaddr_local, IPADDR_IPV4 #2

  integer(c_int), parameter :: IPADDR_IPV4 = 1 #3
  ...
  interface #4

    integer(c_int)                                  & #5
      function ipaddr_local(addr, name, port, mode) & #5
      bind(c, name='dill_ipaddr_local') #6
      import :: c_char, c_int, ipaddr #7
      type(ipaddr), intent(out) :: addr #8
      character(c_char), intent(in) :: name(*) #9
      integer(c_int), value, intent(in) :: port #10
      integer(c_int), value, intent(in) :: mode #10    end function ipaddr_local

  end interface

end module mod_dill

There’s a lot to unpack here, so I’ll go slowly. First, we use the function statement to define the function name and its arguments. This works the same way as with all Fortran functions we worked with before. One key difference is that here we add the bind attribute, like we did when defining the ipaddr derived type. Here, we also specify the name keyword argument in the parentheses, which is the name of the C function that we’ll bind our Fortran interface to. Notice that we’re binding to the name dill_ipaddr_local, rather than just ipaddr_local. This is because libdill by default adds a prefix dill_ to all its functions to avoid name conflicts with any similar library that may export functions with the same name.