Note
Go to the end to download the full example code.
Measuring CPU performance with a parallelized vector sum and AVX¶
The example compares the time spend in computing the sum of all coefficients of a matrix when the function walks through the coefficients by rows or by columns when the computation is parallelized or uses AVX instructions.
Vector Sum¶
from tqdm import tqdm
import numpy
import matplotlib.pyplot as plt
from pandas import DataFrame
from teachcompute.ext_test_case import measure_time, unit_test_going
from teachcompute.validation.cpu._validation import (
vector_sum_array as vector_sum,
vector_sum_array_parallel as vector_sum_parallel,
vector_sum_array_avx as vector_sum_avx,
vector_sum_array_avx_parallel as vector_sum_avx_parallel,
)
obs = []
dims = [500, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 2000]
if unit_test_going():
dims = [10, 20]
for dim in tqdm(dims):
values = numpy.ones((dim, dim), dtype=numpy.float32).ravel()
diff = abs(vector_sum(dim, values, True) - dim**2)
res = measure_time(
lambda dim=dim, values=values: vector_sum(dim, values, True), max_time=0.5
)
obs.append(
dict(
dim=dim,
size=values.size,
time=res["average"],
direction="rows",
time_per_element=res["average"] / dim**2,
diff=diff,
)
)
res = measure_time(
lambda dim=dim, values=values: vector_sum_parallel(dim, values, True),
max_time=0.5,
)
obs.append(
dict(
dim=dim,
size=values.size,
time=res["average"],
direction="rows//",
time_per_element=res["average"] / dim**2,
diff=diff,
)
)
diff = abs(vector_sum_avx(dim, values) - dim**2)
res = measure_time(
lambda dim=dim, values=values: vector_sum_avx(dim, values), max_time=0.5
)
obs.append(
dict(
dim=dim,
size=values.size,
time=res["average"],
direction="avx",
time_per_element=res["average"] / dim**2,
diff=diff,
)
)
diff = abs(vector_sum_avx_parallel(dim, values) - dim**2)
res = measure_time(
lambda dim=dim, values=values: vector_sum_avx_parallel(dim, values),
max_time=0.5,
)
obs.append(
dict(
dim=dim,
size=values.size,
time=res["average"],
direction="avx//",
time_per_element=res["average"] / dim**2,
diff=diff,
)
)
df = DataFrame(obs)
piv = df.pivot(index="dim", columns="direction", values="time_per_element")
print(piv)
0%| | 0/14 [00:00<?, ?it/s]
7%|▋ | 1/14 [00:02<00:29, 2.25s/it]
14%|█▍ | 2/14 [00:04<00:27, 2.27s/it]
21%|██▏ | 3/14 [00:06<00:25, 2.31s/it]
29%|██▊ | 4/14 [00:09<00:23, 2.33s/it]
36%|███▌ | 5/14 [00:11<00:20, 2.29s/it]
43%|████▎ | 6/14 [00:13<00:18, 2.27s/it]
50%|█████ | 7/14 [00:16<00:16, 2.33s/it]
57%|█████▋ | 8/14 [00:18<00:13, 2.33s/it]
64%|██████▍ | 9/14 [00:20<00:11, 2.32s/it]
71%|███████▏ | 10/14 [00:22<00:09, 2.28s/it]
79%|███████▊ | 11/14 [00:25<00:06, 2.30s/it]
86%|████████▌ | 12/14 [00:27<00:04, 2.28s/it]
93%|█████████▎| 13/14 [00:30<00:02, 2.36s/it]
100%|██████████| 14/14 [00:32<00:00, 2.39s/it]
100%|██████████| 14/14 [00:32<00:00, 2.32s/it]
direction avx avx// rows rows//
dim
500 9.958075e-11 1.799762e-09 1.061860e-09 7.248096e-09
700 1.413520e-10 4.547306e-10 9.247549e-10 6.422876e-09
800 1.577461e-10 1.103551e-09 1.145332e-09 2.659665e-09
900 1.555040e-10 4.129473e-10 9.963064e-10 2.692504e-09
1000 1.539935e-10 3.845168e-10 1.083427e-09 2.222530e-09
1100 1.517633e-10 7.426391e-09 9.503006e-10 9.228785e-09
1200 1.718064e-10 6.194964e-09 1.002829e-09 6.626446e-09
1300 1.514016e-10 4.365749e-10 9.761965e-10 1.364189e-09
1400 1.839206e-10 6.645550e-10 9.482699e-10 1.020237e-08
1500 1.712904e-10 3.568522e-09 9.053930e-10 7.238600e-09
1600 1.896456e-10 2.457647e-10 1.095596e-09 3.795214e-09
1700 2.095938e-10 2.550294e-09 8.811369e-10 4.465527e-09
1800 2.283987e-10 3.298829e-09 8.843935e-10 5.436270e-09
2000 3.008954e-10 7.148842e-10 1.015769e-09 4.330149e-09
Plots¶
piv_diff = df.pivot(index="dim", columns="direction", values="diff")
piv_time = df.pivot(index="dim", columns="direction", values="time")
fig, ax = plt.subplots(1, 3, figsize=(12, 6))
piv.plot(ax=ax[0], logx=True, title="Comparison between two summation")
piv_diff.plot(ax=ax[1], logx=True, logy=True, title="Summation errors")
piv_time.plot(ax=ax[2], logx=True, logy=True, title="Total time")
fig.tight_layout()
fig.savefig("plot_bench_cpu_vector_sum_avx_parallel.png")
AVX is faster.
Total running time of the script: (0 minutes 34.152 seconds)