drake 0.8.4.1.1.0 → 0.8.4.1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,42 +0,0 @@
1
- #
2
- # Copyright (c) 2008 James M. Lawrence. All rights reserved.
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining a copy
5
- # of this software and associated documentation files (the "Software"), to deal
6
- # in the Software without restriction, including without limitation the rights
7
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- # copies of the Software, and to permit persons to whom the Software is
9
- # furnished to do so, subject to the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be included in
12
- # all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
- # THE SOFTWARE.
21
- #
22
-
23
- require 'rake/comp_tree/driver'
24
-
25
- module Rake end
26
- module Rake::CompTree
27
- class << self
28
- #
29
- # Build and run a new computation tree.
30
- #
31
- # A Driver instance is passed to the given block.
32
- #
33
- # Options hash:
34
- #
35
- # <tt>:node_class</tt> -- (Class) CompTree::Node subclass from
36
- # which nodes are created.
37
- #
38
- def build(opts = nil)
39
- yield Driver.new(opts)
40
- end
41
- end
42
- end
@@ -1,153 +0,0 @@
1
-
2
- require 'rake/comp_tree/algorithm'
3
- require 'rake/comp_tree/node'
4
- require 'rake/comp_tree/error'
5
-
6
- require 'thread'
7
-
8
- module Rake end
9
- module Rake::CompTree
10
- #
11
- # Driver is the main interface to the computation tree. It is
12
- # responsible for defining nodes and running computations.
13
- #
14
- class Driver
15
- include Algorithm
16
-
17
- #
18
- # Build and run a new computation tree.
19
- #
20
- # Options hash:
21
- #
22
- # <tt>:node_class</tt> -- (Class) CompTree::Node subclass from
23
- # which nodes are created.
24
- #
25
- def initialize(opts = nil)
26
- @node_class = (
27
- if opts and opts[:node_class]
28
- opts[:node_class]
29
- else
30
- Node
31
- end
32
- )
33
- @nodes = Hash.new
34
- end
35
-
36
- #
37
- # Name-to-node hash.
38
- #
39
- attr_reader :nodes
40
-
41
- #
42
- # Define a computation node.
43
- #
44
- # The first argument is the name of the node to define.
45
- # Subsequent arguments are the names of this node's children.
46
- #
47
- # The values of the child nodes are passed to the block. The
48
- # block returns the result of this node.
49
- #
50
- # In this example, a computation node named +area+ is defined
51
- # which depends on the nodes +width+ and +height+.
52
- #
53
- # driver.define(:area, :width, :height) { |width, height|
54
- # width*height
55
- # }
56
- #
57
- def define(*args, &block)
58
- parent_name = args.first
59
- children_names = args[1..-1]
60
-
61
- unless parent_name
62
- raise ArgumentError, "No name given for node"
63
- end
64
-
65
- #
66
- # retrieve or create parent and children
67
- #
68
- parent = @nodes[parent_name] || (
69
- @nodes[parent_name] = @node_class.new(parent_name)
70
- )
71
-
72
- if parent.function
73
- raise RedefinitionError, "Node `#{parent.name.inspect}' redefined."
74
- end
75
- parent.function = block
76
-
77
- children = children_names.map { |child_name|
78
- @nodes[child_name] || (
79
- @nodes[child_name] = @node_class.new(child_name)
80
- )
81
- }
82
-
83
- #
84
- # link
85
- #
86
- parent.children = children
87
- children.each { |child|
88
- child.parents << parent
89
- }
90
- end
91
-
92
- #
93
- # Mark this node and all its children as uncomputed.
94
- #
95
- # Arguments:
96
- #
97
- # +name+ -- unique node identifier (usually a symbol).
98
- #
99
- def reset(name)
100
- @nodes[name].reset
101
- end
102
-
103
- #
104
- # Check for a cyclic graph below the given node. If found,
105
- # returns the names of the nodes (in order) which form a loop.
106
- # Otherwise returns nil.
107
- #
108
- # Arguments:
109
- #
110
- # +name+ -- unique node identifier (usually a symbol).
111
- #
112
- def check_circular(name)
113
- helper = Proc.new { |root, chain|
114
- if chain.include? root
115
- return chain + [root]
116
- end
117
- @nodes[root].children.each { |child|
118
- helper.call(child.name, chain + [root])
119
- }
120
- }
121
- helper.call(name, [])
122
- nil
123
- end
124
-
125
- #
126
- # Compute this node.
127
- #
128
- # Arguments:
129
- #
130
- # +name+ -- unique node identifier (usually a symbol).
131
- #
132
- # +threads+ -- (Integer) number of threads.
133
- #
134
- # compute(:volume, :threads => 4) syntax is also accepted.
135
- #
136
- def compute(name, opts)
137
- threads = (opts.respond_to?(:to_i) ? opts : opts[:threads]).to_i
138
- root = @nodes[name]
139
-
140
- if threads < 1
141
- raise ArgumentError, "threads is #{threads}"
142
- end
143
-
144
- if root.computed
145
- root.result
146
- elsif threads == 1
147
- root.result = root.compute_now
148
- else
149
- compute_multithreaded(root, threads)
150
- end
151
- end
152
- end
153
- end
@@ -1,23 +0,0 @@
1
-
2
- module Rake end
3
- module Rake::CompTree
4
- # Base class for CompTree errors.
5
- class Error < StandardError ; end
6
-
7
- # Internal error inside CompTree. Please send a bug report.
8
- class AssertionFailedError < Error ; end
9
-
10
- # Bad arguments were passed to a method.
11
- class ArgumentError < Error ; end
12
-
13
- #
14
- # Attempt to redefine a Node.
15
- #
16
- # If you wish to only replace the function, set
17
- # driver.nodes[name].function = some_new_lambda
18
- #
19
- class RedefinitionError < Error ; end
20
-
21
- # No function was defined for this node.
22
- class NoFunctionError < Error ; end
23
- end
@@ -1,164 +0,0 @@
1
-
2
- require 'thread'
3
-
4
- module Rake end
5
- module Rake::CompTree
6
- #
7
- # Base class for nodes in the computation tree.
8
- #
9
- class Node
10
- attr_reader :name #:nodoc:
11
-
12
- attr_accessor :parents #:nodoc:
13
- attr_accessor :children #:nodoc:
14
- attr_accessor :function #:nodoc:
15
- attr_accessor :result #:nodoc:
16
- attr_accessor :computed #:nodoc:
17
- attr_accessor :shared_lock #:nodoc:
18
-
19
- #
20
- # Create a node
21
- #
22
- def initialize(name) #:nodoc:
23
- @name = name
24
- @mutex = Mutex.new
25
- @children = []
26
- @parents = []
27
- @function = nil
28
- reset_self
29
- end
30
-
31
- #
32
- # Reset the computation for this node.
33
- #
34
- def reset_self #:nodoc:
35
- @shared_lock = 0
36
- @children_results = nil
37
- @result = nil
38
- @computed = nil
39
- end
40
-
41
- #
42
- # Reset the computation for this node and all children.
43
- #
44
- def reset #:nodoc:
45
- each_downward { |node|
46
- node.reset_self
47
- }
48
- end
49
-
50
- def each_downward(&block) #:nodoc:
51
- block.call(self)
52
- @children.each { |child|
53
- child.each_downward(&block)
54
- }
55
- end
56
-
57
- def each_upward(&block) #:nodoc:
58
- block.call(self)
59
- @parents.each { |parent|
60
- parent.each_upward(&block)
61
- }
62
- end
63
-
64
- def each_child #:nodoc:
65
- @children.each { |child|
66
- yield(child)
67
- }
68
- end
69
-
70
- #
71
- # Force computation of all children; intended for
72
- # single-threaded mode.
73
- #
74
- def compute_now #:nodoc:
75
- unless @children_results
76
- @children_results = @children.map { |child|
77
- child.compute_now
78
- }
79
- end
80
- compute
81
- end
82
-
83
- #
84
- # If all children have been computed, return their results;
85
- # otherwise return nil.
86
- #
87
- # Do not assign to @children_results since own lock is not
88
- # necessarily aquired.
89
- #
90
- def find_children_results #:nodoc:
91
- @children_results or (
92
- @children.map { |child|
93
- unless child.computed
94
- return nil
95
- end
96
- child.result
97
- }
98
- )
99
- end
100
-
101
- def children_results=(value) #:nodoc:
102
- @children_results = value
103
- end
104
-
105
- #def trace_compute #:nodoc:
106
- # debug {
107
- # # --- own mutex
108
- # trace "Computing #{@name}"
109
- # raise AssertionFailedError if @computed
110
- # raise AssertionFailedError unless @mutex.locked?
111
- # raise AssertionFailedError unless @children_results
112
- # }
113
- #end
114
-
115
- #
116
- # Compute this node; children must be computed and lock must be
117
- # already acquired.
118
- #
119
- def compute #:nodoc:
120
- begin
121
- unless @function
122
- raise NoFunctionError,
123
- "No function was defined for node '#{@name.inspect}'"
124
- end
125
- @result = @function.call(*@children_results)
126
- @computed = true
127
- rescue Exception => e
128
- @computed = e
129
- end
130
- @result
131
- end
132
-
133
- def try_lock #:nodoc:
134
- # --- shared tree mutex and own mutex
135
- if @shared_lock == 0 and @mutex.try_lock
136
- #trace "Locking #{@name}"
137
- each_upward { |node|
138
- node.shared_lock += 1
139
- #trace "#{node.name} locked by #{@name}: level: #{node.shared_lock}"
140
- }
141
- true
142
- else
143
- false
144
- end
145
- end
146
-
147
- def unlock #:nodoc:
148
- # --- shared tree mutex and own mutex
149
- #debug {
150
- # raise AssertionFailedError unless @mutex.locked?
151
- # trace "Unlocking #{@name}"
152
- #}
153
- each_upward { |node|
154
- node.shared_lock -= 1
155
- #debug {
156
- # if node.shared_lock == 0
157
- # trace "#{node.name} unlocked by #{@name}"
158
- # end
159
- #}
160
- }
161
- @mutex.unlock
162
- end
163
- end
164
- end
data/test/Rakefile.seq DELETED
@@ -1,20 +0,0 @@
1
-
2
- require 'thread'
3
-
4
- task_names = (1..50).map { |n| n.to_s }
5
- order_invoked = []
6
- mutex = Mutex.new
7
-
8
- task_names.each { |task_name|
9
- task task_name do
10
- mutex.synchronize {
11
- order_invoked.push(task_name)
12
- }
13
- end
14
- }
15
-
16
- task :default => seq[*task_names] do
17
- unless order_invoked == task_names
18
- raise "seq failed"
19
- end
20
- end
data/test/Rakefile.simple DELETED
@@ -1,18 +0,0 @@
1
-
2
- require 'thread'
3
-
4
- task :default => [:a, :b]
5
- task :a => [:x, :y]
6
- task :b
7
-
8
- mutex = Mutex.new
9
- STDOUT.sync = true
10
-
11
- %w[a b x y].each { |name|
12
- task name.to_sym do
13
- mutex.synchronize {
14
- puts "task #{name}"
15
- }
16
- sleep(1)
17
- end
18
- }
data/test/parallel.rb DELETED
@@ -1,4 +0,0 @@
1
- require 'rake'
2
- Rake.application.num_threads = 8
3
- puts "- Testing multi-threaded"
4
-
@@ -1,3 +0,0 @@
1
- require 'rake'
2
- Rake.application.num_threads = 1
3
- puts "- Testing single-threaded"