File: template_03.f90

package info (click to toggle)
lfortran 0.60.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,412 kB
  • sloc: cpp: 173,406; f90: 80,491; python: 17,586; ansic: 9,610; yacc: 2,356; sh: 1,401; fortran: 895; makefile: 37; javascript: 15
file content (52 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download | duplicates (2)
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
module template_03_m

    requirement op(T, U, V, op)
      type, deferred :: T
      type, deferred :: U
      type, deferred :: V
      interface
        elemental function op(a, b)
          type(T), intent(in) :: a
          type(U), intent(in) :: b
          type(V) :: op
        end function
      end interface
    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(:)
        integer :: i
        do i = 1, size(x)
            y(i) = plus(y(i), times(a, x(i)))
        end do
      end subroutine
    end template
    
    contains
    
    subroutine f()
        integer, parameter :: sp = kind(1.0), dp = kind(1.d0)
        instantiate axpy_tmpl(real, integer, real, real, operator(+), operator(*))
        real :: a
        integer :: x(3)
        real :: y(3)
        a = 0.5
        x = 2
        y = 0
        call axpy(a, x, y)
        print *, y
    end subroutine
    
end module
    
program template_03
use template_03_m, only: f
call f()
end program