eldritch 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1cbab97dd7492c7c19829d608f555e6cd1660901
4
- data.tar.gz: a05f7a551bc3c73736f86846b2fced0f51000e67
3
+ metadata.gz: 126e27cd07d65470ca6636e9fdee32863141f810
4
+ data.tar.gz: df821431679824457c23f5970d779b907d9371c6
5
5
  SHA512:
6
- metadata.gz: 679b8be6668f63dbd2b688e94b64f30f58ec858d0d6179ddc06ccc3e434ddd4973214bf7af41846849d970888494d2c8788d07ae11e859f46949416ebedb1351
7
- data.tar.gz: 4478f50cf4c09996245b2407007287a03368731818bdaeb466e7cd6ee6602ff321e6fc7083023f622deee32fe521e6d3a16d766809455f31fda2eef8afd7f452
6
+ metadata.gz: 4f8fe3201d12712f927af4e21a8939feaaf4221e293b43d3581a55376ead5ad364329cc7aba43ab4d8896338777e386630ea1b6f40309469f48d37b442f88e87
7
+ data.tar.gz: 2130979b69b7099a626b06c9e88a9d3640f21c0bc4cdfde89b1d3b0b98f3230677a058b6d5a01dd522a2d5f772cbce7c27ee3c0d8a53c2684e91dfc393a6e6db
data/README.md CHANGED
@@ -99,6 +99,28 @@ together do |group|
99
99
  end
100
100
  ```
101
101
 
102
+ A note on GIL
103
+ -------------
104
+
105
+ MRI has this nasty little feature called a _GIL_ or _Global Interpreter Lock_. This lock makes it so that only one
106
+ thread can run at a time. Let's say that you have 4 cores, running threaded code on MRI will only make use of 1 core.
107
+ Sometimes, you might not gain a speed boost if you make code parallel. This could the case even if theory says otherwise.
108
+
109
+ Not all ruby implementations use a _GIL_. For example, jRuby does not use a _GIL_. The problem with using jRuby, is that
110
+ this gem requires ruby >= 2.1.0 and jRuby only supports up to 1.9.3 (as of writing this).
111
+
112
+ You will probably see a speed boost if your code does a lot of IO or anything that's blocking. In that case running on a
113
+ single core is not that much of a hindrance, because most of the threads will be blocked and your code should run more
114
+ often.
115
+
116
+ Running examples
117
+ ----------------
118
+
119
+ If you installed eldritch with gem, you can just run the examples directly. If you are running them against a clone of
120
+ this repository you need to add `lib/` to the include path.
121
+
122
+ $ ruby -Ilib examples/the_example.rb
123
+
102
124
  Installation
103
125
  ------------
104
126
 
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+ require 'eldritch'
3
+
4
+ def print_matrix(matrix)
5
+ matrix.each do |line|
6
+ formatted = line.map { |i| '% 10.5f' % i }
7
+ puts "[ #{formatted.join(', ')} ]"
8
+ end
9
+ end
10
+
11
+ def create_matrix(n, m)
12
+ Array.new(n+2).map{[-1] * (m+2)}
13
+ end
14
+
15
+ matrix = [
16
+ [-1, -1, -1, -1, -1, -1],
17
+ [-1, 1, 2, 3, 4, -1],
18
+ [-1, 1, 2, 3, 4, -1],
19
+ [-1, 1, 2, 3, 4, -1],
20
+ [-1, 1, 2, 3, 4, -1],
21
+ [-1, -1, -1, -1, -1, -1]
22
+ ]
23
+
24
+ epsilon = 0.001
25
+
26
+ print_matrix matrix
27
+ puts
28
+
29
+ height = matrix.length - 2
30
+ width = matrix.first.length - 2
31
+
32
+ iteration = 1
33
+ begin
34
+ next_matrix = create_matrix(height, width)
35
+
36
+ together do
37
+ (1..height).each do |r|
38
+ async do
39
+ (1..width).each do |c|
40
+ neighbors = [c-1, c+1].product([r-1, r+1]).map{|i, j| matrix[i][j]}
41
+ next_matrix[r][c] = neighbors.reduce(:+) / 4.0
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ diff = matrix.flatten.zip(next_matrix.flatten).map{|i, j| (i - j).abs}.max
48
+
49
+ matrix = next_matrix
50
+
51
+ print_matrix matrix
52
+ puts "iteration = #{iteration}; diff = #{diff}"
53
+ puts
54
+
55
+ iteration += 1
56
+ end while diff > epsilon
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ require 'eldritch'
3
+
4
+ def merge(a, b)
5
+ merged = []
6
+
7
+ until b.empty? || a.empty? do
8
+ if a.first <= b.first
9
+ merged += a.take_while { |i| (i <= b.first) }
10
+ a = a.drop_while { |i| i <= b.first }
11
+ else
12
+ merged += b.take_while { |i| i <= a.first }
13
+ b = b.drop_while { |i| i <= a.first }
14
+ end
15
+ end
16
+ merged + (a.empty? ? b : a)
17
+ end
18
+
19
+ def parallel_merge_sort(array)
20
+ return array if array.size <= 1
21
+
22
+ mid = (array.length / 2).floor
23
+
24
+ first = async { parallel_merge_sort(array.slice(0, mid)) }
25
+ second = parallel_merge_sort(array.slice(mid, array.length - mid))
26
+
27
+ merge(second, first.value)
28
+ end
29
+
30
+ nums = (1..25).to_a.shuffle
31
+
32
+ puts "values: #{nums.join(', ')}"
33
+ puts 'sorting...'
34
+ sorted = parallel_merge_sort(nums)
35
+ puts "values: #{sorted.join(', ')}"
data/lib/eldritch/task.rb CHANGED
@@ -33,6 +33,7 @@ module Eldritch
33
33
  # Waits for the task to complete
34
34
  def wait
35
35
  @thread.join
36
+ @thread.task = nil
36
37
  end
37
38
 
38
39
  # The return value of the task
@@ -1,3 +1,3 @@
1
1
  module Eldritch
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
data/spec/task_spec.rb CHANGED
@@ -46,6 +46,13 @@ describe Eldritch::Task do
46
46
  expect(task.thread).to receive(:join)
47
47
  task.wait
48
48
  end
49
+
50
+ it 'should set the thread task to nil' do
51
+ task.start
52
+
53
+ expect(thread).to receive(:task=).with(nil)
54
+ task.wait
55
+ end
49
56
  end
50
57
 
51
58
  describe '#value' do
@@ -56,6 +63,13 @@ describe Eldritch::Task do
56
63
  task.value
57
64
  end
58
65
 
66
+ it 'should set the thread task to nil' do
67
+ task.start
68
+
69
+ expect(thread).to receive(:task=).with(nil)
70
+ task.value
71
+ end
72
+
59
73
  it 'should return the value' do
60
74
  task.value = 42
61
75
  task.start
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eldritch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boris Bera
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-23 00:00:00.000000000 Z
12
+ date: 2014-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -86,7 +86,9 @@ files:
86
86
  - eldritch.gemspec
87
87
  - examples/async_block.rb
88
88
  - examples/async_method_with_class.rb
89
+ - examples/jacobi.rb
89
90
  - examples/matrix_multiplication.rb
91
+ - examples/parallel_sort.rb
90
92
  - examples/password_cracker.rb
91
93
  - examples/simple_async_method.rb
92
94
  - examples/together_simple.rb