Skip to content

refactor(csp-batch): the per-model host indices and the SFH-weights stage as routines (step 4, second part) - #135

Merged
elijahmathews merged 2 commits into
feat/hotpath-04-driver-stagesfrom
feat/hotpath-04b-sfh-stage
Nov 19, 2026
Merged

elijahmathews merged 2 commits into
feat/hotpath-04-driver-stagesfrom
feat/hotpath-04b-sfh-stage

Conversation

@elijahmathews

Copy link
Copy Markdown
Owner

Stack: the unification plan (doc/HOT_PATHS_PLAN.md), step 4 of 5, second part, after #134. Merge order: ..., #133, #134, this.

What changes

Two more stages leave compute_csp_batch (867 to 809 lines):

  • batch_model_indices: the host loop that forms every model's setup: the age index of its birth-cloud time (it_tesc), its star formation rate at its age (sfr, if asked), whether it has dust emission and the interpolation indices and weights of the dust emission grid, and of the AGN template.
  • batch_sfh_weights: the SFH-weights stage, which is device code: the weights of every model over the ages, copied from the given matrix on the host for few models, copied on the device, or computed on the device with sfh_weights_core (one thread per model). The comments about why the host form is a plain loop (LLVM's libomp crash at one thread, GPU build: compute_csp_batch segfaults on the host with OMP_NUM_THREADS=1 for batches below HOST_SFH_MAX #87) travel with it.

The stage routines take what they read as explicit-shape dummies (the same arrangement as ssp_source_scalars), and the packed parameter array as the other stage routines do (pbuf, the raw words with flang). Conditionals 961 to 962: the #if that selects the declaration of pbuf in the new routine; the baseline is raised by one in this change.

Verification

  • CPU, both trees built with -fno-tree-vectorize (see the finding of refactor(csp-batch): the host-only blocks of the driver as routines (step 4, first part) #134 on libmvec): forward dumps 6 of 6 and gradient dumps 6 of 6 bit for bit (the resident basis, metallicity, [alpha/Fe], the IMF per model with the modification factors, a closed-form field).
  • R9700 (FP32 kernels, ROCm container, both trees rebuilt from scratch, csp_batch.f90 in both compile logs): 45 forward dumps of the free-parameter matrix, 0 differ bit for bit. This part moves a device stage, so these are the check of it.
  • Host FP64 and FP32 suites, the six R9700 device tests, and the nvfortran 26.1 compile for the A100 pass. Not run on an NVIDIA GPU: this PR moves a device kernel launch, so it joins the list for the ICDS session.

Where step 4 stands

The nebular and attenuation calls, smoothing, photometry and the reverse pass (about 180 lines, the 30 arguments of batch_backward) are still inline. They share many arrays (the packed ipk, rpk, lpk, rout and the work arrays), so cutting them needs a workspace type and the device mapping rules checked on the R9700 after each cut; I would do that as its own PRs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5

…tage as routines (unification plan, step 4, second part)

batch_model_indices (birth-cloud age, star formation rate, dust emission and AGN indices of every model) and batch_sfh_weights (the weights of every model, host and device) leave compute_csp_batch (867 to 809 lines). Bit-identical: CPU with -fno-tree-vectorize (forward and gradient dumps), and 45 forward dumps on the R9700. Conditionals 961 to 962 (the pbuf declaration of the moved stage).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.95%. Comparing base (0f7760e) to head (9da47cd).

Files with missing lines Patch % Lines
src/physics/opensps_csp_batch.f90 66.66% 5 Missing and 6 partials ⚠️

❌ Your patch check has failed because the patch coverage (66.66%) is below the target coverage (85.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@                        Coverage Diff                        @@
##           feat/hotpath-04-driver-stages     #135      +/-   ##
=================================================================
- Coverage                          74.98%   74.95%   -0.04%     
=================================================================
  Files                                 51       51              
  Lines                              14461    14472      +11     
  Branches                            6077     6084       +7     
=================================================================
+ Hits                               10844    10847       +3     
- Misses                              1478     1482       +4     
- Partials                            2139     2143       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…ed by it

batch_sfh_weights declared pbuf(n_mod) before n_mod was typed: a GNU extension in gfortran's default mode, an error with -std=f2008 and in LLVM flang (the two CI jobs that failed on this branch).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5
@elijahmathews
elijahmathews marked this pull request as ready for review November 19, 2026 16:55
@elijahmathews
elijahmathews merged commit 57b4f0b into main Nov 19, 2026
54 of 58 checks passed
@elijahmathews
elijahmathews deleted the feat/hotpath-04b-sfh-stage branch November 19, 2026 17:49
elijahmathews added a commit that referenced this pull request Nov 19, 2026
…n programmers (#136)

Stack: after #135. Merge order: ..., #134, #135, this. Documentation
only.

`doc/HOT_PATH_TOUR.md`: a map of the hot path for someone who knows
stellar populations and not necessarily Fortran. The route of a call
from the C API to `compute_csp_batch`; the steps of the routine in order
(checks, per-model setup, SFH weights, age sums, nebular emission, dust
attenuation, dust emission, IGM and AGN, smoothing, magnitudes, the
reverse pass) with the routine and the file of each; the three sources
of the SSPs and the K slabs; the age-sum kernel as a first thing to
read; a glossary of the Fortran and OpenMP constructs the code uses
(`real(WP)`/`real(KP)`, `intent`, assumed and explicit shape, pointers,
`!$omp` directives, the `#if` switch, `.inc` templates); and where to
look when a batch is refused, slow, or differs between builds.

Names are used instead of line numbers, so the tour does not go stale as
the code moves; every routine named in it was checked to exist.

🤖 Generated with [Claude Code](https://claude.ai/code)

https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
elijahmathews added a commit that referenced this pull request Nov 19, 2026
…nes, and a stage index in the driver (step 4, third part) (#137)

Stack: the unification plan (`doc/HOT_PATHS_PLAN.md`), step 4 of 5,
third part, after #136 (the tour). Merge order: ..., #135, #136, this.

## What changes

- `batch_mass_cotangent`: the mass output's cotangent in the reverse
pass (the resident basis): `wbl(k, m)` gains `mass_bar(m)` times the
model's blend of the basis' surviving masses, and with per-model
metallicity and gradients the `logzsol` and `afe` rows gain the mass's
derivative with respect to `dz` and `da`.
- `batch_sfh_gradients`: the SFH parameters' gradients through the
weights' tangents (`sfh_weights_tangent`, FP64 on the host, one model
per thread).
- A stage index at the top of `compute_csp_batch`: a comment that lists
the stages (0 checks and plan, 1 setup, 2 SFH weights, 3 age sums, 4
nebular, 5 attenuation, 6 dust emission / IGM / AGN, 7 smoothing, 8
magnitudes, 9 the reverse pass) with the routine of each, so that
reading the driver gives the map that `doc/HOT_PATH_TOUR.md` explains in
plain terms.

The code of the driver is 759 lines (1037 at the start of step 4), 775
with the index. The plan (`doc/HOT_PATHS_PLAN.md`) gets the status.

## Verification

- CPU, both trees built with `-fno-tree-vectorize` (see #134): forward
dumps 6 of 6 and gradient dumps 6 of 6 bit for bit (the resident basis,
metallicity, [alpha/Fe], the IMF per model with the modification
factors, a closed-form field). The gradient dumps exercise both new
routines (the benchmark gives `mass_bar` and the SFH parameters' rows).
- R9700 (both trees rebuilt from scratch, `csp_batch.f90` in both
compile logs): 45 forward dumps, 0 differ bit for bit.
- Host FP64 and FP32 suites, the six R9700 device tests and the
nvfortran 26.1 compile pass.
- The stage index is a comment (checked: it builds).

🤖 Generated with [Claude Code](https://claude.ai/code)

https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
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