Skip to content

Commit df8d86a

Browse files
authored
Merge pull request #21 from JuliaGeometry/sjk/alloc_fix
Sjk/alloc fix
2 parents fb3486d + fd65418 commit df8d86a

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

src/decompositions.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,27 @@ end
4848
"""
4949
function tet_to_edges!(pair::Vector, pair_set::Set, t)
5050
empty!(pair_set)
51-
for i in eachindex(t)
51+
length(pair) < length(t)*6 && resize!(pair, length(t)*6)
52+
num_pair = 0
53+
@inbounds for i in eachindex(t)
5254
for ep in 1:6
5355
p1 = t[i][tetpairs[ep][1]]
5456
p2 = t[i][tetpairs[ep][2]]
55-
push!(pair_set, p1 > p2 ? (p2,p1) : (p1,p2))
57+
elt = p1 > p2 ? (p2,p1) : (p1,p2)
58+
if !in(elt, pair_set)
59+
push!(pair_set, elt)
60+
num_pair += 1
61+
pair[num_pair] = elt
62+
end
5663
end
5764
end
58-
resize!(pair, length(pair_set))
59-
# copy pair set to array since sets are not sortable
60-
i = 1
61-
for elt in pair_set
62-
pair[i] = elt
63-
i = i + 1
64-
end
6565

6666
# sort the edge pairs for better point lookup
67-
sort!(pair)
68-
end
67+
sort!(view(pair, 1:num_pair))
68+
69+
return num_pair # return the number of pairs
70+
end
71+
72+
function bitpack(xi,yi)
73+
(unsafe_trunc(UInt64, xi) << 32) | unsafe_trunc(UInt64, yi)
74+
end

src/distmeshnd.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function distmesh(fdist::Function,
9595
# information on each iteration
9696
lcount = 0 # iteration counter
9797
triangulationcount = 0 # triangulation counter
98+
num_pairs = 0
9899

99100
@inbounds while true
100101
# if large move, retriangulation
@@ -103,18 +104,20 @@ function distmesh(fdist::Function,
103104
# compute a new delaunay triangulation
104105
retriangulate!(fdist, result, geps, setup, triangulationcount, pt_dists)
105106

106-
tet_to_edges!(pair, pair_set, result.tetrahedra) # Describe each edge by a unique pair of nodes
107+
num_pairs = tet_to_edges!(pair, pair_set, result.tetrahedra) # Describe each edge by a unique pair of nodes
107108

108-
# resize arrays for new pair counts
109-
resize!(bars, length(pair))
110-
resize!(L, length(pair))
111-
non_uniform && resize!(L0, length(pair))
109+
# resize arrays for new pair count
110+
if length(L) < num_pairs
111+
resize!(bars, num_pairs)
112+
resize!(L, num_pairs)
113+
non_uniform && resize!(L0, num_pairs)
114+
end
112115

113116
triangulationcount += 1
114117
stats && push!(result.stats.retriangulations, lcount)
115118
end
116119

117-
compute_displacements!(fh, dp, pair, L, L0, bars, result.points, setup, VertType)
120+
compute_displacements!(fh, dp, pair, num_pairs, L, L0, bars, result.points, setup, VertType)
118121

119122
# Zero out forces on fix points
120123
if have_fixed
@@ -214,16 +217,17 @@ function retriangulate!(fdist, result::DistMeshResult, geps, setup, triangulatio
214217
end
215218

216219

217-
function compute_displacements!(fh, dp, pair, L, L0, bars, p, setup,
218-
::Type{VertType}) where {VertType}
220+
221+
function compute_displacements!(fh, dp, pair, num_pairs, L, L0, bars, p, setup,
222+
::Type{VertType}) where {VertType}
219223

220224
non_uniform = isa(typeof(L0), AbstractVector)
221225

222226
# compute edge lengths (L) and adaptive edge lengths (L0)
223227
# Lp norm (p=3) is partially computed here
224228
Lsum = zero(eltype(L))
225-
L0sum = non_uniform ? zero(eltype(L0)) : length(pair)
226-
for i in eachindex(pair)
229+
L0sum = non_uniform ? zero(eltype(L0)) : num_pairs
230+
for i in 1:num_pairs
227231
pb = pair[i]
228232
b1 = p[pb[1]]
229233
b2 = p[pb[2]]
@@ -245,7 +249,7 @@ function compute_displacements!(fh, dp, pair, L, L0, bars, p, setup,
245249
lscbrt = (1+(0.4/2^2))*cbrt(Lsum/L0sum)
246250

247251
# Move mesh points based on edge lengths L and forces F
248-
for i in eachindex(pair)
252+
for i in 1:num_pairs
249253
if non_uniform && L[i] < L0[i]*lscbrt || L[i] < lscbrt
250254
L0_f = non_uniform ? L0[i].*lscbrt : lscbrt
251255
# compute force vectors

0 commit comments

Comments
 (0)