peach 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ peach (0.5)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ rake (0.9.2.2)
10
+ shoulda (3.0.1)
11
+ shoulda-context (~> 1.0.0)
12
+ shoulda-matchers (~> 1.0.0)
13
+ shoulda-context (1.0.0)
14
+ shoulda-matchers (1.0.0)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ peach!
21
+ rake
22
+ shoulda
data/Rakefile CHANGED
@@ -1,3 +1,6 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
1
4
  require 'rake'
2
5
  require 'rake/testtask'
3
6
 
@@ -1,70 +1,61 @@
1
+ # monkey patch Enumerable by reopening it. Enumerable.send(:include, Peach)
2
+ # doesn't seem to work as it should.
1
3
  module Peach
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!
4
+ class EmptyThreadPoolError < RuntimeError
5
+ end
6
+ end
7
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))}
8
+ module Enumerable
9
+ def _peach_run(pool = nil, &b)
10
+ pool ||= $peach_default_threads || count
11
+ unless pool >= 1
12
+ raise Peach::EmptyThreadPoolError, "Thread pool size less than one"
13
+ end
14
+ div = (count/pool).to_i # should already be integer
15
+ div = 1 unless div >= 1 # each thread better do something!
16
+
17
+ threads = []
18
+ each_slice(div).with_index do |slice, idx|
19
+ threads << Thread.new(slice) do |thread_slice|
20
+ yield thread_slice, idx, div
11
21
  end
12
22
  end
13
- threads.each { |t| t.join }
23
+ threads.each{|t| t.join }
14
24
  self
15
25
  end
16
26
 
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)
27
+ def peach(pool = nil, &b)
28
+ _peach_run(pool) do |thread_slice, idx, div|
29
+ thread_slice.each{|elt| yield elt}
30
+ end
31
+ end
24
32
 
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))}
33
+ def pmap(pool = nil, &b)
34
+ result = Array.new(count)
35
+ lock = Mutex.new
36
+
37
+ _peach_run(pool) do |thread_slice, idx, div|
38
+ thread_slice.each_with_index do |elt, offset|
39
+ local_result = yield elt
40
+ lock.synchronize do
41
+ result[(idx*div)+offset] = local_result
42
+ end
28
43
  end
29
44
  end
30
- threads.each { |t| t.join }
31
45
  result
32
46
  end
33
47
 
34
- def pselect(n = nil, &b)
35
- peach_run(:select, b, n)
36
- end
37
-
38
-
48
+ def pselect(pool = nil, &b)
49
+ results, result = [],[]
50
+ lock = Mutex.new
39
51
 
40
- protected
41
- def peach_run(meth, b, n = nil)
42
- threads, results, result = [],[],[]
43
- peach_divvy(n).each_with_index do |x,i|
44
- threads << Thread.new { results[i] = x.send(meth, &b)}
52
+ _peach_run(pool) do |thread_slice, idx, div|
53
+ local_result = thread_slice.select(&b)
54
+ lock.synchronize do
55
+ results[idx] = local_result
56
+ end
45
57
  end
46
- threads.each {|t| t.join }
47
58
  results.each {|x| result += x if x}
48
59
  result
49
60
  end
50
-
51
- def peach_divvy(n = nil)
52
- return [] if size == 0
53
-
54
- n ||= $peach_default_threads || size
55
- n = size if n > size
56
-
57
- lists = []
58
-
59
- div = (size/n).floor
60
- offset = 0
61
- for i in (0...n-1)
62
- lists << slice(offset, div)
63
- offset += div
64
- end
65
- lists << slice(offset...size)
66
- lists
67
- end
68
61
  end
69
-
70
- Array.send(:include, Peach)
@@ -0,0 +1,3 @@
1
+ module Peach
2
+ VERSION = "0.5"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "peach/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'peach'
7
+ s.version = Peach::VERSION
8
+ s.authors = ['Ben Hughes']
9
+ s.email = 'ben@pixelmachine.org'
10
+ s.summary = 'Parallel Each and other parallel things'
11
+ s.homepage = 'http://peach.rubyforge.org'
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_development_dependency('rake')
19
+ s.add_development_dependency('shoulda')
20
+ end
@@ -10,12 +10,12 @@ class PeachTest < Test::Unit::TestCase
10
10
  normal_f = f.to_s[1..-1].to_sym
11
11
 
12
12
  setup do
13
- @data = [1, 2, 3, 5, 8]
13
+ @data = [1, 2, 3, 5, 8]*10001
14
14
  @block = lambda{|i| i**2}
15
15
  end
16
16
  should "return the same result as #{normal_f}" do
17
17
  assert_equal @data.send(normal_f, &@block),
18
- @data.send(f, nil, &@block)
18
+ @data.send(f, 100, &@block)
19
19
  end
20
20
  end
21
21
  end
@@ -38,39 +38,4 @@ class PeachTest < Test::Unit::TestCase
38
38
  end
39
39
  end
40
40
 
41
- context "divvy" do
42
- setup do
43
- @data = [1, 2, 3, 4, 5]
44
- end
45
-
46
- context "on empty list" do
47
- should "return empty list" do
48
- assert_equal [], [].send(:peach_divvy)
49
- end
50
- end
51
-
52
- context "when n is nil" do
53
- should "put 1 element into each division" do
54
- assert_equal @data.size, @data.send(:peach_divvy).size
55
- end
56
- end
57
-
58
- context "when n is less than array size" do
59
- should "put create n divisions" do
60
- assert_equal 2, @data.send(:peach_divvy, 2).size
61
- end
62
-
63
- should "not lose any array elements" do
64
- assert_equal @data.size, @data.send(:peach_divvy, 2).inject(0) {|sum, i|
65
- sum + i.size
66
- }
67
- end
68
- end
69
-
70
- context "when n is greater than array size" do
71
- should "only create 'array size' divisions" do
72
- assert_equal @data.size, @data.send(:peach_divvy, 42).size
73
- end
74
- end
75
- end
76
41
  end
metadata CHANGED
@@ -1,70 +1,98 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: peach
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 4
8
- version: "0.4"
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.5'
5
+ prerelease:
9
6
  platform: ruby
10
- authors:
11
- - Ben Hughes
7
+ authors:
8
+ - Ben Hughes
12
9
  autorequire:
13
10
  bindir: bin
14
11
  cert_chain: []
15
-
16
- date: 2010-11-17 00:00:00 -05:00
17
- default_executable:
18
- dependencies: []
19
-
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
20
46
  description:
21
47
  email: ben@pixelmachine.org
22
48
  executables: []
23
-
24
49
  extensions: []
25
-
26
50
  extra_rdoc_files: []
27
-
28
- files:
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
51
+ files:
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE
55
+ - README
56
+ - Rakefile
57
+ - bn/peach_bn.rb
58
+ - bn/peach_test.rb
59
+ - lib/peach.rb
60
+ - lib/peach/version.rb
61
+ - peach.gemspec
62
+ - test/peach_test.rb
63
+ - test/test_helper.rb
64
+ - web/Peach.sketch.png
65
+ - web/index.html
40
66
  homepage: http://peach.rubyforge.org
41
67
  licenses: []
42
-
43
68
  post_install_message:
44
69
  rdoc_options: []
45
-
46
- require_paths:
47
- - lib
48
- required_ruby_version: !ruby/object:Gem::Requirement
49
- requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- segments:
53
- - 0
54
- version: "0"
55
- required_rubygems_version: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- segments:
60
- - 0
61
- version: "0"
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: 1425915303928506863
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ segments:
88
+ - 0
89
+ hash: 1425915303928506863
62
90
  requirements: []
63
-
64
91
  rubyforge_project:
65
- rubygems_version: 1.3.6
92
+ rubygems_version: 1.8.24
66
93
  signing_key:
67
94
  specification_version: 3
68
95
  summary: Parallel Each and other parallel things
69
- test_files: []
70
-
96
+ test_files:
97
+ - test/peach_test.rb
98
+ - test/test_helper.rb