Skip to content

Rewrite ApplySymmetryToTensor3 with NumPy/BLAS (11x speedup) - #125

Draft
psavery wants to merge 1 commit into
SSCHAcode:masterfrom
psavery:blas-tensor3-symmetrization
Draft

Rewrite ApplySymmetryToTensor3 with NumPy/BLAS (11x speedup)#125
psavery wants to merge 1 commit into
SSCHAcode:masterfrom
psavery:blas-tensor3-symmetrization

Conversation

@psavery

@psavery psavery commented Aug 1, 2026

Copy link
Copy Markdown

Profiling the free-energy Hessian of a 128-atom PdH supercell in python-sscha showed 82% of the run inside this method: the Fortran symph routines apply the symmetries element by element in serial.

The three steps are redone with numpy array operations: the permutation symmetry as an average over the 6 index orderings, the translation average computed once per group of equivalent atoms, and each symmetry at gamma applied as one matrix product over the cartesian components.

On the PdH case: 181 s -> 16 s, matching the old result to 1e-16. Also checked on two lower-symmetry cells (120 and 216 atoms). Peak memory grows because of numpy temporaries: on the 216-atom cell about 11 GB against 6.5 GB, with the time going 211 s -> 34 s.

The matrix products use numpy's threaded BLAS but do not depend on it: on the 120-atom cell the Fortran path takes 20.4 s and the numpy one 6.5 s on a single thread (3.8 s with threads on a 24-core machine). On shared nodes without core pinning, cap the threads with OPENBLAS_NUM_THREADS.

The Fortran routines (permute_v3, trans_v3, sym_v3) no longer have callers here and can be removed in a follow-up if preferred.

The other half of this is SSCHAcode/python-sscha#427

Profiling the free-energy Hessian of a 128-atom PdH supercell in
python-sscha showed 82% of the run inside this method: the Fortran
symph routines apply the symmetries element by element in serial.

The three steps are redone with numpy array operations: the
permutation symmetry as an average over the 6 index orderings, the
translation average computed once per group of equivalent atoms,
and each symmetry at gamma applied as one matrix product over the
cartesian components.

On the PdH case: 181 s -> 16 s, matching the old result to 1e-16.
Also checked on two lower-symmetry cells (120 and 216 atoms). Peak
memory grows because of numpy temporaries: on the 216-atom cell
about 11 GB against 6.5 GB, with the time going 211 s -> 34 s.

The matrix products use numpy's threaded BLAS but do not depend on
it: on the 120-atom cell the Fortran path takes 20.4 s and the
numpy one 6.5 s on a single thread (3.8 s with threads on a
24-core machine). On shared nodes without core pinning, cap the
threads with OPENBLAS_NUM_THREADS.

The Fortran routines (permute_v3, trans_v3, sym_v3) no longer have
callers here and can be removed in a follow-up if preferred.

Signed-off-by: Patrick Avery <patrick.avery@kitware.com>
@mesonepigreco

Copy link
Copy Markdown
Collaborator

Hi @psavery , thanks for the PR.
It's great to see this kind of speedup. However, I am worried about the memory blow-up (doubling). Memory is currently what bounds Hessian calculations on multi-atom systems, so it is possible that some systems running before will no longer run on the user's computer if the PR is merged as is.

@psavery

psavery commented Aug 3, 2026

Copy link
Copy Markdown
Author

I'll take a look at processing them in blocks to reduce peak memory usage, and also report back how much performance is affected!

@psavery

psavery commented Aug 3, 2026

Copy link
Copy Markdown
Author

I'm at a conference this week though, so I may need to get to it next week.

@mesonepigreco

Copy link
Copy Markdown
Collaborator

Hi yes, no hurry at all. Also maybe wait a moment to implement the batch, as we have two other pending PRs on the free energy hessian, so probably merging will generate new conflicts to solve. One of which is focused in speeding up and reducing the memory footprint of the fortan code, so for sure there are conflicts there.

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.

2 participants