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