Skip to content

avoid repeated sums in mesh plan#1034

Open
Debraheem wants to merge 1 commit into
mainfrom
EbF/improve_mesh_plan_performance
Open

avoid repeated sums in mesh plan#1034
Debraheem wants to merge 1 commit into
mainfrom
EbF/improve_mesh_plan_performance

Conversation

@Debraheem

Copy link
Copy Markdown
Member

Currently, MESA does a sum from 0-> k_new in dq on every iteration of the mesh plan do loop using the fortran instrinsic sum() function:

do  ! pick next point location
    ...
   dq_sum = sum(dq_new(1:k_new))
   
   k_new = k_new + 1
   ...
end do

This branch alters the dq sum inside mesh plan to one that is cumulative, hence changing the speed of this algorithm for the sum from O(n^2) to O(n) in time.

do  ! pick next point location
    ...
   xq_sum = xq_sum + dq_new(k_new)
   
   k_new = k_new + 1
   ...
end do

I did some timing tests below, where i specifically look at the time spent inside the mesh routines, as opposed to the global time the runs took. Generally i noticed a ~ 1% or less increase in total performance, but I can imagine anytime the mesh is changes a lot, this minor improvement will reduce the time spent remeshing.

Star/work mesh timing summary

Measured routine timing:
MESA internal timer: remesh

This timing is for the whole remesh/do_mesh path, not just do_mesh_plan,
but it is useful enough for this benchmark.

Test 1: default star/work to central H depletion
controls:
pgstar off
stop at central h1 < 1d-3
OMP_NUM_THREADS = 4

single warm run:
baseline remesh time: 0.544 s
optimized remesh time: 0.475 s
difference: 0.069 s faster
relative change: 12.7% less remesh time

Test 2: 5 Msun to central He depletion
controls:
pgstar off
initial_mass = 5
mesh_delta_coeff = 0.4
time_delta_coeff = 0.5
stop at central he4 < 1d-3
OMP_NUM_THREADS = 4

run 1, fresh install that generates caches:
baseline remesh time: 11.048 s
optimized remesh time: 6.901 s
difference: 4.147 s faster
relative change: 37.5% less remesh time

run 2, warm:
baseline remesh time: 11.017 s
optimized remesh time: 6.886 s
difference: 4.131 s faster
relative change: 37.5% less remesh time

I don't believe this change needs a changelog entry.

@Debraheem

Copy link
Copy Markdown
Member Author

I change the dq_sum name to xq_sum. I would be fine not changing it, whatever is less confusing.

@Debraheem Debraheem added the numerics issues with math/num/mtx modules label Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

numerics issues with math/num/mtx modules performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant