Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jd/debug lodf #60

Merged
merged 56 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
51abb8a
add debugging code
jd-lara Sep 26, 2023
ea07a25
add more checking
jd-lara Sep 26, 2023
6465028
one more test
jd-lara Sep 26, 2023
6556db8
remove write to vector option
jd-lara Sep 26, 2023
2fcb7a4
split phases
jd-lara Sep 26, 2023
3fe37e7
fix typo
jd-lara Sep 26, 2023
5904898
fix typo
jd-lara Sep 26, 2023
4825d8b
try single solve call
jd-lara Sep 27, 2023
cdbed5d
fix index
jd-lara Sep 27, 2023
b40b98b
try total
jd-lara Sep 27, 2023
9d8fe72
test sequential solves
jd-lara Sep 27, 2023
01e4496
next try
jd-lara Sep 27, 2023
550fdd3
use correct stage
jd-lara Sep 27, 2023
b2e08c1
test using chunks
jd-lara Sep 27, 2023
92dd80f
update calculations
jd-lara Sep 27, 2023
6c35043
increase chunk size
jd-lara Sep 27, 2023
93cc28b
fix indexing
jd-lara Sep 27, 2023
220c25b
fix tmp size
jd-lara Sep 27, 2023
de38f02
address last chunk
jd-lara Sep 27, 2023
35ca575
add sequential Pardiso solve
jd-lara Sep 27, 2023
68f0e76
formatter
jd-lara Sep 27, 2023
7b6a8c7
fix indexing
jd-lara Sep 27, 2023
cde0367
count on 0
jd-lara Sep 27, 2023
2886e34
one more fix to indexing
jd-lara Sep 27, 2023
5f7736c
remove logging
jd-lara Sep 28, 2023
0d78a5d
enable single call
jd-lara Sep 28, 2023
c26a18c
increase LODF chunk size
jd-lara Sep 28, 2023
9a9badd
reduce chunk size
jd-lara Sep 28, 2023
3037b53
try using copyto
jd-lara Sep 28, 2023
a5b6e4c
use dot
jd-lara Sep 28, 2023
ee06bf8
not call release all
jd-lara Sep 28, 2023
2e1eb70
call pardiso again after release
jd-lara Sep 28, 2023
643f7d3
one more try
jd-lara Sep 28, 2023
62370e4
change iparm 12
jd-lara Sep 28, 2023
8e3708f
call pardiso after release
jd-lara Sep 28, 2023
4351315
try with deepcopy
jd-lara Sep 28, 2023
a14f032
try to multiply
jd-lara Sep 28, 2023
e56491b
change order of operations
jd-lara Sep 28, 2023
77b36f1
dont store in full_BA
jd-lara Sep 28, 2023
fd84136
add logging
jd-lara Sep 28, 2023
90cf324
fix to LODF
jd-lara Sep 28, 2023
53b09f8
revert changes
jd-lara Sep 28, 2023
b240879
update pariso params
jd-lara Sep 28, 2023
03acf04
remove messaging
jd-lara Sep 28, 2023
6fa823c
use mkl linear algebra
jd-lara Sep 28, 2023
10781b8
update pardiso calls
jd-lara Sep 28, 2023
54a644c
change parameters pardiso LODF
jd-lara Sep 28, 2023
5780ac9
readd release
jd-lara Sep 28, 2023
a471447
reduce chunk size
jd-lara Sep 28, 2023
96975cc
bump chunk size
jd-lara Sep 28, 2023
d2ed1ac
bump chunk size
jd-lara Sep 28, 2023
08f168d
use openblas again
jd-lara Sep 29, 2023
8fb402b
se lapack directly
jd-lara Sep 29, 2023
451381c
add MKL again
jd-lara Sep 29, 2023
9eea136
update lodf LAPACK code
jd-lara Sep 29, 2023
74e093f
update lodf klu
jd-lara Sep 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/definitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ const MiB = KiB * KiB
const GiB = MiB * KiB
const MAX_CACHE_SIZE_MiB = 100
const ROW_PERSISTENT_CACHE_WARN = 1 * GiB

DEFAULT_LODF_CHUNK_SIZE = 20_000
102 changes: 81 additions & 21 deletions src/lodf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,45 +128,105 @@ function _calculate_LODF_matrix_DENSE(
return lodf_t
end

function _calculate_LODF_matrix_MKLPardiso(
a::SparseArrays.SparseMatrixCSC{Int8, Int},
ptdf::Matrix{Float64},
function _pardiso_sequential_LODF!(
lodf_t::Matrix{Float64},
A::SparseArrays.SparseMatrixCSC{Float64, Int},
ptdf_denominator_t::Matrix{Float64},
chunk_size::Int = DEFAULT_LODF_CHUNK_SIZE,
)
linecount = size(ptdf, 2)
ptdf_denominator_t = a * ptdf
m_I = Int[]
m_V = Float64[]
for iline in 1:linecount
if (1.0 - ptdf_denominator_t[iline, iline]) < 1.0E-06
push!(m_I, iline)
push!(m_V, 1.0)
else
push!(m_I, iline)
push!(m_V, 1 - ptdf_denominator_t[iline, iline])
linecount = size(lodf_t, 1)
ps = Pardiso.MKLPardisoSolver()
Pardiso.pardisoinit(ps)
#Pardiso.set_msglvl!(ps, Pardiso.MESSAGE_LEVEL_ON)

Pardiso.set_phase!(ps, Pardiso.ANALYSIS)
Pardiso.pardiso(
ps,
lodf_t,
A,
ptdf_denominator_t,
)

Pardiso.set_phase!(ps, Pardiso.NUM_FACT)
Pardiso.pardiso(
ps,
A,
Float64[],
)
Pardiso.set_phase!(ps, Pardiso.SOLVE_ITERATIVE_REFINE)
i_count = 1
tmp = zeros(Float64, linecount, chunk_size)
while i_count <= linecount
edge = min(i_count + chunk_size - 1, linecount)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
edge = min(i_count + chunk_size - 1, linecount)
edge = min(i_count + chunk_size - 1, linecount)

if linecount - edge <= 0
tmp = tmp[:, 1:(edge - i_count + 1)]
end
Pardiso.pardiso(
ps,
tmp,
A,
ptdf_denominator_t[:, i_count:edge],
)
lodf_t[:, i_count:edge] .= tmp
i_count = edge + 1
end
Pardiso.set_phase!(ps, Pardiso.RELEASE_ALL)
return
end

# intialize MKLPardiso
function _pardiso_single_LODF!(
lodf_t::Matrix{Float64},
A::SparseArrays.SparseMatrixCSC{Float64, Int},
ptdf_denominator_t::Matrix{Float64},
chunk_size::Int = DEFAULT_LODF_CHUNK_SIZE,
)
linecount = size(lodf_t, 1)
ps = Pardiso.MKLPardisoSolver()
defaults = Pardiso.get_iparms(ps)
Pardiso.pardisoinit(ps)
Pardiso.set_iparm!(ps, 1, 1)
for (ix, v) in enumerate(defaults[2:end])
Pardiso.set_iparm!(ps, ix + 1, v)
end
Pardiso.set_iparm!(ps, 2, 2)
Pardiso.set_iparm!(ps, 59, 2)
Pardiso.set_iparm!(ps, 6, 1)
# inizialize matrix for evaluation
lodf_t = zeros(linecount, linecount)
# solve system
Pardiso.set_iparm!(ps, 12, 1)
#Pardiso.set_msglvl!(ps, Pardiso.MESSAGE_LEVEL_ON)
Pardiso.pardiso(
ps,
lodf_t,
SparseArrays.sparse(m_I, m_I, m_V),
A,
ptdf_denominator_t,
)
Pardiso.set_phase!(ps, Pardiso.RELEASE_ALL)
# set diagonal to -1
return
end


jd-lara marked this conversation as resolved.
Show resolved Hide resolved
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
function _calculate_LODF_matrix_MKLPardiso(
a::SparseArrays.SparseMatrixCSC{Int8, Int},
ptdf::Matrix{Float64},
)
linecount = size(ptdf, 2)
ptdf_denominator_t = a * ptdf
m_I = Int[]
m_V = Float64[]
for iline in 1:linecount
if (1.0 - ptdf_denominator_t[iline, iline]) < 1.0E-06
push!(m_I, iline)
push!(m_V, 1.0)
else
push!(m_I, iline)
push!(m_V, 1 - ptdf_denominator_t[iline, iline])
end
end
lodf_t = zeros(linecount, linecount)
A = SparseArrays.sparse(m_I, m_I, m_V)
if linecount > DEFAULT_LODF_CHUNK_SIZE
_pardiso_sequential_LODF!(lodf_t, A, ptdf_denominator_t)
else
_pardiso_single_LODF!(lodf_t, A, ptdf_denominator_t)
end
lodf_t[LinearAlgebra.diagind(lodf_t)] .= -1.0
return lodf_t
end
Expand Down
12 changes: 8 additions & 4 deletions src/ptdf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ function _calculate_PTDF_matrix_MKLPardiso(
ABA = calculate_ABA_matrix(A, BA, ref_bus_positions)
# Here add the subnetwork detection
ps = Pardiso.MKLPardisoSolver()
Pardiso.pardisoinit(ps)
# Pardiso.set_msglvl!(ps, Pardiso.MESSAGE_LEVEL_ON)
defaults = Pardiso.get_iparms(ps)
Pardiso.set_iparm!(ps, 1, 1)
Expand All @@ -279,6 +280,7 @@ function _calculate_PTDF_matrix_MKLPardiso(
Pardiso.set_iparm!(ps, 2, 2)
Pardiso.set_iparm!(ps, 59, 2)
Pardiso.set_iparm!(ps, 6, 1)
Pardiso.set_iparm!(ps, 12, 1)

# inizialize matrices for evaluation
valid_ix = setdiff(1:buscount, ref_bus_positions)
Expand All @@ -290,15 +292,17 @@ function _calculate_PTDF_matrix_MKLPardiso(
"Distibuted slack is not supported for systems with multiple reference buses.",
)
elseif isempty(dist_slack) && length(ref_bus_positions) != buscount
Pardiso.pardiso(ps, PTDFm_t[valid_ix, :], ABA, full_BA)
PTDFm_t[valid_ix, :] = full_BA
Pardiso.pardiso(ps, similar(full_BA), ABA, full_BA)
PTDFm_t[valid_ix, :] .= full_BA
Pardiso.set_phase!(ps, Pardiso.RELEASE_ALL)
Pardiso.pardiso(ps, Float64[], ABA, full_BA)
return PTDFm_t
elseif length(dist_slack) == buscount
@info "Distributed bus"
Pardiso.pardiso(ps, PTDFm_t[valid_ix, :], ABA, full_BA)
PTDFm_t[valid_ix, :] = full_BA
Pardiso.pardiso(ps, similar(full_BA), ABA, full_BA)
PTDFm_t[valid_ix, :] .= full_BA
Pardiso.set_phase!(ps, Pardiso.RELEASE_ALL)
Pardiso.pardiso(ps, PTDFm_t[valid_ix, :], ABA, full_BA)
slack_array = dist_slack / sum(dist_slack)
slack_array = reshape(slack_array, 1, buscount)
return PTDFm_t - ones(buscount, 1) * (slack_array * PTDFm_t)
Expand Down
Loading