2013-07-10 19:31:53 -05:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::cast;
|
Replaces the free-standing functions in f32, &c.
The free-standing functions in f32, f64, i8, i16, i32, i64, u8, u16,
u32, u64, float, int, and uint are replaced with generic functions in
num instead.
If you were previously using any of those functions, just replace them
with the corresponding function with the same name in num.
Note: If you were using a function that corresponds to an operator, use
the operator instead.
2013-07-08 11:05:17 -05:00
|
|
|
use std::num;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::ptr;
|
|
|
|
use std::sys;
|
|
|
|
use std::vec;
|
2012-09-04 13:23:53 -05:00
|
|
|
use future_spawn = future::spawn;
|
2012-05-30 12:36:29 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The maximum number of tasks this module will spawn for a single
|
|
|
|
* operation.
|
|
|
|
*/
|
2013-06-30 22:51:13 -05:00
|
|
|
static MAX_TASKS : uint = 32u;
|
2012-05-30 12:36:29 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// The minimum number of elements each task will process.
|
2013-06-30 22:51:13 -05:00
|
|
|
static MIN_GRANULARITY : uint = 1024u;
|
2012-05-30 12:36:29 -05:00
|
|
|
|
2012-07-04 16:53:12 -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.
|
|
|
|
*/
|
2013-07-02 14:47:32 -05:00
|
|
|
fn map_slices<A:Clone + Send,B:Clone + Send>(
|
2012-08-28 17:54:45 -05:00
|
|
|
xs: &[A],
|
2013-01-31 19:12:29 -06:00
|
|
|
f: &fn() -> ~fn(uint, v: &[A]) -> B)
|
2012-06-29 18:26:56 -05:00
|
|
|
-> ~[B] {
|
2012-05-30 12:36:29 -05:00
|
|
|
|
|
|
|
let len = xs.len();
|
2013-06-30 22:51:13 -05:00
|
|
|
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.
|
2012-06-29 18:26:56 -05:00
|
|
|
~[f()(0u, xs)]
|
2013-07-02 14:47:32 -05:00
|
|
|
} else {
|
Replaces the free-standing functions in f32, &c.
The free-standing functions in f32, f64, i8, i16, i32, i64, u8, u16,
u32, u64, float, int, and uint are replaced with generic functions in
num instead.
If you were previously using any of those functions, just replace them
with the corresponding function with the same name in num.
Note: If you were using a function that corresponds to an operator, use
the operator instead.
2013-07-08 11:05:17 -05:00
|
|
|
let num_tasks = num::min(MAX_TASKS, len / MIN_GRANULARITY);
|
2012-05-30 12:36:29 -05:00
|
|
|
|
|
|
|
let items_per_task = len / num_tasks;
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
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 {
|
Replaces the free-standing functions in f32, &c.
The free-standing functions in f32, f64, i8, i16, i32, i64, u8, u16,
u32, u64, float, int, and uint are replaced with generic functions in
num instead.
If you were previously using any of those functions, just replace them
with the corresponding function with the same name in num.
Note: If you were using a function that corresponds to an operator, use
the operator instead.
2013-07-08 11:05:17 -05:00
|
|
|
let end = num::min(len, base + items_per_task);
|
2013-07-03 01:34:17 -05:00
|
|
|
do xs.as_imm_buf |p, _len| {
|
2012-05-30 17:18:45 -05:00
|
|
|
let f = f();
|
2013-01-10 12:59:58 -06:00
|
|
|
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;
|
2013-07-29 23:33:52 -05:00
|
|
|
let slice = (ptr::offset(p, base as int),
|
2012-05-30 12:36:29 -05:00
|
|
|
len * sys::size_of::<A>());
|
2013-03-08 14:39:42 -06:00
|
|
|
info!("pre-slice: %?", (base, slice));
|
2012-06-29 18:26:56 -05:00
|
|
|
let slice : &[A] =
|
2013-04-20 09:27:16 -05:00
|
|
|
cast::transmute(slice);
|
2013-05-14 04:52:12 -05:00
|
|
|
info!("slice: %?", (base, slice.len(), end - base));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(slice.len(), end - base);
|
2012-05-30 17:18:45 -05:00
|
|
|
f(base, slice)
|
2012-05-30 12:36:29 -05:00
|
|
|
}
|
2012-06-25 12:55: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()));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(num_tasks, futures.len());
|
2012-05-30 12:36:29 -05:00
|
|
|
|
2013-07-01 10:26:44 -05:00
|
|
|
do futures.consume_iter().transform |ys| {
|
2013-05-03 00:51:12 -05:00
|
|
|
let mut ys = ys;
|
2012-05-30 12:36:29 -05:00
|
|
|
ys.get()
|
2013-07-01 10:26:44 -05:00
|
|
|
}.collect()
|
2012-05-30 12:36:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A parallel version of map.
|
2013-07-10 16:43:25 -05:00
|
|
|
pub fn map<A:Clone + Send,B:Clone + Send>(
|
2013-01-31 19:12:29 -06:00
|
|
|
xs: &[A], fn_factory: &fn() -> ~fn(&A) -> B) -> ~[B] {
|
2012-06-30 18:19:07 -05:00
|
|
|
vec::concat(map_slices(xs, || {
|
2013-01-31 19:12:29 -06:00
|
|
|
let f = fn_factory();
|
2013-03-01 16:15:15 -06:00
|
|
|
let result: ~fn(uint, &[A]) -> ~[B] =
|
2013-06-29 00:05:50 -05:00
|
|
|
|_, slice| slice.iter().transform(|x| f(x)).collect();
|
2013-03-01 16:15:15 -06:00
|
|
|
result
|
2012-06-26 15:55:56 -05:00
|
|
|
}))
|
2012-05-30 12:36:29 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A parallel version of mapi.
|
2013-07-10 16:43:25 -05:00
|
|
|
pub fn mapi<A:Clone + Send,B:Clone + Send>(
|
2013-03-01 16:15:15 -06:00
|
|
|
xs: &[A],
|
|
|
|
fn_factory: &fn() -> ~fn(uint, &A) -> B) -> ~[B] {
|
2012-06-30 18:19:07 -05:00
|
|
|
let slices = map_slices(xs, || {
|
2013-01-31 19:12:29 -06:00
|
|
|
let f = fn_factory();
|
2013-03-01 16:15:15 -06:00
|
|
|
let result: ~fn(uint, &[A]) -> ~[B] = |base, slice| {
|
2013-06-29 00:05:50 -05:00
|
|
|
slice.iter().enumerate().transform(|(i, x)| {
|
2012-09-28 15:00:25 -05:00
|
|
|
f(i + base, x)
|
2013-06-29 00:05:50 -05:00
|
|
|
}).collect()
|
2013-03-01 16:15:15 -06:00
|
|
|
};
|
|
|
|
result
|
2012-06-26 15:55:56 -05:00
|
|
|
});
|
2012-05-30 17:18:45 -05:00
|
|
|
let r = vec::concat(slices);
|
2013-03-08 14:39:42 -06:00
|
|
|
info!("%?", (r.len(), xs.len()));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(r.len(), xs.len());
|
2012-05-30 17:18:45 -05:00
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if the function holds for all elements in the vector.
|
2013-07-02 14:47:32 -05:00
|
|
|
pub fn alli<A:Clone + Send>(
|
2013-01-31 19:12:29 -06:00
|
|
|
xs: &[A],
|
|
|
|
fn_factory: &fn() -> ~fn(uint, &A) -> bool) -> bool
|
|
|
|
{
|
2013-06-07 23:07:55 -05:00
|
|
|
let mapped = map_slices(xs, || {
|
2013-01-31 19:12:29 -06:00
|
|
|
let f = fn_factory();
|
2013-03-01 16:15:15 -06:00
|
|
|
let result: ~fn(uint, &[A]) -> bool = |base, slice| {
|
2013-06-07 23:07:55 -05:00
|
|
|
slice.iter().enumerate().all(|(i, x)| f(i + base, x))
|
2013-03-01 16:15:15 -06:00
|
|
|
};
|
|
|
|
result
|
2013-06-07 23:07:55 -05:00
|
|
|
});
|
|
|
|
mapped.iter().all(|&x| x)
|
2012-05-30 12:36:29 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if the function holds for any elements in the vector.
|
2013-07-02 14:47:32 -05:00
|
|
|
pub fn any<A:Clone + Send>(
|
2013-01-31 19:12:29 -06:00
|
|
|
xs: &[A],
|
|
|
|
fn_factory: &fn() -> ~fn(&A) -> bool) -> bool {
|
2013-06-07 23:07:55 -05:00
|
|
|
let mapped = map_slices(xs, || {
|
2013-01-31 19:12:29 -06:00
|
|
|
let f = fn_factory();
|
2013-07-04 21:13:26 -05:00
|
|
|
let result: ~fn(uint, &[A]) -> bool = |_, slice| slice.iter().any(f);
|
2013-03-01 16:15:15 -06:00
|
|
|
result
|
2013-06-07 23:07:55 -05:00
|
|
|
});
|
2013-07-04 21:13:26 -05:00
|
|
|
mapped.iter().any(|&x| x)
|
2012-05-30 12:36:29 -05:00
|
|
|
}
|