|
| 1 | +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | + |
| 12 | +use std::cast; |
| 13 | +use std::num; |
| 14 | +use std::ptr; |
| 15 | +use std::sys; |
| 16 | +use std::vec; |
| 17 | +use future::Future; |
| 18 | + |
| 19 | +/** |
| 20 | + * The maximum number of tasks this module will spawn for a single |
| 21 | + * operation. |
| 22 | + */ |
| 23 | +static MAX_TASKS : uint = 32u; |
| 24 | + |
| 25 | +/// The minimum number of elements each task will process. |
| 26 | +static MIN_GRANULARITY : uint = 1024u; |
| 27 | + |
| 28 | +/** |
| 29 | + * An internal helper to map a function over a large vector and |
| 30 | + * return the intermediate results. |
| 31 | + * |
| 32 | + * This is used to build most of the other parallel vector functions, |
| 33 | + * like map or alli. |
| 34 | + */ |
| 35 | +fn map_slices<A:Clone + Send,B:Clone + Send>( |
| 36 | + xs: &[A], |
| 37 | + f: &fn() -> ~fn(uint, v: &[A]) -> B) |
| 38 | + -> ~[B] { |
| 39 | + |
| 40 | + let len = xs.len(); |
| 41 | + if len < MIN_GRANULARITY { |
| 42 | + info!("small slice"); |
| 43 | + // This is a small vector, fall back on the normal map. |
| 44 | + ~[f()(0u, xs)] |
| 45 | + } else { |
| 46 | + let num_tasks = num::min(MAX_TASKS, len / MIN_GRANULARITY); |
| 47 | + |
| 48 | + let items_per_task = len / num_tasks; |
| 49 | + |
| 50 | + let mut futures = ~[]; |
| 51 | + let mut base = 0u; |
| 52 | + info!("spawning tasks"); |
| 53 | + while base < len { |
| 54 | + let end = num::min(len, base + items_per_task); |
| 55 | + do xs.as_imm_buf |p, _len| { |
| 56 | + let f = f(); |
| 57 | + let base = base; |
| 58 | + let f = do Future::spawn() || { |
| 59 | + unsafe { |
| 60 | + let len = end - base; |
| 61 | + let slice = (ptr::offset(p, base as int), |
| 62 | + len * sys::size_of::<A>()); |
| 63 | + info!("pre-slice: %?", (base, slice)); |
| 64 | + let slice : &[A] = |
| 65 | + cast::transmute(slice); |
| 66 | + info!("slice: %?", (base, slice.len(), end - base)); |
| 67 | + assert_eq!(slice.len(), end - base); |
| 68 | + f(base, slice) |
| 69 | + } |
| 70 | + }; |
| 71 | + futures.push(f); |
| 72 | + }; |
| 73 | + base += items_per_task; |
| 74 | + } |
| 75 | + info!("tasks spawned"); |
| 76 | + |
| 77 | + info!("num_tasks: %?", (num_tasks, futures.len())); |
| 78 | + assert_eq!(num_tasks, futures.len()); |
| 79 | + |
| 80 | + do futures.move_iter().map |ys| { |
| 81 | + let mut ys = ys; |
| 82 | + ys.get() |
| 83 | + }.collect() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// A parallel version of map. |
| 88 | +pub fn map<A:Clone + Send,B:Clone + Send>( |
| 89 | + xs: &[A], fn_factory: &fn() -> ~fn(&A) -> B) -> ~[B] { |
| 90 | + vec::concat(map_slices(xs, || { |
| 91 | + let f = fn_factory(); |
| 92 | + let result: ~fn(uint, &[A]) -> ~[B] = |
| 93 | + |_, slice| slice.iter().map(|x| f(x)).collect(); |
| 94 | + result |
| 95 | + })) |
| 96 | +} |
| 97 | + |
| 98 | +/// A parallel version of mapi. |
| 99 | +pub fn mapi<A:Clone + Send,B:Clone + Send>( |
| 100 | + xs: &[A], |
| 101 | + fn_factory: &fn() -> ~fn(uint, &A) -> B) -> ~[B] { |
| 102 | + let slices = map_slices(xs, || { |
| 103 | + let f = fn_factory(); |
| 104 | + let result: ~fn(uint, &[A]) -> ~[B] = |base, slice| { |
| 105 | + slice.iter().enumerate().map(|(i, x)| { |
| 106 | + f(i + base, x) |
| 107 | + }).collect() |
| 108 | + }; |
| 109 | + result |
| 110 | + }); |
| 111 | + let r = vec::concat(slices); |
| 112 | + info!("%?", (r.len(), xs.len())); |
| 113 | + assert_eq!(r.len(), xs.len()); |
| 114 | + r |
| 115 | +} |
| 116 | + |
| 117 | +/// Returns true if the function holds for all elements in the vector. |
| 118 | +pub fn alli<A:Clone + Send>( |
| 119 | + xs: &[A], |
| 120 | + fn_factory: &fn() -> ~fn(uint, &A) -> bool) -> bool |
| 121 | +{ |
| 122 | + let mapped = map_slices(xs, || { |
| 123 | + let f = fn_factory(); |
| 124 | + let result: ~fn(uint, &[A]) -> bool = |base, slice| { |
| 125 | + slice.iter().enumerate().all(|(i, x)| f(i + base, x)) |
| 126 | + }; |
| 127 | + result |
| 128 | + }); |
| 129 | + mapped.iter().all(|&x| x) |
| 130 | +} |
| 131 | + |
| 132 | +/// Returns true if the function holds for any elements in the vector. |
| 133 | +pub fn any<A:Clone + Send>( |
| 134 | + xs: &[A], |
| 135 | + fn_factory: &fn() -> ~fn(&A) -> bool) -> bool { |
| 136 | + let mapped = map_slices(xs, || { |
| 137 | + let f = fn_factory(); |
| 138 | + let result: ~fn(uint, &[A]) -> bool = |_, slice| slice.iter().any(f); |
| 139 | + result |
| 140 | + }); |
| 141 | + mapped.iter().any(|&x| x) |
| 142 | +} |
0 commit comments