[英]Python multiprocessing.cpu_count() returns '1' on 4-core Nvidia Jetson TK1
Can anyone tell me why Python's multiprocessing.cpu_count()
function would return 1
when called on a Jetson TK1 with four ARMv7 processors?谁能告诉我为什么 Python 的
multiprocessing.cpu_count()
函数在具有四个 ARMv7 处理器的 Jetson TK1 上调用时会返回1
?
>>> import multiprocessing
>>> multiprocessing.cpu_count()
1
The Jetson TK1 board is more or less straight out of the box, and no one has messed with cpusets. Jetson TK1 开发板或多或少是直接开箱即用的,没有人弄乱过 cpuset。 From within the same Python shell I can print the contents of
/proc/self/status
and it tells me that the process should have access to all four cores:在同一个 Python shell 中,我可以打印
/proc/self/status
,它告诉我该进程应该可以访问所有四个核心:
>>> print open('/proc/self/status').read()
----- (snip) -----
Cpus_allowed: f
Cpus_allowed_list: 0-3
----- (snip) -----
What else could be causing this behavior from cpu_count()
?还有什么可能导致
cpu_count()
出现这种行为?
To test Klaus's hypothesis, I used the following code to run a very simple experiment:为了检验 Klaus 的假设,我使用以下代码运行了一个非常简单的实验:
import multiprocessing
def f(x):
n = 0
for i in xrange(10000):
n = max(n, multiprocessing.cpu_count())
return n
p = multiprocessing.Pool(5)
for i in range(10):
print p.map(f, [1,2,3,4,5])
Which produced the following output:这产生了以下输出:
[3, 3, 3, 3, 1]
[4, 3, 3, 3, 3]
[4, 3, 3, 3, 3]
[3, 3, 4, 3, 3]
[4, 3, 3, 3, 3]
[3, 3, 4, 3, 3]
[4, 3, 3, 3, 3]
[3, 3, 4, 3, 3]
[3, 3, 3, 4, 3]
[4, 3, 3, 3, 3]
Running just a single iteration of p.map(f, [1,2,3,4,5])
usually produces [1, 1, 1, 1, 1]
, although occasionally a 2
will appear as one of the list elements.仅运行
p.map(f, [1,2,3,4,5])
的单次迭代通常会产生[1, 1, 1, 1, 1]
,尽管偶尔会出现2
作为列表元素之一.
On Linux systems multiprocessing.cpu_count()
relies on a sysconf (_SC_NPROCESSORS_ONLN)
call, which returns the number of online CPUs in contrast to sysconf (_SC_NPROCESSORS_CONF)
which returns the number of configured CPUs.在 Linux 系统上,
multiprocessing.cpu_count()
依赖于sysconf (_SC_NPROCESSORS_ONLN)
调用,它返回在线CPU 的数量,而sysconf (_SC_NPROCESSORS_CONF)
则返回已配置的CPU 的数量。
The values might differ in systems with advanced CPU power management functionality that sets CPU cores offline to save energy or with similar dynamic CPU activation functionality.在具有高级 CPU 电源管理功能(将 CPU 内核设置为离线以节省能源)或具有类似动态 CPU 激活功能的系统中,这些值可能会有所不同。
The documentation for os.cpu_count() (which declares that it returns the total number of CPUS, not the number of usable CPUs) provides a means to count usable CPUs: os.cpu_count() 的文档(声明它返回 CPU 总数,而不是可用 CPU 的数量)提供了一种计算可用 CPU 的方法:
len(os.sched_getaffinity(0))
see https://docs.python.org/3/library/os.html#os.cpu_count见https://docs.python.org/3/library/os.html#os.cpu_count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.