Obtain the number of CPU cores in Julia
Asked Answered
B

8

11

I want to obtain the number of cores available in Julia. Currently I am doing the following:

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

This calls a Python function. I would like, however, to use some Julia procedure. How can it be done?

Blindfish answered 13/1, 2015 at 20:35 Comment(3)
The constant Base.CPU_CORES may do the trick (but it counts hyper-threading cores as full-cores).Xanthin
psutil.cpu_count() reports 8 cores on my Intel i7-2720QM-powered laptop so it also counts hyper-threading cores as full cores.Litchi
This question needs a new answer. All the current answers are deprecated.Cotter
O
6

I'm not 100% certain about this, but CPU_CORES returns the number of (hyper-threading) cores on my machine (OSX 10.9.5 and Julia 0.3.5), even when I start Julia in serial mode. I've been checking the number of available cores using nworkers() and nprocs(). Starting up Julia without the -p flag this returns 1 for both.

When I start julia as julia -p 4

julia> nprocs()
5

julia> nworkers()
4

In both cases CPU_CORES returns 8.

Orvalorvan answered 15/1, 2015 at 5:16 Comment(2)
Base.CPU_CORES is deprecated. In order to obtain this information, you should try Sys.CPU_CORES. More details could be found hereImp
For logical cores Sys.CPU_CORES, and for physical cores you can use CpuId.jl or Hwloc.jlFrodina
B
15

Sys.CPU_CORES is not defined in Julia v1.1.0. However, the following does the job.

length(Sys.cpu_info())
Branks answered 9/3, 2019 at 14:28 Comment(1)
It will bring the number of logical cores and not physical cores.Mozellemozes
O
6

I'm not 100% certain about this, but CPU_CORES returns the number of (hyper-threading) cores on my machine (OSX 10.9.5 and Julia 0.3.5), even when I start Julia in serial mode. I've been checking the number of available cores using nworkers() and nprocs(). Starting up Julia without the -p flag this returns 1 for both.

When I start julia as julia -p 4

julia> nprocs()
5

julia> nworkers()
4

In both cases CPU_CORES returns 8.

Orvalorvan answered 15/1, 2015 at 5:16 Comment(2)
Base.CPU_CORES is deprecated. In order to obtain this information, you should try Sys.CPU_CORES. More details could be found hereImp
For logical cores Sys.CPU_CORES, and for physical cores you can use CpuId.jl or Hwloc.jlFrodina
M
3

In recent versions of Julia, you can use Sys.CPU_CORES (and not Base.CPU_CORES as some answers mentioned). Tested on 0.6.

Moorman answered 18/7, 2017 at 15:10 Comment(0)
J
3

According to the documentation, the "number of cores available" can be limited by the JULIA_NUM_THREADS environment variable.

To see the number of threads available to Julia, use

Threads.nthreads()
Joses answered 12/12, 2020 at 8:17 Comment(0)
B
2

Sys.CPU_CORES is undefined in Julia 1.0.0 (at least, running on a macbook—I don't imagine that would make a difference). Instead, use Sys.CPU_THREADS.

Belk answered 17/11, 2018 at 15:33 Comment(2)
Note, however, that cpu threads != cores, in particular for modern intel cpus with hyper threading.Tallboy
But I think the old Sys.CPU_CORES did count HT as cores as well.Tallboy
K
1

I don't know Julia but "psutil.cpu_count(logical=False)" in Python gives you the number of physical CPUs (hyper threaded ones are not counted).

Kiaochow answered 18/1, 2015 at 12:11 Comment(0)
C
0

In Julia 1.10, you can do

length(Sys.cpu_info())
Cotter answered 23/1 at 20:59 Comment(0)
O
0

Hwloc is a useful package for this purpose.

using Hwloc
num_physical_cores()
num_virtual_cores()
Osmanli answered 4/3 at 13:51 Comment(2)
Why do you prefer this over previous answers? What benefits does it provide that they don’t offer?Bernadette
Because, assuming that Hwloc is well implemented, it gives you directly the number of physical and virtual cores.Osmanli

© 2022 - 2024 — McMap. All rights reserved.