rust/src/libstd/par.rs

149 lines
4.3 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::cast;
use core::prelude::*;
use core::ptr;
use core::sys;
use core::uint;
use core::vec;
2012-09-04 13:23:53 -05:00
use future_spawn = future::spawn;
2012-05-30 12:36:29 -05:00
/**
* The maximum number of tasks this module will spawn for a single
* operation.
*/
static max_tasks : uint = 32u;
2012-05-30 12:36:29 -05:00
/// The minimum number of elements each task will process.
static min_granularity : uint = 1024u;
2012-05-30 12:36:29 -05:00
/**
* An internal helper to map a function over a large vector and
* return the intermediate results.
*
* This is used to build most of the other parallel vector functions,
* like map or alli.
*/
fn map_slices<A:Copy + Owned,B:Copy + Owned>(
xs: &[A],
f: &fn() -> ~fn(uint, v: &[A]) -> B)
-> ~[B] {
2012-05-30 12:36:29 -05:00
let len = xs.len();
if len < min_granularity {
2013-03-08 14:39:42 -06:00
info!("small slice");
2012-05-30 12:36:29 -05:00
// This is a small vector, fall back on the normal map.
~[f()(0u, xs)]
2012-05-30 12:36:29 -05:00
}
else {
let num_tasks = uint::min(max_tasks, len / min_granularity);
2012-05-30 12:36:29 -05:00
let items_per_task = len / num_tasks;
let mut futures = ~[];
2012-05-30 12:36:29 -05:00
let mut base = 0u;
2013-03-08 14:39:42 -06:00
info!("spawning tasks");
2012-05-30 12:36:29 -05:00
while base < len {
let end = uint::min(len, base + items_per_task);
do vec::as_imm_buf(xs) |p, _len| {
let f = f();
let base = base;
2013-02-15 01:30:30 -06:00
let f = do future_spawn() || {
2012-05-30 12:36:29 -05:00
unsafe {
let len = end - base;
let slice = (ptr::offset(p, base),
len * sys::size_of::<A>());
2013-03-08 14:39:42 -06:00
info!("pre-slice: %?", (base, slice));
let slice : &[A] =
2012-09-18 19:34:08 -05:00
cast::reinterpret_cast(&slice);
2013-03-08 14:39:42 -06:00
info!("slice: %?",
(base, vec::len(slice), end - base));
fail_unless!((vec::len(slice) == end - base));
f(base, slice)
2012-05-30 12:36:29 -05:00
}
};
2013-02-15 01:30:30 -06:00
futures.push(f);
2012-05-30 12:36:29 -05:00
};
base += items_per_task;
}
2013-03-08 14:39:42 -06:00
info!("tasks spawned");
2012-05-30 12:36:29 -05:00
2013-03-08 14:39:42 -06:00
info!("num_tasks: %?", (num_tasks, futures.len()));
fail_unless!((num_tasks == futures.len()));
2012-05-30 12:36:29 -05:00
2012-06-30 18:19:07 -05:00
let r = do futures.map() |ys| {
2012-05-30 12:36:29 -05:00
ys.get()
};
fail_unless!((r.len() == futures.len()));
2012-05-30 12:36:29 -05:00
r
}
}
/// A parallel version of map.
pub fn map<A:Copy + Owned,B:Copy + Owned>(
xs: &[A], fn_factory: &fn() -> ~fn(&A) -> B) -> ~[B] {
2012-06-30 18:19:07 -05:00
vec::concat(map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> ~[B] =
|_, slice| vec::map(slice, |x| f(x));
result
}))
2012-05-30 12:36:29 -05:00
}
/// A parallel version of mapi.
pub fn mapi<A:Copy + Owned,B:Copy + Owned>(
xs: &[A],
fn_factory: &fn() -> ~fn(uint, &A) -> B) -> ~[B] {
2012-06-30 18:19:07 -05:00
let slices = map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> ~[B] = |base, slice| {
2012-06-30 18:19:07 -05:00
vec::mapi(slice, |i, x| {
2012-09-28 15:00:25 -05:00
f(i + base, x)
})
};
result
});
let r = vec::concat(slices);
2013-03-08 14:39:42 -06:00
info!("%?", (r.len(), xs.len()));
fail_unless!((r.len() == xs.len()));
r
}
/// Returns true if the function holds for all elements in the vector.
pub fn alli<A:Copy + Owned>(
xs: &[A],
fn_factory: &fn() -> ~fn(uint, &A) -> bool) -> bool
{
2012-06-30 18:19:07 -05:00
do vec::all(map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> bool = |base, slice| {
2012-06-30 18:19:07 -05:00
vec::alli(slice, |i, x| {
2012-09-28 15:00:25 -05:00
f(i + base, x)
})
};
result
2012-09-28 00:20:47 -05:00
})) |x| { *x }
2012-05-30 12:36:29 -05:00
}
/// Returns true if the function holds for any elements in the vector.
pub fn any<A:Copy + Owned>(
xs: &[A],
fn_factory: &fn() -> ~fn(&A) -> bool) -> bool {
2012-06-30 18:19:07 -05:00
do vec::any(map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> bool =
|_, slice| vec::any(slice, |x| f(x));
result
2012-09-28 00:20:47 -05:00
})) |x| { *x }
2012-05-30 12:36:29 -05:00
}