class MiGA::Parallel

Parallel execution in MiGA.

Public Class Methods

distribute(enum, threads, &blk) click to toggle source

Distributes enum across threads and calls the passed block with args:

  1. Unitary object from enum

  2. Index of the unitary object

  3. Index of the acting thread

# File lib/miga/parallel.rb, line 22
def distribute(enum, threads, &blk)
  process(threads) { |thr| thread_enum(enum, threads, thr, &blk) }
end
process(threads) { |i| ... } click to toggle source

Executes the passed block with the thread number as argument (0-numbered) in threads processes

# File lib/miga/parallel.rb, line 10
def process(threads)
  threads.times do |i|
    Process.fork { yield(i) }
  end
  Process.waitall
end
thread_enum(enum, threads, thr) { |obj, idx, thr| ... } click to toggle source

Enum through enum executing the passed block only for thread with index thr, one of threads threads. The passed block has the same arguments as the one in #distribute

# File lib/miga/parallel.rb, line 30
def thread_enum(enum, threads, thr)
  enum.each_with_index do |obj, idx|
    yield(obj, idx, thr) if idx % threads == thr
  end
end