compute_tableau: Compute directly in compressed row format

fs11_01
Alinson S. Xavier 2 months ago
parent 5e2b0c2958
commit bb59362571

@ -74,15 +74,15 @@ function compute_tableau(
factor = klu(sparse(lhs_b'))
end
@timeit "Initialize sparse arrays" begin
tableau_rhs::Array{Float64} = zeros(length(rows))
tableau_lhs_I::Array{Int} = Int[]
tableau_lhs_J::Array{Int} = Int[]
tableau_lhs_V::Array{Float64} = Float64[]
estimated_nnz::Int = round(length(rows) * ncols * estimated_density)
sizehint!(tableau_lhs_I, estimated_nnz)
sizehint!(tableau_lhs_J, estimated_nnz)
sizehint!(tableau_lhs_V, estimated_nnz)
@timeit "Initialize arrays" begin
num_rows = length(rows)
tableau_rhs::Array{Float64} = zeros(num_rows)
tableau_rowptr::Array{Int} = zeros(Int, num_rows + 1)
tableau_colval::Array{Int} = Int[]
tableau_nzval::Array{Float64} = Float64[]
estimated_nnz::Int = round(num_rows * ncols * estimated_density)
sizehint!(tableau_colval, estimated_nnz)
sizehint!(tableau_nzval, estimated_nnz)
e::Array{Float64} = zeros(nrows)
sol::Array{Float64} = zeros(nrows)
tableau_row::Array{Float64} = zeros(ncols)
@ -90,49 +90,48 @@ function compute_tableau(
A = data.constr_lhs'
b = data.constr_ub
tableau_rowptr[1] = 1
@timeit "Process rows" begin
for k in eachindex(rows)
@timeit "Solve" begin
fill!(e, 0.0)
e[rows[k]] = 1.0
ldiv!(sol, factor, e)
end
@timeit "Compute row" begin
mul!(tableau_row, A, sol)
tableau_rhs[k] = dot(sol, b)
end
needed_space = length(tableau_lhs_I) + ncols
needed_space = length(tableau_colval) + ncols
if needed_space > estimated_nnz
@timeit "Grow arrays" begin
estimated_nnz *= 2
sizehint!(tableau_lhs_I, estimated_nnz)
sizehint!(tableau_lhs_J, estimated_nnz)
sizehint!(tableau_lhs_V, estimated_nnz)
sizehint!(tableau_colval, estimated_nnz)
sizehint!(tableau_nzval, estimated_nnz)
end
end
@timeit "Collect nonzeros" begin
@timeit "Collect nonzeros for row" begin
for j in 1:ncols
val = tableau_row[j]
if abs(val) > tol
push!(tableau_lhs_I, k)
push!(tableau_lhs_J, j)
push!(tableau_lhs_V, val)
push!(tableau_colval, j)
push!(tableau_nzval, val)
end
end
end
tableau_rowptr[k + 1] = length(tableau_colval) + 1
end
end
@timeit "Shrink arrays" begin
sizehint!(tableau_lhs_I, 0)
sizehint!(tableau_lhs_J, 0)
sizehint!(tableau_lhs_V, 0)
sizehint!(tableau_colval, length(tableau_colval))
sizehint!(tableau_nzval, length(tableau_nzval))
end
@timeit "Build sparse matrix" begin
tableau_lhs = sparse(tableau_lhs_I, tableau_lhs_J, tableau_lhs_V, length(rows), ncols)
tableau_lhs_transposed = SparseMatrixCSC(ncols, num_rows, tableau_rowptr, tableau_colval, tableau_nzval)
tableau_lhs = transpose(tableau_lhs_transposed)
end
@timeit "Compute tableau objective row" begin

Loading…
Cancel
Save