2 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
3 // file at the top-level directory of this distribution and at
4 // http://rust-lang.org/COPYRIGHT.
6 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 // option. This file may not be copied, modified, or distributed
10 // except according to those terms.
12 #![feature(managed_boxes)]
14 extern crate collections;
18 A somewhat reduced test case to expose some Valgrind issues.
20 This originally came from the word-count benchmark.
23 pub fn map(filename: ~str, emit: map_reduce::putter) { emit(filename, ~"1"); }
26 use collections::HashMap;
30 pub type putter<'a> = |~str, ~str|: 'a;
32 pub type mapper = extern fn(~str, putter);
34 enum ctrl_proto { find_reducer(Vec<u8>, Sender<int>), mapper_done, }
36 fn start_mappers(ctrl: Sender<ctrl_proto>, inputs: Vec<~str>) {
37 for i in inputs.iter() {
38 let ctrl = ctrl.clone();
40 task::spawn(proc() map_task(ctrl.clone(), i.clone()) );
44 fn map_task(ctrl: Sender<ctrl_proto>, input: ~str) {
45 let mut intermediates = HashMap::new();
47 fn emit(im: &mut HashMap<~str, int>,
48 ctrl: Sender<ctrl_proto>, key: ~str,
50 if im.contains_key(&key) {
53 let (tx, rx) = channel();
54 println!("sending find_reducer");
55 ctrl.send(find_reducer(Vec::from_slice(key.as_bytes()), tx));
56 println!("receiving");
62 let ctrl_clone = ctrl.clone();
63 ::map(input, |a,b| emit(&mut intermediates, ctrl.clone(), a, b) );
64 ctrl_clone.send(mapper_done);
67 pub fn map_reduce(inputs: Vec<~str>) {
68 let (tx, rx) = channel();
70 // This task becomes the master control task. It spawns others
73 let mut reducers: HashMap<~str, int>;
75 reducers = HashMap::new();
77 start_mappers(tx, inputs.clone());
79 let mut num_mappers = inputs.len() as int;
81 while num_mappers > 0 {
83 mapper_done => { num_mappers -= 1; }
84 find_reducer(k, cc) => {
86 match reducers.find(&str::from_utf8(k.as_slice()).unwrap()
88 Some(&_c) => { c = _c; }
99 map_reduce::map_reduce(vec!(~"../src/test/run-pass/hashmap-memory.rs"));