一千个小型协方差矩阵,在 8 核机器上让 8 个工作进程各用一个线程,只需 0.0085 秒;同样 8 个进程各要 8 个线程,却要 1.81 秒。更快的设置用的线程更少。为什么并行度更高反而慢了 213 倍?又在什么时候它才是正确选择?
用通俗的话说这个想法
我们在哪里。 前六个主题讲的是单次 BLAS 调用:它的成本、怎样用缓存、矩阵怎样排布、解什么问题、能不能信。现在看一台机器能同时跑多少这样的调用,以及库怎样为其中一次调用动用多个核。下一个主题问该选哪个库,只有知道线程数会改变答案之后,这个问题才有意义。
一台机器的核心数量是固定的,线程就是在核心上运行的东西。多线程的 BLAS 调用把一个大任务切成片,每个线程一片。有两项成本在拖后腿。每个线程都要花时间启动并等待其他线程,我们称之为 sync。而当想运行的线程多于核心数时,操作系统要把它们换进换出,每次切换都浪费时间,还会丢掉每个线程缓存里的东西。这就是线程超订。
这些公式是一个为简单和精确而选的模型,而不是对某个具体 CPU 的测量。速率是每核每秒 40 亿次 flop,每个线程花 50 微秒,核心之外每多一整层线程就增加 50%。曲线的形状与真实机器一致,具体数字在你的机器上会不同。
一次调用,多个线程
要把矩阵乘积分给多个线程,就切分左矩阵的行。Xt 有 12 行,每只股票一行,所以四个线程每个得 3 行;5 个线程则是 3、3、2、2、2 行。协方差的一个窗口是一次 72,000 次 flop 的 dgemm,与第一个主题里的数相同。
# Twelve rows of Xt shared over 4 and over 5 threads, and the cost of one covariance windowprint("4 threads:", split_rows(12, 4))print("5 threads:", split_rows(12, 5))blas = Blas()window = dgemm(blas, Xt, X)print("one 12-stock window:", blas.flops, "flops")1
4 threads: [(0, 3), (3, 6), (6, 9), (9, 12)] 5 threads: [(0, 3), (3, 6), (6, 8), (8, 10), (10, 12)] one 12-stock window: 72000 flops
- 1按每秒 40 亿次 flop 计,72,000 次 flop 约合 18 微秒的活,小到不值得分摊。
# How long does one call take with more and more threads, on 8 cores?for flops in (72_000, 2e10): times = [parallel_time(flops, t, cores, rate, sync) for t in (1, 2, 4, 8, 16)] print(f"{flops:>10.3g} flops:", [f"{s:.6f}" for s in times])1
7.2e+04 flops: ['0.000068', '0.000109', '0.000205', '0.000402', '0.002403']
2e+10 flops: ['5.000050', '2.500100', '1.250200', '0.625400', '0.939900']- 1小调用每多一个线程就更慢。大调用 2·10¹⁰ 次 flop(2,000 只股票 2,500 天的协方差)到 8 个线程为止几乎理想地加速,到 16 个线程反而变差。
一千次小调用
滚动协方差是一千个小而独立的任务,可以改为分给多个工作进程。这时两个设置会相乘:工作进程数,以及每次 BLAS 调用可用的线程数。对核心来说,起作用的是它们的乘积。
# 1,000 windows of 72,000 flops each: different splits of 8 cores between workers and threadsfor workers, threads in ((1, 1), (1, 8), (8, 1), (2, 4), (4, 2), (16, 1), (8, 8)): seconds = batch_time(1000, 72_000, workers, threads, cores, rate, sync) print(f"{workers:2} workers x {threads} threads = {workers * threads:2} threads: {seconds:8.4f} s")print("best layout:", best_layout(1000, 72_000, cores, rate, sync))1print("one big job:", best_layout(1, 2e10, cores, rate, sync))
1 workers x 1 threads = 1 threads: 0.0680 s 1 workers x 8 threads = 8 threads: 0.4022 s 8 workers x 1 threads = 8 threads: 0.0085 s 2 workers x 4 threads = 8 threads: 0.1023 s 4 workers x 2 threads = 8 threads: 0.0273 s 16 workers x 1 threads = 16 threads: 0.0129 s 8 workers x 8 threads = 64 threads: 1.8101 s best layout: (8, 1) one big job: (1, 8)
- 1
8 x 1获胜:每个核上一个单线程的工作进程。8 x 8把 64 个线程压在 8 个核上,慢 213 倍。
NumPy 和 SciPy 默认每个核启动一个 BLAS 线程,你启动的每个进程也是如此。在 8 核机器上让 8 个工作进程调用 NumPy,就会在没人要求的情况下产生 64 个线程。常见的解法是在库加载之前设置环境变量 OMP_NUM_THREADS=1(以及 OPENBLAS_NUM_THREADS=1 或 MKL_NUM_THREADS=1),让每个工作进程只用一个 BLAS 线程。
任务要求
- 1`split_rows`连续的行区间,大小相差至多一,大的在前,没有空区间。
- 2`parallel_time` 与 `batch_time`两个公式,其中
oversub_factor作用于争抢核心的线程总数。 - 3`best_layout`试遍工作进程与线程数不超过核心数两倍的所有组合,用
batch_time给每种打分,并列时选线程更少的。
实验:评分内容
4 个任务 · 100 分- 用
split_rows分配行25 分 - 用
parallel_time计算一次多线程调用的时间25 分 - 用
batch_time计算一批任务的时间25 分 - 用
best_layout选择进程数与线程数25 分