Commits

Kelvin Jin  committed 48d3d32

page_rank reducer should work, made nbody main function recursive

  • Participants
  • Parent commits 2433fdb

Comments (0)

Files changed (3)

-# Readme to be changed
+# Readme to be changed

File apps/nbody/nbody.ml

 
 (* Create a transcript of body positions for `steps` time steps *)
 let make_transcript (bodies : (string * body) list) (steps : int) : string = 
-  let bodies_ref = ref bodies in
-  let str = ref "" in
-  let nbody_map_reduce () =
-    let to_string_tuple (id, b) = (id, marshal b) in
-    let kv_pairs = List.map to_string_tuple !bodies_ref in
-    let mapped =
-      Map_reduce.map kv_pairs (marshal !bodies_ref) "apps/nbody/mapper.ml" in
-    let combined = Map_reduce.combine mapped in
-    let reduced =
-      Map_reduce.reduce combined "" "apps/nbody/reducer.ml" in
-    let unwrap (k, v) = 
-      match v with
-      | [] -> failwith "no body was ever recovered"
-      | h::_ -> (k, unmarshal h) in
-    List.map unwrap reduced in
-  str := string_of_bodies bodies;
-  for i = 1 to steps do
-    (bodies_ref := nbody_map_reduce (); 
-    str := string_of_bodies !bodies_ref) done;
-  !str
+  let rec nbody_map_reduce bodies steps =
+    if steps = 0 then string_of_bodies bodies
+    else
+      let to_string_tuple (id, b) = (id, marshal b) in
+      let kv_pairs = List.map to_string_tuple bodies in
+      let mapped =
+        Map_reduce.map kv_pairs (marshal bodies) "apps/nbody/mapper.ml" in
+      let combined = Map_reduce.combine mapped in
+      let reduced =
+        Map_reduce.reduce combined "" "apps/nbody/reducer.ml" in
+      let unwrap (k, v) = 
+        match v with
+        | [] -> failwith "no body was ever recovered"
+        | h::_ -> (k, unmarshal h) in
+      string_of_bodies bodies ^
+        (nbody_map_reduce (List.map unwrap reduced) (steps - 1)) in
+  nbody_map_reduce bodies steps
 
 let simulation_of_string = function
   | "binary_star" -> Simulations.binary_star

File apps/page_rank/reducer.ml

 let (key, values) = Program.get_input() in
-let sum = List.fold_left (fun acc v -> acc + (int_of_string v)) 0 values in
-    Program.set_output [string_of_int sum]
+let sum = List.fold_left (fun acc v -> acc +. (float_of_string v)) 0. values in
+    Program.set_output [string_of_float sum]