qrunch.chemistry.qrackle.matrix_modules.allocation_tracker

Generic allocation tracker for matrix module backends.

Provides stack capture, cumulative allocation counting, and peak memory tracking that is shared between NumpyMatrixModule and CuPyMatrixModule.

Classes

AllocationRecord

Bookkeeping record for one matrix or vector allocation.

AllocationTracker

Tracks live allocations, cumulative counts, and peak memory usage.

class AllocationRecord

Bases: object

Bookkeeping record for one matrix or vector allocation.

Parameters:
  • kind – Either "matrix" or "vector".

  • shape – Shape of the underlying array.

  • number_of_bytes – Number of bytes used by this allocation.

  • stack_summary – Multi-line string describing the call stack at the allocation site (empty when stack tracking is disabled).

__init__(kind: str, shape: tuple[int, ...], number_of_bytes: int, stack_summary: str = '') → None
Parameters:
  • kind (str)

  • shape (tuple[int, ...])

  • number_of_bytes (int)

  • stack_summary (str)

Return type:

None

kind: str
number_of_bytes: int
shape: tuple[int, ...]
stack_summary: str = ''
class AllocationTracker

Bases: Generic[T]

Tracks live allocations, cumulative counts, and peak memory usage.

__init__(*, bytes_of: Callable[[T], int], track_allocation_stack: bool = True, stack_capture_depth: int = 12, stack_frames_to_skip: int = 2) → None

Tracks live allocations, cumulative counts, and peak memory usage.

This is a generic helper used by matrix module backends. The type parameter T is the concrete record type stored per allocation.

Parameters:
  • bytes_of (Callable[[T], int]) – Callable that extracts the byte count from a record.

  • track_allocation_stack (bool) – When True capture a Python call-stack snapshot at every allocation.

  • stack_capture_depth (int) – Maximum number of stack frames captured per allocation.

  • stack_frames_to_skip (int) – Number of innermost stack frames to skip when capturing the call stack. Adjust this so that the deepest reported frame is the actual public allocation site, not internal helper methods.

Return type:

None

capture_stack() → str

Get a textual description of the current Python call stack.

Returns an empty string when stack tracking is disabled.

Return type:

str

property cumulative_matrix_allocations: int

Total number of matrices ever allocated.

property cumulative_vector_allocations: int

Total number of vectors ever allocated.

property current_bytes: int

Number of bytes currently allocated.

deregister(obj_id: int) → T | None

Remove an allocation record and update the current byte count.

Parameters:

obj_id (int) – The id() of the container object.

Return type:

T | None

property number_of_live_allocations: int

Number of currently tracked (not yet freed) objects.

property number_of_matrices: int

Total number of live matrix allocations.

property number_of_vectors: int

Total number of live vector allocations.

property peak_bytes: int

Peak number of bytes that were ever allocated simultaneously.

property records: dict[int, T]

Live allocation records.

register(obj_id: int, kind: str, record: T) → None

Register a new allocation.

Parameters:
  • obj_id (int) – The id() of the container object.

  • kind (str) – Either "matrix" or "vector".

  • record (T) – The record to store for this allocation.

Return type:

None

report_summary_lines(label: str, current_bytes: int | None = None) → list[str]

Summary lines for an allocation report.

Parameters:
  • label (str) – A short label for the memory type (e.g. "total memory", "host memory").

  • current_bytes (int | None) – Override for the current byte count shown in the report. If None, uses current_bytes.

Return type:

list[str]

update_record(obj_id: int, record: T) → None

Replace the record for an existing allocation and adjust byte tracking.

Parameters:
  • obj_id (int) – The id() of the container object.

  • record (T) – The new record to store.

Return type:

None