pmap 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,47 +1,50 @@
1
-
2
- # I'd prefer to create this as a module named "Pmap" and then poke
3
- # "Pmap" into "Enumerable". I haven't figured out how to do it.
4
- # So, I directly reopen "Enumerable" and add "p" methods...
5
-
6
1
  require 'thread' unless defined?(Mutex)
7
2
 
8
3
  # Global variable for the default thread pool size.
9
4
  $pmap_default_thread_count ||= 64
10
5
 
11
- module Enumerable
12
- # Parallel "map" for any Enumerable.
13
- # Requires a block of code to run for each Enumerable item.
14
- # [thread_count] is number of threads to create. Optional.
15
- def pmap(thread_count=nil, &proc)
16
- raise ArgumentError, "thread_count must be at least one." unless
17
- thread_count.nil? or (thread_count.respond_to?(:>=) and thread_count >= 1)
18
- # This seems overly fussy... (code smell)
19
- in_array = self.to_a # I'm not sure how expensive this is...
20
- size = in_array.size
21
- thread_count = [thread_count||$pmap_default_thread_count, size].min
22
- out_array = Array.new(size)
23
- semaphore = Mutex.new
24
- index = -1 # Our use of index is protected by semaphore
25
- threads = (0...thread_count).map {
26
- Thread.new {
27
- i = nil
28
- while (semaphore.synchronize {i = (index += 1)}; i < size)
29
- out_array[i] = yield(in_array[i])
30
- end
31
- }
32
- }
33
- threads.each {|t| t.join}
34
- out_array
35
- end
6
+ module PMap
7
+ def self.included(base)
8
+ base.class_eval do
9
+ # Parallel "map" for any Enumerable.
10
+ # Requires a block of code to run for each Enumerable item.
11
+ # [thread_count] is number of threads to create. Optional.
12
+ def pmap(thread_count=nil, &proc)
13
+ raise ArgumentError, "thread_count must be at least one." unless
14
+ thread_count.nil? or (thread_count.respond_to?(:>=) and thread_count >= 1)
15
+ # This seems overly fussy... (code smell)
16
+ in_array = self.to_a # I'm not sure how expensive this is...
17
+ size = in_array.size
18
+ thread_count = [thread_count||$pmap_default_thread_count, size].min
19
+ out_array = Array.new(size)
20
+ semaphore = Mutex.new
21
+ index = -1 # Our use of index is protected by semaphore
22
+ threads = (0...thread_count).map {
23
+ Thread.new {
24
+ i = nil
25
+ while (semaphore.synchronize {i = (index += 1)}; i < size)
26
+ out_array[i] = yield(in_array[i])
27
+ end
28
+ }
29
+ }
30
+ threads.each {|t| t.join}
31
+ out_array
32
+ end
36
33
 
37
- # Parallel "each" for any Enumerable.
38
- # Requires a block of code to run for each Enumerable item.
39
- # [thread_count] is number of threads to create. Optional.
40
- def peach(thread_count=nil, &proc)
41
- # This is doing some extra work: building a return array that is
42
- # thrown away. How can I share the core code of "pmap" here and omit
43
- # the output array creation?
44
- pmap(thread_count, &proc)
45
- self
34
+ # Parallel "each" for any Enumerable.
35
+ # Requires a block of code to run for each Enumerable item.
36
+ # [thread_count] is number of threads to create. Optional.
37
+ def peach(thread_count=nil, &proc)
38
+ # This is doing some extra work: building a return array that is
39
+ # thrown away. How can I share the core code of "pmap" here and omit
40
+ # the output array creation?
41
+ pmap(thread_count, &proc)
42
+ self
43
+ end
44
+ end
46
45
  end
47
46
  end
47
+
48
+ module Enumerable
49
+ include PMap
50
+ end
@@ -1,3 +1,3 @@
1
1
  module Pmap
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ $LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
4
+
3
5
  require 'test/unit'
4
6
  require 'rubygems'
5
7
  require 'pmap'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pmap
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Bruce Adams
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-03 00:00:00 -05:00
13
+ date: 2011-05-09 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  requirements: []
58
58
 
59
59
  rubyforge_project:
60
- rubygems_version: 1.5.0
60
+ rubygems_version: 1.6.2
61
61
  signing_key:
62
62
  specification_version: 3
63
63
  summary: "Add parallel methods into Enumerable: pmap and peach"