diff options
author | Jules Kouatchou <Jules.Kouatchou-1@nasa.gov> | 2023-04-25 12:33:20 -0400 |
---|---|---|
committer | Jules Kouatchou <Jules.Kouatchou-1@nasa.gov> | 2023-04-25 12:33:20 -0400 |
commit | 0c03969edaf09f23ccfc122f3cf7413e07cfb3f0 (patch) | |
tree | 4eb8387d6a8d353b496bce8d0bfda091d1ef8ae7 /doc/source/reference/c-api | |
parent | f15a5be1d6906ba158ebf2df880c6a4553a9d6f9 (diff) | |
download | numpy-0c03969edaf09f23ccfc122f3cf7413e07cfb3f0.tar.gz |
Added indentation for the sample code to be properly displayed.
Diffstat (limited to 'doc/source/reference/c-api')
-rw-r--r-- | doc/source/reference/c-api/data_memory.rst | 143 |
1 files changed, 71 insertions, 72 deletions
diff --git a/doc/source/reference/c-api/data_memory.rst b/doc/source/reference/c-api/data_memory.rst index ec32223bd..aca98289e 100644 --- a/doc/source/reference/c-api/data_memory.rst +++ b/doc/source/reference/c-api/data_memory.rst @@ -172,75 +172,74 @@ Here is an example on how to use ``np.lib.tracemalloc_domain``: .. code-block:: python -""" - The goal of this example is to show how to trace memory - from an application that has NumPy and non-NumPy sections. - We only select the sections using NumPy related calls. -""" - -import tracemalloc -import numpy as np - -# Flag to determine if we select NumPy domain -use_np_domain = True - -nx = 300 -ny = 500 - -# Start to trace memory -tracemalloc.start() - -# Section 1 -# --------- - -# NumPy related call -a = np.zeros((nx,ny)) - -# non-NumPy related call -b = [i**2 for i in range(nx*ny)] - -snapshot1 = tracemalloc.take_snapshot() -# We filter the snapshot to only select NumPy related calls -np_domain = np.lib.tracemalloc_domain -dom_filter = tracemalloc.DomainFilter(inclusive=use_np_domain, - domain=np_domain) -snapshot1 = snapshot1.filter_traces([dom_filter]) -top_stats1 = snapshot1.statistics('traceback') - -print("================ SNAPSHOT 1 =================") -for stat in top_stats1: - print(f"{stat.count} memory blocks: {stat.size / 1024:.1f} KiB") - print(stat.traceback.format()[-1]) - -# Clear traces of memory blocks allocated by Python -# before moving to the next section. -tracemalloc.clear_traces() - -# Section 2 -#---------- - -# We are only using NumPy -c = np.sum(a*a) - -snapshot2 = tracemalloc.take_snapshot() -top_stats2 = snapshot2.statistics('traceback') - -print() -print("================ SNAPSHOT 2 =================") -for stat in top_stats2: - print(f"{stat.count} memory blocks: {stat.size / 1024:.1f} KiB") - print(stat.traceback.format()[-1]) - -tracemalloc.stop() - -print() -print("============================================") -print("\nTracing Status : ", tracemalloc.is_tracing()) - -try: - print("\nTrying to Take Snapshot After Tracing is Stopped.") - snap = tracemalloc.take_snapshot() -except Exception as e: - print("Exception : ", e) - -... + """ + The goal of this example is to show how to trace memory + from an application that has NumPy and non-NumPy sections. + We only select the sections using NumPy related calls. + """ + + import tracemalloc + import numpy as np + + # Flag to determine if we select NumPy domain + use_np_domain = True + + nx = 300 + ny = 500 + + # Start to trace memory + tracemalloc.start() + + # Section 1 + # --------- + + # NumPy related call + a = np.zeros((nx,ny)) + + # non-NumPy related call + b = [i**2 for i in range(nx*ny)] + + snapshot1 = tracemalloc.take_snapshot() + # We filter the snapshot to only select NumPy related calls + np_domain = np.lib.tracemalloc_domain + dom_filter = tracemalloc.DomainFilter(inclusive=use_np_domain, + domain=np_domain) + snapshot1 = snapshot1.filter_traces([dom_filter]) + top_stats1 = snapshot1.statistics('traceback') + + print("================ SNAPSHOT 1 =================") + for stat in top_stats1: + print(f"{stat.count} memory blocks: {stat.size / 1024:.1f} KiB") + print(stat.traceback.format()[-1]) + + # Clear traces of memory blocks allocated by Python + # before moving to the next section. + tracemalloc.clear_traces() + + # Section 2 + #---------- + + # We are only using NumPy + c = np.sum(a*a) + + snapshot2 = tracemalloc.take_snapshot() + top_stats2 = snapshot2.statistics('traceback') + + print() + print("================ SNAPSHOT 2 =================") + for stat in top_stats2: + print(f"{stat.count} memory blocks: {stat.size / 1024:.1f} KiB") + print(stat.traceback.format()[-1]) + + tracemalloc.stop() + + print() + print("============================================") + print("\nTracing Status : ", tracemalloc.is_tracing()) + + try: + print("\nTrying to Take Snapshot After Tracing is Stopped.") + snap = tracemalloc.take_snapshot() + except Exception as e: + print("Exception : ", e) + |