-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.fpp
124 lines (111 loc) · 3.13 KB
/
common.fpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#:mute
#:set REAL_TYPE='real(wp)'
#:set COMPLEX_TYPE='complex(wp)'
#:set REAL_TYPES=['s','d']
#:set COMPLEX_TYPES=['c','z']
#:set DEFAULT_TYPES=REAL_TYPES+COMPLEX_TYPES
#:set PREFIX_TO_TYPE={ &
's': REAL_TYPE, &
'd': REAL_TYPE, &
'c': COMPLEX_TYPE, &
'z': COMPLEX_TYPE, &
}
#:set PREFIX_TO_KIND={&
's': 'REAL32', &
'd': 'REAL64', &
'c': 'REAL32', &
'z': 'REAL64', &
}
#! Defines a optional variable, creating local corresponding variable by default
#:def optional(dtype, intent, *args)
#:for variable in args
${dtype}$, intent(${intent}$), optional :: ${variable}$
${dtype}$ :: local_${variable}$
#:endfor
#:enddef
#! Handles a value of "variable" depending on "condition"
#:def optval(condition, variable, true_value, false_value)
if (${condition}$) then
${variable}$ = ${true_value}$
else
${variable}$ = ${false_value}$
end if
#:enddef
#! Handles default values of the optional
#:def defaults(**kwargs)
#:for variable, default in kwargs.items()
if (present(${variable}$)) then
local_${variable}$ = ${variable}$
else
local_${variable}$ = ${default}$
end if
#:endfor
#:enddef
#! Handles the input/output arguments
#:def args(dtype, intent, *args)
#:for variable in args
${dtype}$, intent(${intent}$) :: ${variable}$
#:endfor
#:enddef
#! Handles parameters (usage: working precision)
#:def parameter(dtype, **kwargs)
#:for variable, value in kwargs.items()
${dtype}$, parameter :: ${variable}$ = ${value}$
#:endfor
#:enddef
#! Handles the implementation of the modern interface to each supported type and kind
#:def mfi_implement(name, supports, code)
#:for PREFIX in supports
#:set MFI_NAME = f"mfi_{name.replace('?',PREFIX)}"
#:set F77_NAME = f"f77_{name.replace('?','')}"
#:set TYPE = PREFIX_TO_TYPE.get(PREFIX,None)
#:set KIND = PREFIX_TO_KIND.get(PREFIX,None)
$:code(MFI_NAME,F77_NAME,TYPE,KIND)
#:endfor
#:enddef
#! Define mfi interfaces to implemented routines
#:def mfi_interface(name, types)
interface mfi_${name.replace('?','')}$
#:for T in types
module procedure mfi_${name.replace('?',T)}$
#:endfor
end interface
#:enddef
#! Define f77 interfaces to implemented routines
#:def f77_interface_internal(name, types)
interface f77_${name.replace('?','')}$
#:for T in types
module procedure ${name.replace('?',T)}$
#:endfor
end interface
#:enddef
#! Define a f77 interfaces to the external blas/lapack library
#:def f77_interface(name, supports, code)
interface f77_${name.replace('?','')}$
#:for PREFIX in supports
#:set NAME = name.replace('?',PREFIX)
#:set TYPE = PREFIX_TO_TYPE.get(PREFIX,None)
#:set KIND = PREFIX_TO_KIND.get(PREFIX,None)
$:code(NAME,TYPE,KIND)
#:endfor
end interface
#:enddef
#! Implements a f77 function / extension
#:def f77_implement(name, supports, code)
#:for PREFIX in supports
#:set NAME = name.replace('?',PREFIX)
#:set TYPE = PREFIX_TO_TYPE.get(PREFIX,None)
#:set KIND = PREFIX_TO_KIND.get(PREFIX,None)
$:code(NAME,TYPE,KIND)
#:endfor
#:enddef
#:def timeit(message, code)
block
real :: t1, t2
call cpu_time(t1)
$:code
call cpu_time(t2)
print '(A,G0)', ${message}$, t2-t1
end block
#:enddef
#:endmute