@@ -56,8 +56,9 @@ LinearSolve.@concrete mutable struct DualLinearCache{DT}
5656 primal_u_cache
5757 primal_b_cache
5858
59- # Cache validity flag for RHS precalculation optimization
60- rhs_cache_valid
59+ # Cache validity flags for when partials of A or b changes
60+ A_partials_valid
61+ b_partials_valid
6162
6263 dual_A
6364 dual_b
@@ -94,15 +95,13 @@ function linearsolve_forwarddiff_solve!(cache::DualLinearCache, alg, args...; kw
9495
9596 rhs_list = cache. rhs_list
9697
97- # Update cached partials lists if cache is invalid
98- if ! cache. rhs_cache_valid
99- if ! isnothing (∂_A)
100- update_partials_list! (∂_A, cache. partials_A_list)
101- end
102- if ! isnothing (∂_b)
103- update_partials_list! (∂_b, cache. partials_b_list)
104- end
105- cache. rhs_cache_valid = true
98+ if ! cache. A_partials_valid && ! isnothing (∂_A)
99+ update_partials_list! (∂_A, cache. partials_A_list)
100+ cache. A_partials_valid = true
101+ end
102+ if ! cache. b_partials_valid && ! isnothing (∂_b)
103+ update_partials_list! (∂_b, cache. partials_b_list)
104+ cache. b_partials_valid = true
106105 end
107106
108107 A_list = cache. partials_A_list
@@ -177,11 +176,13 @@ function xp_linsolve_rhs!(
177176 ∂_b:: Union{<:Partials, <:AbstractArray{<:Partials}} , cache:: DualLinearCache
178177 )
179178
180- # Update cached partials lists if cache is invalid
181- if ! cache. rhs_cache_valid
179+ if ! cache. A_partials_valid
182180 update_partials_list! (∂_A, cache. partials_A_list)
181+ cache. A_partials_valid = true
182+ end
183+ if ! cache. b_partials_valid
183184 update_partials_list! (∂_b, cache. partials_b_list)
184- cache. rhs_cache_valid = true
185+ cache. b_partials_valid = true
185186 end
186187
187188 A_list = cache. partials_A_list
@@ -201,10 +202,9 @@ function xp_linsolve_rhs!(
201202 ∂_b:: Nothing , cache:: DualLinearCache
202203 )
203204
204- # Update cached partials list for A if cache is invalid
205- if ! cache. rhs_cache_valid
205+ if ! cache. A_partials_valid
206206 update_partials_list! (∂_A, cache. partials_A_list)
207- cache. rhs_cache_valid = true
207+ cache. A_partials_valid = true
208208 end
209209
210210 A_list = cache. partials_A_list
@@ -222,10 +222,9 @@ function xp_linsolve_rhs!(
222222 cache:: DualLinearCache
223223 )
224224
225- # Update cached partials list for b if cache is invalid
226- if ! cache. rhs_cache_valid
225+ if ! cache. b_partials_valid
227226 update_partials_list! (∂_b, cache. partials_b_list)
228- cache. rhs_cache_valid = true
227+ cache. b_partials_valid = true
229228 end
230229
231230 b_list = cache. partials_b_list
@@ -380,6 +379,7 @@ function __dual_init(
380379 similar (non_partial_cache. u), # primal_u_cache
381380 similar (new_b), # primal_b_cache
382381 true , # Cache is initially valid
382+ true ,
383383 A,
384384 b,
385385 dual_u_init
@@ -495,8 +495,7 @@ function setA!(dc::DualLinearCache, A)
495495 partial_vals! (getfield (dc, :partials_A ), A)
496496 end
497497
498- # Invalidate cache (if setting A or b)
499- return setfield! (dc, :rhs_cache_valid , false )
498+ return setfield! (dc, :A_partials_valid , false )
500499end
501500function setb! (dc:: DualLinearCache , b)
502501 # Put the Dual-stripped versions in the LinearCache
@@ -510,8 +509,7 @@ function setb!(dc::DualLinearCache, b)
510509 partial_vals! (getfield (dc, :partials_b ), b)
511510 end
512511
513- # Invalidate cache (if setting A or b)
514- return setfield! (dc, :rhs_cache_valid , false )
512+ return setfield! (dc, :b_partials_valid , false )
515513end
516514function setu! (dc:: DualLinearCache{DT} , u) where {DT}
517515 # Put the Dual-stripped versions in the LinearCache
0 commit comments