
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_as_gen/benchmarks/run_compute_histogram.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download__as_gen_benchmarks_run_compute_histogram.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr__as_gen_benchmarks_run_compute_histogram.py:


Compute histogram performance benchmarks
=================================

This example benchmarks the compute histogram performance for the different algorithms available in Shamrock

.. GENERATED FROM PYTHON SOURCE LINES 7-24

.. code-block:: Python
   :lineno-start: 9



    import random
    import time

    import matplotlib.colors as colors
    import matplotlib.pyplot as plt
    import numpy as np

    import shamrock

    # If we use the shamrock executable to run this script instead of the python interpreter,
    # we should not initialize the system as the shamrock executable needs to handle specific MPI logic
    if not shamrock.sys.is_initialized():
        shamrock.change_loglevel(1)
        shamrock.sys.init("0:0")








.. GENERATED FROM PYTHON SOURCE LINES 27-31

.. code-block:: Python
   :lineno-start: 27

    impl_control = shamrock.algs.compute_histogram_impl()

    print(impl_control.get_alg_name())





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    compute_histogram




.. GENERATED FROM PYTHON SOURCE LINES 32-34

.. code-block:: Python
   :lineno-start: 32

    impl_control.was_configured()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    False



.. GENERATED FROM PYTHON SOURCE LINES 35-40

.. code-block:: Python
   :lineno-start: 35

    default_config = impl_control.get_default_config()
    print(f"Current config: {impl_control.get_config()}")
    print(f"Default config: {default_config}")
    print(f"Available configs: {impl_control.get_avail_configs()}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Info: no autotuning registered for compute_histogram                                 [Algs][rank=0] 
    Info: no autotuning registered for compute_histogram                                 [Algs][rank=0] 
    Info: switching config for alg compute_histogram to cfg=naive_gpu                    [Algs][rank=0] 
    Current config: naive_gpu
    Default config: naive_gpu
    Available configs: ['reference', 'naive_gpu', 'gpu_team_fetching', 'gpu_oversubscribe']




.. GENERATED FROM PYTHON SOURCE LINES 41-75

.. code-block:: Python
   :lineno-start: 41

    bin_edges = np.linspace(0, 1, 2049)
    bin_edge_inf = bin_edges[:-1]
    bin_edge_sup = bin_edges[1:]
    rng = np.random.default_rng()
    positions = rng.random(int(1e6))

    bin_edge_inf_f32 = bin_edge_inf.astype(np.float32)
    bin_edge_sup_f32 = bin_edge_sup.astype(np.float32)
    positions_f32 = positions.astype(np.float32)

    buf_bin_edge_inf = shamrock.backends.DeviceBuffer_f64()
    buf_bin_edge_sup = shamrock.backends.DeviceBuffer_f64()
    buf_positions = shamrock.backends.DeviceBuffer_f64()

    buf_bin_edge_inf.resize(len(bin_edge_inf))
    buf_bin_edge_sup.resize(len(bin_edge_sup))
    buf_positions.resize(len(positions))

    buf_bin_edge_inf.copy_from_stdvec(bin_edge_inf)
    buf_bin_edge_sup.copy_from_stdvec(bin_edge_sup)
    buf_positions.copy_from_stdvec(positions)

    buf_bin_edge_inf_f32 = shamrock.backends.DeviceBuffer_f32()
    buf_bin_edge_sup_f32 = shamrock.backends.DeviceBuffer_f32()
    buf_positions_f32 = shamrock.backends.DeviceBuffer_f32()

    buf_bin_edge_inf_f32.resize(len(bin_edge_inf_f32))
    buf_bin_edge_sup_f32.resize(len(bin_edge_sup_f32))
    buf_positions_f32.resize(len(positions_f32))

    buf_bin_edge_inf_f32.copy_from_stdvec(bin_edge_inf_f32)
    buf_bin_edge_sup_f32.copy_from_stdvec(bin_edge_sup_f32)
    buf_positions_f32.copy_from_stdvec(positions_f32)








.. GENERATED FROM PYTHON SOURCE LINES 76-91

.. code-block:: Python
   :lineno-start: 76

    results_f64 = {}
    results_f32 = {}
    avail_configs = impl_control.get_avail_configs()
    for config in avail_configs:
        impl_control.set_config(config)
        time_f64 = shamrock.algs.benchmark_compute_histogram_basic_f64(
            buf_bin_edge_inf, buf_bin_edge_sup, buf_positions
        )
        time_f32 = shamrock.algs.benchmark_compute_histogram_basic_f32(
            buf_bin_edge_inf_f32, buf_bin_edge_sup_f32, buf_positions_f32
        )
        print(f"Config: {config}, Time f64: {time_f64 * 1000}ms, Time f32: {time_f32 * 1000}ms")
        results_f64[config] = time_f64 * 1000
        results_f32[config] = time_f32 * 1000





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Info: switching config for alg compute_histogram to cfg=reference                    [Algs][rank=0] 
    Config: reference, Time f64: 2371.757527ms, Time f32: 2366.8604170000003ms
    Info: switching config for alg compute_histogram to cfg=naive_gpu                    [Algs][rank=0] 
    Config: naive_gpu, Time f64: 894.8318885000001ms, Time f32: 893.2677315000001ms
    Info: switching config for alg compute_histogram to cfg=gpu_team_fetching            [Algs][rank=0] 
    Config: gpu_team_fetching, Time f64: 1212.12335ms, Time f32: 1252.430123ms
    Info: switching config for alg compute_histogram to cfg=gpu_oversubscribe            [Algs][rank=0] 
    Config: gpu_oversubscribe, Time f64: 2985.355484ms, Time f32: 2296.522044ms




.. GENERATED FROM PYTHON SOURCE LINES 92-93

plot the histogram

.. GENERATED FROM PYTHON SOURCE LINES 93-99

.. code-block:: Python
   :lineno-start: 93

    result = shamrock.algs.compute_histogram_basic_f64(
        buf_bin_edge_inf, buf_bin_edge_sup, buf_positions
    )
    plt.plot(result.copy_to_stdvec())
    plt.show()




.. image-sg:: /_as_gen/benchmarks/images/sphx_glr_run_compute_histogram_001.png
   :alt: run compute histogram
   :srcset: /_as_gen/benchmarks/images/sphx_glr_run_compute_histogram_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 100-101

plot the results

.. GENERATED FROM PYTHON SOURCE LINES 101-127

.. code-block:: Python
   :lineno-start: 101

    plt.figure(layout="constrained")

    configs = list(results_f64.keys())
    vals_f64 = [results_f64[c] for c in configs]
    vals_f32 = [results_f32[c] for c in configs]
    x = np.arange(len(configs))
    bar_w = 0.35
    plt.bar(x - bar_w / 2, vals_f64, bar_w, label="f64")
    plt.bar(x + bar_w / 2, vals_f32, bar_w, label="f32")
    plt.xticks(x, configs, rotation=45, ha="right")
    for tick_label, cfg in zip(plt.gca().get_xticklabels(), configs):
        if cfg == default_config:
            tick_label.set_color("red")

    plt.ylabel("Time (ms)")
    plt.yscale("log")

    _ymin, _ymax = plt.gca().get_ylim()
    _ymin = 10 ** int(np.floor(np.log10(_ymin)))
    _ymax = 10 ** int(np.ceil(np.log10(_ymax)))
    plt.ylim(_ymin, _ymax * 1.1)

    plt.title("Compute histogram performance benchmarks")
    plt.legend()
    plt.grid(True, alpha=0.3)
    plt.show()



.. image-sg:: /_as_gen/benchmarks/images/sphx_glr_run_compute_histogram_002.png
   :alt: Compute histogram performance benchmarks
   :srcset: /_as_gen/benchmarks/images/sphx_glr_run_compute_histogram_002.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

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

**Estimated memory usage:**  157 MB


.. _sphx_glr_download__as_gen_benchmarks_run_compute_histogram.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: run_compute_histogram.ipynb <run_compute_histogram.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: run_compute_histogram.py <run_compute_histogram.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: run_compute_histogram.zip <run_compute_histogram.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
