[英]CPU count for Xeon processor confusion
My iMac Pro has an Intel 3GHz Xeon 10-core W-2150B processor.我的 iMac Pro 配备 Intel 3GHz Xeon 10 核 W-2150B 处理器。
My understanding is that this is one CPU albeit with 10 cores.我的理解是,尽管有 10 个内核,但这是一个 CPU。
Now consider this trivial Python code:-现在考虑这个微不足道的 Python 代码:-
import os
print(f'Number of CPUs={os.cpu_count()}')
This will emit:- Number of CPUs=20这将发出:- CPU 数量 = 20
...whereas I would expect it to tell that there's 1 CPU or, arguably, 10. ...而我希望它告诉我们有 1 个 CPU,或者可以说是 10 个。
What's going on here?这里发生了什么?
Python simply reports the number it gets from the OS. Python 只是报告它从操作系统获得的数字。 The OS simply reports the number it gets from the firmware.操作系统只是报告它从固件中获得的数字。 The firmware simply reports the number it gets from the CPU.固件只是报告它从 CPU 获得的数字。
So, the short answer to your question is: Python is reporting 20 CPUs because that is what Intel has decided to report.因此,对您的问题的简短回答是:Python 报告了 20 个 CPU,因为这是英特尔决定报告的。
Now, the question is of course: does it make sense to report 20 CPUs?现在,问题当然是:报告 20 个 CPU 有意义吗? And the answer to that is not simple.答案并不简单。 At the time when the system calls that report the number of CPUs were designed, the relationship between sockets, chips, dies, CPUs, execution cores, and threads was a very simple 1:1:1:1:1:1 relationship.在设计报告 CPU 数量的系统调用时,sockets、芯片、裸片、CPU、执行核心和线程之间的关系是非常简单的 1:1:1:1:1:1 关系。 So, nobody ever thought about whether the number should mean the number of sockets, the number of CPUs, the number of cores, the number of threads, or whatever, because it didn't matter.所以,没有人想过这个数字是否应该表示 sockets 的数量,CPU 的数量,内核的数量,线程的数量,或者其他什么,因为这并不重要。 It was all the same.一切都一样。
But nowadays, the relationship is much more complex:但如今,这种关系要复杂得多:
So, you have to think about what it actually is that you are interested in. And Intel has decided that what you are most likely interested in, is the number of hardware threads that can be executed in parallel.因此,您必须考虑您真正感兴趣的是什么。英特尔已经确定您最有可能感兴趣的是可以并行执行的硬件线程数。 And since on the particular CPU that you have, there are 10 execution cores in the CPU and each core can execute 2 threads, the CPU reports itself as 20 CPUs, even though it is only 1 CPU.由于在您拥有的特定 CPU 上,CPU 中有 10 个执行核心,每个核心可以执行 2 个线程,因此 CPU 将自己报告为 20 个 CPU,尽管它只有 1 个 CPU。
Because as a programmer, you most likely don't care how many blobs of sand you have in your computer, but what you can do with them .因为作为一名程序员,您很可能不关心计算机中有多少沙子,而是您可以用它们做什么。
Although in reality, the question is even more complex because the two threads per execution core are not totally independent, and depending on your specific workload, you might actually only want to use one thread per CPU, so you might actually need to know the difference between execution cores and threads.尽管在现实中,这个问题更加复杂,因为每个执行核心的两个线程并不完全独立,并且根据您的特定工作负载,您实际上可能只想在每个 CPU 上使用一个线程,因此您实际上可能需要了解其中的区别在执行核心和线程之间。 Additionally, some recent CPUs have different types of cores, eg the Apple M1 CPU has 8 cores with identical Instruction Sets, but 4 of them are optimized for resources (and thus somewhat slower) and 4 of them are optimized for performance (and thus consume more power and produce more waste heat).此外,一些最近的 CPU 有不同类型的内核,例如 Apple M1 CPU 有 8 个内核,指令集相同,但其中 4 个针对资源进行了优化(因此速度稍慢),其中 4 个针对性能进行了优化(因此消耗更多的功率并产生更多的废热)。
The current SPARC CPUs from Oracle can schedule up to 8 threads per execution core, of which they can execute up to 2 simultaneously.当前 Oracle 的 SPARC CPU 可以为每个执行核心调度多达 8 个线程,其中它们最多可以同时执行 2 个。 So, should this CPU report itself as 2 CPUs or 8?那么,这个 CPU 应该将自己报告为 2 个 CPU 还是 8 个? And so on, there are dozens of such examples that show that the answer to the question "how many CPUs do I have" is not so simple, and depends heavily on what, precisely , you actually mean by "CPU".等等,有几十个这样的例子表明,“我有多少个 CPU”这个问题的答案并不那么简单,而且在很大程度上取决于“ CPU ”的实际含义。
If you want to write high-performance and/or energy-efficient code, a simple number is not enough.如果您想编写高性能和/或节能代码,一个简单的数字是不够的。 You need to know the full hierarchy and dependencies between the different elements.您需要了解不同元素之间的完整层次结构和依赖关系。
EDIT: check out this thread: https://stackoverflow.com/a/1715612/9983575 - there's multiple different ways discussed in the different answers, and a simple one is to run sysctl -n hw.ncpu
from the command line (note this is the command line, and not the python interpreter).编辑:查看此线程: https://stackoverflow.com/a/1715612/9983575 - 在不同的答案中讨论了多种不同的方法,一个简单的方法是从命令行运行sysctl -n hw.ncpu
(注意这是命令行,而不是 python 解释器)。 This should match the output you see from这应该与您从中看到的 output 匹配
import os
os.cpu_count()
Specifically, some more details from the comments section:具体来说,评论部分的一些更多细节:
how many physical cores does the machine have and what chip is it?机器有多少个物理内核,它是什么芯片? If it's a core i7 with 2 physical cores for example, it will show as 4 because the chip supports hyper-threading and presents itself to the OS as if it has 4 addressable cores.例如,如果它是具有 2 个物理核心的核心 i7,它将显示为 4,因为该芯片支持超线程并将其自身呈现给操作系统,就好像它具有 4 个可寻址核心一样。 – jkp Sep 26 '11 at 8:51 – jkp 2011 年 9 月 26 日 8:51
ORIGINAL ANSWER:原始答案:
Just tried this and I also got twice the number of CPUs as expected, and looking at other documentation and methods didn't help much.刚刚试了一下,我的 CPU 数量也达到了预期的两倍,查看其他文档和方法也无济于事。
Things I checked:我检查的事情:
help(os.cpu_count)
):帮助文档(来自help(os.cpu_count)
):Help on built-in function cpu_count in module posix:
cpu_count()
Return the number of CPUs in the system; return None if indeterminable.
This number is not equivalent to the number of CPUs the current process can
use. The number of usable CPUs can be obtained with
``len(os.sched_getaffinity(0))``
It also looks like os.sched_getaffinity(0)
doesn't seem to work for me locally and I can't figure out why, since the method is included in both help(os.cpu_count)
and https://docs.python.org/3/library/os.html#os.sched_getaffinity .看起来os.sched_getaffinity(0)
似乎在本地对我不起作用,我不明白为什么,因为该方法包含在help(os.cpu_count)
和https://docs.python 中。 org/3/library/os.html#os.sched_getaffinity 。
My guess is that each core on a Mac has 2 CPUs - this would explain the
20
result for you on a 10 core machine, and the
8
result for me on a quad core machine.
我的猜测是 Mac 上的每个内核都有 2 个 CPU - 这可以解释在 10 核机器上的
20
结果,以及在四核机器上的
8
个结果。
I haven't been able to find any specifics about Mac cores/CPUs yet, but will update my answer here if I do.
我还没有找到关于 Mac 核心/CPU 的任何细节,但如果我找到了,我会在这里更新我的答案。
See the EDIT and links - my understanding is that hyperthreading causes "1 physical core" to appear as "2 cores" to the operating system查看编辑和链接——我的理解是超线程导致“1 个物理内核”在操作系统中显示为“2 个内核”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.