Class: Skynet::Partitioners::SimplePartitionData

Inherits:
Skynet::Partitioners show all
Defined in:
lib/skynet/skynet_partitioners.rb

Overview

Split one block of data into partitions

Class Method Summary collapse

Methods included from SkynetDebugger

#args_pp, #debug, #debug_header, #error, #fatal, included, #info, #log, #printlog, #stderr, #stdout, #warn

Class Method Details

.reduce_partition(data, partitions) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/skynet/skynet_partitioners.rb', line 10

def self.reduce_partition(data, partitions)    
  partitioned_data = Array.new

  # If data size is significantly greater than the number of desired
  # partitions, we can divide the data roughly but the last partition
  # may be smaller than the others.
  #      
  return data if (not data) or data.empty?
    
  if partitions >= data.length
    data.each do |datum|
     partitioned_data << [datum]
    end
  elsif (data.length >= partitions * 2)
    # Use quicker but less "fair" method
    size = data.length / partitions

    if (data.length % partitions != 0)
      size += 1 # Last slice of leftovers
    end

    (0..partitions - 1).each do |i|
      partitioned_data[i] = data[i * size, size]
    end
  else
    # Slower method, but partitions evenly
    partitions = (data.size < partitions ? data.size : partitions)
    (0..partitions - 1).each { |i| partitioned_data[i] = Array.new }
  
    data.each_with_index do |datum, i|
      partitioned_data[i % partitions] << datum
    end
  end
  
  partitioned_data
end