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
|
use fparkan_script::{Handler2Record, Handler2RecordKey};
use std::collections::BTreeSet;
#[derive(Default)]
pub(super) struct StartPass {
attempted: BTreeSet<Handler2RecordKey>,
stalled: bool,
}
impl StartPass {
pub(super) fn next_batch(
&mut self,
records: &[Handler2Record],
in_progress: impl Fn(Handler2RecordKey) -> bool,
) -> Vec<Handler2Record> {
if self.stalled {
return Vec::new();
}
let mut highest = 0.0;
let mut batch = Vec::new();
for record in records {
if record.scalar_1 <= 0.0
|| self.attempted.contains(&record.key)
|| in_progress(record.key)
{
continue;
}
if record.scalar_1 > highest {
highest = record.scalar_1;
batch.clear();
}
if record.scalar_1 == highest {
batch.push(record.clone());
}
}
self.attempted.extend(batch.iter().map(|record| record.key));
batch.reverse();
batch
}
pub(super) fn stop_after_batch(&mut self) {
self.stalled = true;
}
}
#[cfg(test)]
mod tests {
use super::*;
use fparkan_script::{Handler2RecordInput, Handler2RecordScheduler};
#[test]
fn attempts_all_tied_requests_and_keeps_lower_weights_waiting_if_one_stalls() {
let mut queue = Handler2RecordScheduler::default();
for (kind, weight) in [(1, 5.0), (2, 5.0), (3, 1.0), (4, 0.0), (5, -1.0)] {
submit(&mut queue, kind, weight);
}
let mut pass = StartPass::default();
let mut batch = pass.next_batch(queue.records(), |_| false);
assert_eq!(batch.pop().unwrap().key.word_0, 1);
pass.stop_after_batch();
assert_eq!(batch.pop().unwrap().key.word_0, 2);
assert!(batch.is_empty());
assert!(pass.next_batch(queue.records(), |_| false).is_empty());
let mut next_pass = StartPass::default();
assert_eq!(
kinds(next_pass.next_batch(queue.records(), |key| key.word_0 == 2)),
[1]
);
}
#[test]
fn reselects_from_live_weights_after_a_batch_advances() {
let mut queue = Handler2RecordScheduler::default();
for (kind, weight) in [(1, 5.0), (2, 5.0), (3, 1.0), (4, 8.0)] {
submit(&mut queue, kind, weight);
}
let mut pass = StartPass::default();
assert_eq!(
kinds(pass.next_batch(queue.records(), |key| key.word_0 == 4)),
[1, 2]
);
submit(&mut queue, 5, 7.0);
for expected in [5, 3] {
assert_eq!(
kinds(pass.next_batch(queue.records(), |key| key.word_0 == 4)),
[expected]
);
}
assert!(pass
.next_batch(queue.records(), |key| key.word_0 == 4)
.is_empty());
}
fn kinds(batch: Vec<Handler2Record>) -> Vec<u32> {
batch.iter().rev().map(|record| record.key.word_0).collect()
}
fn submit(queue: &mut Handler2RecordScheduler, word_0: u32, scalar_1: f32) {
queue.submit(Handler2RecordInput {
word_0,
scalar_1,
word_2: 0,
word_3: 0,
argument_0: 0,
argument_1: 0,
argument_2: 0,
});
}
}
|