-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathremapping_interpolate_array.jl
196 lines (170 loc) · 4.97 KB
/
remapping_interpolate_array.jl
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import ClimaCore.Remapping: interpolate_slab!
import ClimaCore: Topologies, Spaces, Fields, Operators
import CUDA
using CUDA: @cuda
function interpolate_slab!(
output_array,
field::Fields.Field,
slab_indices,
weights,
device::ClimaComms.CUDADevice,
)
space = axes(field)
FT = Spaces.undertype(space)
output_cuarray = CuArray(zeros(FT, length(output_array)))
cuweights = CuArray(weights)
cuslab_indices = CuArray(slab_indices)
nitems = length(output_array)
nthreads, nblocks = _configure_threadblock(nitems)
args = (output_cuarray, field, cuslab_indices, cuweights)
auto_launch!(
interpolate_slab_kernel!,
args;
threads_s = (nthreads),
blocks_s = (nblocks),
)
call_post_op_callback() && post_op_callback(
output_array,
output_array,
field,
slab_indices,
weights,
device,
)
output_array .= Array(output_cuarray)
end
# GPU kernel for 3D configurations
function interpolate_slab_kernel!(
output_array,
field,
slab_indices,
weights::AbstractArray{Tuple{A, A}},
) where {A}
index = threadIdx().x + (blockIdx().x - 1) * blockDim().x
index <= length(output_array) || return nothing
space = axes(field)
FT = Spaces.undertype(space)
@inbounds begin
I1, I2 = weights[index]
Nq1, Nq2 = length(I1), length(I2)
output_array[index] = zero(FT)
for j in 1:Nq2, i in 1:Nq1
ij = CartesianIndex((i, j))
output_array[index] +=
I1[i] *
I2[j] *
Operators.get_node(space, field, ij, slab_indices[index])
end
end
return nothing
end
# GPU kernel for 2D configurations
function interpolate_slab_kernel!(
output_array,
field,
slab_indices,
weights::AbstractArray{Tuple{A}},
) where {A}
index = threadIdx().x + (blockIdx().x - 1) * blockDim().x
index <= length(output_array) || return nothing
@inbounds begin
space = axes(field)
FT = Spaces.undertype(space)
I1, = weights[index]
Nq = length(I1)
output_array[index] = zero(FT)
for i in 1:Nq
ij = CartesianIndex((i))
output_array[index] +=
I1[i] *
Operators.get_node(space, field, ij, slab_indices[index])
end
end
return nothing
end
# GPU
function interpolate_slab_level!(
output_array,
field::Fields.Field,
vidx_ref_coordinates,
h::Integer,
Is::Tuple,
device::ClimaComms.CUDADevice,
)
cuvidx_ref_coordinates = CuArray(vidx_ref_coordinates)
output_cuarray = CuArray(
zeros(Spaces.undertype(axes(field)), length(vidx_ref_coordinates)),
)
nitems = length(vidx_ref_coordinates)
nthreads, nblocks = _configure_threadblock(nitems)
args = (output_cuarray, field, cuvidx_ref_coordinates, h, Is)
auto_launch!(
interpolate_slab_level_kernel!,
args;
threads_s = (nthreads),
blocks_s = (nblocks),
)
output_array .= Array(output_cuarray)
end
# GPU kernel for 3D configurations
function interpolate_slab_level_kernel!(
output_array,
field,
vidx_ref_coordinates,
h,
(I1, I2)::Tuple{<:AbstractArray, <:AbstractArray},
)
index = threadIdx().x + (blockIdx().x - 1) * blockDim().x
index <= length(vidx_ref_coordinates) || return nothing
@inbounds begin
space = axes(field)
FT = Spaces.undertype(space)
Nq1, Nq2 = length(I1), length(I2)
v_lo, v_hi, ξ3 = vidx_ref_coordinates[index]
f_lo = zero(FT)
f_hi = zero(FT)
for j in 1:Nq2, i in 1:Nq1
ij = CartesianIndex((i, j))
f_lo +=
I1[i] *
I2[j] *
Operators.get_node(space, field, ij, Fields.SlabIndex(v_lo, h))
f_hi +=
I1[i] *
I2[j] *
Operators.get_node(space, field, ij, Fields.SlabIndex(v_hi, h))
end
output_array[index] = ((1 - ξ3) * f_lo + (1 + ξ3) * f_hi) / 2
end
return nothing
end
# GPU kernel for 2D configurations
function interpolate_slab_level_kernel!(
output_array,
field,
vidx_ref_coordinates,
h,
(I1,)::Tuple{<:AbstractArray},
)
index = threadIdx().x + (blockIdx().x - 1) * blockDim().x
index <= length(vidx_ref_coordinates) || return nothing
@inbounds begin
space = axes(field)
FT = Spaces.undertype(space)
Nq = length(I1)
v_lo, v_hi, ξ3 = vidx_ref_coordinates[index]
f_lo = zero(FT)
f_hi = zero(FT)
for i in 1:Nq
ij = CartesianIndex((i,))
f_lo +=
I1[i] *
Operators.get_node(space, field, ij, Fields.SlabIndex(v_lo, h))
f_hi +=
I1[i] *
Operators.get_node(space, field, ij, Fields.SlabIndex(v_hi, h))
end
output_array[index] = ((1 - ξ3) * f_lo + (1 + ξ3) * f_hi) / 2
end
return nothing
end