.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/creating_and_transforming_sfgs/c_arrays_to_sfg.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_creating_and_transforming_sfgs_c_arrays_to_sfg.py: ======================================= C arrays as memory, streams, or state ======================================= An array in C does not determine the SFG interface. For example, an FFT can read and write memory or expose scalar streaming ports. The same pointer can also be used by a different algorithm to hold persistent state. :class:`~b_asic.llvm.LLVMArray` makes these choices explicit. This example skips conversion if Clang or llvmlite is unavailable. See :doc:`/llvm` for details and the supported LLVM subset. .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python import sys from importlib.util import find_spec from pathlib import Path from shutil import which from b_asic import LLVMArray, Simulation, c_to_sfg .. GENERATED FROM PYTHON SOURCE LINES 24-33 Two different uses of arrays ---------------------------- The :download:`C source ` contains an in-place FFT butterfly and a separate stateful accumulator. Each butterfly invocation receives fresh data; the accumulator retains its state between invocations. .. literalinclude:: llvm_array_kernel.c :language: c .. GENERATED FROM PYTHON SOURCE LINES 33-43 .. code-block:: Python source_path = Path("llvm_array_kernel.c") if not source_path.is_file(): source_path = Path(sys.argv[0]).resolve().with_name("llvm_array_kernel.c") clang = which("clang") available = clang is not None and find_spec("llvmlite") is not None if not available: print('Skipping conversion: install Clang and the "b-asic[llvm]" extra.') .. GENERATED FROM PYTHON SOURCE LINES 44-50 An input/output memory ---------------------- ``role="inout"`` creates reads and writes in the same named memory. It does not introduce feedback across SFG iterations. Constant C offsets become zero-based element addresses on MemoryRead and MemoryWrite operations. .. GENERATED FROM PYTHON SOURCE LINES 50-63 .. code-block:: Python memory_sfg = None if available: memory_sfg = c_to_sfg( source_path, "butterfly", clang=clang, arrays={"data": LLVMArray("inout", 2, interface="memory")}, ) print(memory_sfg.evaluate(3.0, 1.0)) memory_sfg # noqa: B018 - Display the SFG in the gallery. .. image-sg:: /examples/creating_and_transforming_sfgs/images/sphx_glr_c_arrays_to_sfg_001.png :alt: c arrays to sfg :srcset: /examples/creating_and_transforming_sfgs/images/sphx_glr_c_arrays_to_sfg_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [4.0, 2.0] .. GENERATED FROM PYTHON SOURCE LINES 64-70 The same algorithm with ordinary ports --------------------------------------- Changing only ``interface`` creates ordinary Input/Output ports instead. Here, "stream" means one scalar port per element per SFG iteration; a serial streaming protocol and its scheduling are not inferred. .. GENERATED FROM PYTHON SOURCE LINES 70-83 .. code-block:: Python stream_sfg = None if available: stream_sfg = c_to_sfg( source_path, "butterfly", clang=clang, arrays={"data": LLVMArray("inout", 2, interface="stream")}, ) print(stream_sfg.evaluate(3.0, 1.0)) stream_sfg # noqa: B018 - Display the SFG in the gallery. .. image-sg:: /examples/creating_and_transforming_sfgs/images/sphx_glr_c_arrays_to_sfg_002.png :alt: c arrays to sfg :srcset: /examples/creating_and_transforming_sfgs/images/sphx_glr_c_arrays_to_sfg_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [4.0, 2.0] .. GENERATED FROM PYTHON SOURCE LINES 84-91 Persistent state becomes a delay -------------------------------- ``role="state"`` represents storage internal to the SFG. The array disappears from the external ports and its previous value comes from a Delay. This is a separate algorithm: combining state arrays with input/output arrays is currently unsupported. Scalar inputs and returns remain available. .. GENERATED FROM PYTHON SOURCE LINES 91-104 .. code-block:: Python state_sfg = None if available: state_sfg = c_to_sfg( source_path, "accumulate", clang=clang, arrays={"state": LLVMArray("state", 1, initial_values=[0.0])}, ) simulation = Simulation(state_sfg, [2.0]) print([simulation.step()[0] for _ in range(3)]) state_sfg # noqa: B018 - Display the SFG in the gallery. .. image-sg:: /examples/creating_and_transforming_sfgs/images/sphx_glr_c_arrays_to_sfg_003.png :alt: c arrays to sfg :srcset: /examples/creating_and_transforming_sfgs/images/sphx_glr_c_arrays_to_sfg_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [2.0, 4.0, 6.0] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.400 seconds) .. _sphx_glr_download_examples_creating_and_transforming_sfgs_c_arrays_to_sfg.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: c_arrays_to_sfg.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: c_arrays_to_sfg.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: c_arrays_to_sfg.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_