Created
October 14, 2012 09:39
-
-
Save arashbm/3888115 to your computer and use it in GitHub Desktop.
MapReduce Concept With Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is our given particle | |
given_particle = { :x => 0.0, :y => 0.0, :m => 1} | |
# Generating random data | |
particles = Array.new | |
50.times do | |
# rand is a function that generates random number between 0 and 1 | |
x = (rand - 0.5) * 1000 | |
y= (rand - 0.5) * 1000 | |
particles << { :x => x, :y => y * 1000, :m => rand * 5 } | |
end | |
# Map: | |
# now our particles array contains 50 random particles | |
# Here we are just slicing our list of particles to sub lists of 10, then | |
# putting these sublists in a queue. then we append an empty total_force vector | |
# (well, a hash actually!) to each slice (sub-problem). the empty hash will be | |
# filled by the worker who solves the sub problem. | |
# | |
# Each member of the queue will be served to one worker | |
job_queue = particles.each_slice(10).to_a | |
results = Array.new | |
# Here it will serve every request from workers from job queue and fetch the results | |
forces << serve_worker({ :data => job_queue.pull, :given => given_particle }) until job_queue.empty? | |
# Reduce | |
# | |
# To calculate final results we just sum over x and y of forces: | |
total_force = { :x => forces.map(&:x).inject(:+), | |
:y => forces.map(&:y).inject(:+) } | |
# then we print the results on screen | |
p total_force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solving Sub-problems | |
# | |
# Workers will run this block once for every sub problem. It will fill the | |
# empty result vector. | |
G = 1.0 | |
until sub_prob = get_a_sub_problem_from_master | |
forces = Array.new | |
given_particle = sub_prob[:given] | |
sub_prob[:data].each do |particle| | |
deltax = particle[:x] - given_particle[:x] | |
deltay = particle[:y] - given_particle[:y] | |
distance2 = deltax**2 + deltay**2 | |
forces << { :x => ((G * particle[:m] * given_particle[:m] * deltax) / distance2**(3.0/2)), | |
:y => ((G * particle[:m] * given_particle[:m] * deltay) / distance2**(3.0/2)) } | |
end | |
send_result_to_master({ :x => forces.map(&:x).inject(:+), :y => forces.map(&:y).inject(:+)}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment