-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathmacros.fpp
More file actions
161 lines (145 loc) · 5.84 KB
/
Copy pathmacros.fpp
File metadata and controls
161 lines (145 loc) · 5.84 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#:include 'parallel_macros.fpp'
#:def LOG(expr)
#ifdef MFC_DEBUG
block
use iso_fortran_env, only: output_unit
print *, '${_FILE_.split('/')[-1]}$:${_LINE_}$: ', ${expr}$
call flush (output_unit)
end block
#endif
#:enddef
! Caution: This macro requires the use of a binding script to set CUDA_VISIBLE_DEVICES, such that we have one GPU device per MPI
! rank. That's because for both cudaMemAdvise (preferred location) and cudaMemPrefetchAsync we use location = device_id = 0. For an
! example see misc/nvidia_uvm/bind.sh.
#:def PREFER_GPU(*args)
#ifdef __NVCOMPILER_GPU_UNIFIED_MEM
block
! NVIDIA CUDA Fortran 25.3+: uses submodules (cuda_runtime_api, gpu_reductions, sort) See
! https://docs.nvidia.com/hpc-sdk/compilers/cuda-fortran-prog-guide/index.html#fortran-host-modules
#if __NVCOMPILER_MAJOR__ < 25 || (__NVCOMPILER_MAJOR__ == 25 && __NVCOMPILER_MINOR__ < 3)
use cudafor, gpu_sum => sum, gpu_maxval => maxval, gpu_minval => minval
#else
use cuda_runtime_api
#endif
integer :: istat
if (nv_uvm_pref_gpu) then
#:for arg in args
! print*, "Moving ${arg}$ to GPU => ", SHAPE(${arg}$) set preferred location GPU
istat = cudaMemAdvise(c_devloc(${arg}$), SIZEOF(${arg}$), cudaMemAdviseSetPreferredLocation, 0)
if (istat /= cudaSuccess) then
write (*, "('Error code: ',I0, ': ')") istat
! write(*,*) cudaGetErrorString(istat)
end if
! set accessed by CPU
istat = cudaMemAdvise(c_devloc(${arg}$), SIZEOF(${arg}$), cudaMemAdviseSetAccessedBy, cudaCpuDeviceId)
if (istat /= cudaSuccess) then
write (*, "('Error code: ',I0, ': ')") istat
! write(*,*) cudaGetErrorString(istat)
end if
! prefetch to GPU - physically populate memory pages
istat = cudaMemPrefetchAsync(c_devloc(${arg}$), SIZEOF(${arg}$), 0, 0)
if (istat /= cudaSuccess) then
write (*, "('Error code: ',I0, ': ')") istat
! write(*,*) cudaGetErrorString(istat)
end if
#:endfor
end if
end block
#endif
#:enddef
! Allocate and create GPU device memory
#:def ALLOCATE(*args)
@:LOG({'@:ALLOCATE(${re.sub(' +', ' ', ', '.join(args))}$)'})
#:set allocated_variables = ', '.join(args)
allocate (${allocated_variables}$)
#:set cleaned = []
#:for a in args
#:set s = a.rstrip()
#:if s.endswith(')')
#:set rev = s[::-1]
#:set pos = next(i for i, ch, d in ( (j, c, sum(1 if t==')' else -1 if t=='(' else 0 for t in rev[:j+1])) for j, &
& c in enumerate(rev) ) if ch == '(' and d == 0 )
#:set s = s[:len(s)-1-pos]
#:endif
$:cleaned.append(s)
#:endfor
#:set joined = ', '.join(cleaned)
$:GPU_ENTER_DATA(create='[' + joined + ']')
#:enddef ALLOCATE
! Free GPU device memory and deallocate
#:def DEALLOCATE(*args)
@:LOG({'@:DEALLOCATE(${re.sub(' +', ' ', ', '.join(args))}$)'})
#:set allocated_variables = ', '.join(args)
$:GPU_EXIT_DATA(delete=('[' + allocated_variables + ']'))
deallocate (${allocated_variables}$)
#:enddef DEALLOCATE
! Cray-specific GPU pointer setup for vector fields
#:def ACC_SETUP_VFs(*args)
#ifdef _CRAYFTN
block
integer :: macros_setup_vfs_i
@:LOG({'@:ACC_SETUP_VFs(${', '.join(args)}$)'})
#:for arg in args
$:GPU_ENTER_DATA(copyin=('[' + arg + ']'))
$:GPU_ENTER_DATA(copyin=('[' + arg + '%vf]'))
if (allocated(${arg}$%vf)) then
do macros_setup_vfs_i = lbound(${arg}$%vf, 1), ubound(${arg}$%vf, 1)
if (associated(${arg}$%vf(macros_setup_vfs_i)%sf)) then
$:GPU_ENTER_DATA(copyin=('[' + arg + '%vf(macros_setup_vfs_i)]'))
$:GPU_ENTER_DATA(copyin=('[' + arg + '%vf(macros_setup_vfs_i)%sf]'))
end if
end do
end if
#:endfor
end block
#endif
#:enddef
! Cray-specific GPU pointer setup for scalar fields
#:def ACC_SETUP_SFs(*args)
#ifdef _CRAYFTN
block
@:LOG({'@:ACC_SETUP_SFs(${', '.join(args)}$)'})
#:for arg in args
$:GPU_ENTER_DATA(copyin=('[' + arg + ']'))
if (associated(${arg}$%sf)) then
$:GPU_ENTER_DATA(copyin=('[' + arg + '%sf]'))
end if
#:endfor
end block
#endif
#:enddef
! Cray-specific GPU pointer setup for acoustic source spatials
#:def ACC_SETUP_source_spatials(*args)
#ifdef _CRAYFTN
block
@:LOG({'@:ACC_SETUP_source_spatials(${', '.join(args)}$)'})
#:for arg in args
$:GPU_ENTER_DATA(copyin=('[' + arg + ']'))
if (associated(${arg}$%coord)) then
$:GPU_ENTER_DATA(copyin=('[' + arg + '%coord]'))
end if
if (associated(${arg}$%val)) then
$:GPU_ENTER_DATA(copyin=('[' + arg + '%val]'))
end if
if (associated(${arg}$%angle)) then
$:GPU_ENTER_DATA(copyin=('[' + arg + '%angle]'))
end if
if (associated(${arg}$%xyz_to_r_ratios)) then
$:GPU_ENTER_DATA(copyin=('[' + arg + '%xyz_to_r_ratios]'))
end if
#:endfor
end block
#endif
#:enddef
#:def PROHIBIT(condition, message = None)
if (${condition}$) then
call s_prohibit_abort("${condition}$", ${message or '""'}$)
end if
#:enddef
#:def ASSERT(predicate, message = None)
if (.not. (${predicate}$)) then
call s_mpi_abort("${_FILE_.split('/')[-1]}$:${_LINE_}$: " // "Assertion failed: ${predicate}$. " &
& // ${message or '"No error description."'}$)
end if
#:enddef
! New line at end of file is required for FYPP