1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
program template_03
requirement op(T, U, V, op)
type, deferred :: T
type, deferred :: U
type, deferred :: V
elemental function op(a, b) result(op)
type(T), intent(in) :: a
type(U), intent(in) :: b
type(V) :: op
end function
end requirement
template axpy_tmpl(T, U, V, W, plus, times)
public :: axpy
require :: op(V, W, V, plus)
require :: op(T, U, W, times)
contains
subroutine axpy(a, x, y)
type(T), intent(in) :: a
type(U), intent(in) :: x(:)
type(V), intent(inout) :: y(:)
y = plus(y, times(a, x))
end subroutine
end template
call f()
contains
elemental function my_mul(a, b) result(op)
integer, parameter :: sp = kind(1.0)
real(sp), intent(in) :: a
integer, intent(in) :: b
real(sp) :: op
op = a * b
end function
elemental function my_add(a, b) result(op)
integer, parameter :: sp = kind(1.0), dp = kind(1.d0)
real(dp), intent(in) :: a
real(sp), intent(in) :: b
real(dp) :: op
op = a + b
end function
subroutine my_axpy(a, x, y)
integer, parameter :: sp = kind(1.0), dp = kind(1.d0)
real(sp), intent(in) :: a
integer, intent(in) :: x(:)
real(dp), intent(inout) :: y(:)
y = my_add(y, my_mul(a, x))
end subroutine
subroutine f()
integer, parameter :: sp = kind(1.0), dp = kind(1.d0)
instantiate axpy_tmpl(real(sp), integer, real(dp), real(sp), operator(+), operator(*))
real(sp) :: a
integer :: x(3)
real(dp) :: y(3)
a = 0.5
x = 2
y = 2
call axpy(a, x, y)
! call my_axpy(a, x, y) ! non-generic does not work too
end subroutine
end program
|