|
| 1 | +/** |
| 2 | + A somewhat reduced test case to expose some Valgrind issues. |
| 3 | +
|
| 4 | + This originally came from the word-count benchmark. |
| 5 | +*/ |
| 6 | + |
| 7 | +use std; |
| 8 | + |
| 9 | +import std::io; |
| 10 | +import option = std::option::t; |
| 11 | +import std::option::some; |
| 12 | +import std::option::none; |
| 13 | +import std::str; |
| 14 | +import std::vec; |
| 15 | +import std::map; |
| 16 | + |
| 17 | +fn map(str filename, map_reduce::putter emit) { |
| 18 | + emit(filename, "1"); |
| 19 | +} |
| 20 | + |
| 21 | +mod map_reduce { |
| 22 | + export putter; |
| 23 | + export mapper; |
| 24 | + export map_reduce; |
| 25 | + |
| 26 | + type putter = fn(str, str) -> (); |
| 27 | + |
| 28 | + type mapper = fn(str, putter); |
| 29 | + |
| 30 | + tag ctrl_proto { |
| 31 | + find_reducer(str, chan[int]); |
| 32 | + mapper_done; |
| 33 | + } |
| 34 | + |
| 35 | + fn start_mappers(chan[ctrl_proto] ctrl, |
| 36 | + vec[str] inputs) { |
| 37 | + for(str i in inputs) { |
| 38 | + spawn map_task(ctrl, i); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fn map_task(chan[ctrl_proto] ctrl, |
| 43 | + str input) { |
| 44 | + |
| 45 | + auto intermediates = map::new_str_hash(); |
| 46 | + |
| 47 | + fn emit(&map::hashmap[str, int] im, |
| 48 | + chan[ctrl_proto] ctrl, |
| 49 | + str key, str val) { |
| 50 | + auto c; |
| 51 | + alt(im.find(key)) { |
| 52 | + case(some(?_c)) { |
| 53 | + c = _c |
| 54 | + } |
| 55 | + case(none) { |
| 56 | + auto p = port(); |
| 57 | + log_err "sending find_reducer"; |
| 58 | + ctrl <| find_reducer(key, chan(p)); |
| 59 | + log_err "receiving"; |
| 60 | + p |> c; |
| 61 | + log_err c; |
| 62 | + im.insert(key, c); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + map(input, bind emit(intermediates, ctrl, _, _)); |
| 68 | + ctrl <| mapper_done; |
| 69 | + } |
| 70 | + |
| 71 | + fn map_reduce (vec[str] inputs) { |
| 72 | + auto ctrl = port[ctrl_proto](); |
| 73 | + |
| 74 | + // This task becomes the master control task. It spawns others |
| 75 | + // to do the rest. |
| 76 | + |
| 77 | + let map::hashmap[str, int] reducers; |
| 78 | + |
| 79 | + reducers = map::new_str_hash(); |
| 80 | + |
| 81 | + start_mappers(chan(ctrl), inputs); |
| 82 | + |
| 83 | + auto num_mappers = vec::len(inputs) as int; |
| 84 | + |
| 85 | + while(num_mappers > 0) { |
| 86 | + auto m; |
| 87 | + ctrl |> m; |
| 88 | + |
| 89 | + alt(m) { |
| 90 | + case(mapper_done) { num_mappers -= 1; } |
| 91 | + case(find_reducer(?k, ?cc)) { |
| 92 | + auto c; |
| 93 | + alt(reducers.find(k)) { |
| 94 | + case(some(?_c)) { c = _c; } |
| 95 | + case(none) { |
| 96 | + c = 0; |
| 97 | + } |
| 98 | + } |
| 99 | + cc <| c; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fn main(vec[str] argv) { |
| 107 | + map_reduce::map_reduce(["../src/test/run-pass/hashmap-memory.rs"]); |
| 108 | +} |
0 commit comments