Tue, 19 Nov 2019 10:11:55 -0500
For inlining to work we cannot do function assignments
0 | 1 | ######################## |
2 | # Discretised gradients | |
3 | ######################## | |
4 | ||
5 | module Gradient | |
6 | ||
7 | ############## | |
8 | # Our exports | |
9 | ############## | |
10 | ||
11 | export ∇₂!, ∇₂ᵀ!, ∇₂fold!, | |
12 | ∇₂_norm₂₂_est, ∇₂_norm₂₂_est², | |
13 | ∇₂_norm₂∞_est, ∇₂_norm₂∞_est², | |
4
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
14 | ∇₂c!, ∇₂cfold!, |
0 | 15 | ∇₃!, ∇₃ᵀ!, |
16 | vec∇₃!, vec∇₃ᵀ! | |
17 | ||
18 | ################## | |
19 | # Helper routines | |
20 | ################## | |
21 | ||
22 | @inline function imfold₂′!(f_aa!, f_a0!, f_ab!, | |
23 | f_0a!, f_00!, f_0b!, | |
24 | f_ba!, f_b0!, f_bb!, | |
25 | n, m, state) | |
26 | # First row | |
27 | state = f_aa!(state, (1, 1)) | |
28 | for j = 2:m-1 | |
29 | state = f_a0!(state, (1, j)) | |
30 | end | |
31 | state = f_ab!(state, (1, m)) | |
32 | ||
33 | # Middle rows | |
34 | for i=2:n-1 | |
35 | state = f_0a!(state, (i, 1)) | |
36 | for j = 2:m-1 | |
37 | state = f_00!(state, (i, j)) | |
38 | end | |
39 | state = f_0b!(state, (i, m)) | |
40 | end | |
41 | ||
42 | # Last row | |
43 | state = f_ba!(state, (n, 1)) | |
44 | for j =2:m-1 | |
45 | state = f_b0!(state, (n, j)) | |
46 | end | |
47 | return f_bb!(state, (n, m)) | |
48 | end | |
49 | ||
50 | ######################### | |
51 | # 2D forward differences | |
52 | ######################### | |
53 | ||
54 | ∇₂_norm₂₂_est² = 8 | |
55 | ∇₂_norm₂₂_est = √∇₂_norm₂₂_est² | |
56 | ∇₂_norm₂∞_est² = 2 | |
57 | ∇₂_norm₂∞_est = √∇₂_norm₂∞_est² | |
58 | ||
59 | function ∇₂!(u₁, u₂, u) | |
60 | @. @views begin | |
61 | u₁[1:(end-1), :] = u[2:end, :] - u[1:(end-1), :] | |
62 | u₁[end, :, :] = 0 | |
63 | ||
64 | u₂[:, 1:(end-1)] = u[:, 2:end] - u[:, 1:(end-1)] | |
65 | u₂[:, end] = 0 | |
66 | end | |
67 | return u₁, u₂ | |
68 | end | |
69 | ||
70 | function ∇₂!(v, u) | |
71 | ∇₂!(@view(v[1, :, :]), @view(v[2, :, :]), u) | |
72 | end | |
73 | ||
74 | @inline function ∇₂fold!(f!::Function, u, state) | |
75 | g! = (state, pt) -> begin | |
76 | (i, j) = pt | |
77 | g = @inbounds [u[i+1, j]-u[i, j], u[i, j+1]-u[i, j]] | |
78 | return f!(g, state, pt) | |
79 | end | |
80 | gr! = (state, pt) -> begin | |
81 | (i, j) = pt | |
82 | g = @inbounds [u[i+1, j]-u[i, j], 0.0] | |
83 | return f!(g, state, pt) | |
84 | end | |
85 | gb! = (state, pt) -> begin | |
86 | (i, j) = pt | |
87 | g = @inbounds [0.0, u[i, j+1]-u[i, j]] | |
88 | return f!(g, state, pt) | |
89 | end | |
90 | g0! = (state, pt) -> begin | |
91 | return f!([0.0, 0.0], state, pt) | |
92 | end | |
93 | return imfold₂′!(g!, g!, gr!, | |
94 | g!, g!, gr!, | |
95 | gb!, gb!, g0!, | |
96 | size(u, 1), size(u, 2), state) | |
97 | end | |
98 | ||
99 | function ∇₂ᵀ!(v, v₁, v₂) | |
100 | @. @views begin | |
101 | v[2:(end-1), :] = v₁[1:(end-2), :] - v₁[2:(end-1), :] | |
102 | v[1, :] = -v₁[1, :] | |
103 | v[end, :] = v₁[end-1, :] | |
104 | ||
105 | v[:, 2:(end-1)] += v₂[:, 1:(end-2)] - v₂[:, 2:(end-1)] | |
106 | v[:, 1] += -v₂[:, 1] | |
107 | v[:, end] += v₂[:, end-1] | |
108 | end | |
109 | return v | |
110 | end | |
111 | ||
112 | function ∇₂ᵀ!(u, v) | |
113 | ∇₂ᵀ!(u, @view(v[1, :, :]), @view(v[2, :, :])) | |
114 | end | |
115 | ||
116 | ################################################## | |
117 | # 2D central differences (partial implementation) | |
118 | ################################################## | |
119 | ||
120 | function ∇₂c!(v, u) | |
121 | @. @views begin | |
122 | v[1, 2:(end-1), :] = (u[3:end, :] - u[1:(end-2), :])/2 | |
123 | v[1, end, :] = (u[end, :] - u[end-1, :])/2 | |
124 | v[1, 1, :] = (u[2, :] - u[1, :])/2 | |
125 | ||
126 | v[2, :, 2:(end-1)] = (u[:, 3:end] - u[:, 1:(end-2)])/2 | |
127 | v[2, :, end] = (u[:, end] - u[:, end-1])/2 | |
128 | v[2, :, 1] = (u[:, 2] - u[:, 1])/2 | |
129 | end | |
130 | end | |
131 | ||
4
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
132 | @inline function ∇₂cfold!(f!::Function, u, state) |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
133 | n, m = size(u) |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
134 | g! = (state, pt) -> begin |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
135 | (i, j) = pt |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
136 | g = @inbounds [(u[i+1, j]-u[i-1, j])/2, (u[i, j+1]-u[i, j-1])/2] |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
137 | return f!(g, state, pt) |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
138 | end |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
139 | gb! = (state, pt) -> begin |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
140 | (i, j) = pt |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
141 | g = @inbounds [(u[min(i+1,n), j]-u[max(i-1,1), j])/2, |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
142 | (u[i, min(j+1,m)]-u[i, max(j-1,1)])/2] |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
143 | return f!(g, state, pt) |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
144 | end |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
145 | return imfold₂′!(gb!, gb!, gb!, |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
146 | gb!, g!, gb!, |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
147 | gb!, gb!, gb!, |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
148 | size(u, 1), size(u, 2), state) |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
149 | end |
5c0f579a5d0f
Added central differences fold
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
150 | |
0 | 151 | ######################### |
152 | # 3D forward differences | |
153 | ######################### | |
154 | ||
155 | function ∇₃!(u₁,u₂,u₃,u) | |
156 | @. @views begin | |
157 | u₁[1:(end-1), :, :] = u[2:end, :, :] - u[1:(end-1), :, :] | |
158 | u₁[end, :, :] = 0 | |
159 | ||
160 | u₂[:, 1:(end-1), :] = u[:, 2:end, :] - u[:, 1:(end-1), :] | |
161 | u₂[:, end, :] = 0 | |
162 | ||
163 | u₃[:, :, 1:(end-1)] = u[:, :, 2:end] - u[:, :, 1:(end-1)] | |
164 | u₃[:, :, end] = 0 | |
165 | end | |
166 | return u₁, u₂, u₃ | |
167 | end | |
168 | ||
169 | function ∇₃ᵀ!(v,v₁,v₂,v₃) | |
170 | @. @views begin | |
171 | v[2:(end-1), :, :] = v₁[1:(end-2), :, :] - v₁[2:(end-1), :, :] | |
172 | v[1, :, :] = -v₁[1, :, :] | |
173 | v[end, :, :] = v₁[end-1, :, :] | |
174 | ||
175 | v[:, 2:(end-1), :] += v₂[:, 1:(end-2), :] - v₂[:, 2:(end-1), :] | |
176 | v[:, 1, :] += -v₂[:, 1, :] | |
177 | v[:, end, :] += v₂[:, end-1, :] | |
178 | ||
179 | v[:, :, 2:(end-1)] += v₃[:, :, 1:(end-2)] - v₃[:, :, 2:(end-1)] | |
180 | v[:, :, 1] += -v₃[:, :, 1] | |
181 | v[:, :, end] += v₃[:, :, end-1] | |
182 | end | |
183 | return v | |
184 | end | |
185 | ||
186 | ########################################### | |
187 | # 3D forward differences for vector fields | |
188 | ########################################### | |
189 | ||
190 | function vec∇₃!(u₁,u₂,u₃,u) | |
191 | @. @views for j=1:size(u, 1) | |
192 | ∇₃!(u₁[j, :, :, :],u₂[j, :, :, :],u₃[j, :, :, :],u[j, :, :, :]) | |
193 | end | |
194 | return u₁, u₂, u₃ | |
195 | end | |
196 | ||
197 | function vec∇₃ᵀ!(u,v₁,v₂,v₃) | |
198 | @. @views for j=1:size(u, 1) | |
199 | ∇₃ᵀ!(u[j, :, :, :],v₁[j, :, :, :],v₂[j, :, :, :],v₃[j, :, :, :]) | |
200 | end | |
201 | return u | |
202 | end | |
203 | ||
5
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
204 | ##################################################### |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
205 | # Precompilation hints to speed up compilation time |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
206 | # for projects depending on this package (hopefully). |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
207 | ###################################################### |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
208 | |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
209 | precompile(∇₂!, (Array{Float64,2}, Array{Float64,2}, Array{Float64,2})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
210 | precompile(∇₂!, (Array{Float64,3}, Array{Float64,2})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
211 | precompile(∇₂ᵀ!, (Array{Float64,2}, Array{Float64,2}, Array{Float64,2})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
212 | precompile(∇₂ᵀ!, (Array{Float64,2}, Array{Float64,3})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
213 | precompile(∇₂c!, (Array{Float64,3}, Array{Float64,2})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
214 | precompile(∇₃!, (Array{Float64,3}, Array{Float64,3}, Array{Float64,3},Array{Float64,3})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
215 | precompile(∇₃ᵀ!, (Array{Float64,3}, Array{Float64,3}, Array{Float64,3},Array{Float64,3})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
216 | precompile(vec∇₃!, (Array{Float64,4}, Array{Float64,4}, Array{Float64,4},Array{Float64,4})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
217 | precompile(vec∇₃ᵀ!, (Array{Float64,4}, Array{Float64,4}, Array{Float64,4},Array{Float64,4})) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
218 | |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
219 | # The folding functions cannot be precompiled as theyre' meant to be (hopefully) |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
220 | # inlined in such a way that the parameter function also gets inlined withou our |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
221 | # code |
29b38780d52b
Add precompilation hints. Do they help or not?
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
222 | |
0 | 223 | end # Module |