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.
13 A somewhat reduced test case to expose some Valgrind issues.
15 This originally came from the word-count benchmark.
18 pub fn map(filename: String, emit: map_reduce::putter) {
19 emit(filename, "1".to_string());
23 use std::collections::HashMap;
24 use std::sync::mpsc::{channel, Sender};
26 use std::thread::Thread;
28 pub type putter<'a> = |String, String|: 'a;
30 pub type mapper = extern fn(String, putter);
32 enum ctrl_proto { find_reducer(Vec<u8>, Sender<int>), mapper_done, }
34 fn start_mappers(ctrl: Sender<ctrl_proto>, inputs: Vec<String>) {
35 for i in inputs.iter() {
36 let ctrl = ctrl.clone();
38 Thread::spawn(move|| map_task(ctrl.clone(), i.clone()) ).detach();
42 fn map_task(ctrl: Sender<ctrl_proto>, input: String) {
43 let mut intermediates = HashMap::new();
45 fn emit(im: &mut HashMap<String, int>,
46 ctrl: Sender<ctrl_proto>, key: String,
48 if im.contains_key(&key) {
51 let (tx, rx) = channel();
52 println!("sending find_reducer");
53 ctrl.send(ctrl_proto::find_reducer(key.as_bytes().to_vec(), tx)).unwrap();
54 println!("receiving");
55 let c = rx.recv().unwrap();
60 let ctrl_clone = ctrl.clone();
61 ::map(input, |a,b| emit(&mut intermediates, ctrl.clone(), a, b) );
62 ctrl_clone.send(ctrl_proto::mapper_done).unwrap();
65 pub fn map_reduce(inputs: Vec<String>) {
66 let (tx, rx) = channel();
68 // This task becomes the master control task. It spawns others
71 let mut reducers: HashMap<String, int>;
73 reducers = HashMap::new();
75 start_mappers(tx, inputs.clone());
77 let mut num_mappers = inputs.len() as int;
79 while num_mappers > 0 {
80 match rx.recv().unwrap() {
81 ctrl_proto::mapper_done => { num_mappers -= 1; }
82 ctrl_proto::find_reducer(k, cc) => {
84 match reducers.get(&str::from_utf8(
85 k.as_slice()).unwrap().to_string()) {
86 Some(&_c) => { c = _c; }
97 map_reduce::map_reduce(
98 vec!("../src/test/run-pass/hashmap-memory.rs".to_string()));