GALAHAD SSLS package#
purpose#
Given a (possibly rectangular) matrix \(A\) and symmetric matrices \(H\) and \(C\), form and factorize the block, symmetric matrix
SLS
Full advantage is taken of any zero coefficients in the matrices
\(H\), \(A\) and \(C\).
See Section 4 of $GALAHAD/doc/ssls.pdf for additional details.
method#
The method simply assembles \(K\) from its components, and then relies
on SLS
for analysis, factorization and solves.
matrix storage#
unsymmetric storage#
The unsymmetric \(m\) by \(n\) matrix \(A\) may be presented and stored in a variety of convenient input formats.
Dense storage format: The matrix \(A\) is stored as a compact dense matrix by rows, that is, the values of the entries of each row in turn are stored in order within an appropriate real one-dimensional array. In this case, component \(n \ast i + j\) of the storage array A_val will hold the value \(A_{ij}\) for \(1 \leq i \leq m\), \(1 \leq j \leq n\). The string A_type = ‘dense’ should be specified.
Dense by columns storage format: The matrix \(A\) is stored as a compact dense matrix by columns, that is, the values of the entries of each column in turn are stored in order within an appropriate real one-dimensional array. In this case, component \(m \ast j + i\) of the storage array A_val will hold the value \(A_{ij}\) for \(1 \leq i \leq m\), \(1 \leq j \leq n\). The string A_type = ‘dense_by_columns’ should be specified.
Sparse co-ordinate storage format: Only the nonzero entries of the matrices are stored. For the \(l\)-th entry, \(1 \leq l \leq ne\), of \(A\), its row index i, column index j and value \(A_{ij}\), \(1 \leq i \leq m\), \(1 \leq j \leq n\), are stored as the \(l\)-th components of the integer arrays A_row and A_col and real array A_val, respectively, while the number of nonzeros is recorded as A_ne = \(ne\). The string A_type = ‘coordinate’should be specified.
Sparse row-wise storage format: Again only the nonzero entries are stored, but this time they are ordered so that those in row i appear directly before those in row i+1. For the i-th row of \(A\) the i-th component of the integer array A_ptr holds the position of the first entry in this row, while A_ptr(m+1) holds the total number of entries plus one. The column indices j, \(1 \leq j \leq n\), and values \(A_{ij}\) of the nonzero entries in the i-th row are stored in components l = A_ptr(i), \(\ldots\), A_ptr(i+1)-1, \(1 \leq i \leq m\), of the integer array A_col, and real array A_val, respectively. For sparse matrices, this scheme almost always requires less storage than its predecessor. The string A_type = ‘sparse_by_rows’ should be specified.
Sparse column-wise storage format: Once again only the nonzero entries are stored, but this time they are ordered so that those in column j appear directly before those in column j+1. For the j-th column of \(A\) the j-th component of the integer array A_ptr holds the position of the first entry in this column, while A_ptr(n+1) holds the total number of entries plus one. The row indices i, \(1 \leq i \leq m\), and values \(A_{ij}\) of the nonzero entries in the j-th columnsare stored in components l = A_ptr(j), \(\ldots\), A_ptr(j+1)-1, \(1 \leq j \leq n\), of the integer array A_row, and real array A_val, respectively. As before, for sparse matrices, this scheme almost always requires less storage than the co-ordinate format. The string A_type = ‘sparse_by_columns’ should be specified.
symmetric storage#
The symmetric \(n\) by \(n\) matrix \(H\), as well as the \(m\) by \(m\) matrix \(C\), may also be presented and stored in a variety of formats. But crucially symmetry is exploited by only storing values from the lower triangular part (i.e, those entries that lie on or below the leading diagonal). We focus on \(H\), but everything we say applied equally to \(C\).
Dense storage format: The matrix \(H\) is stored as a compact dense matrix by rows, that is, the values of the entries of each row in turn are stored in order within an appropriate real one-dimensional array. Since \(H\) is symmetric, only the lower triangular part (that is the part \(H_{ij}\) for \(1 \leq j \leq i \leq n\)) need be held. In this case the lower triangle should be stored by rows, that is component \((i-1) * i / 2 + j\) of the storage array H_val will hold the value \(H_{ij}\) (and, by symmetry, \(H_{ji}\)) for \(1 \leq j \leq i \leq n\). The string H_type = ‘dense’ should be specified.
Sparse co-ordinate storage format: Only the nonzero entries of the matrices are stored. For the \(l\)-th entry, \(1 \leq l \leq ne\), of \(H\), its row index i, column index j and value \(H_{ij}\), \(1 \leq j \leq i \leq n\), are stored as the \(l\)-th components of the integer arrays H_row and H_col and real array H_val, respectively, while the number of nonzeros is recorded as H_ne = \(ne\). Note that only the entries in the lower triangle should be stored. The string H_type = ‘coordinate’ should be specified.
Sparse row-wise storage format: Again only the nonzero entries are stored, but this time they are ordered so that those in row i appear directly before those in row i+1. For the i-th row of \(H\) the i-th component of the integer array H_ptr holds the position of the first entry in this row, while H_ptr(n+1) holds the total number of entries plus one. The column indices j, \(1 \leq j \leq i\), and values \(H_{ij}\) of the entries in the i-th row are stored in components l = H_ptr(i), …, H_ptr(i+1)-1 of the integer array H_col, and real array H_val, respectively. Note that as before only the entries in the lower triangle should be stored. For sparse matrices, this scheme almost always requires less storage than its predecessor. The string H_type = ‘sparse_by_rows’ should be specified.
Diagonal storage format: If \(H\) is diagonal (i.e., \(H_{ij} = 0\) for all \(1 \leq i \neq j \leq n\)) only the diagonals entries \(H_{ii}\), \(1 \leq i \leq n\) need be stored, and the first n components of the array H_val may be used for the purpose. The string H_type = ‘diagonal’ should be specified.
Multiples of the identity storage format: If \(H\) is a multiple of the identity matrix, (i.e., \(H = \alpha I\) where \(I\) is the n by n identity matrix and \(\alpha\) is a scalar), it suffices to store \(\alpha\) as the first component of H_val. The string H_type = ‘scaled_identity’ should be specified.
The identity matrix format: If \(H\) is the identity matrix, no values need be stored. The string H_type = ‘identity’ should be specified.
The zero matrix format: The same is true if \(H\) is the zero matrix, but now the string H_type = ‘zero’ or ‘none’ should be specified.
introduction to function calls#
To solve a given problem, functions from the ssls package must be called in the following order:
ssls_initialize - provide default control parameters and set up initial data structures
ssls_read_specfile (optional) - override control values by reading replacement values from a file
ssls_import - set up matrix data structures
ssls_reset_control (optional) - possibly change control parameters if a sequence of problems are being solved
ssls_factorize_matrix - form and factorize the block matrix from its components
ssls_solve_system - solve the block linear system of equations
ssls_information (optional) - recover information about the solution and solution process
ssls_terminate - deallocate data structures
See the examples section for illustrations of use.
parametric real type T and integer type INT#
Below, the symbol T refers to a parametric real type that may be Float32 (single precision), Float64 (double precision) or, if supported, Float128 (quadruple precision). The symbol INT refers to a parametric integer type that may be Int32 (32-bit integer) or Int64 (64-bit integer).
callable functions#
function ssls_initialize(T, INT, data, control, status)
Set default control values and initialize private data
Parameters:
data |
holds private internal data |
control |
is a structure containing control information (see ssls_control_type) |
status |
is a scalar variable of type INT that gives the exit status from the package. Possible values are (currently):
|
function ssls_read_specfile(T, INT, control, specfile)
Read the content of a specification file, and assign values associated with given keywords to the corresponding control parameters. An in-depth discussion of specification files is available, and a detailed list of keywords with associated default values is provided in $GALAHAD/src/ssls/SSLS.template. See also Table 2.1 in the Fortran documentation provided in $GALAHAD/doc/ssls.pdf for a list of how these keywords relate to the components of the control structure.
Parameters:
control |
is a structure containing control information (see ssls_control_type) |
specfile |
is a one-dimensional array of type Vararg{Cchar} that must give the name of the specification file |
function ssls_import(T, INT, control, data, status, n, m, H_type, H_ne, H_row, H_col, H_ptr, A_type, A_ne, A_row, A_col, A_ptr, C_type, C_ne, C_row, C_col, C_ptr)
Import structural matrix data into internal storage prior to solution and analyse the resuting structure.
Parameters:
control |
is a structure whose members provide control parameters for the remaining procedures (see ssls_control_type) |
data |
holds private internal data |
status |
is a scalar variable of type INT that gives the exit status from the package. Possible values are:
|
n |
is a scalar variable of type INT that holds the number of rows in the symmetric matrix \(H\). |
m |
is a scalar variable of type INT that holds the number of rows in the symmetric matrix \(C\). |
H_type |
is a one-dimensional array of type Vararg{Cchar} that specifies the symmetric storage scheme used for the matrix \(H\). It should be one of ‘coordinate’, ‘sparse_by_rows’, ‘dense’, ‘diagonal’, ‘scaled_identity’, ‘identity’, ‘zero’ or ‘none’, the latter pair if \(H=0\); lower or upper case variants are allowed. |
H_ne |
is a scalar variable of type INT that holds the number of entries in the lower triangular part of \(H\) in the sparse co-ordinate storage scheme. It need not be set for any of the other schemes. |
H_row |
is a one-dimensional array of size H_ne and type INT that holds the row indices of the lower triangular part of \(H\) in the sparse co-ordinate storage scheme. It need not be set for any of the other three schemes, and in this case can be C_NULL. |
H_col |
is a one-dimensional array of size H_ne and type INT that holds the column indices of the lower triangular part of \(H\) in either the sparse co-ordinate, or the sparse row-wise storage scheme. It need not be set when the dense, diagonal or (scaled) identity storage schemes are used, and in this case can be C_NULL. |
H_ptr |
is a one-dimensional array of size n+1 and type INT that holds the starting position of each row of the lower triangular part of \(H\), as well as the total number of entries, in the sparse row-wise storage scheme. It need not be set when the other schemes are used, and in this case can be C_NULL. |
A_type |
is a one-dimensional array of type Vararg{Cchar} that specifies the unsymmetric storage scheme used for the matrix \(A\). It should be one of ‘coordinate’, ‘sparse_by_rows’, ‘dense’ or ‘absent’, the latter if access to the Jacobian is via matrix-vector products; lower or upper case variants are allowed. |
A_ne |
is a scalar variable of type INT that holds the number of entries in \(A\) in the sparse co-ordinate storage scheme. It need not be set for any of the other schemes. |
A_row |
is a one-dimensional array of size A_ne and type INT that holds the row indices of \(A\) in the sparse co-ordinate storage scheme. It need not be set for any of the other schemes, and in this case can be C_NULL. |
A_col |
is a one-dimensional array of size A_ne and type INT that holds the column indices of \(A\) in either the sparse co-ordinate, or the sparse row-wise storage scheme. It need not be set when the dense or diagonal storage schemes are used, and in this case can be C_NULL. |
A_ptr |
is a one-dimensional array of size n+1 and type INT that holds the starting position of each row of \(A\), as well as the total number of entries, in the sparse row-wise storage scheme. It need not be set when the other schemes are used, and in this case can be C_NULL. |
C_type |
is a one-dimensional array of type Vararg{Cchar} that specifies the symmetric storage scheme used for the matrix \(C\). It should be one of ‘coordinate’, ‘sparse_by_rows’, ‘dense’, ‘diagonal’, ‘scaled_identity’, ‘identity’, ‘zero’ or ‘none’, the latter pair if \(C=0\); lower or upper case variants are allowed. |
C_ne |
is a scalar variable of type INT that holds the number of entries in the lower triangular part of \(C\) in the sparse co-ordinate storage scheme. It need not be set for any of the other schemes. |
C_row |
is a one-dimensional array of size C_ne and type INT that holds the row indices of the lower triangular part of \(C\) in the sparse co-ordinate storage scheme. It need not be set for any of the other three schemes, and in this case can be C_NULL. |
C_col |
is a one-dimensional array of size C_ne and type INT that holds the column indices of the lower triangular part of \(C\) in either the sparse co-ordinate, or the sparse row-wise storage scheme. It need not be set when the dense, diagonal or (scaled) identity storage schemes are used, and in this case can be C_NULL. |
C_ptr |
is a one-dimensional array of size n+1 and type INT that holds the starting position of each row of the lower triangular part of \(C\), as well as the total number of entries, in the sparse row-wise storage scheme. It need not be set when the other schemes are used, and in this case can be C_NULL. |
function ssls_reset_control(T, INT, control, data, status)
Reset control parameters after import if required.
Parameters:
control |
is a structure whose members provide control parameters for the remaining procedures (see ssls_control_type) |
data |
holds private internal data |
status |
is a scalar variable of type INT that gives the exit status from the package. Possible values are:
|
function ssls_factorize_matrix(T, INT, data, status, n, h_ne, H_val, a_ne, A_val, c_ne, C_val)
Form and factorize the block matrix
Parameters:
data |
holds private internal data |
status |
is a scalar variable of type INT that gives the exit status from the package. Possible values are:
|
n |
is a scalar variable of type INT that holds the number of rows in the symmetric matrix \(H\). |
h_ne |
is a scalar variable of type INT that holds the number of entries in the lower triangular part of the symmetric matrix \(H\). |
H_val |
is a one-dimensional array of size h_ne and type T that holds the values of the entries of the lower triangular part of the symmetric matrix \(H\) in any of the available storage schemes |
a_ne |
is a scalar variable of type INT that holds the number of entries in the unsymmetric matrix \(A\). |
A_val |
is a one-dimensional array of size a_ne and type T that holds the values of the entries of the unsymmetric matrix \(A\) in any of the available storage schemes. |
c_ne |
is a scalar variable of type INT that holds the number of entries in the lower triangular part of the symmetric matrix \(C\). |
C_val |
is a one-dimensional array of size c_ne and type T that holds the values of the entries of the lower triangular part of the symmetric matrix \(C\) in any of the available storage schemes |
function ssls_solve_system(T, INT, data, status, n, m, sol)
Solve the block linear system
Parameters:
data |
holds private internal data |
status |
is a scalar variable of type INT that gives the exit status from the package. Possible values are:
|
n |
is a scalar variable of type INT that holds the number of entries in the vector \(a\). |
m |
is a scalar variable of type INT that holds the number of entries in the vector \(b\). |
sol |
is a one-dimensional array of size n + m and type T. on entry, its first n entries must hold the vector \(a\), and the following entries must hold the vector \(b\). On a successful exit, its first n entries contain the solution components \(x\), and the following entries contain the components \(y\). |
function ssls_information(T, INT, data, inform, status)
Provides output information
Parameters:
data |
holds private internal data |
inform |
is a structure containing output information (see ssls_inform_type) |
status |
is a scalar variable of type INT that gives the exit status from the package. Possible values are (currently):
|
function ssls_terminate(T, INT, data, control, inform)
Deallocate all internal private storage
Parameters:
data |
holds private internal data |
control |
is a structure containing control information (see ssls_control_type) |
inform |
is a structure containing output information (see ssls_inform_type) |
available structures#
ssls_control_type structure#
struct ssls_control_type{T,INT} f_indexing::Bool error::INT out::INT print_level::INT space_critical::Bool deallocate_error_fatal::Bool symmetric_linear_solver::NTuple{31,Cchar} prefix::NTuple{31,Cchar} sls_control::sls_control_type{T,INT}
detailed documentation#
control derived type as a Julia structure
components#
Bool f_indexing
use C or Fortran sparse matrix indexing
INT error
unit for error messages
INT out
unit for monitor output
INT print_level
Bool space_critical
if space is critical, ensure allocated arrays are no bigger than needed
Bool deallocate_error_fatal
exit if any deallocation fails
char symmetric_linear_solver[31]
indefinite linear equation solver
NTuple{31,Cchar} prefix
all output lines will be prefixed by prefix(2:LEN(TRIM(.prefix))-1) where prefix contains the required string enclosed in quotes, e.g. “string” or ‘string’
struct sls_control_type sls_control
control parameters for SLS
ssls_time_type structure#
struct ssls_time_type{T} total::T analyse::T factorize::T solve::T clock_total::T clock_analyse::T clock_factorize::T clock_solve::T
detailed documentation#
time derived type as a Julia structure
components#
T total
total cpu time spent in the package
T analyse
cpu time spent forming and analysing \(K\)
T factorize
cpu time spent factorizing \(K\)
T solve
cpu time spent solving linear systems inolving \(K\)
T clock_total
total clock time spent in the package
T clock_analyse
clock time spent forming and analysing \(K\)
T clock_factorize
clock time spent factorizing \(K\)
T clock_solve
clock time spent solving linear systems inolving \(K\)
ssls_inform_type structure#
struct ssls_inform_type{T,INT} status::INT alloc_status::INT bad_alloc::NTuple{81,Cchar} factorization_integer::Int64 factorization_real::Int64 rank::INT rank_def::Bool time::ssls_time_type{T} sls_inform::sls_inform_type{T,INT}
detailed documentation#
inform derived type as a Julia structure
components#
INT status
return status. See SSLS_form_and_factorize for details
INT alloc_status
the status of the last attempted allocation/deallocation
NTuple{81,Cchar} bad_alloc
Int64 factorization_integer
the total integer workspace required for the factorization
Int64 factorization_real
the total real workspace required for the factorization
INT rank
the computed rank of \(K\)
Bool rank_def
is the matrix \(K\) rank defficient?
struct ssls_time_type time
timings (see above)
struct sls_inform_type sls_inform
inform parameters from the GALAHAD package SLS used
example calls#
This is an example of how to use the package to solve a block system of linear equations; the code is available in $GALAHAD/src/ssls/Julia/test_ssls.jl . A variety of supported matrix storage formats are shown.
# test_ssls.jl
# Simple code to test the Julia interface to SSLS
using GALAHAD
using Test
using Printf
using Accessors
using Quadmath
function test_ssls(::Type{T}, ::Type{INT}; sls::String="sytr") where {T,INT}
# Derived types
data = Ref{Ptr{Cvoid}}()
control = Ref{ssls_control_type{T,INT}}()
inform = Ref{ssls_inform_type{T,INT}}()
# Set problem data
n = INT(3) # dimension of H
m = INT(2) # dimension of C
H_ne = INT(4) # number of elements of H
A_ne = INT(3) # number of elements of A
C_ne = INT(3) # number of elements of C
H_dense_ne = INT(6) # number of elements of H
A_dense_ne = INT(6) # number of elements of A
C_dense_ne = INT(3) # number of elements of C
H_row = INT[1, 2, 3, 3] # row indices, NB lower triangle
H_col = INT[1, 2, 3, 1]
H_ptr = INT[1, 2, 3, 5]
A_row = INT[1, 1, 2]
A_col = INT[1, 2, 3]
A_ptr = INT[1, 3, 4]
C_row = INT[1, 2, 2] # row indices, NB lower triangle
C_col = INT[1, 1, 2]
C_ptr = INT[1, 2, 4]
H_val = T[1.0, 2.0, 3.0, 1.0]
A_val = T[2.0, 1.0, 1.0]
C_val = T[4.0, 1.0, 2.0]
H_dense = T[1.0, 0.0, 2.0, 1.0, 0.0, 3.0]
A_dense = T[2.0, 1.0, 0.0, 0.0, 0.0, 1.0]
C_dense = T[4.0, 1.0, 2.0]
H_diag = T[1.0, 1.0, 2.0]
C_diag = T[4.0, 2.0]
H_scid = T[2.0]
C_scid = T[2.0]
st = ' '
status = Ref{INT}()
@printf(" Fortran sparse matrix indexing\n\n")
@printf(" basic tests of storage formats\n\n")
for d in 1:7
# Initialize SSLS
ssls_initialize(T, INT, data, control, status)
# Linear solvers
@reset control[].symmetric_linear_solver = galahad_linear_solver(sls)
# sparse co-ordinate storage
if d == 1
st = 'C'
ssls_import(T, INT, control, data, status, n, m,
"coordinate", H_ne, H_row, H_col, C_NULL,
"coordinate", A_ne, A_row, A_col, C_NULL,
"coordinate", C_ne, C_row, C_col, C_NULL)
ssls_factorize_matrix(T, INT, data, status,
H_ne, H_val,
A_ne, A_val,
C_ne, C_val)
end
# sparse by rows
if d == 2
st = 'R'
ssls_import(T, INT, control, data, status, n, m,
"sparse_by_rows", H_ne, C_NULL, H_col, H_ptr,
"sparse_by_rows", A_ne, C_NULL, A_col, A_ptr,
"sparse_by_rows", C_ne, C_NULL, C_col, C_ptr)
ssls_factorize_matrix(T, INT, data, status,
H_ne, H_val,
A_ne, A_val,
C_ne, C_val)
end
# dense
if d == 3
st = 'D'
ssls_import(T, INT, control, data, status, n, m,
"dense", H_ne, C_NULL, C_NULL, C_NULL,
"dense", A_ne, C_NULL, C_NULL, C_NULL,
"dense", C_ne, C_NULL, C_NULL, C_NULL)
ssls_factorize_matrix(T, INT, data, status,
H_dense_ne, H_dense,
A_dense_ne, A_dense,
C_dense_ne, C_dense)
end
# diagonal
if d == 4
st = 'L'
ssls_import(T, INT, control, data, status, n, m,
"diagonal", H_ne, C_NULL, C_NULL, C_NULL,
"dense", A_ne, C_NULL, C_NULL, C_NULL,
"diagonal", C_ne, C_NULL, C_NULL, C_NULL)
ssls_factorize_matrix(T, INT, data, status,
n, H_diag,
A_dense_ne, A_dense,
m, C_diag )
end
# scaled identity
if d == 5
st = 'S'
ssls_import(T, INT, control, data, status, n, m,
"scaled_identity", H_ne, C_NULL, C_NULL, C_NULL,
"dense", A_ne, C_NULL, C_NULL, C_NULL,
"scaled_identity", C_ne, C_NULL, C_NULL, C_NULL)
ssls_factorize_matrix(T, INT, data, status,
1, H_scid,
A_dense_ne, A_dense,
1, C_scid)
end
# identity
if d == 6
st = 'I'
ssls_import(T, INT, control, data, status, n, m,
"identity", H_ne, C_NULL, C_NULL, C_NULL,
"dense", A_ne, C_NULL, C_NULL, C_NULL,
"identity", C_ne, C_NULL, C_NULL, C_NULL)
ssls_factorize_matrix(T, INT, data, status,
0, H_val,
A_dense_ne, A_dense,
0, C_val)
end
# zero
if d == 7
st = 'Z'
ssls_import(T, INT, control, data, status, n, m,
"identity", H_ne, C_NULL, C_NULL, C_NULL,
"dense", A_ne, C_NULL, C_NULL, C_NULL,
"zero", C_ne, C_NULL, C_NULL, C_NULL)
ssls_factorize_matrix(T, INT, data, status,
0, H_val,
A_dense_ne, A_dense,
0, C_NULL)
end
# check that the factorization succeeded
if status[] != 0
ssls_information(T, INT, data, inform, status)
@printf("%c: SSLS_solve factorization exit status = %1i\n", st, inform[].status)
return 1
end
# Set right-hand side (a, b)
if d == 4
sol = T[3.0, 2.0, 3.0, -1.0, -1.0]
elseif d == 5
sol = T[4.0, 3.0, 3.0, 1.0, -1.0]
elseif d == 6
sol = T[3.0, 2.0, 2.0, 2.0, 0.0]
elseif d == 7
sol = T[3.0, 2.0, 2.0, 3.0, 1.0]
else
sol = T[4.0, 3.0, 5.0, -2.0, -2.0]
end
ssls_solve_system(T, INT, data, status, n, m, sol)
if status[] != 0
ssls_information(T, INT, data, inform, status)
@printf("%c: SSLS_solve exit status = %i\n", st, inform[].status)
continue
end
ssls_information(T, INT, data, inform, status)
if inform[].status == 0
@printf("%c: status = %1i\n", st, inform[].status)
# @printf("sol: ")
# for i = 1:n+m
# @printf("%f ", x[i])
# end
else
@printf("%c: SSLS_solve exit status = %1i\n", st, inform[].status)
end
# Delete internal workspace
ssls_terminate(T, INT, data, control, inform)
end
return 0
end
for (T, INT, libgalahad) in ((Float32 , Int32, GALAHAD.libgalahad_single ),
(Float32 , Int64, GALAHAD.libgalahad_single_64 ),
(Float64 , Int32, GALAHAD.libgalahad_double ),
(Float64 , Int64, GALAHAD.libgalahad_double_64 ),
(Float128, Int32, GALAHAD.libgalahad_quadruple ),
(Float128, Int64, GALAHAD.libgalahad_quadruple_64))
if isfile(libgalahad)
@testset "SSLS -- $T -- $INT" begin
@test test_ssls(T, INT) == 0
end
end
end