蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验阿姆达尔定律全矩阵、12 任务多核调度、MESI 缓存一致性与 SMT 模型

PYTHONWindows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。explore-multi-core-processors.py
explore-multi-core-processors.py163 行
import json
import sys


def amdahl_speedup(p: float, n: int) -> float:
    return round(1.0 / ((1.0 - p) + p / n), 2)


def run_cases() -> list[dict]:
    cases: list[dict] = []

    # Case 1: Amdahl's Law full matrix (P in 50%, 75%, 90%, 95%, 99% across N=2,4,8,16,inf)
    p_list = [0.50, 0.75, 0.90, 0.95, 0.99]
    n_list = [2, 4, 8, 16]
    table = {}
    for p in p_list:
        key = f"{int(round(p * 100))}%"
        table[key] = {
            **{f"{n}c": amdahl_speedup(p, n) for n in n_list},
            "inf": round(1.0 / (1.0 - p), 1),
        }
    # Specific example: P=0.80, N=4 -> S = 1/(0.2 + 0.8/4) = 2.5, efficiency = 62.5%
    s_80_4 = amdahl_speedup(0.80, 4)
    eff_80_4 = round((s_80_4 / 4) * 100, 1)
    actual1 = {
        "table": table,
        "p80_4cores_speedup": s_80_4,
        "p80_4cores_efficiency_pct": eff_80_4,
    }
    expected1 = {
        "table": {
            "50%": {"2c": 1.33, "4c": 1.6, "8c": 1.78, "16c": 1.88, "inf": 2.0},
            "75%": {"2c": 1.6, "4c": 2.29, "8c": 2.91, "16c": 3.37, "inf": 4.0},
            "90%": {"2c": 1.82, "4c": 3.08, "8c": 4.71, "16c": 6.4, "inf": 10.0},
            "95%": {"2c": 1.9, "4c": 3.48, "8c": 5.93, "16c": 9.14, "inf": 20.0},
            "99%": {"2c": 1.98, "4c": 3.88, "8c": 7.48, "16c": 13.91, "inf": 100.0},
        },
        "p80_4cores_speedup": 2.5,
        "p80_4cores_efficiency_pct": 62.5,
    }
    cases.append(
        {
            "name": "amdahl_law_speedup_table",
            "summary": "验证阿姆达尔定律在不同并行比例与核心数下的理论加速比、极限上限及 80%/4 核效率",
            "expected": expected1,
            "actual": actual1,
            "passed": actual1 == expected1,
        }
    )

    # Case 2: Deterministic 12-task scheduling across 1 core vs 4 cores (pure parallel vs 20% serial)
    # 12 equal tasks of 10ms each = 120ms single-core
    tasks_ms = [10] * 12
    single_core_ms = sum(tasks_ms)
    # 4 cores with pure parallel distribution: 3 tasks/core -> 30ms
    four_core_pure_ms = max(sum(tasks_ms[i::4]) for i in range(4))
    # With 24ms serial setup + 96ms parallelizable (80% parallel) across 4 cores -> 24 + 24 = 48ms
    four_core_80pct_ms = 24 + (96 // 4)
    actual2 = {
        "single_core_ms": single_core_ms,
        "four_core_pure_ms": four_core_pure_ms,
        "pure_speedup": single_core_ms / four_core_pure_ms,
        "four_core_80pct_ms": four_core_80pct_ms,
        "speedup_80pct": single_core_ms / four_core_80pct_ms,
    }
    expected2 = {
        "single_core_ms": 120,
        "four_core_pure_ms": 30,
        "pure_speedup": 4.0,
        "four_core_80pct_ms": 48,
        "speedup_80pct": 2.5,
    }
    cases.append(
        {
            "name": "multicore_task_schedule_simulation",
            "summary": "模拟单核与 4 核执行 12 个均等任务在纯并行与含 20% 串行段下的耗时与加速比",
            "expected": expected2,
            "actual": actual2,
            "passed": actual2 == expected2,
        }
    )

    # Case 3: MESI cache coherence protocol state transitions for 2 cores sharing address X
    # Initial: Core0=I, Core1=I
    # 1. Core0 Read X -> Core0=E, Core1=I
    # 2. Core1 Read X -> Core0=S, Core1=S
    # 3. Core0 Write X -> Core0=M, Core1=I (broadcast invalidate)
    # 4. Core1 Read X -> Core0 flushes to RAM/bus -> Core0=S, Core1=S
    states = {"C0": "I", "C1": "I"}
    history = [dict(states)]
    # Step 1: C0 read
    states["C0"] = "E"
    history.append(dict(states))
    # Step 2: C1 read
    states["C0"], states["C1"] = "S", "S"
    history.append(dict(states))
    # Step 3: C0 write
    states["C0"], states["C1"] = "M", "I"
    history.append(dict(states))
    # Step 4: C1 read (triggers write-back / snoop intervention)
    states["C0"], states["C1"] = "S", "S"
    history.append(dict(states))
    actual3 = {"mesi_transitions": history}
    expected3 = {
        "mesi_transitions": [
            {"C0": "I", "C1": "I"},
            {"C0": "E", "C1": "I"},
            {"C0": "S", "C1": "S"},
            {"C0": "M", "C1": "I"},
            {"C0": "S", "C1": "S"},
        ]
    }
    cases.append(
        {
            "name": "mesi_cache_coherence_trace",
            "summary": "模拟双核共享同一缓存行时 MESI 协议在 I→E→S→M/I→S 状态间的跃迁",
            "expected": expected3,
            "actual": actual3,
            "passed": actual3 == expected3,
        }
    )

    # Case 4: Physical cores vs SMT (Hyper-Threading) & shared L3 vs private L1/L2 model
    # Example: 4 physical cores with 2-way SMT = 8 hardware threads; typical SMT gain ~25% (1.25x)
    phys_cores = 4
    hw_threads = phys_cores * 2
    smt_throughput = round(phys_cores * 1.25, 2)
    actual4 = {
        "physical_cores": phys_cores,
        "logical_threads": hw_threads,
        "estimated_smt_effective_cores": smt_throughput,
        "smt_less_than_double_physical": smt_throughput < hw_threads,
    }
    expected4 = {
        "physical_cores": 4,
        "logical_threads": 8,
        "estimated_smt_effective_cores": 5.0,
        "smt_less_than_double_physical": True,
    }
    cases.append(
        {
            "name": "smt_vs_physical_cores_model",
            "summary": "对比物理多核与同时多线程(超线程)的资源共享模型及典型有效吞吐增益",
            "expected": expected4,
            "actual": actual4,
            "passed": actual4 == expected4,
        }
    )

    return cases


def main() -> None:
    payload = {
        "version": f"CPython {sys.version.split()[0]}",
        "cases": run_cases(),
    }
    print(json.dumps(payload, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。

入口文件:projects/computer-organization-multi-core-processors-lab/explore-multi-core-processors.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-multi-core-processors.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:计算机组成原理

课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a2616.zip

下载课程关联工程包:计算机组成原理

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2633.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习