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
#![allow(clippy::too_many_arguments)]
use crate::infallible_use::XapUse;
use crate::runner::ParRunner;
use crate::sizes::SizePair;
use orx_concurrent_iter::{ChunkPuller, ConcurrentIter};
pub fn reduce<Q, U, I, M, E, X1, X2, S, F>(
_: S,
u: &mut U,
th_idx: usize,
state: &Q::State,
iter: &I,
x1: X1,
x2: X2,
f: F,
) -> Result<Option<X2::O>, E>
where
Q: ParRunner,
I: ConcurrentIter,
X1: XapUse<U = U, I = I::Item, O = Result<M, E>>,
X2: XapUse<U = U, I = M>,
S: SizePair<S1 = X1::Size, S2 = X2::Size>,
F: Fn(&mut U, X2::O, X2::O) -> X2::O,
{
let u = u as *mut U;
let mut chunk_puller = iter.chunk_puller_by(0, th_idx);
let mut acc = None;
// discover first aggregate
loop {
let chunk_size = Q::next_chunk_size(state, iter.size_hint());
let chunk_state = Q::begin_chunk(th_idx, chunk_size);
match chunk_size {
0 | 1 => {
match iter.next_by(th_idx) {
Some(i) => {
for a in S::xap_use_res(u, x1, x2, i) {
acc = match (a, acc.is_some()) {
(Ok(a), true) => acc.map(|agg| f(unsafe { &mut *u }, agg, a)),
(Ok(a), false) => Some(a),
(Err(e), _) => {
Q::broadcast_stop(iter, state, chunk_state);
return Err(e);
}
};
}
}
// TODO: a good back-off strategy might be used, needs benchmark with ConcurrentQueue
None if iter.is_completed_when_none_returned() => break,
None => {}
}
}
c => {
chunk_puller.resize_for_chunk_size(c);
match chunk_puller.pull() {
Some(chunk) => {
for a in chunk.flat_map(|i| S::xap_use_res(u, x1, x2, i)) {
acc = match (a, acc.is_some()) {
(Ok(a), true) => acc.map(|agg| f(unsafe { &mut *u }, agg, a)),
(Ok(a), false) => Some(a),
(Err(e), _) => {
Q::broadcast_stop(iter, state, chunk_state);
return Err(e);
}
};
}
}
None if iter.is_completed_when_none_returned() => break,
None => {}
}
}
}
Q::complete_chunk(state, chunk_state);
if acc.is_some() {
break;
}
}
// fold over the aggregate
let result = match acc {
None => None,
Some(mut acc) => {
loop {
let chunk_size = Q::next_chunk_size(state, iter.size_hint());
let chunk_state = Q::begin_chunk(th_idx, chunk_size);
match chunk_size {
0 | 1 => {
match iter.next_by(th_idx) {
Some(i) => {
for a in S::xap_use_res(u, x1, x2, i) {
acc = match a {
Ok(a) => f(unsafe { &mut *u }, acc, a),
Err(e) => {
Q::broadcast_stop(iter, state, chunk_state);
return Err(e);
}
};
}
}
// TODO: a good back-off strategy might be used, needs benchmark with ConcurrentQueue
None if iter.is_completed_when_none_returned() => break,
None => {}
}
}
c => {
chunk_puller.resize_for_chunk_size(c);
match chunk_puller.pull() {
Some(chunk) => {
let u2 = u;
for a in chunk.flat_map(|i| S::xap_use_res(u, x1, x2, i)) {
acc = match a {
Ok(a) => f(unsafe { &mut *u2 }, acc, a),
Err(e) => {
Q::broadcast_stop(iter, state, chunk_state);
return Err(e);
}
};
}
}
None if iter.is_completed_when_none_returned() => break,
None => {}
}
}
}
Q::complete_chunk(state, chunk_state);
}
Some(acc)
}
};
Ok(result)
}