Get the number of processor cores in Julia

I want to get the number of cores available in Julia. I am currently doing the following:

using PyCall @pyimport psutil nCores = psutil.cpu_count() 

This calls the Python function. However, I would like to use the Julia procedure. How can I do that?

+8
parallel-processing julia-lang cpu-cores
source share
6 answers

I am not 100% sure about this, but CPU_CORES returns the number of cores (hyperthreads) on my machine (OSX 10.9.5 and Julia 0.3.5), even when I run Julia in serial mode, I checked the number of available cores using nworkers() and nprocs() . Running Julia without the -p flag returns 1 for both.

When I run julia like julia -p 4

 julia> nprocs() 5 julia> nworkers() 4 

In both cases, CPU_CORES returns 8.

+6
source share

In recent versions of Julia, you can use Sys.CPU_CORES (rather than Base.CPU_CORES , as mentioned in some answers). Tested at 0.6.

+3
source share

I don’t know Julia, but "psutil.cpu_count (logical = False)" in Python gives you the number of physical processors (hyper-threads are not counted).

+1
source share

Sys.CPU_CORES not defined in Julia 1.0.0 (at least it works on a MacBook - I don’t think it will matter). Use Sys.CPU_THREADS .

0
source share

"Current answer" - Sys.CPU_THREADS returns the number of "hyperthreading", the kernel * "scheduled threads to the kernel", starting with version no lower than 1.0.2.

0
source share

JoshAdel's answer is correct: Base.CPU_CORES stores the number of available cores, including virtual ones.

I am adding this answer to mark an alternative: using the Hwloc package. From the project description

This Julia package wraps the hwloc library.

The Portable Locality (hwloc) software package provides portable abstractions (via the OS, versions, architectures, ...) of the hierarchical topology of modern architectures, including NUMA memory nodes, sockets, shared caches, cores, and simultaneous multithreading. It also collects various system attributes, such as information cache and memory, as well as the location of I / O devices, such as network interfaces, InfiniBand HCA, or GPUs. First of all, these are applications with the collection of information about modern computing in order to use it accordingly and effectively.

Also on the project page, a method for obtaining the number of physical cores and {physical, virtual} cores (that is, processing units) is as follows:

 import Hwloc topology = Hwloc.topology_load() counts = Hwloc.histmap(topology) ncores = counts[:Core] npus = counts[:PU] println("This machine has $ncores cores and $npus PUs (processing units)") 

The advantage of using this package is its portable ability to distinguish between physical and virtual kernels, which is currently not available in Julia. There is, however, a petition to include this ability in the language base.

-one
source share

All Articles