Class: Gem::Dependent::Parallel

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/dependent/parallel.rb

Defined Under Namespace

Classes: Break, DeadWorker, ExceptionWrapper, Worker

Constant Summary collapse

VERSION =
"0.8.0"

Class Method Summary collapse

Class Method Details

.each(array, options = {}, &block) ⇒ Object



88
89
90
91
# File 'lib/rubygems/dependent/parallel.rb', line 88

def each(array, options={}, &block)
  map(array, options.merge(:preserve_results => false), &block)
  array
end

.each_with_index(array, options = {}, &block) ⇒ Object



93
94
95
# File 'lib/rubygems/dependent/parallel.rb', line 93

def each_with_index(array, options={}, &block)
  each(array, options.merge(:with_index => true), &block)
end

.in_processes(options = {}, &block) ⇒ Object



82
83
84
85
86
# File 'lib/rubygems/dependent/parallel.rb', line 82

def in_processes(options = {}, &block)
  count, options = extract_count_from_options(options)
  count ||= processor_count
  map(0...count, options.merge(:in_processes => count), &block)
end

.in_threads(options = {:count => 2}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubygems/dependent/parallel.rb', line 65

def in_threads(options={:count => 2})
  count, options = extract_count_from_options(options)

  out = []
  threads = []

  count.times do |i|
    threads[i] = Thread.new do
      out[i] = yield(i)
    end
  end

  kill_on_ctrl_c(threads) { wait_for_threads(threads) }

  out
end

.map(array, options = {}, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubygems/dependent/parallel.rb', line 97

def map(array, options = {}, &block)
  array = array.to_a # turn Range and other Enumerable-s into an Array

  if options[:in_threads]
    method = :in_threads
    size = options[method]
  else
    method = :in_processes
    size = options[method] || processor_count
  end
  size = [array.size, size].min

  return work_direct(array, options, &block) if size == 0

  if method == :in_threads
    work_in_threads(array, options.merge(:count => size), &block)
  else
    work_in_processes(array, options.merge(:count => size), &block)
  end
end

.map_with_index(array, options = {}, &block) ⇒ Object



118
119
120
# File 'lib/rubygems/dependent/parallel.rb', line 118

def map_with_index(array, options={}, &block)
  map(array, options.merge(:with_index => true), &block)
end

.physical_processor_countObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rubygems/dependent/parallel.rb', line 145

def physical_processor_count
  @physical_processor_count ||= begin
    ppc = case RbConfig::CONFIG['host_os']
    when /darwin1/, /freebsd/
      `sysctl -n hw.physicalcpu`.to_i
    when /linux/
      cores_per_physical = `grep cores /proc/cpuinfo`[/\d+/].to_i
      physicals = `grep 'physical id' /proc/cpuinfo |sort|uniq|wc -l`.to_i
      physicals * cores_per_physical
    when /mswin|mingw/
      require 'win32ole'
      wmi = WIN32OLE.connect("winmgmts://")
      cpu = wmi.ExecQuery("select NumberOfProcessors from Win32_Processor")
      cpu.to_enum.first.NumberOfLogicalProcessors
    else
      processor_count
    end
    # fall back to logical count if physical info is invalid
    ppc > 0 ? ppc : processor_count
  end
end

.processor_countObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rubygems/dependent/parallel.rb', line 122

def processor_count
  @processor_count ||= case RbConfig::CONFIG['host_os']
  when /darwin9/
    `hwprefs cpu_count`.to_i
  when /darwin/
    (hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
  when /linux|cygwin/
    `grep -c ^processor /proc/cpuinfo`.to_i
  when /(net|open|free)bsd/
    `sysctl -n hw.ncpu`.to_i
  when /mswin|mingw/
    require 'win32ole'
    wmi = WIN32OLE.connect("winmgmts://")
    cpu = wmi.ExecQuery("select NumberOfLogicalProcessors from Win32_Processor")
    cpu.to_enum.first.NumberOfLogicalProcessors
  when /solaris2/
    `psrinfo -p`.to_i # this is physical cpus afaik
  else
    $stderr.puts "Unknown architecture ( #{RbConfig::CONFIG["host_os"]} ) assuming one processor."
    1
  end
end