surpass/extend.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 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 313 314
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
macro_rules! extend_tuple {
( $name:ident, $( ( $fields:tt $types:ident ) ),+ ) => {
pub struct $name<'a, $($types),+> {
tuple: ($(&'a mut Vec<$types>),+),
}
impl<'a, $($types),+> $name<'a, $($types),+> {
pub fn new(tuple: ($(&'a mut Vec<$types>),+)) -> Self {
Self { tuple }
}
}
impl<$($types),+> ::rayon::iter::ParallelExtend<($($types),+)> for $name<'_, $($types),+>
where
$(
$types: Send,
)+
{
fn par_extend<PI>(&mut self, par_iter: PI)
where
PI: ::rayon::iter::IntoParallelIterator<Item = ($($types),+)>,
{
use ::std::{
collections::LinkedList, ptr, slice, sync::atomic::{AtomicUsize, Ordering},
};
use ::rayon::{
iter::plumbing::{Consumer, Folder, Reducer, UnindexedConsumer},
prelude::*,
};
struct NoopReducer;
impl Reducer<()> for NoopReducer {
fn reduce(self, _left: (), _right: ()) {}
}
struct CollectTupleConsumer<'c, $($types: Send),+> {
writes: &'c AtomicUsize,
targets: ($(&'c mut [$types]),+),
}
struct CollectTupleFolder<'c, $($types: Send),+> {
global_writes: &'c AtomicUsize,
local_writes: usize,
targets: ($(slice::IterMut<'c, $types>),+),
}
impl<'c, $($types: Send + 'c),+> Consumer<($($types),+)>
for CollectTupleConsumer<'c, $($types),+>
{
type Folder = CollectTupleFolder<'c, $($types),+>;
type Reducer = NoopReducer;
type Result = ();
fn split_at(self, index: usize) -> (Self, Self, NoopReducer) {
let CollectTupleConsumer { writes, targets } = self;
let splits = (
$(
targets.$fields.split_at_mut(index),
)+
);
(
CollectTupleConsumer {
writes,
targets: (
$(
splits.$fields.0,
)+
),
},
CollectTupleConsumer {
writes,
targets: (
$(
splits.$fields.1,
)+
),
},
NoopReducer,
)
}
fn into_folder(self) -> CollectTupleFolder<'c, $($types),+> {
CollectTupleFolder {
global_writes: self.writes,
local_writes: 0,
targets: (
$(
self.targets.$fields.iter_mut(),
)+
),
}
}
fn full(&self) -> bool {
false
}
}
impl<'c, $($types: Send + 'c),+> Folder<($($types),+)>
for CollectTupleFolder<'c, $($types),+>
{
type Result = ();
fn consume(
mut self,
item: ($($types),+),
) -> CollectTupleFolder<'c, $($types),+> {
$(
let head = self
.targets
.$fields
.next()
.expect("too many values pushed to consumer");
unsafe {
ptr::write(head, item.$fields);
}
)+
self.local_writes += 1;
self
}
fn complete(self) {
self.global_writes.fetch_add(self.local_writes, Ordering::Relaxed);
}
fn full(&self) -> bool {
false
}
}
impl<'c, $($types: Send + 'c),+> UnindexedConsumer<($($types),+)>
for CollectTupleConsumer<'c, $($types),+>
{
fn split_off_left(&self) -> Self {
unreachable!("CollectTupleConsumer must be indexed!")
}
fn to_reducer(&self) -> Self::Reducer {
NoopReducer
}
}
struct CollectTuple<'c, $($types: Send),+> {
writes: AtomicUsize,
tuple: ($(&'c mut Vec<$types>),+),
len: usize,
}
impl<'c, $($types: Send),+> CollectTuple<'c, $($types),+> {
pub fn new(tuple: ($(&'c mut Vec<$types>),+), len: usize) -> Self {
Self {
writes: AtomicUsize::new(0),
tuple,
len,
}
}
pub fn as_consumer(&mut self) -> CollectTupleConsumer<'_, $($types),+> {
$(
self.tuple.$fields.reserve(self.len);
)+
CollectTupleConsumer {
writes: &self.writes,
targets: (
$(
{
let vec = &mut self.tuple.$fields;
let start = vec.len();
let slice = &mut vec[start..];
unsafe {
slice::from_raw_parts_mut(
slice.as_mut_ptr(),
self.len,
)
}
}
),+
),
}
}
pub fn complete(mut self) {
unsafe {
let actual_writes = self.writes.load(Ordering::Relaxed);
assert!(
actual_writes == self.len,
"expected {} total writes, but got {}",
self.len,
actual_writes
);
$(
let vec = &mut self.tuple.$fields;
let new_len = vec.len() + self.len;
vec.set_len(new_len);
)+
}
}
}
let par_iter = par_iter.into_par_iter();
match par_iter.opt_len() {
Some(len) => {
let mut collect = CollectTuple::new(($(self.tuple.$fields),+), len);
par_iter.drive_unindexed(collect.as_consumer());
collect.complete()
}
None => {
let list = par_iter
.into_par_iter()
.fold(|| ($(Vec::<$types>::new()),+), |mut vecs, elem| {
$(
vecs.$fields.push(elem.$fields);
)+
vecs
})
.map(|item| {
let mut list = LinkedList::new();
list.push_back(item);
list
})
.reduce(LinkedList::new, |mut list1, mut list2| {
list1.append(&mut list2);
list1
});
let len = list.iter().map(|vecs| vecs.0.len()).sum();
$(
self.tuple.$fields.reserve(len);
)+
for mut vecs in list {
$(
self.tuple.$fields.append(&mut vecs.$fields);
)+
}
}
}
}
}
};
}
extend_tuple!(ExtendTuple3, (0 A), (1 B), (2 C));
extend_tuple!(ExtendTuple10, (0 A), (1 B), (2 C), (3 D), (4 E), (5 F), (6 G), (7 H), (8 I), (9 J));
pub struct ExtendVec<'a, T> {
vec: &'a mut Vec<T>,
}
impl<'a, T> ExtendVec<'a, T> {
pub fn new(vec: &'a mut Vec<T>) -> Self {
Self { vec }
}
}
impl<T: Send> rayon::iter::ParallelExtend<T> for ExtendVec<'_, T> {
fn par_extend<I>(&mut self, par_iter: I)
where
I: rayon::iter::IntoParallelIterator<Item = T>,
{
self.vec.par_extend(par_iter);
}
}
#[cfg(test)]
mod tests {
use super::*;
use rayon::prelude::*;
#[test]
fn tuple10() {
let mut vec0 = vec![];
let mut vec1 = vec![];
let mut vec2 = vec![];
let mut vec3 = vec![];
let mut vec4 = vec![];
let mut vec5 = vec![];
let mut vec6 = vec![];
let mut vec7 = vec![];
let mut vec8 = vec![];
let mut vec9 = vec![];
ExtendTuple10::new((
&mut vec0, &mut vec1, &mut vec2, &mut vec3, &mut vec4, &mut vec5, &mut vec6, &mut vec7,
&mut vec8, &mut vec9,
))
.par_extend(
(0..3)
.into_par_iter()
.map(|i| (i, i + 1, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7, i + 8, i + 9)),
);
assert_eq!(vec0, [0, 1, 2]);
assert_eq!(vec1, [1, 2, 3]);
assert_eq!(vec2, [2, 3, 4]);
assert_eq!(vec3, [3, 4, 5]);
assert_eq!(vec4, [4, 5, 6]);
assert_eq!(vec5, [5, 6, 7]);
assert_eq!(vec6, [6, 7, 8]);
assert_eq!(vec7, [7, 8, 9]);
assert_eq!(vec8, [8, 9, 10]);
assert_eq!(vec9, [9, 10, 11]);
}
}