Sha256: ed9685bb6e2b2a089de82b45db0376ac93a0af4a77bc73195f13b9f4aad73a6d

Contents?: true

Size: 764 Bytes

Versions: 1

Compression:

Stored size: 764 Bytes

Contents

module Elaine
  class Coordinator
    attr_reader :workers

    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

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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
elaine-0.0.3 lib/elaine/coordinator.rb