Method: CompTree::Driver#compute

Defined in:
lib/comp_tree/driver.rb

#compute(name, max_threads) ⇒ Object

name – unique node identifier (for example a symbol).

max_threads – maximum number of threads, or 0 to indicate no limit.

Compute the tree below name and return the result.

If a node’s computation raises an exception, the exception will be transferred to the caller of compute(). The tree will be left in a dirty state so that individual nodes may be examined. It is your responsibility to call reset() before attempting the computation again, otherwise the result will be undefined.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/comp_tree/driver.rb', line 106

def compute(name, max_threads)
  begin
    max_threads = max_threads.to_int
  rescue NoMethodError
    raise TypeError, "can't convert #{max_threads.class} into Integer"
  end
  if max_threads < 0
    raise RangeError, "max threads must be nonnegative"
  end

  root = @nodes[name] or raise NoNodeError.new(name)
  if root.computed
    root.result
  elsif max_threads == 1
    root.compute_now
  else
    Algorithm.compute_parallel(root, max_threads == 0 ? nil : max_threads)
  end
end