博客 / 詳情

返回

以 Core i9-13900HX 實例講解CPU概念:物理CPU,內核,邏輯CPU

緣起

# 完整代碼見:https://github.com/luminousmen/grokking_concurrency/blob/master/Chapter%205/password_cracking_parallel.py

def crack_password_parallel(crypto_hash: str, length: int) -> None:
    """Orchestrate cracking the password between different processes"""
    # getting number of available processors
    num_cores = os.cpu_count()
   。。。

看書的時候看到一行代碼加一句註釋包含了三個概念:processorcpucore。因為之前沒用過 os.cpu_count(),所以一下子有點懵,這個函數到底返回什麼,為什麼用了三個概念?

在梳理概念的過程中發現很多文章缺乏圖,單純的文字不大好理解。於是以自己電腦的 Core i9-13900HX 處理器為例,進行實例講解,希望對那些對此也有困惑的人員有所幫助。

物理 CPU/處理器(processor)

定義

安裝在主板插槽(socket)上的一個物理元件(物理的意思:看得見,摸得着)。稱為:物理 CPU,處理器(processor)。我們平時去買電腦時會問“這台電腦是什麼處理器?“,其實就是問這台電腦的物理 CPU 是什麼?老闆可能會回答”這台聯想拯救者(Y9000P)電腦的處理器是 Core i9-13900HX “。

實物圖

Core i9-13900HX

如上圖所示:我的聯想拯救者(Y9000P)電腦有 1 個物理 CPU(處理器),型號是 Core i9-13900HX。

物理內核(Physical core)/內核(Cores)

定義

位於 CPU 內部的一個物理元件。稱為:物理內核(physical core),內核(core)

實物圖

sorry~ 因為核是封裝在物理 CPU 內部的,具體長什麼樣我也不確定,待確定後再補充。

Core i9-13900HX


如上圖所示:Core i9-13900HX 是大小核架構:性能核(Performance-core) + 能效核(Efficient-core)。性能核有 8 個,能效核有 16 個,總計:8 + 16 = 24。即一個物理 CPU Core i9-13900HX 有 24 個內核,或者説有“一個物理 CPU Core i9-13900HX 有 24 個物理內核”

邏輯CPU(logical CPUs)/線程(Threads)

定義

一個虛擬元件(虛擬的意思:邏輯上的,看不見,摸不着)。稱為:邏輯 CPU(logical CPU),邏輯處理器(processor),邏輯內核(logical core),線程(thread)

Core i9-13900HX

如上圖所示:每個性能核(Performance-core)有 2 個線程,能效核(Efficient-core)有 1 個線程,總計:8 x 2 + 16 x 1 = 32 。即 “一個物理 CPU Core i9-13900HX 有 24 個內核,32 個線程”,或者説 “一個物理 CPU Core i9-13900HX 有 24 個內核,32 個邏輯 CPU”。

注:

1.這裏的“線程”和“編程中多線程的線程”不是同一個概念。

2.編程中有些描述會直接用 CPU 表示 logical CPU,如:

# 得到的也是 logial CPUs
multiprocessing.cpu_count()
    Return the number of CPUs in the system.

3.有些文章用 logical cores 表示 logical CPUs,個人覺得這樣容易和 physical core 混淆,應該少用 logical cores , 多用 logical CPUs。

編程實戰

psutil.cpu_count(logical=True)

# 參考資料:https://psutil.readthedocs.io/en/latest/#psutil.cpu_count
psutil.cpu_count(logical=True)
    Return the number of logical CPUs in the system (same as os.cpu_count) or None if undetermined. “logical CPUs” means the number of physical cores multiplied by the number of threads that can run on each core (this is known as Hyper Threading). If logical is False return the number of physical cores only, or None if undetermined. On OpenBSD and NetBSD psutil.cpu_count(logical=False) always return None. 

如上所述:psutil.cpu_count(logical=True) 獲取的“邏輯CPU(logical CPUs)”的數量,psutil.cpu_count(logical=False) 獲取的“物理內核(Physical core)/內核(Cores)”的數量。

os.cpu_count()

# 參考資料: https://docs.python.org/3/library/os.html
os.cpu_count()
    Return the number of logical CPUs in the system. Returns None if undetermined.

如上所述: os.cpu_count() 獲取的是“邏輯CPU數(logical CPUs)”。

import os

import psutil

num_cores = psutil.cpu_count(logical=False)
# Core i9-13900HX 電腦上輸出為 24
print(f'CPU cores: {num_cores}')

# 也可以用 psutil.cpu_count(logical=True)
num_logical_cpus = os.cpu_count()
# Core i9-13900HX 電腦上輸出為 32
print(f'logical CPUs: {num_logical_cpus}')



歡迎搜索及關注:編程人(a_codists)

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.