-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNondetBFS.sml
177 lines (152 loc) · 5.39 KB
/
NondetBFS.sml
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
(* nondeterministic direction-optimized BFS, using CAS on outneighbors to
* construct next frontier. *)
structure NondetBFS =
struct
type 'a seq = 'a Seq.t
(* structure DS = DelayedSeq *)
structure G = AdjacencyGraph(Int)
structure V = G.Vertex
type vertex = G.vertex
val sub = Array.sub
val upd = Array.update
val vtoi = V.toInt
val itov = V.fromInt
(* fun ASsub s =
let val (a, i, _) = ArraySlice.base s
in sub (a, i+s)
end *)
val GRAIN = 10000
fun strip s =
let val (s', start, _) = ArraySlice.base s
in if start = 0 then s' else raise Fail "strip base <> 0"
end
fun bfs {diropt: bool} (g : G.graph) (s : vertex) =
let
val n = G.numVertices g
val parent = strip (Seq.tabulate (fn _ => ~1) n)
(* Choose method of filtering the frontier: either frontier always
* only consists of valid vertex ids, or it allows invalid vertices and
* pretends that these vertices are isolated. *)
fun degree v = G.degree g v
fun filterFrontier s = Seq.filter (fn x => x <> itov (~1)) s
(*
fun degree v = if v < 0 then 0 else Graph.degree g v
fun filterFrontier s = s
*)
val denseThreshold = G.numEdges g div 20
fun sumOfOutDegrees frontier =
SeqBasis.reduce 10000 op+ 0 (0, Seq.length frontier) (degree o Seq.nth frontier)
(* DS.reduce op+ 0 (DS.map degree (DS.fromArraySeq frontier)) *)
fun shouldProcessDense frontier =
diropt andalso
let
val n = Seq.length frontier
val m = sumOfOutDegrees frontier
in
n + m > denseThreshold
end
fun bottomUp frontier =
let
val flags = Seq.tabulate (fn _ => false) n
val _ = Seq.foreach frontier (fn (_, v) =>
ArraySlice.update (flags, v, true))
fun inFrontier v = Seq.nth flags (vtoi v)
fun processVertex v =
if sub (parent, v) <> ~1 then NONE else
let
val nbrs = G.neighbors g (itov v)
val deg = ArraySlice.length nbrs
fun loop i =
if i >= deg then
NONE
else
let
val u = Seq.nth nbrs i
in
if inFrontier u then
(upd (parent, v, u); SOME v)
else
loop (i+1)
end
in
loop 0
end
in
ArraySlice.full (SeqBasis.tabFilter 1000 (0, n) processVertex)
end
fun topDown frontier =
let
val nf = Seq.length frontier
val offsets = SeqBasis.scan GRAIN op+ 0 (0, nf) (degree o Seq.nth frontier)
val mf = sub (offsets, nf)
val outNbrs = ForkJoin.alloc mf
(* attempt to claim parent of u as v *)
fun claim (u, v) =
sub (parent, u) = ~1
andalso
~1 = Concurrency.casArray (parent, u) (~1, v)
fun visitNeighbors offset v nghs =
Util.for (0, Seq.length nghs) (fn i =>
let val u = Seq.nth nghs i
in if not (claim (vtoi u, vtoi v))
then upd (outNbrs, offset + i, itov (~1))
else upd (outNbrs, offset + i, u)
end)
fun visitMany offlo lo hi =
if lo = hi then () else
let
val v = Seq.nth frontier offlo
val voffset = sub (offsets, offlo)
val k = Int.min (hi - lo, sub (offsets, offlo+1) - lo)
in
if k = 0 then visitMany (offlo+1) lo hi
else ( visitNeighbors lo v (Seq.subseq (G.neighbors g v) (lo - voffset, k))
; visitMany (offlo+1) (lo+k) hi
)
end
fun parVisitMany (offlo, offhi) (lo, hi) =
if hi - lo <= GRAIN then
visitMany offlo lo hi
else
let
val mid = lo + (hi - lo) div 2
val (i, j) = OffsetSearch.search mid offsets (offlo, offhi)
val _ = ForkJoin.par
( fn _ => parVisitMany (offlo, i) (lo, mid)
, fn _ => parVisitMany (j-1, offhi) (mid, hi)
)
in
()
end
(* Either one of the following is correct, but the second one has
* significantly better granularity control for graphs that have a
* small number of vertices with huge degree. *)
(* val _ = ParUtil.parfor 100 (0, nf) (fn i =>
visitMany i (sub (offsets, i)) (sub (offsets, i+1))) *)
val _ = parVisitMany (0, nf + 1) (0, mf)
in
filterFrontier (ArraySlice.full outNbrs)
end
fun search frontier =
if Seq.length frontier = 0 then
()
else if shouldProcessDense frontier then
let
val (nextFrontier, tm) = Util.getTime (fn _ => bottomUp frontier)
in
print ("dense " ^ Time.fmt 4 tm ^ "\n");
search nextFrontier
end
else
let
val (nextFrontier, tm) = Util.getTime (fn _ => topDown frontier)
in
print ("sparse " ^ Time.fmt 4 tm ^ "\n");
search nextFrontier
end
val _ = upd (parent, vtoi s, s)
val _ = search (Seq.fromList [s])
in
ArraySlice.full parent
end
end