Commit f3ce0c5
linCmtB(): thread-safe forward-mode AD Jacobian solves (#1126)
* linCmtB(): thread-safe forward-mode AD Jacobian solves
Forward-mode AD (the default linCmtB Jacobian path, sensType 3/30/auto)
is stack-local fvar with no shared Stan arena, so parallel subject solves
are safe. Two fixes enable it:
- linCmtScaleInitPar/N/linCmtZeroJac read the current thread's __linCmtB
slot (rx_get_thread) instead of the hardcoded [0] slot that another
thread mutates and may resize during a parallel solve.
- The notThreadLinCmtB solve-time guard now single-cores only the
reverse-mode AD (31) and finite-difference paths; the forward-AD path
threads like any thread-safe method.
Validated: 80-subject linCmtB sensitivity solve is bitwise identical at
cores=1 vs cores=4; reverse-AD path stays serial and correct.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* linCmtB(): regression test + NEWS for thread-safe forward-AD solves
Add a parallel-vs-serial regression test (cores=1 == cores=2, bitwise)
to the linCmtB sensitivity suite, refresh the stale "not thread safe"
parse-time comment, and note the change in NEWS.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* linCmtB(): skip finite-difference H setup on the AD paths
setupLinH() ran shi21ForwardH() for the default AD sensType (3, and
auto=100 which remaps to 3) -- a base solve plus several per-parameter
probe solves used to size a finite-difference step. The AD Jacobian
(linCmtFwdJac / reverse jacobian) never reads ind->linH, so those solves
were pure waste on every linCmt sensitivity setup.
Give linCmtSensIsAD() external linkage (its finite-difference set is
exactly the sensTypes linCmtB reads ind->linH for) and early-out of
setupLinH() for the AD paths with a constant linH fill. The now-dead
case 3 shi21ForwardH branch is removed.
Validated: AD linCmtB vs ODE reference unchanged (3.5e-7 < 1e-5);
lincmt-solve (1623) and lincmt-solve-sens (9) pass.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* linCmtB(): address PR review -- shared sensType classifier, no narrowing
- Move the linCmt sensitivity-method classifiers into a shared header
src/linCmtSensType.h so linCmt.cpp, par_solve.cpp and rxData.cpp use one
definition instead of a cross-TU forward declaration. Add
linCmtSensForwardAdThreadSafe() (3/30/100) and document why auto (100) is
safe -- setupLinH() remaps 100->3 and nothing resolves auto to reverse-mode
AD or a finite-difference method -- and why reverse-mode AD (31) and the FD
paths are excluded. The thread gate in rxData.cpp now calls this predicate.
- Cast the __linCmtB pool size to int once per accessor (linCmtScaleInitPar/N,
linCmtZeroJac) since rx_get_thread() takes an int, avoiding a narrowing
conversion.
- Skip the thread-safety regression test unless getRxThreads() >= 2 so it does
not pass vacuously on a single-core runner.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>1 parent 602960e commit f3ce0c5
7 files changed
Lines changed: 125 additions & 26 deletions
File tree
- src
- tests/testthat
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
89 | 94 | | |
90 | 95 | | |
91 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
395 | 396 | | |
396 | 397 | | |
397 | 398 | | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
398 | 405 | | |
399 | | - | |
| 406 | + | |
| 407 | + | |
400 | 408 | | |
401 | 409 | | |
402 | 410 | | |
403 | | - | |
| 411 | + | |
| 412 | + | |
404 | 413 | | |
405 | 414 | | |
406 | 415 | | |
407 | 416 | | |
408 | | - | |
| 417 | + | |
| 418 | + | |
409 | 419 | | |
410 | 420 | | |
411 | 421 | | |
| |||
484 | 494 | | |
485 | 495 | | |
486 | 496 | | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
501 | 501 | | |
502 | 502 | | |
503 | 503 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
6792 | 6793 | | |
6793 | 6794 | | |
6794 | 6795 | | |
| 6796 | + | |
| 6797 | + | |
| 6798 | + | |
| 6799 | + | |
| 6800 | + | |
| 6801 | + | |
| 6802 | + | |
| 6803 | + | |
| 6804 | + | |
| 6805 | + | |
6795 | 6806 | | |
6796 | 6807 | | |
6797 | 6808 | | |
6798 | 6809 | | |
6799 | 6810 | | |
6800 | 6811 | | |
6801 | 6812 | | |
6802 | | - | |
6803 | | - | |
6804 | | - | |
6805 | 6813 | | |
6806 | 6814 | | |
6807 | 6815 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
196 | | - | |
197 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
198 | 199 | | |
199 | 200 | | |
200 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
5937 | 5938 | | |
5938 | 5939 | | |
5939 | 5940 | | |
| 5941 | + | |
| 5942 | + | |
| 5943 | + | |
| 5944 | + | |
| 5945 | + | |
| 5946 | + | |
| 5947 | + | |
5940 | 5948 | | |
5941 | 5949 | | |
5942 | 5950 | | |
| |||
5957 | 5965 | | |
5958 | 5966 | | |
5959 | 5967 | | |
5960 | | - | |
5961 | | - | |
| 5968 | + | |
| 5969 | + | |
| 5970 | + | |
| 5971 | + | |
| 5972 | + | |
| 5973 | + | |
| 5974 | + | |
5962 | 5975 | | |
5963 | 5976 | | |
5964 | 5977 | | |
| |||
5979 | 5992 | | |
5980 | 5993 | | |
5981 | 5994 | | |
5982 | | - | |
5983 | | - | |
| 5995 | + | |
| 5996 | + | |
| 5997 | + | |
| 5998 | + | |
| 5999 | + | |
| 6000 | + | |
| 6001 | + | |
5984 | 6002 | | |
5985 | 6003 | | |
5986 | 6004 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
120 | 151 | | |
0 commit comments