Skip to content

Cut the peak memory of binsparse_to_ssmc_problem - #34

Merged
BenBrock merged 1 commit into
mainfrom
matlab-converter-memory-reductions
Aug 11, 2026
Merged

Cut the peak memory of binsparse_to_ssmc_problem#34
BenBrock merged 1 commit into
mainfrom
matlab-converter-memory-reductions

Conversation

@BenBrock

Copy link
Copy Markdown
Contributor

Memory optimizations to reduce the amount of memory used by when converting from BSP to a SSMC Matlab struct.

Reading a very large problem back needs several times the memory of writing
it, because every intermediate in the conversion is an nnz-length array and
the biggest matrices in the collection have nnz in the billions.  For
Sybrandt/AGATHA_2015 (n=183,964,077, nnz=11,588,725,964) the export runs but
the read back fails with MATLAB:pmaxsize, which on Linux is an out-of-memory
error in disguise.

None of this changes what the converter produces; it changes how much scratch
space it takes to produce it.

- checked_indices validates on the stored integer type before widening to
  double.  For an unsigned index array the finiteness and integrality tests
  are vacuous and the range tests collapse to min and max reductions, so the
  check no longer builds a second nnz-length temporary to hold fix(indices).

- matrix_values keeps an ISO matrix in ISO form and returns a flag saying so.
  sparse() expands a scalar over the index vectors, so the single stored value
  never has to be repmat'ed to nnz length.  expand_iso does the expansion in
  the three dense branches, which genuinely need an array.

- expand_structure keeps the ISO form across mirroring whenever mirroring
  leaves the stored value alone, which covers every symmetric matrix along
  with the real hermitian and all-zero skew-symmetric ones.

- convert_matrix takes a separate branch for ISO matrices, whose entries are
  either all zero or all nonzero, so it needs no mask and no masked copies of
  the index vectors.  For the general case it tests values == 0 directly, as
  == already compares both parts of a complex value; the old form called
  imag() on real values and got back an nnz-length array of zeros.  When
  nothing is an explicit zero it hands the index vectors to sparse()
  untouched rather than copying them through a mask.

- require_ordered_pairs and require_strictly_increasing scan in blocks with a
  one-entry overlap.  require_ordered_pairs also evaluated diff(first) twice.

Tests cover the new ISO paths: a nonzero ISO COOR matrix, ISO with
symmetric_lower staying ISO, ISO with skew_symmetric_lower and with
hermitian_lower dropping it, and a dense ISO DVEC.  The 14-matrix BSP test
collection still round-trips byte-exact through ssread; five of those
matrices, Pajek/IMDB among them, store ISO values and so exercise the new
path on real data.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@BenBrock
BenBrock merged commit 3535d4b into main Aug 11, 2026
3 checks passed
@BenBrock
BenBrock deleted the matlab-converter-memory-reductions branch August 11, 2026 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant