Skip to content

Commit 5e3e039

Browse files
authored
Merge pull request #120 from mschauer/asynch
Fix nv_ and add queue tests
2 parents e4dcd02 + 3343628 commit 5e3e039

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ZigZagBoomerang"
22
uuid = "36347407-b186-4a6a-8c98-4f4567861712"
33
authors = ["Sebastiano Grazzi and Moritz Schauer"]
4-
version = "0.13.0"
4+
version = "0.13.1"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

src/morepriorityqueues.jl

+8-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ struct PartialQueue{U,T,S,R}
5858
minima::R
5959
nregions::Int
6060
end
61-
nv_(G) = length(G)
61+
nv_(G::Vector) = length(G)
62+
nv_(G::AbstractGraph) = nv(G)
63+
6264
div1(a,b) = (a-1)÷b + 1
6365
rkey(q, key) = div1(q.nregions*key, nv_(q.G))
6466
rkey((nregions, nv)::Tuple, key) = div1(nregions*key, nv)
@@ -85,25 +87,27 @@ function check(q::PartialQueue)
8587
end
8688
end
8789

88-
function checkqueue(q::PartialQueue)
90+
function checkqueue(q::PartialQueue; fullcheck=false)
8991
minima = [Pair{Int64, Float64}[] for _ in 1:q.nregions]
9092
for i in 1:length(q.vals)
9193
q.ripes[i] == localmin(q, i) || error("Internal error")
9294
q.ripes[i] && push!(minima[div1(q.nregions*i, nv_(q.G))], i=>q.vals[i])
9395
end
94-
CHECKPQ && for i in 1:q.nregions
96+
(CHECKPQ || fullcheck) && for i in 1:q.nregions
9597
if Set(first.(q.minima[i])) != Set(first.(minima[i]))
9698
println(setdiff(minima[i], q.minima[i]))
9799
println(setdiff(q.minima[i], minima[i]))
98-
99100
error("corrupted")
100101
end
101102
end
103+
return
102104
end
103105

104106

105107

106108

109+
110+
107111
function collectmin(q::PartialQueue)
108112
all(isempty.(q.minima)) || error("Full queue")
109113
for i in findall(q.ripes)

test/priority.jl

+54
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
using DataStructures
22
using ZigZagBoomerang: LinearQueue, PriorityQueues, rcat
3+
using ZigZagBoomerang, Graphs
4+
using ZigZagBoomerang: PartialQueue, dequeue!, saturate, rkey, div1
5+
using Graphs: Edge
6+
using Random
7+
using Base.Threads
38

49
@testset "Queues" begin
510
L = LinearQueue(0:2, [3.0, 1.0, 0.5])
@@ -31,4 +36,53 @@ using Test
3136
Q = PartialQueue(G, vals)
3237

3338
Q[2] = 3
39+
40+
41+
42+
43+
Random.seed!(1)
44+
45+
# number of regions
46+
nregions = 2
47+
# number of coordinates/keys
48+
d = 10
49+
50+
# time surface
51+
times = rand(d)
52+
53+
# undirected graph of neighbours
54+
G = Graph(Edge.(([i=>i+1 for i in 1:d-1])))
55+
56+
# Make a Queue-structure keeping track of local minima
57+
# (a coordinate is a local minima if it's time is smaller than all neighbours' (via the graph) times)
58+
Q = PartialQueue(G, times, nregions)
59+
60+
# key 1:5 is in region 1, keys 6:10 in region 2
61+
# we can work on different regions in parallel... see below
62+
@test rkey(Q,5) == 1
63+
@test rkey(Q,6) == 2
64+
65+
66+
# dequeue! some local minima to work with
67+
minima = dequeue!(Q)
68+
69+
@test peek(Q) == [[],[]] # currently all local minima are removed from the queue to be worked on
70+
71+
# update/increment local time, (in parallel thanks to the threads makro)
72+
@threads for r in 1:nregions
73+
for (i,t) in minima[r]
74+
println("work with $i, $t on $(Threads.threadid())")
75+
Q[i] = t + rand()
76+
end
77+
end
78+
79+
# internal check
80+
@test try
81+
ZigZagBoomerang.checkqueue(Q, fullcheck=true) == nothing
82+
catch
83+
false
84+
end
85+
86+
# By now we have a new set of local minimas we can handle in threads
87+
@test peek(Q) != [[],[]]
3488
end

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ include("staticarrays.jl")
1818
include("sticky.jl")
1919
include("sticky_test.jl")
2020

21+
include("priority.jl")
2122
#include("forwarddiff.jl")

0 commit comments

Comments
 (0)