pmap 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pmap.rb +42 -39
- data/lib/pmap/version.rb +1 -1
- data/test/pmap_test.rb +2 -0
- metadata +3 -3
data/lib/pmap.rb
CHANGED
@@ -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
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
data/lib/pmap/version.rb
CHANGED
data/test/pmap_test.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: pmap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
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-
|
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.
|
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"
|