나는 병렬로 처리하고 싶은 녹의 나무 구조를 가지고있다. 내 진짜 문제는 더 복잡하다, 그러나 이것은 내가 지금이 기본적으로 직렬 버전입니다 :녹에서 평행 한 나무를 처리하기
이#[derive(Debug)]
enum BinaryTree {
Node(Box<BinaryTree>, Box<BinaryTree>),
Leaf(i32),
}
use BinaryTree::*;
impl BinaryTree {
/// Applies the given function to every leaf in the tree
fn map<F: Fn(i32) -> i32>(&mut self, f: &F) {
match *self {
Node(ref mut tree1, ref mut tree2) => {
tree1.map(f);
tree2.map(f);
}
Leaf(ref mut n) => *n = f(*n),
}
}
}
내가 사용하지 않는이 병렬화 싶습니다
- 없음 잠금
- 스레드 풀을, 또는 그렇지 않으면 다시 만들 스레드에 있지 않는 는
- (바람직하게는) 안전하지 않은 코드
문제는 매우 자연스러운 t 보인다 병렬화 : 모든 노드에서 각 하위 노드를 동시에 처리하여 잠재적으로 직렬 버전으로 폴백 할 수 있습니다. 그러나 표준 라이브러리에 아직없는 범위가 지정된 스레드가 필요합니다. 나는 scoped-pool 상자에 대한 정착하고,이 솔루션에 도착 :
extern crate scoped_pool;
impl BinaryTree {
/// Applies the given function to every leaf in the tree
fn map_parallel<F>(&mut self, f: &F, scope: &scoped_pool::Scope)
where F: Fn(i32) -> i32 + Send + Sync
{
match self {
&mut Node(ref mut tree1, ref mut tree2) => {
// Create new, smaller scope
scope.zoom(|scope2| {
// Concurrently process child nodes
scope2.recurse(|scope3| {
tree1.map_parallel(f, scope3);
});
scope2.recurse(|scope3| {
tree2.map_parallel(f, scope3);
});
}
);},
&mut Leaf(ref mut n) => *n = f(*n),
}
}
}
fn main() {
let mut tree = Node(
Box::new(Node(
Box::new(Node(
Box::new(Node(
Box::new(Node(
Box::new(Leaf(11)),
Box::new(Leaf(15)))),
Box::new(Leaf(13)))),
Box::new(Leaf(19)))),
Box::new(Leaf(5)))),
Box::new(Leaf(10)));
let thread_pool = scoped_pool::Pool::new(4);
tree.map_parallel(&|n| n + 1, &scoped_pool::Scope::forever(thread_pool));
println!("{:?}", tree);
}
그러나이 교착 상태에 갇혀 얻을 나타나고, 그 이유를 이해하지 않습니다. Rust에서 병렬로 나무를 처리하는 관용적 인 방법은 무엇입니까?
교착 상태에있는 스레드 (있는 경우)와 개체를 찾는 데 도움이되는 VisualVM과 같은 프로파일 링 도구를 사용하는 것이 좋습니다. –