gpt4 book ai didi

rust - 我怎样才能得到真正的核心数?

转载 作者:行者123 更新时间:2023-11-29 08:01:07 29 4
gpt4 key购买 nike

num_cpus crate 给了我内核的数量,但这包括超线程内核。在我的例子中,这比使用真正的核心慢得多(几乎是 2 倍)。

最佳答案

您可以使用 rust binding 来做到这一点对于 hwloc此博客中描述的库 post .

它可以在各种平台下使用,并允许您像这样获取物理内核和逻辑处理单元的数量(在超线程的情况下)(来自博客文章的代码):

extern crate hwloc;

use hwloc::{Topology, ObjectType};

fn main() {
// Create a new Topology
let topology = Topology::new();

// Get all objects with type "Core"
let cores = topology.objects_with_type(&ObjectType::Core);

// Match on the returned Result and print the length if successful.
match cores {
Ok(c) => println!("There are {} cores on this machine.", c.len()),
Err(e) => panic!(format!("Could not load cores because of: {:?}", e))
}
}

关于rust - 我怎样才能得到真正的核心数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34205771/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com