Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine number of cores using Rust

Tags:

I want to spawn a certain number of tasks based on the cores that a machine has. Is there anything in Rust that can find the number of cores, or should I just run external commands and parse the output?

like image 514
zephyrthenoble Avatar asked Mar 03 '14 19:03

zephyrthenoble


People also ask

How do I check the number of cores?

Open Task Manager (press Ctrl+Shift+Esc) Select Performance tab. Look for Cores and Logical Processors (Threads)

How many cores does rust utilize?

[LOW FPS] RUST ONLY USE 1 CPU CORE :: Rust General Discussions.

How CPU cores are calculated?

To calculate the total number of the core of each node = CPU SOCKET* CPU CORE.

How many threads do I have per core?

Each CPU core can have two threads. So a processor with two cores will have four threads. A processor with eight cores will have 16 threads.


2 Answers

There's now a crate to do this: https://crates.io/crates/num_cpus

Add this to your Cargo.toml:

[dependencies] num_cpus = "0.2" 

Then in your source:

extern crate num_cpus; let num = num_cpus::get(); 
like image 85
Matt Godbolt Avatar answered Oct 07 '22 17:10

Matt Godbolt


You could use std::os::num_cpus. Example:

fn main() {     println!("{}", std::os::num_cpus()); } 
like image 21
rodrigo Avatar answered Oct 07 '22 18:10

rodrigo