Skip to content

Remove unnecessary allocations in Nematic OP#1428

Merged
janbridley merged 3 commits into
mainfrom
optimize/nematic-allocations
May 8, 2026
Merged

Remove unnecessary allocations in Nematic OP#1428
janbridley merged 3 commits into
mainfrom
optimize/nematic-allocations

Conversation

@janbridley

@janbridley janbridley commented May 8, 2026

Copy link
Copy Markdown
Contributor

Description

The nematic OP used Managedarray{3, 3} which is a vector backed data structure. This resulted in an unnecessary allocation in the innermost loop. Worse yet, the fancy multi-index of managed arrays allocates an extra vector for every call to operator(), so each particle was performing a huge number of unnecessary allocations. This PR swaps to using a basic float[9] as the intermediate buffer, which is then distributed to the particle and global tensors as needed.

A future PR revising the ManagedArray indexing is probably in order too -- see #1429. This PR is a separate change though, as nematic is so fast that the single managed array allocation quarters the performance.

Motivation and Context

Benchmarks run on battery power on an M1 Pro MacBook.

BEFORE:
  scenario                            median     per 1k         min         max     order
  --------------------------------------------------------------------------------------
  uniform random                  med  43.230 ms  ( 432.30 us/k)  min  41.262  max  52.311  OP=0.002568
  perturbed theta=5 deg           med  40.345 ms  ( 403.45 us/k)  min  39.501  max  46.995  OP=0.988540
  perturbed theta=15 deg          med  40.231 ms  ( 402.31 us/k)  min  39.558  max  49.638  OP=0.899437
  perturbed theta=30 deg          med  40.669 ms  ( 406.69 us/k)  min  39.564  max  76.491  OP=0.625012
  50/50 mixed (theta=5)           med  43.379 ms  ( 433.79 us/k)  min  39.575  max 121.277  OP=0.494433
  50/50 mixed (theta=30)          med  40.859 ms  ( 408.59 us/k)  min  39.738  max 107.235  OP=0.313669
  10/90 mixed (theta=5)           med  40.122 ms  ( 401.22 us/k)  min  39.511  max  48.554  OP=0.098905


AFTER:

  scenario                            median     per 1k         min         max     order
  --------------------------------------------------------------------------------------
  uniform random                  med   1.177 ms  (  11.77 us/k)  min   1.074  max   1.726  OP=0.002568
  perturbed theta=5 deg           med   1.170 ms  (  11.70 us/k)  min   1.090  max   1.406  OP=0.988483
  perturbed theta=15 deg          med   1.168 ms  (  11.68 us/k)  min   1.092  max   1.400  OP=0.899524
  perturbed theta=30 deg          med   1.166 ms  (  11.66 us/k)  min   1.062  max   1.386  OP=0.625011
  50/50 mixed (theta=5)           med   1.169 ms  (  11.69 us/k)  min   1.095  max   1.408  OP=0.494429
  50/50 mixed (theta=30)          med   1.167 ms  (  11.67 us/k)  min   1.094  max   1.428  OP=0.313670
  10/90 mixed (theta=5)           med   1.168 ms  (  11.68 us/k)  min   1.098  max   1.424  OP=0.098907

How Has This Been Tested?

Checklist:

@janbridley

Copy link
Copy Markdown
Contributor Author

@ispivack , hopefully this is useful!

@janbridley
janbridley marked this pull request as ready for review May 8, 2026 00:45
@janbridley janbridley changed the title Remove unnecessary allocations in order/Nematic Remove unnecessary allocations in Nematic OP May 8, 2026

@joaander joaander left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Please update the change log.

@joaander joaander left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@janbridley
janbridley merged commit b7baee1 into main May 8, 2026
23 checks passed
@janbridley
janbridley deleted the optimize/nematic-allocations branch May 8, 2026 14:03
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