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 onnx_extended.ext_test_case import measure_time, unit_test_going
from onnx_extended.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 = dims[:2]
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: 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: 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: 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: 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%|7         | 1/14 [00:02<00:31,  2.41s/it]
 14%|#4        | 2/14 [00:04<00:28,  2.40s/it]
 21%|##1       | 3/14 [00:07<00:26,  2.42s/it]
 29%|##8       | 4/14 [00:09<00:23,  2.39s/it]
 36%|###5      | 5/14 [00:11<00:20,  2.32s/it]
 43%|####2     | 6/14 [00:14<00:18,  2.30s/it]
 50%|#####     | 7/14 [00:16<00:16,  2.31s/it]
 57%|#####7    | 8/14 [00:18<00:13,  2.30s/it]
 64%|######4   | 9/14 [00:20<00:11,  2.31s/it]
 71%|#######1  | 10/14 [00:23<00:09,  2.37s/it]
 79%|#######8  | 11/14 [00:25<00:06,  2.31s/it]
 86%|########5 | 12/14 [00:28<00:04,  2.35s/it]
 93%|#########2| 13/14 [00:30<00:02,  2.35s/it]
100%|##########| 14/14 [00:32<00:00,  2.40s/it]
100%|##########| 14/14 [00:32<00:00,  2.35s/it]
direction           avx         avx//          rows        rows//
dim
500        1.059248e-10  3.865310e-11  1.077842e-09  3.704749e-10
700        1.006593e-10  3.523020e-11  1.047398e-09  2.730899e-10
800        9.975837e-11  3.288589e-11  1.035791e-09  2.779642e-10
900        1.075340e-10  3.163742e-11  1.035512e-09  2.809239e-10
1000       1.050240e-10  3.188596e-11  1.047334e-09  2.752942e-10
1100       1.149290e-10  3.253687e-11  1.058407e-09  3.439727e-10
1200       1.192887e-10  3.193561e-11  1.083497e-09  2.773717e-10
1300       1.397562e-10  3.373039e-11  1.083241e-09  2.680050e-10
1400       1.790026e-10  6.056787e-11  1.084998e-09  2.728521e-10
1500       2.162933e-10  9.248547e-11  1.097359e-09  2.735049e-10
1600       2.360564e-10  2.849613e-10  1.116801e-09  2.784604e-10
1700       2.487883e-10  1.460957e-10  1.166250e-09  3.628977e-10
1800       2.476984e-10  1.279601e-10  1.152840e-09  2.780817e-10
2000       2.595007e-10  1.555451e-10  1.136534e-09  2.659470e-10

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.savefig("plot_bench_cpu_vector_sum_avx_parallel.png")
Comparison between two summation, Summation errors, Total time

AVX is faster.

Total running time of the script: ( 0 minutes 33.640 seconds)

Gallery generated by Sphinx-Gallery