# File lib/parallel.rb, line 29 def self.each(array, options={}, &block) map(array, options.merge(:preserve_results => false), &block) array end
# File lib/parallel.rb, line 34 def self.each_with_index(array, options={}, &block) each(array, options.merge(:with_index => true), &block) end
# File lib/parallel.rb, line 23 def self.in_processes(options = {}, &block) count, options = extract_count_from_options(options) count ||= processor_count map(0...count, options.merge(:in_processes => count), &block) end
# File lib/parallel.rb, line 6 def self.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 wait_for_threads(threads) out end
# File lib/parallel.rb, line 38 def self.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
# File lib/parallel.rb, line 59 def self.map_with_index(array, options={}, &block) map(array, options.merge(:with_index => true), &block) end
# File lib/parallel.rb, line 86 def self.physical_processor_count case RbConfig::CONFIG['host_os'] when %rdarwin1/, %rfreebsd/ %xsysctl -n hw.physicalcpu`.to_i when %rlinux/ %xgrep cores /proc/cpuinfo`[%r\d+/].to_i when %rmswin|mingw/ require 'win32ole' wmi = WIN32OLE.connect("winmgmts://") cpu = wmi.ExecQuery("select NumberOfProcessors from Win32_Processor") cpu.to_enum.first.NumberOfLogicalProcessors else processor_count end end
# File lib/parallel.rb, line 63 def self.processor_count case RbConfig::CONFIG['host_os'] when %rdarwin9/ %xhwprefs cpu_count`.to_i when %rdarwin/ (hwprefs_available? ? %xhwprefs thread_count` : %xsysctl -n hw.ncpu`).to_i when %rlinux|cygwin/ %xgrep -c processor /proc/cpuinfo`.to_i when %r(open|free)bsd/ %xsysctl -n hw.ncpu`.to_i when %rmswin|mingw/ require 'win32ole' wmi = WIN32OLE.connect("winmgmts://") cpu = wmi.ExecQuery("select NumberOfLogicalProcessors from Win32_Processor") cpu.to_enum.first.NumberOfLogicalProcessors when %rsolaris2/ %xpsrinfo -p`.to_i # this is physical cpus afaik else $stderr.puts "Unknown architecture ( #{RbConfig::CONFIG["host_os"]} ) assuming one processor." 1 end end