Process vs Threads、Hardware Threads vs Software Threads、Hyperthreading 今日の高性能なソフトウェアの世界では、 あなたがリアルタイムの取引エンジン、ゲームエンジン、または単純なウェブサーバーを構築しているかどうか、並行してタスクを実行する方法を理解することは、パフォーマンスとユーザー体験を劇的に向上させることができます。 遊びに入る。 concurrency threading 以前のブログでは、私たちは、 様々な ――多線系と先進的並列主義の基本的な構成要素. Now that we have laid the groundwork, it’s time to move beyond theory and dive into the core question: 今、我々は基礎を置いたので、理論を超え、コアの問題に潜入する時が来た。 あなたがこれらの以前のストーリーをまだ読んでいない場合は、より良い文脈のために最初にそれらをチェックすることを強くお勧めします。 Flynnのタクソノミー 並行プログラミングモデル トレードとは何ですか? What does it do? Flynnのタクソノミー Flynnのタクソノミー 並行プログラミングモデル 並行プログラミングモデル トレードとは何ですか? What does it do? UI を更新しながらファイルをダウンロードし、ユーザーの入力に対応しながら結果を計算したり、入力するネットワークリクエストを並行して処理したりします。 C++のようなプログラミング言語は、それらを利用するための強力なツールを提供します。 Threading は、プログラムが一度に 1 つ以上のことを行うことを許可します。 hardware threads Threading は、プログラムが一度に 1 つ以上のことを行うことを許可します。 あなたがマルチトレードに新しいか、あなたの基礎を強化しようとしているかに関係なく、このストーリーはあなたに堅実なスタートを与えます。 Highlights: Threads vs processes ハードウェア ソフトウェア Hyperthreading Fork/join threading model What Is a Thread? トレードって何? 競合について学ぶ初心者のための最初の障害の1つは、理解することです。 . difference between a thread and a process A 実行中の独立したプログラムです. 独自のメモリスペース、ファイル説明器、およびシステムリソースが、他のプロセスから隔離されています. たとえば、ブラウザまたは端末を開くと、新しいプロセスを起動します. オペレーティングシステムはプロセスを管理し、明示的に設定されていない限り、メモリスペースを共有しません. process A , on the other hand, is the smallest unit of execution within a process. Multiple threads can exist within the same process, running simultaneously and sharing the same memory space. This shared environment allows for faster communication between threads, but also opens the door to そして 適切に管理されていない場合 thread race conditions synchronization issues Conceptually, you can think of a thread as a 独自の実行の独立した流れ で、 そして、 しかし、共有 同じプロセスで他のトレードと。 lightweight process program counter registers stack heap and global memory トレードを論じるときは、区別することが重要です。 そして どちらも「実行単位」を指しますが、コンピューティングステック内の非常に異なるレベルで動作します。 hardware threads software threads What is a Hardware Thread? ハードウェア・トレードって何? A 実行ストリーム 実際はA コア内で、独立して指示のフローを取得、解読、および実行することができる。 hardware thread directly supported by the processor dedicated control unit 伝統的に、 つまり、物理CPUごとに1つの制御ユニットで、複数のソケット(例えばサーバーマザーボード)を持つシステムでは、 しかし、このモデルは、導入によって急速に進化した。 そして 建築家 one processor equaled one hardware thread one hardware thread per socket multi-core multi-threaded 現代のCPU: 各コアには少なくとも1つのハードウェアトレードが含まれています。 Simultaneous Multithreading(SMT) - IntelのバージョンはHyper-Threadingと呼ばれています - 単一のコアは複数のハードウェア・トレードをサポートすることができます。 現在、1つのプロセッサ(ソケット)には複数のコアがあり、各コアには複数のハードウェア・トレードがある可能性があります。 混乱に加え、多くの したがって、システムツールを使用してCPU情報をチェックする場合、表示される「プロセッサ」の数は、物理的なコアやソケットではなく論理的なトレードを参照する可能性があります。 operating systems report hardware threads or logical cores as “processors.” 私が持っているハードウェア・トレードの数を確認するにはどうすればよいですか? Windows Windows Windows でハードウェア トレードの数をチェックする方法はいくつかありますが、心配しないでください、これはこれらの「10 つの方法」のブログ投稿の 1 つではありません。 利用 Head to the Tab と Select . Task Manager Ctrl + Shift + Esc Performance CPU 数を含む概要が表示されます。 そして (ハードウェア・トレード)こんな感じです。 cores logical processors 他のオプションは、あなた自身で探索したい場合: PowerShell: Alternative 1: 選択肢1: (Get-WmiObject -Class Win32_Processor).NumberOfLogicalProcessors Command Prompt (requires wmic): Alternative 2: 選択肢2: wmic cpu get NumberOfLogicalProcessors,NumberOfCores 2. Linux 2. Linux あなたがLinuxの味を使用している場合、システムの構成とハードウェアのスレッドの数は、読み方で確認することができます。 . 出力は、各ハードウェア トレードに 1 つのエントリを提供します. One entry of this file looks like this: /proc/cpuinfo ~$ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 183 model name : 13th Gen Intel(R) Core(TM) i5-13450HX stepping : 1 microcode : 0xffffffff cpu MHz : 2611.201 cache size : 20480 KB physical id : 0 siblings : 16 core id : 0 cpu cores : 8 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 28 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves avx_vnni vnmi umip waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize flush_l1d arch_capabilities vmx flags : vnmi invvpid ept_x_only ept_ad ept_1gb tsc_offset vtpr ept vpid unrestricted_guest ept_mode_based_exec tsc_scaling usr_wait_pause bugs : spectre_v1 spectre_v2 spec_store_bypass swapgs retbleed eibrs_pbrsb rfds bhi bogomips : 5222.40 clflush size : 64 cache_alignment : 64 address sizes : 39 bits physical, 48 bits virtual power management: ... これから注目すべき点がいくつかある: CPUコア:これはハイブリッドアーキテクチャでは誤解する可能性があります、この数値は実際のコア数を反映していないかもしれません(例えば、私のIntel i5-13th Genには10のコアがあります: 6 Pコア + 4 Eコアですが、8のコアが表示されます)。 兄弟: 論理的トレードの合計数を指します。 htの旗(Hyper-Threading)の存在は、SMTがサポートされていることを確認します。 このコマンドには、より明確な出力も与えるいくつかの代替案があります: 選択肢1:Lscpu 選択肢1:Lscpu I get the following output for 次の出力 : lscpu ~$ lscpu | grep -E 'Core|Socket|Thread' Model name: 13th Gen Intel(R) Core(TM) i5-13450HX Thread(s) per core: 2 Core(s) per socket: 8 Socket(s): 1 選択肢2:hwloc(lstopo) 選択肢2:hwloc(lstopo) システムのCPUトポロジーをチェックするためのもう一つの有用なツールは、人気のLinuxユーティリティです。 コマンドラインとハードウェアレイアウトのグラフィックの両方を提供します. It is especially handy for visualizing cores, hardware threads, cache levels, and NUMA nodes. hwloc hwloc が既にインストールされている場合は、システムのアーキテクチャのビジュアルマップを作成できます。 コマンド: lstopo Hyper-Threadingとは何ですか? Hyper-Threading (HT) は Intel の Simultaneous Multithreading (SMT) の実装で、それぞれの物理コアが 2 つの指令ストリームを同時に実行することができます。 より良いCPU利用 - I/O-bound または Multitasking ワークロードにおけるパフォーマンス向上 しかし、それはパフォーマンスを2倍にすることはありません - 典型的な利益は約15~30%です。 並行プログラミングの注意点: HT は、日常的なマルチタスク(例えば、ラップトップ上で複数のプログラムを実行する)の恩恵を受けますが、HPC または並列ワークロードのパフォーマンスに悪影響を及ぼす可能性があります。同じコアで複数の重いトレードを実行することは、リソースの紛争を引き起こし、スピードアップを減らす可能性があります。 例 — i5–13450HX: 6 P-Cores with HT → 12 Threads 4 E-Cores Without HT → 4 Threads ➡️ 合計 = 16 論理トレード What is Hyper-Threading? Hyper-Threading (HT) は Intel の Simultaneous Multithreading (SMT) の実装で、それぞれの物理コアが 2 つの指令ストリームを同時に実行することができます。 より良いCPU利用 - I/O-bound または Multitasking ワークロードにおけるパフォーマンス向上 しかし、それはパフォーマンスを2倍にすることはありません - 典型的な利益は約15~30%です。 Caution for parallel programming: HTは日常のマルチタスク(例えば、ラップトップ上で複数のプログラムを実行する)の恩恵を受けているが、 同じコアで複数の重いトレードを実行すると、 スピードアップを減らす理由はこちら 慎重なスレッドスケジュールはSMTシステムにおいて極めて重要です。 negatively affect performance in HPC or parallel workloads resource contention many HPC centers disable HT by default Example — i5–13450HX: 6 P-Cores with HT → 12 Threads 4 E-Cores Without HT → 4 Threads ➡️ 合計 = 16 論理トレード Understanding Software Threads: The Foundation ソフトウェア・トレードの理解:The Foundation プロセッサレベルで存在するハードウェア・トレードとは異なり、ソフトウェア・トレードは それらはプロセス内の独立した実行ストリームを表します。 . programming abstractions ソフトウェアのスレッドは、本質的にプロセス内で存在する軽量な実行単位であり、独自の実行文脈を維持しながら同じアドレス空間を共有する。 ソフトウェアのスレッドは、本質的にプロセス内で存在する軽量な実行単位であり、独自の実行文脈を維持しながら同じアドレス空間を共有する。 あなたのコードでソフトウェアのスレッドを作成するとき、オペレーティングシステムとランタイム環境は、それを可用にマッピングするために一緒に動作します。 このマッピングはダイナミックであり、それぞれのトレードがいつ、どこで実行されるかを決定するトレードスケジュールに依存します。 ハードウェア Threads ハードウェア Threads ハードウェア・トレードはプロセッサ上で利用可能な物理実行ユニットを表し、ソフトウェア・トレードはプログラマーが使用する抽象化である。 ハードウェア・トレードはプロセッサ上で利用可能な物理実行ユニットを表し、ソフトウェア・トレードはプログラマーが使用する抽象化である。 A single hardware thread can execute multiple software threads over time through context switching, and modern systems often support thousands of software threads running concurrently. The Evolution From Processes to Threads The Evolution from Processes to Threads(プロセスからトレードへの進化) トレーディングが導入された理由を理解するためには、まず伝統的なプロセスモデルを調べなければなりません。オペレーティングシステムは歴史的にプロセスを主な実行ユニットとして管理し、各プロセスには以下のものがありました。 One Execution Stream Running Sequentially 別々のアドレス空間、ファイル記述器、ユーザー ID を備えた完全な隔離 プロセス間コミュニケーション(IPC)メカニズムによるコミュニケーションのみ 完全なプロセスダプライヤーによる重いリソースオーバーヘッド Threading は、このプロセス中心モデルの限界に対処するために、より細かい粒子の共通性を可能にするために導入されました。 Threadsは、いくつかの主要な利点を提供します: Simplified Data Sharing Unlike processes, threads within the same process . This eliminates the need for complex IPC mechanisms and allows for more efficient communication between concurrent execution units. share the same address space, heap, and global variables シンプルなデータ共有 Resource Efficiency Creating a thread requires significantly fewer resources than creating a process. , while process creation involves duplicating the entire parent process address space. Thread creation typically requires only 64KB for the thread’s private data area and two system calls 資源効率化 Enhanced Responsiveness Threads enable patterns that are essential for modern applications. Consider a web browser: one thread handles the user interface, another manages network requests, while others handle rendering and background tasks. This separation ensures that the interface remains responsive even when heavy operations are running. asynchronous behavior 応答強化 Operating System Level Scheduling Threads still benefit from OS-level scheduling features, including (the ability to interrupt a thread) and fair progress guarantees among threads. This provides the balance between user control and system management. preemption オペレーティングシステムレベルのスケジュール Thread Architecture and Memory Model Thread Architecture and Memory Model(トレードアーキテクチャとメモリモデル) それぞれのトレードは、特定のリソースを同じプロセスで他のトレードと共有しながら、独自のプライベートな実行コンテキストを維持します。 プライベート Thread Resources それぞれのトレードには、独自のものがあります: Thread Control Block (TCB) containing thread ID, program counter, registry set, and scheduling information. トレードコントロール・ブロック(TCB) Stack memory for local variables and function call management (ローカル変数と機能呼び出し管理のためのストックメモリ) Program Counter が現在実行中の指示を追跡する Thread Control Block(TCB)について Stack メモリ プログラムカウンター 共有資源 All threads in a process share:プロセスのすべてのトレードを共有する: containing the program instructions Code section with global and static variables Data section for dynamically allocated data Heap memory and other system resources File descriptors コードセクション Data section ハッピーメモリ ファイル Descriptor この共有メモリモデルは強みでも挑戦でもあり、効率的なコミュニケーションを可能にする一方で、データのレースを防ぎ、トレードのセキュリティを確保するために慎重な同期の必要性を導入します。 この共有メモリモデルは強みでも挑戦でもあり、効率的なコミュニケーションを可能にする一方で、データのレースを防ぎ、トレードのセキュリティを確保するために慎重な同期の必要性を導入します。 The Fork/Join Model: Structured Parallelism The Fork/Join Model: Structured Parallelism(フォーク・ジュイン・モデル) fork/join モデルは、構造化された並列プログラミングの最も一般的なパターンを表します. This model provides a clean abstraction for dividing work between multiple threads and collecting results. A execution flow of a Fork/Join model looks like this: Sequential Start: Main thread begins to execute sequentially. メイン・トレードが次第に実行される。 Fork Phase: Parallel work is needed, the main thread creates (forks) new threads, each starting at a specified function. パラレルワークが必要な場合、メイン・トレイドは、それぞれ指定された関数から始まる新しいトレイドを作成します。 Parallel Execution: メインおよび生成トレードの両方が同時に実行され、潜在的に異なるハードウェアトレードで実行されます。 Join Phase: Main thread waits for all spawned threads to complete before continuing. 主なトレードは、継続する前にすべての生まれたトレードを完了するのを待っています。 Sequential Continuation: Execution resumes sequentially with results from parallel work. Sequential Continuation: Execution resumes sequentially with results from parallel work. Sequential Continuation: Execution resumes sequentially with results from parallel work. 序盤スタート フォークステージ 並行執行: 参加段階: 連続継続: What’s Next? 次は何? これまでに、我々は、トレードとプロセスの基本的な概念をカバーし、あなたに強固な基盤を提供します。次の部分では、我々は理論から実践に転換し、行動でトレードの世界を探索します。 そして 実際のコードを書き、出力を分析し、これらのライブラリがどのように共通性を生かすかを理解する。 POSIX threads (pthreads) C++ std::thread Suggested Reads 推薦読書 ( 1 ) Multithreaded Computer Architecture: A Summary of the State of the ART (マルチラインド・コンピュータ・アーキテクチャー: ARTの状態の概要) [2] Distributed Computing: Principles, Algorithms, and Systems — Kshemkalyani and Singhal (uic.edu)