Class: Pregel::Coordinator

Inherits:
Object
  • Object
show all
Defined in:
lib/pregel/coordinator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, options = {}) ⇒ Coordinator

Returns a new instance of Coordinator.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/pregel/coordinator.rb', line 5

def initialize(graph, options = {})
  raise "empty graph" if graph.empty?

  @workers = []
  @options = {
    :partitions => 1
  }.merge(options)

  partition(graph) do |subgraph|
    @workers << Worker.new(subgraph)
  end
end

Instance Attribute Details

#workersObject (readonly)

Returns the value of attribute workers.



3
4
5
# File 'lib/pregel/coordinator.rb', line 3

def workers
  @workers
end

Instance Method Details

#partition(graph) ⇒ Object



18
19
20
21
# File 'lib/pregel/coordinator.rb', line 18

def partition(graph)
  size = (graph.size.to_f / @options[:partitions]).ceil
  graph.each_slice(size) { |slice| yield slice }
end

#runObject



23
24
25
26
27
28
29
30
31
# File 'lib/pregel/coordinator.rb', line 23

def run
  loop do
    # execute a superstep and wait for workers to complete
    step = @workers.select {|w| w.active > 0}.collect {|w| w.superstep }
    step.each {|t| t.join}

    break if @workers.select {|w| w.active > 0}.size.zero?
  end
end