Skip to content

Commit e3a9e22

Browse files
committed
deubg: avoid race condition in OrthogonalCollocation
1 parent 10999f2 commit e3a9e22

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

src/controller/transcription.jl

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ function con_nonlinprogeq!(
13741374
nk = get_nk(model, transcription)
13751375
D̂0 = mpc.D̂0
13761376
X̂0_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)]
1377-
for j=0:Hp-1 # prefilling Û0 to avoid race-condition (both û0 and ûnext are needed):
1377+
for j=0:Hp-1 # prefilling Û0 to avoid race-condition (both û0 and û0next are needed):
13781378
x̂0_Z̃ = @views j < 1 ? mpc.estim.x̂0[1:nx̂] : X̂0_Z̃[(1 + nx̂*(j-1)):(nx̂*j)]
13791379
u0, û0 = @views U0[(1 + nu*j):(nu*(j+1))], Û0[(1 + nu*j):(nu*(j+1))]
13801380
f̂_input!(û0, mpc.estim, model, x̂0_Z̃, u0)
@@ -1402,16 +1402,21 @@ function con_nonlinprogeq!(
14021402
fs!(x̂0next, mpc.estim, model, x̂0_Z̃)
14031403
ssnext .= @. xsnext - xsnext_Z̃
14041404
# ----------------- deterministic defects: trapezoidal collocation -------------
1405-
û0 = @views Û0[(1 + nu*(j-1)):(nu*j)]
1406-
û0next = @views h < 1 || j Hp ? û0 : Û0[(1 + nu*j):(nu*(j+1))]
1405+
û0 = @views Û0[(1 + nu*(j-1)):(nu*j)]
14071406
if f_threads || h < 1 || j < 2
14081407
# we need to recompute k1 with multi-threading, even with h==1, since the
14091408
# last iteration (j-1) may not be executed (iterations are re-orderable)
14101409
model.f!(k̇1, x0_Z̃, û0, d̂0, p)
14111410
else
14121411
k̇1 .= @views K̇[(1 + nk*(j-1)-nx):(nk*(j-1))] # k2 of of the last iter. j-1
14131412
end
1414-
model.f!(k̇2, x0next_Z̃, û0next, d̂0next, p)
1413+
if h < 1
1414+
model.f!(k̇2, x0next_Z̃, û0, d̂0next, p)
1415+
else
1416+
# j = Hp special case: u(k+Hp-1) = u(k+Hp) since Hc≤Hp implies Δu(k+Hp) = 0:
1417+
û0next = @views j Hp ? û0 : Û0[(1 + nu*j):(nu*(j+1))]
1418+
model.f!(k̇2, x0next_Z̃, û0next, d̂0next, p)
1419+
end
14151420
sdnext .= @. x0_Z̃ - x0next_Z̃ + 0.5*Ts*(k̇1 + k̇2)
14161421
end
14171422
return geq
@@ -1480,7 +1485,7 @@ function con_nonlinprogeq!(
14801485
mpc::PredictiveController, model::NonLinModel, transcription::OrthogonalCollocation,
14811486
U0, Z̃
14821487
)
1483-
nu, nx̂, nd, nx = model.nu, mpc.estim.nx̂, model.nd, model.nx
1488+
nu, nx̂, nd, nx, h = model.nu, mpc.estim.nx̂, model.nd, model.nx, transcription.h
14841489
Hp, Hc = mpc.Hp, mpc.Hc
14851490
nΔU, nX̂ = nu*Hc, nx̂*Hp
14861491
f_threads = transcription.f_threads
@@ -1491,7 +1496,12 @@ function con_nonlinprogeq!(
14911496
nx̂_nk = nx̂ + nk
14921497
D̂0 = mpc.D̂0
14931498
X̂0_Z̃, K_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)], Z̃[(nΔU+nX̂+1):(nΔU+nX̂+nk*Hp)]
1494-
di = mpc.estim.buffer.d
1499+
D̂temp = mpc.buffer.
1500+
for j=0:Hp-1 # prefilling Û0 to avoid race-condition (both û0 and û0next are needed):
1501+
x̂0_Z̃ = @views j < 1 ? mpc.estim.x̂0[1:nx̂] : X̂0_Z̃[(1 + nx̂*(j-1)):(nx̂*j)]
1502+
u0, û0 = @views U0[(1 + nu*j):(nu*(j+1))], Û0[(1 + nu*j):(nu*(j+1))]
1503+
f̂_input!(û0, mpc.estim, model, x̂0_Z̃, u0)
1504+
end
14951505
@threadsif f_threads for j=1:Hp
14961506
if j < 2
14971507
x̂0_Z̃ = @views mpc.estim.x̂0[1:nx̂]
@@ -1516,28 +1526,28 @@ function con_nonlinprogeq!(
15161526
fs!(x̂0next, mpc.estim, model, x̂0_Z̃)
15171527
ssnext .= @. xsnext - xsnext_Z̃
15181528
# ----------------- collocation constraint defects -----------------------------
1519-
u0 = @views U0[(1 + nu*(j-1)):(nu*j)]
1520-
if j Hp
1521-
# j = Hp special case: u(k+Hp-1) = u(k+Hp) since Hc ≤ Hp implies Δu(k+Hp) = 0
1522-
u0next = u0
1523-
else
1524-
u0next = @views U0[(1 + nu*j):(nu*(j+1))]
1525-
end
1526-
15271529
û0 = @views Û0[(1 + nu*(j-1)):(nu*j)]
1528-
f̂_input!(û0, mpc.estim, model, x̂0_Z̃, u0)
1529-
f̂_input!(û0next, mpc.estim, model, x̂0next_Z̃, u0next)
1530-
15311530
Δk =
15321531
for i=1:no
15331532
Δk[(1 + (i-1)*nx):(i*nx)] = @views k_Z̃[(1 + (i-1)*nx):(i*nx)] .- x0_Z̃
15341533
end
15351534
mul!(sk, Mo, Δk)
1535+
d̂i = @views D̂temp[(1 + nd*(j-1)):(nd*j)]
1536+
if h > 0
1537+
ûi = similar(û0) # TODO: remove this allocation
1538+
end
15361539
for i=1:no
15371540
k̇i = @views k̇[(1 + (i-1)*nx):(i*nx)]
15381541
ki_Z̃ = @views k_Z̃[(1 + (i-1)*nx):(i*nx)]
1539-
di .= (1-τ[i]).*d̂0 .+ τ[i].*d̂0next
1540-
model.f!(k̇i, ki_Z̃, û0, d̂0, p)
1542+
d̂i .= (1-τ[i]).*d̂0 .+ τ[i].*d̂0next
1543+
if h < 1
1544+
model.f!(k̇i, ki_Z̃, û0, d̂i, p)
1545+
else
1546+
# j = Hp special case: u(k+Hp-1) = u(k+Hp) since Hc≤Hp implies Δu(k+Hp) = 0:
1547+
û0next = @views j Hp ? û0 : Û0[(1 + nu*j):(nu*(j+1))]
1548+
ûi .= (1-τ[i]).*û0 .+ τ[i].*û0next
1549+
model.f!(k̇i, ki_Z̃, ûi, d̂i, p)
1550+
end
15411551
end
15421552
sk .-=
15431553
# ----------------- continuity constraint defects ------------------------------

0 commit comments

Comments
 (0)