parallel 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,11 @@
1
1
  Run any kind of code in parallel Processes or Threads, to speedup computation by factor #{your_cpus} X.
2
2
 
3
3
  - Child processes are killed when your main process is killed through Ctrl+c or kill -2
4
+ - Processes/threads are workers, they grab the next piece of work when they finish
4
5
 
5
6
  Install
6
7
  =======
7
- sudo gem install grosser-parallel -s http://gems.github.com/
8
+ sudo gem install parallel
8
9
 
9
10
  Usage
10
11
  =====
@@ -19,7 +20,6 @@ Usage
19
20
  - Global data can be modified
20
21
  - No extra memory used
21
22
 
22
- Map-Reduce-Style
23
23
  # 2 CPUs -> finished after 2 runs (a,b + c)
24
24
  results = Parallel.map(['a','b','c']) do |one_letter|
25
25
  expensive_calculation(letter)
@@ -31,22 +31,8 @@ Map-Reduce-Style
31
31
  # 3 Threads -> finished after 1 run
32
32
  results = Parallel.map(['a','b','c'], :in_threads=>3){|one_letter| ... }
33
33
 
34
-
35
- Normal
36
- #i -> 0...number_of_your_cpus
37
- results = Parallel.in_processes do |i|
38
- expensive_computation(data[i])
39
- end
40
-
41
- #i -> 0...4
42
- results = Parallel.in_processes(4) do |i|
43
- expensive_computation(data[i])
44
- end
45
-
46
- # Threads
47
- results = Parallel.in_threads(4) do |i|
48
- blocking_computation(data[i])
49
- end
34
+ Same can be done with `each`
35
+ Parallel.each(['a','b','c']){|one_letter| ... }
50
36
 
51
37
  TODO
52
38
  ====
data/Rakefile CHANGED
@@ -5,7 +5,6 @@ task :default do
5
5
  system("spec #{options} #{files}")
6
6
  end
7
7
 
8
- # fake task so that rubyforge:release works
9
8
  task :rdoc do
10
9
  end
11
10
 
@@ -20,6 +19,13 @@ begin
20
19
  gem.authors = ["Michael Grosser"]
21
20
  gem.rubyforge_project = 'parallel'
22
21
  end
22
+
23
+ # fake task so that rubyforge:release works
24
+ task :rdoc do
25
+ `mkdir rdoc`
26
+ `echo documentation is at http://github.com/grosser/#{project_name} > rdoc/README.rdoc`
27
+ end
28
+
23
29
  Jeweler::RubyforgeTasks.new
24
30
  rescue LoadError
25
31
  puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -1,3 +1,5 @@
1
+ require 'thread' # to get Thread.exclusive
2
+
1
3
  class Parallel
2
4
  VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
3
5
 
@@ -51,9 +53,12 @@ class Parallel
51
53
  out.map{|x| Marshal.load(x) } # Deserialize results
52
54
  end
53
55
 
54
- def self.map(array, options = {})
55
- require 'thread' # to get Thread.exclusive
56
+ def self.each(array, options={}, &block)
57
+ map(array, options, &block)
58
+ array
59
+ end
56
60
 
61
+ def self.map(array, options = {})
57
62
  if options[:in_threads]
58
63
  method = :in_threads
59
64
  size = options[method]
@@ -89,6 +94,7 @@ class Parallel
89
94
 
90
95
  private
91
96
 
97
+ # split an array into groups of size items
92
98
  def self.in_groups_of(array, size)
93
99
  results = []
94
100
  loop do
@@ -102,7 +108,7 @@ class Parallel
102
108
  results
103
109
  end
104
110
 
105
- #handle user interrup (Ctrl+c)
111
+ #handle user interrupt (Ctrl+c)
106
112
  def self.kill_on_ctrl_c(pids)
107
113
  Signal.trap :SIGINT do
108
114
  $stderr.puts 'Parallel execution interrupted, exiting ...'
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{parallel}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2009-10-04}
12
+ s.date = %q{2009-10-11}
13
13
  s.email = %q{grosser.michael@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.markdown"
@@ -20,6 +20,8 @@ Gem::Specification.new do |s|
20
20
  "VERSION",
21
21
  "lib/parallel.rb",
22
22
  "parallel.gemspec",
23
+ "rdoc/README.rdoc",
24
+ "spec/cases/parallel_each.rb",
23
25
  "spec/cases/parallel_influence_outside_data.rb",
24
26
  "spec/cases/parallel_map.rb",
25
27
  "spec/cases/parallel_map_sleeping.rb",
@@ -49,7 +51,8 @@ Gem::Specification.new do |s|
49
51
  "spec/cases/parallel_map_sleeping.rb",
50
52
  "spec/cases/parallel_map_uneven.rb",
51
53
  "spec/cases/parallel_with_detected_cpus.rb",
52
- "spec/cases/parallel_map.rb"
54
+ "spec/cases/parallel_map.rb",
55
+ "spec/cases/parallel_each.rb"
53
56
  ]
54
57
 
55
58
  if s.respond_to? :specification_version then
@@ -0,0 +1 @@
1
+ documentation is at http://github.com/grosser/parallel
@@ -0,0 +1,9 @@
1
+ require 'spec/spec_helper.rb'
2
+ STDOUT.sync = true # otherwise results can go weird...
3
+
4
+ x = ['a','b','c','d']
5
+ result = Parallel.each(x) do |x|
6
+ sleep 0.1 if x == 'a'
7
+ print "-#{x}-"
8
+ end
9
+ print result * ' '
@@ -90,6 +90,12 @@ describe Parallel do
90
90
  end
91
91
  end
92
92
 
93
+ describe :each do
94
+ it "returns original array, works like map" do
95
+ `ruby spec/cases/parallel_each.rb`.should == '-b--c--d--a-a b c d'
96
+ end
97
+ end
98
+
93
99
  describe :in_groups_of do
94
100
  it "works for empty" do
95
101
  Parallel.send(:in_groups_of, [], 3).should == []
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parallel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-04 00:00:00 +02:00
12
+ date: 2009-10-11 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -27,6 +27,8 @@ files:
27
27
  - VERSION
28
28
  - lib/parallel.rb
29
29
  - parallel.gemspec
30
+ - rdoc/README.rdoc
31
+ - spec/cases/parallel_each.rb
30
32
  - spec/cases/parallel_influence_outside_data.rb
31
33
  - spec/cases/parallel_map.rb
32
34
  - spec/cases/parallel_map_sleeping.rb
@@ -78,3 +80,4 @@ test_files:
78
80
  - spec/cases/parallel_map_uneven.rb
79
81
  - spec/cases/parallel_with_detected_cpus.rb
80
82
  - spec/cases/parallel_map.rb
83
+ - spec/cases/parallel_each.rb