-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathMKLPardisoExt.jl
More file actions
185 lines (173 loc) · 5.9 KB
/
Copy pathMKLPardisoExt.jl
File metadata and controls
185 lines (173 loc) · 5.9 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module MKLPardisoExt
import PowerNetworkMatrices as PNM
using Pardiso
import SparseArrays
import LinearAlgebra
"""
Function for internal use only.
Computes the PTDF matrix by means of the MKL Pardiso for dense matrices.
# Arguments
- `A::SparseArrays.SparseMatrixCSC{Int8, Int}`:
Incidence Matrix
- `BA::SparseArrays.SparseMatrixCSC{Float64, Int}`:
BA matrix
- `ref_bus_positions::Set{Int}`:
vector containing the indexes of the reference slack buses.
- `dist_slack::Vector{Float64}`:
vector containing the weights for the distributed slacks.
"""
function PNM._calculate_PTDF_matrix_MKLPardiso(
A::SparseArrays.SparseMatrixCSC{Int8, Int},
BA::SparseArrays.SparseMatrixCSC{Float64, Int},
ref_bus_positions::Set{Int},
dist_slack::Vector{Float64})
linecount = size(BA, 2)
buscount = size(BA, 1)
ABA = PNM.calculate_ABA_matrix(A, BA, ref_bus_positions)
@assert LinearAlgebra.issymmetric(ABA)
ps = Pardiso.MKLPardisoSolver()
Pardiso.set_matrixtype!(ps, Pardiso.REAL_SYM)
Pardiso.pardisoinit(ps)
# Pardiso.set_msglvl!(ps, Pardiso.MESSAGE_LEVEL_ON)
defaults = Pardiso.get_iparms(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)
Pardiso.set_iparm!(ps, 12, 1)
Pardiso.set_iparm!(ps, 11, 0)
Pardiso.set_iparm!(ps, 13, 0)
Pardiso.set_iparm!(ps, 32, 1)
# initialize matrices for evaluation
valid_ix = setdiff(1:buscount, ref_bus_positions)
PTDFm_t = zeros(buscount, linecount)
full_BA = Matrix(BA[valid_ix, :])
if !isempty(dist_slack) && length(ref_bus_positions) != 1
error(
"Distributed 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.set_phase!(ps, Pardiso.RELEASE_ALL)
Pardiso.pardiso(ps)
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.set_phase!(ps, Pardiso.RELEASE_ALL)
Pardiso.pardiso(ps)
slack_array = dist_slack / sum(dist_slack)
slack_array = reshape(slack_array, 1, buscount)
return PTDFm_t - ones(buscount, 1) * (slack_array * PTDFm_t)
else
error("Distributed bus specification doesn't match the number of buses.")
end
return
end
function PNM._pardiso_sequential_LODF!(
lodf_t::Matrix{Float64},
A::SparseArrays.SparseMatrixCSC{Float64, Int},
ptdf_denominator_t::Matrix{Float64},
chunk_size::Int = PNM.DEFAULT_LODF_CHUNK_SIZE,
)
@info "Line Count too large for single compute using Pardiso. Employing Sequential Calculations using a chunk_size=$(chunk_size)"
linecount = size(lodf_t, 1)
@assert LinearAlgebra.ishermitian(A)
ps = Pardiso.MKLPardisoSolver()
Pardiso.set_matrixtype!(ps, Pardiso.REAL_SYM)
Pardiso.pardisoinit(ps)
# Pardiso.set_msglvl!(ps, Pardiso.MESSAGE_LEVEL_ON)
defaults = Pardiso.get_iparms(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, 12, 1)
Pardiso.set_iparm!(ps, 11, 0)
Pardiso.set_iparm!(ps, 13, 0)
Pardiso.set_iparm!(ps, 32, 1)
#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)
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)
Pardiso.pardiso(ps)
return
end
function PNM._pardiso_single_LODF!(
lodf_t::Matrix{Float64},
A::SparseArrays.SparseMatrixCSC{Float64, Int},
ptdf_denominator_t::Matrix{Float64},
)
@assert LinearAlgebra.ishermitian(A)
ps = Pardiso.MKLPardisoSolver()
Pardiso.set_matrixtype!(ps, Pardiso.REAL_SYM_POSDEF)
Pardiso.pardisoinit(ps)
Pardiso.set_iparm!(ps, 1, 1)
defaults = Pardiso.get_iparms(ps)
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, 12, 1)
#Pardiso.set_msglvl!(ps, Pardiso.MESSAGE_LEVEL_ON)
Pardiso.pardiso(
ps,
lodf_t,
A,
ptdf_denominator_t,
)
Pardiso.set_phase!(ps, Pardiso.RELEASE_ALL)
Pardiso.pardiso(ps)
return
end
function PNM._calculate_LODF_matrix_MKLPardiso(
a::SparseArrays.SparseMatrixCSC{Int8, Int},
ptdf::Matrix{Float64},
)
# The demand matrix `diag(1 - PTDF·A[i,i])` is diagonal, so the
# "solve" is a row-wise element-wise division. The Pardiso path that
# used to factor it and back-solve via `_pardiso_sequential_LODF!` /
# `_pardiso_single_LODF!` is no longer needed for this stage.
linecount = size(ptdf, 2)
ptdf_denominator_t = a * ptdf
m_V = PNM._build_lodf_demand(ptdf_denominator_t, linecount)
PNM._apply_lodf_demand!(ptdf_denominator_t, m_V)
return ptdf_denominator_t
end
end # module