Skip to content

Commit

Permalink
Using Adapt
Browse files Browse the repository at this point in the history
  • Loading branch information
michel2323 committed Sep 15, 2023
1 parent 8f2920c commit d412393
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 196 deletions.
230 changes: 35 additions & 195 deletions examples/admm/acopf_admm_gpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,16 @@ function get_generator_data(data::OPFData, unknown)
error("Unknown device $typeof(unknown)")
end

function get_generator_data(data::OPFData, ::KA.CPU)
function get_generator_data(data::OPFData, backend::KA.Backend)
ngen = length(data.generators)

pgmin = Array{Float64}(undef, ngen)
pgmax = Array{Float64}(undef, ngen)
qgmin = Array{Float64}(undef, ngen)
qgmax = Array{Float64}(undef, ngen)
c2 = Array{Float64}(undef, ngen)
c1 = Array{Float64}(undef, ngen)
c0 = Array{Float64}(undef, ngen)

Pmin = Float64[data.generators[g].Pmin for g in 1:ngen]
Pmax = Float64[data.generators[g].Pmax for g in 1:ngen]
Qmin = Float64[data.generators[g].Qmin for g in 1:ngen]
Qmax = Float64[data.generators[g].Qmax for g in 1:ngen]
coeff0 = Float64[data.generators[g].coeff[3] for g in 1:ngen]
coeff1 = Float64[data.generators[g].coeff[2] for g in 1:ngen]
coeff2 = Float64[data.generators[g].coeff[1] for g in 1:ngen]
copyto!(pgmin, Pmin)
copyto!(pgmax, Pmax)
copyto!(qgmin, Qmin)
copyto!(qgmax, Qmax)
copyto!(c0, coeff0)
copyto!(c1, coeff1)
copyto!(c2, coeff2)

return pgmin,pgmax,qgmin,qgmax,c2,c1,c0
end

function get_generator_data(data::OPFData, ::CUDABackend)
ngen = length(data.generators)

pgmin = CuArray{Float64}(undef, ngen)
pgmax = CuArray{Float64}(undef, ngen)
qgmin = CuArray{Float64}(undef, ngen)
qgmax = CuArray{Float64}(undef, ngen)
c2 = CuArray{Float64}(undef, ngen)
c1 = CuArray{Float64}(undef, ngen)
c0 = CuArray{Float64}(undef, ngen)

Pmin = Float64[data.generators[g].Pmin for g in 1:ngen]
Pmax = Float64[data.generators[g].Pmax for g in 1:ngen]
Qmin = Float64[data.generators[g].Qmin for g in 1:ngen]
Qmax = Float64[data.generators[g].Qmax for g in 1:ngen]
coeff0 = Float64[data.generators[g].coeff[3] for g in 1:ngen]
coeff1 = Float64[data.generators[g].coeff[2] for g in 1:ngen]
coeff2 = Float64[data.generators[g].coeff[1] for g in 1:ngen]
copyto!(pgmin, Pmin)
copyto!(pgmax, Pmax)
copyto!(qgmin, Qmin)
copyto!(qgmax, Qmax)
copyto!(c0, coeff0)
copyto!(c1, coeff1)
copyto!(c2, coeff2)

return pgmin,pgmax,qgmin,qgmax,c2,c1,c0
end

function get_generator_data(data::OPFData, ::ROCBackend)
ngen = length(data.generators)

pgmin = ROCArray{Float64}(undef, ngen)
pgmax = ROCArray{Float64}(undef, ngen)
qgmin = ROCArray{Float64}(undef, ngen)
qgmax = ROCArray{Float64}(undef, ngen)
c2 = ROCArray{Float64}(undef, ngen)
c1 = ROCArray{Float64}(undef, ngen)
c0 = ROCArray{Float64}(undef, ngen)
pgmin = adapt(backend, zeros(ngen))
pgmax = adapt(backend, zeros(ngen))
qgmin = adapt(backend, zeros(ngen))
qgmax = adapt(backend, zeros(ngen))
c2 = adapt(backend, zeros(ngen))
c1 = adapt(backend, zeros(ngen))
c0 = adapt(backend, zeros(ngen))

Pmin = Float64[data.generators[g].Pmin for g in 1:ngen]
Pmax = Float64[data.generators[g].Pmax for g in 1:ngen]
Expand All @@ -93,7 +35,7 @@ function get_bus_data(data::OPFData, unknown)
error("Unknown device $typeof(unknown)")
end

function get_bus_data(data::OPFData, ::KA.CPU)
function get_bus_data(data::OPFData, backend::KA.Backend)
nbus = length(data.buses)

FrIdx = [l for b=1:nbus for l in data.FromLines[b]]
Expand All @@ -106,64 +48,14 @@ function get_bus_data(data::OPFData, ::KA.CPU)
Pd = Float64[data.buses[i].Pd for i=1:nbus]
Qd = Float64[data.buses[i].Qd for i=1:nbus]

return FrStart,FrIdx,ToStart,ToIdx,GenStart,GenIdx,Pd,Qd
end

function get_bus_data(data::OPFData, ::CUDABackend)
nbus = length(data.buses)

FrIdx = [l for b=1:nbus for l in data.FromLines[b]]
ToIdx = [l for b=1:nbus for l in data.ToLines[b]]
GenIdx = [g for b=1:nbus for g in data.BusGenerators[b]]
FrStart = accumulate(+, vcat([1], [length(data.FromLines[b]) for b=1:nbus]))
ToStart = accumulate(+, vcat([1], [length(data.ToLines[b]) for b=1:nbus]))
GenStart = accumulate(+, vcat([1], [length(data.BusGenerators[b]) for b=1:nbus]))

Pd = Float64[data.buses[i].Pd for i=1:nbus]
Qd = Float64[data.buses[i].Qd for i=1:nbus]

cuFrIdx = CuArray{Int}(undef, length(FrIdx))
cuToIdx = CuArray{Int}(undef, length(ToIdx))
cuGenIdx = CuArray{Int}(undef, length(GenIdx))
cuFrStart = CuArray{Int}(undef, length(FrStart))
cuToStart = CuArray{Int}(undef, length(ToStart))
cuGenStart = CuArray{Int}(undef, length(GenStart))
cuPd = CuArray{Float64}(undef, nbus)
cuQd = CuArray{Float64}(undef, nbus)

copyto!(cuFrIdx, FrIdx)
copyto!(cuToIdx, ToIdx)
copyto!(cuGenIdx, GenIdx)
copyto!(cuFrStart, FrStart)
copyto!(cuToStart, ToStart)
copyto!(cuGenStart, GenStart)
copyto!(cuPd, Pd)
copyto!(cuQd, Qd)

return cuFrStart,cuFrIdx,cuToStart,cuToIdx,cuGenStart,cuGenIdx,cuPd,cuQd
end

function get_bus_data(data::OPFData, ::ROCBackend)
nbus = length(data.buses)

FrIdx = [l for b=1:nbus for l in data.FromLines[b]]
ToIdx = [l for b=1:nbus for l in data.ToLines[b]]
GenIdx = [g for b=1:nbus for g in data.BusGenerators[b]]
FrStart = accumulate(+, vcat([1], [length(data.FromLines[b]) for b=1:nbus]))
ToStart = accumulate(+, vcat([1], [length(data.ToLines[b]) for b=1:nbus]))
GenStart = accumulate(+, vcat([1], [length(data.BusGenerators[b]) for b=1:nbus]))

Pd = Float64[data.buses[i].Pd for i=1:nbus]
Qd = Float64[data.buses[i].Qd for i=1:nbus]

cuFrIdx = ROCArray{Int}(undef, length(FrIdx))
cuToIdx = ROCArray{Int}(undef, length(ToIdx))
cuGenIdx = ROCArray{Int}(undef, length(GenIdx))
cuFrStart = ROCArray{Int}(undef, length(FrStart))
cuToStart = ROCArray{Int}(undef, length(ToStart))
cuGenStart = ROCArray{Int}(undef, length(GenStart))
cuPd = ROCArray{Float64}(undef, nbus)
cuQd = ROCArray{Float64}(undef, nbus)
cuFrIdx = adapt(backend, zeros(Int, length(FrIdx)))
cuToIdx = adapt(backend, zeros(Int, length(ToIdx)))
cuGenIdx = adapt(backend, zeros(Int, length(GenIdx)))
cuFrStart = adapt(backend, zeros(Int, length(FrStart)))
cuToStart = adapt(backend, zeros(Int, length(ToStart)))
cuGenStart = adapt(backend, zeros(Int, length(GenStart)))
cuPd = adapt(backend, zeros(nbus))
cuQd = adapt(backend, zeros(nbus))

copyto!(cuFrIdx, FrIdx)
copyto!(cuToIdx, ToIdx)
Expand All @@ -181,20 +73,7 @@ function get_branch_data(data::OPFData, unknown)
error("Unknown device $typeof(unknown)")
end

function get_branch_data(data::OPFData, device::KA.CPU)
buses = data.buses
lines = data.lines
BusIdx = data.BusIdx
nline = length(data.lines)
ybus = Ybus{Array{Float64}}(computeAdmitances(data.lines, data.buses, data.baseMVA, device; VI=Array{Int}, VD=Array{Float64})...)
frBound = [ x for l=1:nline for x in (buses[BusIdx[lines[l].from]].Vmin^2, buses[BusIdx[lines[l].from]].Vmax^2) ]
toBound = [ x for l=1:nline for x in (buses[BusIdx[lines[l].to]].Vmin^2, buses[BusIdx[lines[l].to]].Vmax^2) ]

return ybus.YshR, ybus.YshI, ybus.YffR, ybus.YffI, ybus.YftR, ybus.YftI,
ybus.YttR, ybus.YttI, ybus.YtfR, ybus.YtfI, frBound, toBound
end

function get_branch_data(data::OPFData, device::CUDABackend)
function get_branch_data(data::OPFData, device::KA.Backend)
buses = data.buses
lines = data.lines
BusIdx = data.BusIdx
Expand All @@ -203,18 +82,18 @@ function get_branch_data(data::OPFData, device::CUDABackend)
frBound = [ x for l=1:nline for x in (buses[BusIdx[lines[l].from]].Vmin^2, buses[BusIdx[lines[l].from]].Vmax^2) ]
toBound = [ x for l=1:nline for x in (buses[BusIdx[lines[l].to]].Vmin^2, buses[BusIdx[lines[l].to]].Vmax^2) ]

cuYshR = CuArray{Float64}(undef, length(ybus.YshR))
cuYshI = CuArray{Float64}(undef, length(ybus.YshI))
cuYffR = CuArray{Float64}(undef, nline)
cuYffI = CuArray{Float64}(undef, nline)
cuYftR = CuArray{Float64}(undef, nline)
cuYftI = CuArray{Float64}(undef, nline)
cuYttR = CuArray{Float64}(undef, nline)
cuYttI = CuArray{Float64}(undef, nline)
cuYtfR = CuArray{Float64}(undef, nline)
cuYtfI = CuArray{Float64}(undef, nline)
cuFrBound = CuArray{Float64}(undef, 2*nline)
cuToBound = CuArray{Float64}(undef, 2*nline)
cuYshR = adapt(device, zeros(length(ybus.YshR)))
cuYshI = adapt(device, zeros(length(ybus.YshI)))
cuYffR = adapt(device, zeros(nline))
cuYffI = adapt(device, zeros(nline))
cuYftR = adapt(device, zeros(nline))
cuYftI = adapt(device, zeros(nline))
cuYttR = adapt(device, zeros(nline))
cuYttI = adapt(device, zeros(nline))
cuYtfR = adapt(device, zeros(nline))
cuYtfI = adapt(device, zeros(nline))
cuFrBound = adapt(device, zeros(2*nline))
cuToBound = adapt(device, zeros(2*nline))
copyto!(cuYshR, ybus.YshR)
copyto!(cuYshI, ybus.YshI)
copyto!(cuYffR, ybus.YffR)
Expand All @@ -232,45 +111,7 @@ function get_branch_data(data::OPFData, device::CUDABackend)
cuYttR, cuYttI, cuYtfR, cuYtfI, cuFrBound, cuToBound
end

function get_branch_data(data::OPFData, device::ROCBackend)
buses = data.buses
lines = data.lines
BusIdx = data.BusIdx
nline = length(data.lines)
ybus = Ybus{Array{Float64}}(computeAdmitances(data.lines, data.buses, data.baseMVA, device; VI=Array{Int}, VD=Array{Float64})...)
frBound = [ x for l=1:nline for x in (buses[BusIdx[lines[l].from]].Vmin^2, buses[BusIdx[lines[l].from]].Vmax^2) ]
toBound = [ x for l=1:nline for x in (buses[BusIdx[lines[l].to]].Vmin^2, buses[BusIdx[lines[l].to]].Vmax^2) ]

cuYshR = ROCArray{Float64}(undef, length(ybus.YshR))
cuYshI = ROCArray{Float64}(undef, length(ybus.YshI))
cuYffR = ROCArray{Float64}(undef, nline)
cuYffI = ROCArray{Float64}(undef, nline)
cuYftR = ROCArray{Float64}(undef, nline)
cuYftI = ROCArray{Float64}(undef, nline)
cuYttR = ROCArray{Float64}(undef, nline)
cuYttI = ROCArray{Float64}(undef, nline)
cuYtfR = ROCArray{Float64}(undef, nline)
cuYtfI = ROCArray{Float64}(undef, nline)
cuFrBound = ROCArray{Float64}(undef, 2*nline)
cuToBound = ROCArray{Float64}(undef, 2*nline)
copyto!(cuYshR, ybus.YshR)
copyto!(cuYshI, ybus.YshI)
copyto!(cuYffR, ybus.YffR)
copyto!(cuYffI, ybus.YffI)
copyto!(cuYftR, ybus.YftR)
copyto!(cuYftI, ybus.YftI)
copyto!(cuYttR, ybus.YttR)
copyto!(cuYttI, ybus.YttI)
copyto!(cuYtfR, ybus.YtfR)
copyto!(cuYtfI, ybus.YtfI)
copyto!(cuFrBound, frBound)
copyto!(cuToBound, toBound)

return cuYshR, cuYshI, cuYffR, cuYffI, cuYftR, cuYftI,
cuYttR, cuYttI, cuYtfR, cuYtfI, cuFrBound, cuToBound
end

function init_solution!(env::AdmmEnv, sol::SolutionOneLevel, ybus::Ybus, rho_pq, rho_va)
function init_solution!(env::AdmmEnv, sol::SolutionOneLevel, ybus::Ybus, rho_pq, rho_va, device)
data, model = env.data, env.model

lines = data.lines
Expand All @@ -283,10 +124,10 @@ function init_solution!(env::AdmmEnv, sol::SolutionOneLevel, ybus::Ybus, rho_pq,
YttR = ybus.YttR; YttI = ybus.YttI
YftR = ybus.YftR; YftI = ybus.YftI
YtfR = ybus.YtfR; YtfI = ybus.YtfI
AT = typeof(sol.v_curr)

for g=1:ngen
pg_idx = model.gen_mod.gen_start + 2*(g-1)
sol.v_curr[pg_idx:pg_idx+1] .= AT([
sol.v_curr[pg_idx:pg_idx+1] .= adapt(device, [
0.5*(data.generators[g].Pmin + data.generators[g].Pmax),
0.5*(data.generators[g].Qmin + data.generators[g].Qmax),
])
Expand All @@ -296,15 +137,14 @@ function init_solution!(env::AdmmEnv, sol::SolutionOneLevel, ybus::Ybus, rho_pq,
fill!(sol.u_curr, 0.0)
fill!(sol.v_curr, 0.0)

AT = typeof(sol.u_curr)
for l=1:nline
wij0 = (buses[BusIdx[lines[l].from]].Vmax^2 + buses[BusIdx[lines[l].from]].Vmin^2) / 2
wji0 = (buses[BusIdx[lines[l].to]].Vmax^2 + buses[BusIdx[lines[l].to]].Vmin^2) / 2
wR0 = sqrt(wij0 * wji0)

pij_idx = model.line_start + 8*(l-1)

sol.u_curr[pij_idx:pij_idx+3] .= AT([
sol.u_curr[pij_idx:pij_idx+3] .= adapt(device, [
YffR[l] * wij0 + YftR[l] * wR0,
-YffI[l] * wij0 - YftI[l] * wR0,
YttR[l] * wji0 + YtfR[l] * wR0,
Expand All @@ -318,7 +158,7 @@ function init_solution!(env::AdmmEnv, sol::SolutionOneLevel, ybus::Ybus, rho_pq,
=#
# wRIij[2*(l-1)+1] = wR0
# wRIij[2*l] = 0.0
sol.v_curr[pij_idx+4:pij_idx+7] .= AT([
sol.v_curr[pij_idx+4:pij_idx+7] .= adapt(device, [
wij0,
wji0,
0.0,
Expand Down
2 changes: 1 addition & 1 deletion examples/admm/environment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ mutable struct AdmmEnv{T,TD,TI,TM}
env.data.lines, env.data.buses, env.data.baseMVA, device; VI=Array{Int}, VD=Array{T})...)

env.solution = SolutionOneLevel{T,TD}(env.model)
init_solution!(env, env.solution, ybus, rho_pq, rho_va)
init_solution!(env, env.solution, ybus, rho_pq, rho_va, device)

env.membuf = TM(undef, (31, env.model.nline))
fill!(env.membuf, 0.0)
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
Expand Down
1 change: 1 addition & 0 deletions test/admmtest.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KernelAbstractions
using AMDGPU
using Adapt
using CUDA
using LinearAlgebra
using Printf
Expand Down

0 comments on commit d412393

Please sign in to comment.