GithubHelp home page GithubHelp logo

peach's Introduction

Parallel Each  (for ruby with threads) 

It is pretty common to have iterations over Arrays that can be safely run in parallel. With multicore chips becoming pretty common, single threaded processing is about as cool as Pog. Unfortunately, standard Ruby hates real threads pretty hardcore at the present time; however, for some ruby projects alternate VMs like  JRuby do give multicores some lovin'. Peach exists to make this power simple to use with minimal code changes. 

Functions like map, each, and delete_if are often used in a functional, side-effect free style. If the operation in the block is computationally intense, performance can often be gained by multithreading the process. That's where Peach comes in. In the simplest case, you are one letter away from harnessing the power of parallelism and unlocking the secret of a guilt-free tan. At this stage, the goggles are purely optional. 

Using Peach

Suppose you are going about your day job hacking away at code for the WOPR when you stumble upon the code: 

cities.each {|city| thermonuclear_war(city)}
        
Clearly, the only winning move is to declare war in parallel. With Peach, the new code is: 
require 'peach'

cities.peach {|city| thermonuclear_war(city)}
        
 Requiring peach.rb monkey patches Array into submission. Currently Peach provides peach, pmap, and pdelete_if. Each of these functions takes an optional argument n, which represents the desired number of worker threads with the default being one thread per Array element. For cheaper operations on a large number of elements, you probably want to set n to something reasonably low. 

(0...10000).to_a.pmap(4) {|x| process(x)}
        
 Constructing the threads and adding on a few layers of indirection does add a bit of overhead to the iteration especially on MRI. Keep this in mind and remember to benchmark when unsure. 

peach's People

Contributors

magnusmorton avatar schleyfox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

peach's Issues

block is executed too often

ruby -e 'require "peach"; (1..10).to_a.peach{|x| sleep 0.01*x; puts x; sleep 1}'

Expected result:

1
2
3
4
5
6
7
8
9
10

Actual result:

1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
1
7
8
9
10

Reproduced with:
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]
jruby 1.5.2 (ruby 1.8.7 patchlevel 249) (2010-08-20 1c5e29d) (OpenJDK 64-Bit Server VM 1.6.0_0) [amd64-java]

Apply to Hash as well as Array?

It'd be great if I could use .peach on instances of Hash (for Hash#each, Hash#each_key, Hash#each_value and Hash#each_pair) as well as Array.

wrong number of parallel threads when number of elements is relatively small compared to degree of parallelism

hi!

thanks a lot for peach, i think it's a great way to parallelize things, but there's a bug when the number of elements to be looped is less than 2*threads:

#!/usr/bin/env ruby

require 'peach'


@max_threads=10
@elements=19

exports = Array.new
puts "start"


$i = 0;
$num = @elements;

while $i < $num  do
    exports.push($i)
   $i +=1;
end

puts "array initialized"

def do_export(num)
    print " |export #{num} "
    %x(sleep 5)
end

puts "max_threads: #{@max_threads}"

exports.peach(@max_threads) do |export|
    do_export(export)
end

this code will execute 19 threads instead of 10.

Clean monkeypatching

Add a unless method_defined? ... at the end of each def so it's sure you don't overwrite anything. Oh, and make _peach_pool private and remove the _ prefix - too much javascript here :-)

MRI & Threads

The README is a little outdated now, as Ruby 2.0 has better support for threads.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.