Fortran Templates and Compiler-Driven Design Goals

 
A Primer
 
Fortran Templates
 
J3 Generics Subgroup
 
Template Design Goals
 
Compiler ensures the template is:
Self consistent
Specifies what makes a valid combination of parameters
Work with derived and intrinsic types
Template doesn’t dictate spelling
 
Some Example Syntax
 
REQUIREMENT fizz(T, buzz)
    TYPE, DEFERRED :: T
 
FUNCTION buzz(…)
 
END FUNCTION
END REQUIREMENT
 
TEMPLATE foo(bar, baz, …)
    REQUIRES fizz(bar, baz)
END TEMPLATE
 
INSTANTIATE foo(integer, operator(+), …), ONLY: a, b => c, …
 
A Motivating Example: AXPY
 
subroutine axpy(a, x, y)
  real, intent(in) :: a
  real, intent(in) :: x(:)
  real, intent(inout) :: y(:)
 
  y = a * x + y
end subroutine
 
A Motivating Example: AXPY (cont)
 
template axpy_tmpl(k)
  private
  public :: axpy
  integer, constant :: k
  interface axpy
    procedure axpy_
  end interface
contains
  subroutine axpy_(a, x, y)
    real(k), intent(in) :: a
    real(k), intent(in) :: x(:)
    real(k), intent(inout) :: y(:)
 
    y = a * x + y
  end subroutine
end template
 
integer, parameter :: sp = kind(1.0), dp = kind(1.d0)
instantiate axpy_tmpl(sp)
instantiate axpy_tmpl(dp)
real(sp) :: a, x(10), y(10)
real(dp) :: da, dx(10), dy(10)
call axpy(a, x, y)
call axpy(da, dx, dy)
 
A Motivating Example: AXPY (cont)
 
template axpy_tmpl(T, plus, times)
  private
  public :: axpy
  requires bin_op(T, plus)
  requires bin_op(T, times)
  interface axpy
    procedure axpy_
  end interface
contains
  subroutine axpy_(a, x, y)
    type(T), intent(in) :: a
    type(T), intent(in) :: x(:)
    type(T), intent(inout) :: y(:)
 
    y = plus(times(a,  x), y)
  end subroutine
end template
 
integer, parameter :: sp = kind(1.0), dp = kind(1.d0)
instantiate axpy_tmpl(real(sp), operator(+), operator(*))
instantiate axpy_tmpl(real(dp), operator(+), operator(*))
instantiate axpy_tmpl(integer, operator(+), operator(*))
real(sp) :: a, x(10), y(10)
real(dp) :: da, dx(10), dy(10)
integer :: ia, ix(10), iy(10)
call axpy(a, x, y)
call axpy(da, dx, dy)
call axpy(ia, ix, iy)
 
requirement bin_op(T, op)
  type, deferred :: T
  elemental function op(a, b)
    type(T), intent(in) :: a, b
    type(T) :: op
  end function
end requirement
 
A Motivating Example: AXPY
 
subroutine axpy(a, x, y)
  real, intent(in) :: a
  real, intent(in) :: x(:)
  real, intent(inout) :: y(:)
 
  y = a * x + y
end subroutine
 
A Motivating Example: AXPY (cont)
 
template axpy_tmpl(T, U, V, plus, times)
  private
  public :: axpy
  requires bin_op(T, U, U, times)
  requires bin_op(U, V, V, plus)
  interface axpy
    procedure axpy_
  end interface
contains
  subroutine axpy_(a, x, y)
    type(T), intent(in) :: a
    type(U), intent(in) :: x(:)
    type(V), intent(inout) :: y(:)
 
    y = plus(times(a,  x), y)
  end subroutine
end template
 
integer, parameter :: sp = kind(1.0), dp = kind(1.d0)
instantiate axpy_tmpl( &
    real(sp), integer, real(dp), operator(+), operator(*))
real(sp) :: a
integer :: x(10)
real(dp) :: y(10)
call axpy(a, x, y)
 
requirement bin_op(T, U, V, op)
  type, deferred :: T, U, V
  elemental function op(a, b)
    type(T), intent(in) :: a
    type(U), intent(in) :: b
    type(V) :: op
  end function
end requirement
Slide Note
Embed
Share

Delve into the world of Fortran templates through a primer on generics, showcasing design goals set by the compiler. Explore syntax examples and motivating examples such as the AXPY subroutine, offering insight into the self-consistent nature of templates and their flexibility in parameter combinations.

  • Fortran
  • Templates
  • Compiler
  • Generics
  • Design Goals

Uploaded on Dec 10, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Fortran Templates A Primer J3 Generics Subgroup

  2. Template Design Goals Compiler ensures the template is: Self consistent Specifies what makes a valid combination of parameters Work with derived and intrinsic types Template doesn t dictate spelling

  3. Some Example Syntax REQUIREMENT fizz(T, buzz) TYPE, DEFERRED :: T FUNCTION buzz( ) END FUNCTION END REQUIREMENT TEMPLATE foo(bar, baz, ) REQUIRES fizz(bar, baz) END TEMPLATE INSTANTIATE foo(integer, operator(+), ), ONLY: a, b => c,

  4. A Motivating Example: AXPY subroutine axpy(a, x, y) real, intent(in) :: a real, intent(in) :: x(:) real, intent(inout) :: y(:) y = a * x + y end subroutine

  5. A Motivating Example: AXPY (cont) template axpy_tmpl(k) integer, parameter :: sp = kind(1.0), dp = kind(1.d0) private instantiate axpy_tmpl(sp) public :: axpy instantiate axpy_tmpl(dp) integer, constant :: k real(sp) :: a, x(10), y(10) interface axpy real(dp) :: da, dx(10), dy(10) procedure axpy_ end interface call axpy(a, x, y) contains call axpy(da, dx, dy) subroutine axpy_(a, x, y) real(k), intent(in) :: a real(k), intent(in) :: x(:) real(k), intent(inout) :: y(:) y = a * x + y end subroutine end template

  6. A Motivating Example: AXPY (cont) template axpy_tmpl(T, plus, times) requirement bin_op(T, op) private type, deferred :: T integer, parameter :: sp = kind(1.0), dp = kind(1.d0) public :: axpy elemental function op(a, b) instantiate axpy_tmpl(real(sp), operator(+), operator(*)) requires bin_op(T, plus) type(T), intent(in) :: a, b instantiate axpy_tmpl(real(dp), operator(+), operator(*)) requires bin_op(T, times) type(T) :: op instantiate axpy_tmpl(integer, operator(+), operator(*)) interface axpy end function real(sp) :: a, x(10), y(10) procedure axpy_ end requirement real(dp) :: da, dx(10), dy(10) end interface integer :: ia, ix(10), iy(10) contains subroutine axpy_(a, x, y) call axpy(a, x, y) type(T), intent(in) :: a call axpy(da, dx, dy) type(T), intent(in) :: x(:) call axpy(ia, ix, iy) type(T), intent(inout) :: y(:) y = plus(times(a, x), y) end subroutine end template

  7. A Motivating Example: AXPY subroutine axpy(a, x, y) real, intent(in) :: a real, intent(in) :: x(:) real, intent(inout) :: y(:) y = a * x + y end subroutine

  8. A Motivating Example: AXPY (cont) template axpy_tmpl(T, U, V, plus, times) requirement bin_op(T, U, V, op) integer, parameter :: sp = kind(1.0), dp = kind(1.d0) private type, deferred :: T, U, V instantiate axpy_tmpl( & public :: axpy elemental function op(a, b) real(sp), integer, real(dp), operator(+), operator(*)) requires bin_op(T, U, U, times) type(T), intent(in) :: a real(sp) :: a requires bin_op(U, V, V, plus) type(U), intent(in) :: b integer :: x(10) interface axpy type(V) :: op real(dp) :: y(10) procedure axpy_ end function end interface end requirement call axpy(a, x, y) contains subroutine axpy_(a, x, y) type(T), intent(in) :: a type(U), intent(in) :: x(:) type(V), intent(inout) :: y(:) y = plus(times(a, x), y) end subroutine end template

  9. Thank You

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#