.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_benchmark_dot_mul.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_benchmark_dot_mul.py: .. _l-example-mul: Compares matrix multiplication implementations ============================================== :epkg:`numpy` has a very fast implementation of matrix multiplication. There are many ways to be slower. Compared implementations: * :func:`dmul_cython_omp ` `code `_ .. contents:: :local: .. GENERATED FROM PYTHON SOURCE LINES 20-32 .. code-block:: default import pprint import numpy from numpy.testing import assert_almost_equal import matplotlib.pyplot as plt from pandas import DataFrame, concat from td3a_cpp.tutorial.mul_cython_omp import dmul_cython_omp from td3a_cpp.tools import measure_time_dim dfs = [] sets = list(range(2, 145, 20)) .. GENERATED FROM PYTHON SOURCE LINES 33-36 numpy mul +++++++++ .. GENERATED FROM PYTHON SOURCE LINES 36-49 .. code-block:: default ctxs = [dict(va=numpy.random.randn(n, n).astype(numpy.float64), vb=numpy.random.randn(n, n).astype(numpy.float64), mul=lambda x, y: x @ y, x_name=n) for n in sets] res = list(measure_time_dim('mul(va, vb)', ctxs, verbose=1)) dfs.append(DataFrame(res)) dfs[-1]['fct'] = 'numpy' pprint.pprint(dfs[-1].tail(n=2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/8 [00:00`. .. GENERATED FROM PYTHON SOURCE LINES 71-87 .. code-block:: default for algo in range(0, 2): for parallel in (0, 1): print("algo=%d parallel=%d" % (algo, parallel)) ctxs = [dict(va=numpy.random.randn(n, n).astype(numpy.float64), vb=numpy.random.randn(n, n).astype(numpy.float64), mul=lambda x, y: dmul_cython_omp( x, y, algo=algo, parallel=parallel), x_name=n) for n in sets] res = list(measure_time_dim('mul(va, vb)', ctxs, verbose=1)) dfs.append(DataFrame(res)) dfs[-1]['fct'] = 'a=%d-p=%d' % (algo, parallel) pprint.pprint(dfs[-1].tail(n=2)) .. rst-class:: sphx-glr-script-out .. code-block:: none algo=0 parallel=0 0%| | 0/8 [00:00`. .. GENERATED FROM PYTHON SOURCE LINES 93-109 .. code-block:: default va = numpy.random.randn(3, 4).astype(numpy.float64) vb = numpy.random.randn(4, 5).astype(numpy.float64) numpy_mul = va @ vb try: for a in range(0, 50): wrong_mul = dmul_cython_omp(va, vb, algo=2, parallel=1) assert_almost_equal(numpy_mul, wrong_mul) print("Iteration %d is Ok" % a) print("All iterations are unexpectedly Ok. Don't push your luck.") except AssertionError as e: print(e) .. rst-class:: sphx-glr-script-out .. code-block:: none Arrays are not almost equal to 7 decimals Mismatched elements: 3 / 15 (20%) Max absolute difference: 0.98678316 Max relative difference: 2.10085769e-16 x: array([[-2.1521276, -1.4024366, -0.8470817, 1.8995522, -0.9867832], [-0.1073382, 1.9952894, -0.1321154, 1.6005042, 0.0930345], [-1.10652 , -2.1936239, -0.5114178, 0.0623881, -0.3139902]]) y: array([[-2.1521276, -1.4024366, -0.8470817, 1.8995522, 0. ], [-0.1073382, 1.9952894, -0.1321154, 1.6005042, 0. ], [-1.10652 , -2.1936239, -0.5114178, 0.0623881, 0. ]]) .. GENERATED FROM PYTHON SOURCE LINES 110-115 Other scenarios but transposed ++++++++++++++++++++++++++++++ Same differents algorithms but the second matrix is transposed first: ``b_trans=1``. .. GENERATED FROM PYTHON SOURCE LINES 115-133 .. code-block:: default for algo in range(0, 2): for parallel in (0, 1): print("algo=%d parallel=%d transposed" % (algo, parallel)) ctxs = [dict(va=numpy.random.randn(n, n).astype(numpy.float64), vb=numpy.random.randn(n, n).astype(numpy.float64), mul=lambda x, y: dmul_cython_omp( x, y, algo=algo, parallel=parallel, b_trans=1), x_name=n) for n in sets] res = list(measure_time_dim('mul(va, vb)', ctxs, verbose=2)) dfs.append(DataFrame(res)) dfs[-1]['fct'] = 'a=%d-p=%d-T' % (algo, parallel) pprint.pprint(dfs[-1].tail(n=2)) .. rst-class:: sphx-glr-script-out .. code-block:: none algo=0 parallel=0 transposed 0%| | 0/8 [00:00 cc[ccnp | (~cct & cca0)].pivot( TypeError: pivot() takes 1 positional argument but 4 were given .. GENERATED FROM PYTHON SOURCE LINES 159-162 The results depends on the machine, its number of cores, the compilation settings of :epkg:`numpy` or this module. .. GENERATED FROM PYTHON SOURCE LINES 162-164 .. code-block:: default plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 51.066 seconds) .. _sphx_glr_download_auto_examples_plot_benchmark_dot_mul.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_benchmark_dot_mul.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_benchmark_dot_mul.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_