-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathipc.rs
118 lines (101 loc) · 2.7 KB
/
ipc.rs
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
use std::{num::NonZeroU8, time::Duration};
use shakmaty::{fen::Fen, uci::UciMove, variant::Variant};
use tokio::{sync::oneshot, time::Instant};
use url::Url;
use crate::{
api::{AnalysisPart, BatchId, PositionIndex, Score, Work},
assets::EngineFlavor,
util::grow_with_and_get_mut,
};
#[derive(Debug)]
pub struct Chunk {
pub work: Work,
pub deadline: Instant,
pub variant: Variant,
pub flavor: EngineFlavor,
pub positions: Vec<Position>,
}
impl Chunk {
pub const MAX_POSITIONS: usize = 6;
}
#[derive(Debug, Clone)]
pub struct Position {
pub work: Work,
pub position_index: Option<PositionIndex>,
pub url: Option<Url>,
pub skip: bool,
pub root_fen: Fen,
pub moves: Vec<UciMove>,
}
#[derive(Debug, Clone)]
pub struct PositionResponse {
pub work: Work,
pub position_index: Option<PositionIndex>,
pub url: Option<Url>,
pub scores: Matrix<Score>,
pub pvs: Matrix<Vec<UciMove>>,
pub best_move: Option<UciMove>,
pub depth: u8,
pub nodes: u64,
pub time: Duration,
pub nps: Option<u32>,
}
impl PositionResponse {
pub fn to_best(&self) -> AnalysisPart {
AnalysisPart::Best {
pv: self.pvs.best().cloned().unwrap_or_default(),
score: self.scores.best().copied().expect("got score"),
depth: self.depth,
nodes: self.nodes,
time: self.time.as_millis() as u64,
nps: self.nps,
}
}
pub fn into_matrix(self) -> AnalysisPart {
AnalysisPart::Matrix {
pv: self.pvs.matrix,
score: self.scores.matrix,
depth: self.depth,
nodes: self.nodes,
time: self.time.as_millis() as u64,
nps: self.nps,
}
}
}
#[derive(Debug, Clone)]
pub struct Matrix<T> {
matrix: Vec<Vec<Option<T>>>,
}
impl<T> Matrix<T> {
pub fn new() -> Matrix<T> {
Matrix { matrix: Vec::new() }
}
pub fn set(&mut self, multipv: NonZeroU8, depth: u8, v: T) {
let row = grow_with_and_get_mut(&mut self.matrix, usize::from(multipv.get() - 1), Vec::new);
*grow_with_and_get_mut(row, usize::from(depth), || None) = Some(v);
}
pub fn best(&self) -> Option<&T> {
self.matrix
.first()
.and_then(|row| row.last().and_then(|v| v.as_ref()))
}
}
#[derive(Debug)]
pub struct ChunkFailed {
pub batch_id: BatchId,
}
#[derive(Debug)]
pub struct Pull {
pub responses: Result<Vec<PositionResponse>, ChunkFailed>,
pub callback: oneshot::Sender<Chunk>,
}
impl Pull {
pub fn split(
self,
) -> (
Result<Vec<PositionResponse>, ChunkFailed>,
oneshot::Sender<Chunk>,
) {
(self.responses, self.callback)
}
}