Skip to content

Commit

Permalink
Merge pull request #333 from M-Yamashita01/fix-inline-method-in-proce…
Browse files Browse the repository at this point in the history
…ssor-count

Inline the methods in the ProcessorCount module
  • Loading branch information
grosser committed Mar 17, 2023
2 parents ae35ac4 + 0506c4a commit 3928204
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
42 changes: 39 additions & 3 deletions lib/parallel.rb
@@ -1,11 +1,8 @@
# frozen_string_literal: true
require 'rbconfig'
require 'parallel/version'
require 'parallel/processor_count'

module Parallel
extend ProcessorCount

Stop = Object.new.freeze

class DeadWorker < StandardError
Expand Down Expand Up @@ -307,6 +304,45 @@ def flat_map(*args, &block)
map(*args, &block).flatten(1)
end

# Number of physical processor cores on the current system.
def physical_processor_count
@physical_processor_count ||= begin
ppc =
case RbConfig::CONFIG["target_os"]
when /darwin[12]/
IO.popen("/usr/sbin/sysctl -n hw.physicalcpu").read.to_i
when /linux/
cores = {} # unique physical ID / core ID combinations
phy = 0
File.read("/proc/cpuinfo").scan(/^physical id.*|^core id.*/) do |ln|
if ln.start_with?("physical")
phy = ln[/\d+/]
elsif ln.start_with?("core")
cid = "#{phy}:#{ln[/\d+/]}"
cores[cid] = true unless cores[cid]
end
end
cores.count
when /mswin|mingw/
require 'win32ole'
result_set = WIN32OLE.connect("winmgmts://").ExecQuery(
"select NumberOfCores from Win32_Processor"
)
result_set.to_enum.collect(&:NumberOfCores).reduce(:+)
else
processor_count
end
# fall back to logical count if physical info is invalid
ppc > 0 ? ppc : processor_count
end
end

# Number of processors seen by the OS, used for process scheduling
def processor_count
require 'etc'
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Etc.nprocessors)
end

def worker_number
Thread.current[:parallel_worker_number]
end
Expand Down
44 changes: 0 additions & 44 deletions lib/parallel/processor_count.rb

This file was deleted.

0 comments on commit 3928204

Please sign in to comment.