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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
type metrics =
{ instr_counter : int Option.t
; distance_to_unreachable : int Option.t
; depth : int
}
let v ~instr_counter ~distance_to_unreachable ~depth =
{ instr_counter; distance_to_unreachable; depth }
let dummy = { instr_counter = None; distance_to_unreachable = None; depth = 0 }
let low =
{ instr_counter = Some 100_000; distance_to_unreachable = None; depth = 0 }
(* In the following, we use the convention that a path with a *high priority* (i.e. that should be explored first) corresponds to a path with a high "integer". *)
let compare_with_highest_first l r = Int.compare r l [@@inline]
let unknown_instruction_counter = 100
let max_rarity = 1_000_000
let use_logarithmic_depth = true
let depth_scale = 4.
let max_depth = if use_logarithmic_depth then 20 else 1_000
let max_loop_penalty = 10_000
let unknown_loop_penalty = max_loop_penalty / 10
let rarity_of_metrics { instr_counter; _ } =
let c =
match instr_counter with Some c -> c | None -> unknown_instruction_counter
in
let c = min c max_rarity in
max_int - c
let depth_of_metrics { depth; _ } =
let depth =
if use_logarithmic_depth then
let depth = Float.of_int depth in
let depth = depth_scale *. log (depth +. 1.) in
Int.of_float depth
else depth
in
min depth max_depth
let loop_penalty_of_metrics { instr_counter; _ } =
match instr_counter with
| Some c -> ~-(min (c * c) max_loop_penalty)
| None -> ~-unknown_loop_penalty
module type T = sig
type t
val of_metrics : metrics -> t
val compare : t -> t -> int
val requires_random : Bool.t
end
module FIFO : T = struct
type t = int
let of_metrics =
let counter = Atomic.make max_int in
fun { instr_counter = _; distance_to_unreachable = _; depth = _ } ->
Atomic.fetch_and_add counter ~-1
let compare = compare_with_highest_first
let requires_random = false
end
module LIFO : T = struct
type t = int
let of_metrics =
let counter = Atomic.make 0 in
fun { instr_counter = _; distance_to_unreachable = _; depth = _ } ->
Atomic.fetch_and_add counter 1
let compare = compare_with_highest_first
let requires_random = false
end
module Random_prio : T = struct
type t = int
let of_metrics { instr_counter = _; distance_to_unreachable = _; depth = _ } =
Random.int 10_000
let compare = compare_with_highest_first
let requires_random = true
end
module Random_unseen_then_random : T = struct
type t = int
let of_metrics { instr_counter; distance_to_unreachable = _; depth = _ } =
match instr_counter with
| Some 0 -> max_int - Random.int 10_000
| None | Some _ -> Random.int 10_000
let compare = compare_with_highest_first
let requires_random = true
end
module Rarity : T = struct
type t = int
let of_metrics s = rarity_of_metrics s
let compare = compare_with_highest_first
let requires_random = false
end
module Hot_path_penalty : T = struct
type t = int
let of_metrics s = loop_penalty_of_metrics s
let compare = compare_with_highest_first
let requires_random = false
end
module Rarity_aging : T = struct
type t =
{ rarity : int
; age : int
}
let age_counter = Atomic.make 0
let of_metrics s =
let rarity = rarity_of_metrics s in
let age = Atomic.fetch_and_add age_counter ~-1 in
{ rarity; age }
let compare p1 p2 =
let rarity = compare_with_highest_first p1.rarity p2.rarity in
if rarity = 0 then compare_with_highest_first p1.age p2.age else rarity
let requires_random = false
end
module Rarity_depth_aging : T = struct
type t =
{ rarity : int
; depth : int
; age : int
}
let age_counter = Atomic.make 0
let of_metrics s =
let rarity = rarity_of_metrics s in
let depth = depth_of_metrics s in
let age = Atomic.fetch_and_add age_counter ~-1 in
{ rarity; depth; age }
let compare p1 p2 =
let rarity = compare_with_highest_first p1.rarity p2.rarity in
if rarity = 0 then
let depth = compare_with_highest_first p1.depth p2.depth in
if depth = 0 then compare_with_highest_first p1.age p2.age else depth
else rarity
let requires_random = false
end
module Rarity_depth_loop_aging : T = struct
type t =
{ rarity : int
; depth : int
; loop_penalty : int
; age : int
}
let age_counter = Atomic.make 0
let of_metrics s =
let rarity = rarity_of_metrics s in
let depth = depth_of_metrics s in
let loop_penalty = loop_penalty_of_metrics s in
let age = Atomic.fetch_and_add age_counter ~-1 in
{ rarity; depth; loop_penalty; age }
let compare p1 p2 =
let rarity = compare_with_highest_first p1.rarity p2.rarity in
if rarity = 0 then
let depth = compare_with_highest_first p1.depth p2.depth in
if depth = 0 then
let loop_penalty =
compare_with_highest_first p1.loop_penalty p2.loop_penalty
in
if loop_penalty = 0 then compare_with_highest_first p1.age p2.age
else loop_penalty
else depth
else rarity
let requires_random = false
end
module Rarity_depth_loop_aging_random : T = struct
type t =
{ rarity : int
; depth : int
; loop_penalty : int
; age : int
; random : int
}
let age_counter = Atomic.make 0
let of_metrics s =
let rarity = rarity_of_metrics s in
let depth = depth_of_metrics s in
let loop_penalty = loop_penalty_of_metrics s in
let age = Atomic.fetch_and_add age_counter ~-1 in
let random = Random.int 7 in
{ rarity; depth; loop_penalty; age; random }
let compare p1 p2 =
let rarity = compare_with_highest_first p1.rarity p2.rarity in
if rarity = 0 then
let depth = compare_with_highest_first p1.depth p2.depth in
if depth = 0 then
let loop_penalty =
compare_with_highest_first p1.loop_penalty p2.loop_penalty
in
if loop_penalty = 0 then
let age = compare_with_highest_first p1.age p2.age in
if age = 0 then compare_with_highest_first p1.random p2.random
else age
else loop_penalty
else depth
else rarity
let requires_random = true
end
module type S = sig
type !'a t
(** Create a new queue *)
val make : unit -> 'a t
(** Add a new element to the queue *)
val push : 'a -> metrics -> 'a t -> unit
(** Make a new pledge, ie indicate that new elements may be pushed to the
queue and that calls to pop should block waiting for them. *)
val new_pledge : 'a t -> unit
(** End one pledge. *)
val end_pledge : 'a t -> unit
(** Mark the queue as closed: all threads trying to pop from it will get no
element. *)
val close : 'a t -> unit
(** Pop all elements from the queue in a lazy Seq.t, *)
val read_as_seq : 'a t -> 'a Seq.t
val work_while : ('a -> (metrics * 'a -> unit) -> unit) -> 'a t -> unit
end
module Make (P : T) : S = struct
module Priority_queue = Priority_queue.Make (P)
type 'a t = ('a, metrics * 'a) Synchronizer.t
let pop q ~pledge = Synchronizer.get q ~pledge
let new_pledge = Synchronizer.new_pledge
let end_pledge = Synchronizer.end_pledge
let rec read_as_seq (q : 'a t) : 'a Seq.t =
fun () ->
match pop q ~pledge:false with
| None -> Nil
| Some v -> Cons (v, read_as_seq q)
let push v prio q = Synchronizer.write q (prio, v)
let work_while f q = Synchronizer.work_while f q
let close = Synchronizer.close
let writter (prio, v) q =
let prio = P.of_metrics prio in
let prio_and_value = (prio, v) in
Priority_queue.push prio_and_value q
let make () =
let q = Priority_queue.empty () in
let writter prio_and_value = writter prio_and_value q in
Synchronizer.init (fun () -> Priority_queue.pop q) writter
end