Module: Parallel

### Home Classes Methods

### In Files

- lib/parallel.rb
- lib/parallel/version.rb

### Namespace

- CLASS Parallel::Break
- CLASS Parallel::DeadWorker
- CLASS Parallel::ExceptionWrapper
- CLASS Parallel::Worker

### Methods

- ::each
- ::each_with_index
- ::in_processes
- ::in_threads
- ::map
- ::map_with_index
- ::physical_processor_count
- ::processor_count

### Class/Module Index

Quicksearch

- Parallel
- Parallel::Break
- Parallel::DeadWorker
- Parallel::ExceptionWrapper
- Parallel::Worker

No matching classes.

# Parallel

### Constants

VERSION

### Public Class Methods

each(array, options={}, &block) click to toggle source

    # File lib/parallel.rb, line 80
    def each(array, options={}, &block)
      map(array, options.merge(:preserve_results => false), &block)
      array
    end

each_with_index(array, options={}, &block) click to toggle source

    # File lib/parallel.rb, line 85
    def each_with_index(array, options={}, &block)
      each(array, options.merge(:with_index => true), &block)
    end

in_processes(options = {}, &block) click to toggle source

    # File lib/parallel.rb, line 74
    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}) click to toggle source

    # File lib/parallel.rb, line 57
    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) click to toggle source

    # File lib/parallel.rb, line 89
    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) click to toggle source

    # File lib/parallel.rb, line 110
    def map_with_index(array, options={}, &block)
      map(array, options.merge(:with_index => true), &block)
    end

physical_processor_count() click to toggle source

    # File lib/parallel.rb, line 137
    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.NumberOfProcessors
        else
          processor_count
        end
        # fall back to logical count if physical info is invalid
        ppc > 0 ? ppc : processor_count
      end
    end

processor_count() click to toggle source

    # File lib/parallel.rb, line 114
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.