Commits

Kelvin Jin  committed e15aa6d

implemented (untested) nbody code.

  • Participants
  • Parent commits 07aa38f

Comments (0)

Files changed (7)

File apps/inverted_index/inverted_index.ml

   if Array.length args < 3 then 
     Printf.printf "Usage: inverted_index <filename>"
   else begin
-    let filename = args.(1) in
+    let filename = args.(2) in
     let (_, results) = 
       Map_reduce.map_reduce "inverted_index" "mapper" "reducer" filename in
     print_reduce_results results end

File apps/inverted_index/reducer.ml

   match acc with
   | [] -> [v]
   | h::t -> if v = h then acc else v::acc in
-let indices = List.fold_left helper [] values in
+let indices = List.sort (List.fold_left helper [] values) in
     Program.set_output [string_of_int sum]

File apps/nbody/mapper.ml

+let ((_, (mass1, position1, _)), bodies) = Program.get_input() in
+let helper acc other =
+  let (_, (mass2, position2, _)) = other in
+  let dist = distance position1 position2 in
+  let g_force = (cBIG_G *. mass1) /. (dist *. dist) in
+  let direction = unit_vector position2 position1 in
+  (other, scale_point g_force direction)::acc in
+Program.set_output (List.fold_left helper []
+                   bodies)

File apps/nbody/reducer.ml

+let (key, values) = Program.get_input() in
+let helper acc v =
+  v_plus acc (unmarshal v) in
+let acceleration = List.fold_left helper (0., 0.) values in
+let result =
+  let (id, (mass, location, velocity)) = key in
+  let new_position =
+    v_plus (v_plus location velocity) (scale_point acceleration 0.5) in
+  let new_velocity = v_plus velocity acceleration in
+  (id, (mass, new_position, new_velocity)) in
+Program.set_output [result]

File apps/page_rank/mapper.ml

+let (key, value) = Program.get_input() in
+Program.set_output (List.fold_left (fun acc k -> (k, "1")::acc) []
+                   (List.map String.lowercase (Util.split_words value)))

File apps/page_rank/page_rank.ml

 let main (args : string array) : unit = 
   if Array.length args < 3 then
     Printf.printf "Usage: page_rank <num_iterations> <filename>"
-  else
-    failwith "You're not my mother. What kind of demon are you?"
+  else begin
+    let filename = args.(3) in
+    let (_, results) = 
+      Map_reduce.map_reduce "page_rank" "mapper" "reducer" filename in
+    print_reduce_results results end

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]