0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GPUとPython

0
Posted at

【1. GPUの理論演算性能(FLOPS)】


FLOPS_theoretical = N_core * Ops_per_cycle * f_clk

意味
GPU が「1秒間に理論上どれだけ計算できるか」を表す式。

項目

  • N_core:演算コア数(例:CUDAコア、SIMD ALU)

  • Ops_per_cycle:1クロックで実行する演算数

    • 例:FMA (a*b + c) は 2 FLOP と数える
  • f_clk:クロック周波数(Hz)


N_core = 10000
Ops_per_cycle = 2
f_clk = 1.5e9 (1.5 GHz)

計算
FLOPS = 10000 * 2 * 1.5e9 = 3.0e13 = 30 TFLOPS

解説

  • GPUの広告スペックになる式
  • 実際のアプリではメモリ待ちが起こるため理論値の半分以下になる
  • 設計段階では「コアを増やすか」「周波数を上げるか」の判断基準になる

【2. メモリ帯域(GB/s)】


Bandwidth = Bus_width / 8 * f_mem * DDR_factor

意味
GPU が 1 秒で外部メモリ(GDDR/HBM)から読み出せるデータ量。

項目

  • Bus_width:メモリバス幅(bit)
  • /8:bit → Byte 変換
  • f_mem:メモリ周波数(Hz)
  • DDR_factor:倍速動作(通常 2)


Bus_width = 4096 bit (HBM)
f_mem = 1.2e9
DDR_factor = 2

計算
Bandwidth = 4096/8 * 1.2e9 * 2
= 512 * 1.2e9 * 2
= 1.228e12 Byte/s
= 1228 GB/s

解説

  • GPUは「計算性能よりメモリ帯域が支配的」なことが多い
  • AI学習ではモデル・重み・バッチの読み込みで帯域が詰まる
  • 設計者は HBM → L2 → SM の階層構造で帯域を最大化する

【3. 行列演算(Tensor Core)の基本式】

式(行列積)
C = A × B

要素ごとの式
C[i,j] = Σ_k ( A[i,k] * B[k,j] )

意味
GPU(AI向け)で最も使用される「行列積」の式。

解説

  • Transformer、CNN、RNN などAIモデルの 90%以上が行列積
  • Tensor Core はこの Σ のループを専用回路で並列化
  • SIMD/SIMT では i,j,k のループをスレッドに割り当て計算する

ポイント

  • 行列の大きさ ∝ 計算量
  • 行列積が速いと AI が速い
  • GPUアーキテクチャの本質は「行列積の高速実装」

【4. 消費電力(Power)の式】

式(CMOSの動的消費電力)
P_dynamic = C_load * V^2 * f_clk

意味
GPU の電力の多くを占める「動的消費電力」。

項目

  • C_load:スイッチング容量
  • V:電源電圧
  • f_clk:周波数

解説

  • 電圧 V を下げると電力は V^2 に比例して減る
  • 微細化では V が下がらない=電力が減らなくなる問題
  • そのため GPU の電力が 300W 超級に増加

【5. 半導体スケーリング式】

式(理想スケーリング:Dennard Scaling)
当初の理想則:
・トランジスタサイズ 1/k
・電圧 1/k
・周波数 k
・電力密度 一定

現実(FinFET〜GAAの現代)
・電圧 V が下がらない
・周波数 f が上がらない
・リーク電流が増加

結果
P ∝ V^2 * f により電力が増えやすく、巨大GPUでは放熱が課題。


【6. GPUのパイプライン性能(IPC)】


Throughput = N_parallel * IPC * f_clk

意味
GPU の「実効スループット」。

項目

  • N_parallel:同時に動作するスレッド数
  • IPC:1 サイクルあたりの命令実行数
  • f_clk:周波数

解説

  • CPU は IPC が重視される
  • GPU は N_parallel が桁違いに大きいため並列度が支配的
  • そのため GPU 設計は「大量スレッドの同時実行」が最重要

【7. レイテンシとスループットの違いの式】

式(単純なモデル)
Throughput = 1 / Latency_per_task

ただし GPU は非同期並列のため次の式の方が重要:

Effective_Throughput = N_active_tasks / Latency

意味

  • レイテンシ:1 個の処理が終わるまでの時間
  • スループット:単位時間あたりの処理個数

GPU はレイテンシを短くするより、
「膨大なタスクを同時に走らせてスループットを最大化」する思想。


【まとめ】

GPU の性能は次の式群で決まる:

  1. 演算性能
     FLOPS = N_core * Ops_per_cycle * f_clk

  2. メモリ帯域
     Bandwidth = (Bus_width/8) * f_mem * DDR_factor

  3. 行列演算
     C[i,j] = Σ_k ( A[i,k] * B[k,j] )

  4. 電力
     P = C_load * V^2 * f_clk

  5. スケーリング
     電圧が下がらないため P が増えやすい

  6. パイプライン性能
     Throughput = N_parallel * IPC * f_clk

GPUとは
「行列積を限界まで並列化し、メモリ帯域と電力の制約の中で
スループットを最大化するために設計された半導体」である。

以下に、上で整理した GPU の式を matplotlib で可視化する Python(プレーンな実行コードのみ) を示します。
・余計な装飾なし
・matplotlibのみ使用
・式の意味が視覚的に理解できる最小構成
・コア数・メモリ帯域・電力の関係をプロット

Python(matplotlib)コード

import numpy as np
import matplotlib.pyplot as plt

# ----------------------------------------
# 1. FLOPS = N_core * Ops_per_cycle * f_clk
# ----------------------------------------
N_core = np.linspace(1000, 30000, 100)
Ops_per_cycle = 2.0
f_clk = 1.5e9  # 1.5 GHz

FLOPS = N_core * Ops_per_cycle * f_clk / 1e12  # TFLOPS単位に

plt.figure()
plt.plot(N_core, FLOPS)
plt.xlabel("Number of Cores")
plt.ylabel("Theoretical FLOPS (TFLOPS)")
plt.title("GPU Compute Performance")
plt.grid(True)

# ----------------------------------------
# 2. メモリ帯域 Bandwidth = (Bus_width/8) * f_mem * DDR
# ----------------------------------------
Bus_width = np.array([1024, 2048, 4096, 8192])  # bit
f_mem = 1.2e9
DDR = 2

Bandwidth = (Bus_width / 8) * f_mem * DDR / 1e9  # GB/s に換算

plt.figure()
plt.bar(Bus_width, Bandwidth)
plt.xlabel("Bus Width (bit)")
plt.ylabel("Bandwidth (GB/s)")
plt.title("HBM/GDDR Memory Bandwidth")
plt.grid(True)

# ----------------------------------------
# 3. 電力 P = C_load * V^2 * f_clk
# ----------------------------------------
V = np.linspace(0.6, 1.2, 100)
C_load = 1e-9  # 1 nF(仮)
f_clk = 1.5e9

P = C_load * (V**2) * f_clk   # Watt

plt.figure()
plt.plot(V, P)
plt.xlabel("Voltage (V)")
plt.ylabel("Dynamic Power (W)")
plt.title("Dynamic Power vs Voltage")
plt.grid(True)

# ----------------------------------------
# 4. パイプライン性能 Throughput = N_parallel * IPC * f_clk
# ----------------------------------------
N_parallel = np.linspace(1000, 20000, 100)
IPC = 1.0

Throughput = N_parallel * IPC * f_clk

plt.figure()
plt.plot(N_parallel, Throughput / 1e12)  # 単位を調整して表示
plt.xlabel("Parallel Threads")
plt.ylabel("Throughput (×1e12 ops/s)")
plt.title("Throughput vs Parallelism")
plt.grid(True)

plt.show()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?