.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/design_examples/streaming_sorting.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_examples_design_examples_streaming_sorting.py: ========================= Streaming sorting network ========================= Sort blocks of five real-valued samples with one sample per clock cycle at both the input and the output. Within each block, inputs arrive in index order and outputs leave in ascending or descending value order. Compare cyclic schedules using one and two shared Comparators for each output order. The five-input bubble/insertion network performs ten comparisons per block. With one Comparator, each five-sample burst therefore needs a ten-cycle block period, leaving five idle I/O cycles between blocks. Two Comparators allow a five-cycle period and continuous one-sample-per-cycle input and output streams after the pipeline has filled. .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: Python from b_asic.core_operations import Comparator from b_asic.list_schedulers import HybridScheduler from b_asic.schedule import Schedule from b_asic.sfg_generators import bubble_sort .. GENERATED FROM PYTHON SOURCE LINES 24-27 Build and show the sorting SFG. Each Comparator returns the smaller value at output 0 and the larger value at output 1. Give each Comparator one cycle of latency and an execution time of one cycle. .. GENERATED FROM PYTHON SOURCE LINES 27-33 .. code-block:: Python size = 5 sfg = bubble_sort(size) sfg.set_latency_of_type(Comparator, 1) sfg.set_execution_time_of_type(Comparator, 1) sfg .. image-sg:: /examples/design_examples/images/sphx_glr_streaming_sorting_001.png :alt: streaming sorting :srcset: /examples/design_examples/images/sphx_glr_streaming_sorting_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 34-35 Check a block containing both positive and negative samples. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python samples = [7, -2, 4, 1, -5] print("Input block:", samples) print("Sorted output block:", sfg.evaluate_outputs(samples)) .. rst-class:: sphx-glr-script-out .. code-block:: none Input block: [7, -2, 4, 1, -5] Sorted output block: [-5, -2, 1, 4, 7] .. GENERATED FROM PYTHON SOURCE LINES 40-43 Serialize the logical input and output ports by assigning consecutive clock cycles. Output times are offsets from the first output; HybridScheduler chooses the output latency so all results are ready when they are needed. .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python input_times = {f"in{i}": i for i in range(size)} output_delta_times = {f"out{i}": i for i in range(size)} .. GENERATED FROM PYTHON SOURCE LINES 47-51 Schedule with one shared Comparator. The ten comparisons occupy all ten cycles of the period. Each five-cycle input/output burst is followed by five idle cycles. Cyclic schedules wrap operations and outputs across the period boundary, with stored values carrying data between periods. .. GENERATED FROM PYTHON SOURCE LINES 51-63 .. code-block:: Python one_comparator = Schedule( sfg, scheduler=HybridScheduler( max_resources={Comparator.type_name(): 1}, input_times=input_times, output_delta_times=output_delta_times, ), schedule_time=size * (size - 1) // 2, cyclic=True, ) one_comparator .. image-sg:: /examples/design_examples/images/sphx_glr_streaming_sorting_002.svg :alt: streaming sorting :srcset: /examples/design_examples/images/sphx_glr_streaming_sorting_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-67 Schedule with two shared Comparators. A new five-sample block can now start every five cycles. Thus both the input and output interfaces sustain one sample per cycle, while independent comparisons can execute in parallel. .. GENERATED FROM PYTHON SOURCE LINES 67-79 .. code-block:: Python two_comparators = Schedule( sfg, scheduler=HybridScheduler( max_resources={Comparator.type_name(): 2}, input_times=input_times, output_delta_times=output_delta_times, ), schedule_time=size, cyclic=True, ) two_comparators .. image-sg:: /examples/design_examples/images/sphx_glr_streaming_sorting_003.svg :alt: streaming sorting :srcset: /examples/design_examples/images/sphx_glr_streaming_sorting_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-83 Emit the largest value first by reversing the output time offsets. The SFG still assigns the smallest value to out0 and the largest to out4; scheduling out4 first makes the serialized output stream descend in value. .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python descending_output_delta_times = {f"out{i}": size - 1 - i for i in range(size)} .. image-sg:: /examples/design_examples/images/sphx_glr_streaming_sorting_004.svg :alt: streaming sorting :srcset: /examples/design_examples/images/sphx_glr_streaming_sorting_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-88 Schedule descending outputs with one shared Comparator and the same ten-cycle block period as the ascending case. .. GENERATED FROM PYTHON SOURCE LINES 88-100 .. code-block:: Python one_comparator_descending = Schedule( sfg, scheduler=HybridScheduler( max_resources={Comparator.type_name(): 1}, input_times=input_times, output_delta_times=descending_output_delta_times, ), schedule_time=size * (size - 1) // 2, cyclic=True, ) one_comparator_descending .. image-sg:: /examples/design_examples/images/sphx_glr_streaming_sorting_005.svg :alt: streaming sorting :srcset: /examples/design_examples/images/sphx_glr_streaming_sorting_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-103 Schedule descending outputs with two shared Comparators and a five-cycle block period, keeping both input and output streams continuous. .. GENERATED FROM PYTHON SOURCE LINES 103-114 .. code-block:: Python two_comparators_descending = Schedule( sfg, scheduler=HybridScheduler( max_resources={Comparator.type_name(): 2}, input_times=input_times, output_delta_times=descending_output_delta_times, ), schedule_time=size, cyclic=True, ) two_comparators_descending .. image-sg:: /examples/design_examples/images/sphx_glr_streaming_sorting_006.svg :alt: streaming sorting :srcset: /examples/design_examples/images/sphx_glr_streaming_sorting_006.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.654 seconds) .. _sphx_glr_download_examples_design_examples_streaming_sorting.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: streaming_sorting.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: streaming_sorting.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: streaming_sorting.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_