peach 0.2 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/peach.rb +28 -4
  2. data/test/peach_test.rb +22 -2
  3. metadata +33 -25
@@ -1,10 +1,34 @@
1
1
  module Peach
2
- def peach(n = nil, &b)
3
- peach_run(:each, b, n)
2
+ def peach(pool = nil, &b)
3
+ pool ||= $peach_default_threads || size
4
+ raise "Thread pool size less than one?" unless pool >= 1
5
+ div = (size/pool).to_i # should already be integer
6
+ div = 1 unless div >= 1 # each thread better do something!
7
+
8
+ threads = (0...size).step(div).map do |chunk|
9
+ Thread.new(chunk, [chunk+div,size].min) do |lower, upper|
10
+ (lower...upper).each{|j| yield(slice(j))}
11
+ end
12
+ end
13
+ threads.each { |t| t.join }
14
+ self
4
15
  end
5
16
 
6
- def pmap(n = nil, &b)
7
- peach_run(:map, b, n)
17
+ def pmap(pool = nil, &b)
18
+ pool ||= $peach_default_threads || size
19
+ raise "Thread pool size less than one?" unless pool >= 1
20
+ div = (size/pool).to_i # should already be integer
21
+ div = 1 unless div >= 1 # each thread better do something!
22
+
23
+ result = Array.new(size)
24
+
25
+ threads = (0...size).step(div).map do |chunk|
26
+ Thread.new(chunk, [chunk+div,size].min) do |lower, upper|
27
+ (lower...upper).each{|j| result[j] = yield(slice(j))}
28
+ end
29
+ end
30
+ threads.each { |t| t.join }
31
+ result
8
32
  end
9
33
 
10
34
  def pselect(n = nil, &b)
@@ -2,6 +2,8 @@ require File.join(File.dirname(__FILE__), "test_helper")
2
2
 
3
3
  require File.join(File.dirname(__FILE__), "..", "lib", "peach")
4
4
 
5
+ require 'thread'
6
+
5
7
  class PeachTest < Test::Unit::TestCase
6
8
  [:peach, :pmap, :pselect].each do |f|
7
9
  context "Parallel function #{f}" do
@@ -18,6 +20,24 @@ class PeachTest < Test::Unit::TestCase
18
20
  end
19
21
  end
20
22
 
23
+ [:peach, :pmap, :pselect].each do |f|
24
+ context "#{f}" do
25
+ [nil, 101, 99, 51, 49, 20, 5, 1].each do |pool|
26
+ should "invoke the block exactly once for each array item," +
27
+ " with thread pool size of #{pool.inspect}" do
28
+ source_array = (0...100).to_a
29
+ q = Queue.new()
30
+ source_array.send(f, pool) {|i| q.push(i)}
31
+ result_array = []
32
+ until q.empty?
33
+ result_array << q.pop
34
+ end
35
+ assert_equal source_array, result_array.sort
36
+ end
37
+ end
38
+ end
39
+ end
40
+
21
41
  context "divvy" do
22
42
  setup do
23
43
  @data = [1, 2, 3, 4, 5]
@@ -41,12 +61,12 @@ class PeachTest < Test::Unit::TestCase
41
61
  end
42
62
 
43
63
  should "not lose any array elements" do
44
- assert_equal @data.size, @data.send(:peach_divvy, 2).inject(0) {|sum, i|
64
+ assert_equal @data.size, @data.send(:peach_divvy, 2).inject(0) {|sum, i|
45
65
  sum + i.size
46
66
  }
47
67
  end
48
68
  end
49
-
69
+
50
70
  context "when n is greater than array size" do
51
71
  should "only create 'array size' divisions" do
52
72
  assert_equal @data.size, @data.send(:peach_divvy, 42).size
metadata CHANGED
@@ -1,15 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peach
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ version: "0.4"
5
9
  platform: ruby
6
10
  authors:
7
- - Ben Hughes
11
+ - Ben Hughes
8
12
  autorequire:
9
13
  bindir: bin
10
14
  cert_chain: []
11
15
 
12
- date: 2009-04-05 00:00:00 -04:00
16
+ date: 2010-11-17 00:00:00 -05:00
13
17
  default_executable:
14
18
  dependencies: []
15
19
 
@@ -22,41 +26,45 @@ extensions: []
22
26
  extra_rdoc_files: []
23
27
 
24
28
  files:
25
- - README
26
- - LICENSE
27
- - Rakefile
28
- - lib/peach.rb
29
- - bn/peach_bn.rb
30
- - bn/peach_test.rb
31
- - test/test_helper.rb
32
- - test/peach_test.rb
33
- - web/index.html
34
- - web/Peach.sketch.png
35
- has_rdoc: false
29
+ - README
30
+ - LICENSE
31
+ - Rakefile
32
+ - lib/peach.rb
33
+ - bn/peach_bn.rb
34
+ - bn/peach_test.rb
35
+ - test/test_helper.rb
36
+ - test/peach_test.rb
37
+ - web/index.html
38
+ - web/Peach.sketch.png
39
+ has_rdoc: true
36
40
  homepage: http://peach.rubyforge.org
41
+ licenses: []
42
+
37
43
  post_install_message:
38
44
  rdoc_options: []
39
45
 
40
46
  require_paths:
41
- - lib
47
+ - lib
42
48
  required_ruby_version: !ruby/object:Gem::Requirement
43
49
  requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: "0"
47
- version:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
48
55
  required_rubygems_version: !ruby/object:Gem::Requirement
49
56
  requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: "0"
53
- version:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
54
62
  requirements: []
55
63
 
56
64
  rubyforge_project:
57
- rubygems_version: 1.3.1
65
+ rubygems_version: 1.3.6
58
66
  signing_key:
59
- specification_version: 2
67
+ specification_version: 3
60
68
  summary: Parallel Each and other parallel things
61
69
  test_files: []
62
70