GALAHAD NREK package#
purpose#
The nrek package uses an extended-Krylov-subspace iteration to find the
global minimizer of a norm-regularized quadratic objective function;
this is commonly known as the norm-regularization subproblem.
The aim is to minimize the regularized quadratic objective function
Factorization of the matrices \(H\) and, if present, \(S\)
will be required, so this package is most suited
for the case where such a factorization may be found efficiently. If
this is not the case, the package glrt may be preferred.
See Section 4 of $GALAHAD/doc/nrek.pdf for additional details.
method#
The required solution \(x_*\) necessarily satisfies the optimality condition \(H x_* + \lambda_* S x_* + c = 0\), where \(\lambda_* \sigma \|x_*\|^{p-2}\) is a Lagrange multiplier for the regularization. In addition in all cases, the matrix \(H + \lambda_* S\) will be positive semi-definite; in most instances it will actually be positive definite, but in special “hard” cases singularity is a possibility.
The method is iterative, and is based upon building a solution approximation from an orthogonal basis of the evolving extended Krylov subspaces \({\cal K}_{2m+1}(H,c) = \mbox{span}\{c,H^{-1}c,H c,H^{-2}c,H^2c,\ldots,\) \(H^{-m}c,H^{m}c\}\) as \(m\) increases. The key observations are (i) the manifold of solutions to the optimality system \[ ( H + \lambda I ) x(\lambda) = - c\] as a function of \(\sigma\) is of approximately very low rank, (ii) the subspace \({\cal K}_{2m+1}(H,c)\) rapidly gives a very good approximation to this manifold, (iii) it is straightforward to build an orthogonal basis of \({\cal K}_{2m+1}(H,c)\) using short-term recurrences and a single factorization of \(H\), and (iv) solutions to the norm-regularization subproblem restricted to elements of the orthogonal subspace may be found very efficiently using effective high-order root-finding methods. Coping with general scalings \(S\) is a straightforward extension so long as factorization of \(S\) is also possible.
reference#
The method is described in detail in
H. Al Daas and N. I. M. Gould. Extended-Krylov-subspace methods for trust-region and norm-regularization subproblems. Preprint STFC-P-2025-002, Rutherford Appleton Laboratory, Oxfordshire, England.
matrix storage#
The symmetric \(n\) by \(n\) matrices \(H\) and, optionally, \(S\) 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).
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 \(0 \leq j \leq i \leq n-1\)) need be held. In this case the lower triangle should be stored by rows, that is component \(i * i / 2 + j\) of the storage array H_val will hold the value \(H_{ij}\) (and, by symmetry, \(H_{ji}\)) for \(0 \leq j \leq i \leq n-1\). 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, \(0 \leq l \leq ne-1\), of \(H\), its row index i, column index j and value \(H_{ij}\), \(0 \leq j \leq i \leq n-1\), 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) holds the total number of entries. The column indices j, \(0 \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 \(0 \leq i \neq j \leq n-1\)) only the diagonals entries \(H_{ii}\), \(0 \leq i \leq n-1\) 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 nrek package must be called in the following order:
nrek_initialize - provide default control parameters and set up initial data structures
nrek_read_specfile (optional) - override control values by reading replacement values from a file
nrek_import - set up problem data structures and fixed values
nrek_import_s - (optional) set up problem data structures and fixed values for the scaling matrix \(S\), if any
nrek_reset_control (optional) - possibly change control parameters if a sequence of problems are being solved
nrek_solve_problem - solve the trust-region problem
nrek_information (optional) - recover information about the solution and solution process
nrek_terminate - deallocate data structures
See the examples section for illustrations of use.
callable functions#
overview of functions provided#
// typedefs typedef float spc_; typedef double rpc_; typedef int ipc_; // structs struct nrek_control_type; struct nrek_time_type; struct nrek_inform_type; // global functions void nrek_initialize(void **data, struct nrek_control_type* control, ipc_ *status); void nrek_read_specfile(struct nrek_control_type* control, const char specfile[]); void nrek_import( struct nrek_control_type* control, void **data, ipc_ *status, ipc_ n, const char H_type[], ipc_ H_ne, const ipc_ H_row[], const ipc_ H_col[], const ipc_ H_ptr[] ); void nrek_import_s( void **data, ipc_ *status, ipc_ n, const char S_type[], ipc_ S_ne, const ipc_ S_row[], const ipc_ S_col[], const ipc_ S_ptr[] ); void nrek_reset_control( struct nrek_control_type* control, void **data, ipc_ *status ); void nrek_solve_problem( void **data, ipc_ *status, ipc_ n, ipc_ H_ne, const rpc_ H_val[], const rpc_ c[], const rpc_ power, const rpc_ weight, rpc_ x[], ipc_ S_ne, const rpc_ S_val[] ); void nrek_information(void **data, struct nrek_inform_type* inform, ipc_ *status); void nrek_terminate( void **data, struct nrek_control_type* control, struct nrek_inform_type* inform );
typedefs#
typedef float spc_
spc_ is real single precision
typedef double rpc_
rpc_ is the real working precision used, but may be changed to float by
defining the preprocessor variable REAL_32 or (if supported) to
__real128 using the variable REAL_128.
typedef int ipc_
ipc_ is the default integer word length used, but may be changed to
int64_t by defining the preprocessor variable INTEGER_64.
function and structure names#
The function and structure names described below are appropriate for the
default real working precision (double) and integer word length
(int32_t). To use the functions and structures with different precisions
and integer word lengths, an additional suffix must be added to their names
(and the arguments set accordingly). The appropriate suffices are:
_s for single precision (float) reals and
standard 32-bit (int32_t) integers;
_q for quadruple precision (__real128) reals (if supported) and
standard 32-bit (int32_t) integers;
_64 for standard precision (double) reals and
64-bit (int64_t) integers;
_s_64 for single precision (float) reals and
64-bit (int64_t) integers; and
_q_64 for quadruple precision (__real128) reals (if supported) and
64-bit (int64_t) integers.
Thus a call to nrek_initialize below will instead be
void nrek_initialize_s_64(void **data, struct nrek_control_type_s_64* control, int64_t *status)
if single precision (float) reals and 64-bit (int64_t) integers are
required. Thus it is possible to call functions for this package
with more that one precision and/or integer word length at same time. An
example is provided for the package expo,
and the obvious modifications apply equally here.
function calls#
void nrek_initialize(void **data, struct nrek_control_type* control, ipc_ *status)
Set default control values and initialize private data
Parameters:
data |
holds private internal data |
control |
is a struct containing control information (see nrek_control_type) |
status |
is a scalar variable of type ipc_, that gives the exit status from the package. Possible values are (currently):
|
void nrek_read_specfile(struct nrek_control_type* control, const char 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/nrek/NREK.template. See also Table 2.1 in the Fortran documentation provided in $GALAHAD/doc/nrek.pdf for a list of how these keywords relate to the components of the control structure.
Parameters:
control |
is a struct containing control information (see nrek_control_type) |
specfile |
is a character string containing the name of the specification file |
void nrek_import( struct nrek_control_type* control, void **data, ipc_ *status, ipc_ n, const char H_type[], ipc_ H_ne, const ipc_ H_row[], const ipc_ H_col[], const ipc_ H_ptr[] )
Import problem data into internal storage prior to solution.
Parameters:
control |
is a struct whose members provide control paramters for the remaining prcedures (see nrek_control_type) |
data |
holds private internal data |
status |
is a scalar variable of type ipc_, that gives the exit status from the package. Possible values are:
|
n |
is a scalar variable of type ipc_, that holds the number of rows (and columns) of H. |
H_type |
is a one-dimensional array of type char that specifies the symmetric storage scheme used for the Hessian, \(H\). It should be one of ‘coordinate’, ‘sparse_by_rows’, ‘dense’, ‘diagonal’, ‘scaled-identity’, ‘identity’, ‘zero’ or ‘none’; lower or upper case variants are allowed. |
H_ne |
is a scalar variable of type ipc_, 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 ipc_, 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 NULL. |
H_col |
is a one-dimensional array of size H_ne and type ipc_, 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 or diagonal storage schemes are used, and in this case can be NULL. |
H_ptr |
is a one-dimensional array of size n+1 and type ipc_, 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 NULL. |
void nrek_import_s( void **data, ipc_ *status, ipc_ n, const char S_type[], ipc_ S_ne, const ipc_ S_row[], const ipc_ S_col[], const ipc_ S_ptr[] )
Import data for the scaling matrix \(S\) into internal storage prior to solution.
Parameters:
data |
holds private internal data |
status |
is a scalar variable of type ipc_, that gives the exit status from the package. Possible values are:
|
n |
is a scalar variable of type ipc_, that holds the number of rows (and columns) of M. |
S_type |
is a one-dimensional array of type char that specifies the symmetric storage scheme used for the scaling matrix, \(S\). It should be one of ‘coordinate’, ‘sparse_by_rows’, ‘dense’, or ‘diagonal’; lower or upper case variants are allowed. |
S_ne |
is a scalar variable of type ipc_, that holds the number of entries in the lower triangular part of \(S\) in the sparse co-ordinate storage scheme. It need not be set for any of the other schemes. |
S_row |
is a one-dimensional array of size S_ne and type ipc_, that holds the row indices of the lower triangular part of \(S\) 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 NULL. |
S_col |
is a one-dimensional array of size S_ne and type ipc_, that holds the column indices of the lower triangular part of \(S\) in either the sparse co-ordinate, or the sparse row-wise storage scheme. It need not be set when the dense, diagonal or identity storage schemes are used, and in this case can be NULL. |
S_ptr |
is a one-dimensional array of size n+1 and type ipc_, that holds the starting position of each row of the lower triangular part of \(S\), 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 NULL. |
void nrek_reset_control( struct nrek_control_type* control, void **data, ipc_ *status )
Reset control parameters after import if required.
Parameters:
control |
is a struct whose members provide control paramters for the remaining prcedures (see nrek_control_type) |
data |
holds private internal data |
status |
is a scalar variable of type ipc_, that gives the exit status from the package. Possible values are:
|
void nrek_solve_problem( void **data, ipc_ *status, ipc_ n, ipc_ H_ne, const rpc_ H_val[], const rpc_ c[], const rpc_ power, const rpc_ weight, rpc_ x[], ipc_ S_ne, const rpc_ S_val[] )
Solve the norm-regularization subproblem.
Parameters:
data |
holds private internal data |
status |
is a scalar variable of type ipc_, that gives the entry and exit status from the package. On initial entry, status must be set to 1. Possible exit values are:
|
n |
is a scalar variable of type ipc_, that holds the number of variables |
H_ne |
is a scalar variable of type ipc_, that holds the number of entries in the lower triangular part of the Hessian matrix \(H\). |
H_val |
is a one-dimensional array of size h_ne and type rpc_, that holds the values of the entries of the lower triangular part of the Hessian matrix \(H\) in any of the available storage schemes. |
c |
is a one-dimensional array of size n and type rpc_, that holds the linear term \(c\) of the objective function. The j-th component of c, j = 0, … , n-1, contains \(c_j\). |
power |
is a scalar of type rpc_, that holds the regularization power, \(p\), used. power must be strictly larger than two |
weight |
is a scalar of type rpc_, that holds the regularization weight, \(\sigma\), used. weight must be strictly positive |
x |
is a one-dimensional array of size n and type rpc_, that holds the values \(x\) of the optimization variables. The j-th component of x, j = 0, … , n-1, contains \(x_j\). |
S_ne |
is a scalar variable of type ipc_, that holds the number of entries in the scaling matrix \(S\) if it not the identity matrix. |
S_val |
is a one-dimensional array of size S_ne and type rpc_, that holds the values of the entries of the scaling matrix \(S\), if it is not the identity matrix, in any of the available storage schemes. If S_val is NULL, \(S\) will be taken to be the identity matrix. |
void nrek_information(void **data, struct nrek_inform_type* inform, ipc_ *status)
Provides output information
Parameters:
data |
holds private internal data |
inform |
is a struct containing output information (see nrek_inform_type) |
status |
is a scalar variable of type ipc_, that gives the exit status from the package. Possible values are (currently):
|
void nrek_terminate( void **data, struct nrek_control_type* control, struct nrek_inform_type* inform )
Deallocate all internal private storage
Parameters:
data |
holds private internal data |
control |
is a struct containing control information (see nrek_control_type) |
inform |
is a struct containing output information (see nrek_inform_type) |
available structures#
nrek_control_type structure#
#include <galahad_nrek.h> struct nrek_control_type { // fields bool f_indexing; ipc_ error; ipc_ out; ipc_ print_level; ipc_ eks_max; ipc_ it_max; rpc_ f; rpc_ increase; rpc_ stop_residual; bool reorthogonalize; bool s_version_52; bool perturb_c; bool stop_check_all_orders; bool new_weight; bool new_values; bool space_critical; bool deallocate_error_fatal; char linear_solver[31]; char linear_solver_for_s[31]; char prefix[31]; struct sls_control_type sls_control; struct sls_control_type sls_s_control; struct rqs_control_type rqs_control; };
detailed documentation#
control derived type as a C struct
components#
bool f_indexing
use C or Fortran sparse matrix indexing
ipc_ error
unit for error messages
ipc_ out
unit for monitor output
ipc_ print_level
controls level of diagnostic output
ipc_ eks_max
maximum dimension of the extended Krylov space employed. If a negative value is given, the value 100 will be used instead
ipc_ it_max
the maximum number of iterations allowed. If a negative value is given, the value 100 will be used instead
rpc_ f
the value of \(f\) in the objective function. This value has no effect on the computed \(x\), and takes the value 0.0 by default
rpc_ increase
the value of the increase factor for a suggested subsequent regularization weight, see control[‘next_weight’]. The suggested weight will be increase times thecurrent weight.
rpc_ stop_residual
the value of the stopping tolerance used by the algorithm. The iteration stops as soon as \(x\) and \(\lambda\) are found to satisfy \(\| ( H + \lambda S ) x + c \| <\) stop_residual \(\times \max( 1, \|c\| )\).
bool reorthogonalize
should be set to true if the generated basis of the extended-Krylov subspace is to be reorthogonalized at every iteration. This can be very expensive, and is generally not warranted
bool s_version_52
should be set to true if Algorithm 5.2 in the paper is used to generate the extended Krylov space recurrences when a non-unit \(S\) is given, and false if those from Algorithm B.3 ares used instead. In practice, there is very little difference in performance and accuracy
bool perturb_c
should be set to true if the user wishes to make a tiny pseudo-random perturbations to the components of the term \(c\) to try to protect from the so-called (probability zero) “hard” case. Perturbations are generally not needed, and should only be used in very exceptional cases
bool stop_check_all_orders
should be set to true if the algorithm checks for termination for each new member of the extended Krylov space. Such checks incur some extra cost, and experience shows that testing every second member is sufficient
bool new_weight
should be set to true if the call retains the previous \(H\), \(S\) and \(c\), but with a new, larger weight
bool new_values
should be set to true if the any of the values of \(H\), \(S\) and \(c\) has changed since a previous call
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 linear_solver[31]
the name of the linear equation solver used to solve any symmetric positive-definite linear system involving \(H\) that might arise. Possible choices are currently: ‘sils’, ‘ma27’, ‘ma57’, ‘ma77’, ‘ma86’, ‘ma87’, ‘ma97’, ssids, ‘pardiso’, ‘wsmp’, ‘sytr’, ‘potr’ and ‘pbtr’ although only ‘sytr’, ‘potr’, ‘pbtr’ and, for OMP 4.0-compliant compilers, ‘ssids’ are installed by default; others are easily installed (see README.external). More details of the capabilities of each solver are provided in the documentation for galahad_sls.
char linear_solver_for_s[31]
the name of the linear equation solver used to solve any symmetric positive-definite linear system involving the optional \(S\) that might arise. Possible choices are currently: ‘sils’, ‘ma27’, ‘ma57’, ‘ma77’, ‘ma86’, ‘ma87’, ‘ma97’, ssids, ‘pardiso’, ‘wsmp’, ‘sytr’, ‘potr’ and ‘pbtr’ although only ‘sytr’, ‘potr’, ‘pbtr’ and, for OMP 4.0-compliant compilers, ‘ssids’ are installed by default; others are easily installed (see README.external). More details of the capabilities of each solver are provided in the documentation for galahad_sls.
char prefix[31]
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 the Cholesky factorization and solution (see sls_c documentation)
struct sls_control_type sls_s_control
control parameters for the Cholesky factorization and solution when applied to \(S\) (see sls_c documentation)
struct rqs_control_type rqs_control
control parameters for the diagonal subproblem solve (see rqs_c documentation)
nrek_time_type structure#
#include <galahad_nrek.h> struct nrek_time_type { // fields rpc_ total; rpc_ assemble; rpc_ analyse; rpc_ factorize; rpc_ solve; rpc_ clock_total; rpc_ clock_assemble; rpc_ clock_analyse; rpc_ clock_factorize; rpc_ clock_solve; };
detailed documentation#
time derived type as a C struct
components#
rpc_ total
total CPU time spent in the package
rpc_ assemble
CPU time spent building \(H\) and \(S\).
rpc_ analyse
CPU time spent reordering \(H\) and \(S\) prior to factorization.
rpc_ factorize
CPU time spent factorizing \(H\) and \(S\).
rpc_ solve
CPU time spent solving linear systems inolving \(H\) and \(S\).
rpc_ clock_total
total clock time spent in the package
rpc_ clock_assemble
clock time spent building \(H\) and \(S\)
rpc_ clock_analyse
clock time spent reordering \(H\) and \(S\) prior to factorization
rpc_ clock_factorize
clock time spent factorizing \(H\) and \(S\)
rpc_ clock_solve
clock time spent solving linear systems inolving \(H\) and \(S\)
nrek_inform_type structure#
#include <galahad_nrek.h> struct nrek_inform_type { // fields ipc_ status; ipc_ alloc_status; ipc_ iter; ipc_ n_vec; rpc_ obj; rpc_ obj_regularized; rpc_ x_norm; rpc_ multiplier; rpc_ weight; rpc_ next_weight; rpc_ error; char bad_alloc[81]; struct nrek_time_type time; struct sls_inform_type sls_inform; struct sls_inform_type sls_s_inform; struct rqs_inform_type rqs_inform; };
detailed documentation#
inform derived type as a C struct
components#
ipc_ status
reported return status:
0
the solution has been found
-1
an array allocation has failed
-2
an array deallocation has failed
-3
n and/or \(\Delta\) is not positive, or the requirement that type contains its relevant string ‘dense’, ‘coordinate’, ‘sparse_by_rows’, ‘diagonal’, ‘scaled_identity’, ‘identity’, ‘zero’ or ‘none’ has been violated.
-9
the analysis phase of the factorization failed; the return status from the factorization package is given by inform.sls_inform.status or inform.sls_s_inform.status as appropriate
-10
the factorization failed; the return status from the factorization package is given by inform.sls_inform.status or inform.sls_s_inform.status as appropriate
-11
the solution of a set of linear equations using factors from the factorization package failed; the return status from the factorization package is given by inform.sls_inform.status or inform.sls_s_inform.status as appropriate
-15
\(S\) does not appear to be strictly diagonally dominant
-16
ill-conditioning has prevented further progress
-18
too many iterations have been required. This may happen if control.eks max is too small, but may also be symptomatic of a badly scaled problem.
-31
a resolve call has been made before an initial call (see control.new_weight and control.new_values)
-38
an error occurred in a call to an LAPACK subroutine
ipc_ alloc_status
STAT value after allocate failure
ipc_ iter
the total number of iterations required
ipc_ n_vec
the number of orthogonal vectors required (the dimension of the extended-Krylov subspace)
rpc_ obj
the value of the quadratic function \(f + c^T x + \frac{1}{2} x^T H x\)
rpc_ obj_regularized
the value of the regularized quadratic function \(r(x)\)
rpc_ x_norm
the \(S\) -norm of \(x\), \(||x||_S\)
rpc_ multiplier
the Lagrange multiplier associated with the regularization
rpc_ weight
the value of the current weight
rpc_ next_weight
the value of the the proposed next weight to be used if the current weight proves to be too small (see inform.increase).
rpc_ error
the value of the norm of the maximum relative residual error, \(\|(H+\lambda S) x + c\|/\max(1,\|c\|)\)
char bad_alloc[81]
name of array that provoked an allocate failure
struct nrek_time_type time
time information
struct sls_inform_type sls_inform
Cholesky information for factorization and solves with \(H\) (see sls_c documentation)
struct sls_inform_type sls_s_inform
Cholesky information for factorization and solves with \(S\) (see sls_c documentation)
struct rqs_inform_type rqs_inform
diagonal subproblem solve information (see rqs_c documentation)
example calls#
This is an example of how to use the package to solve a trust-region subproblem; the code is available in $GALAHAD/src/nrek/C/nrekt.c . A variety of supported Hessian and constraint matrix storage formats are shown.
Notice that C-style indexing is used, and that this is flagged by setting
control.f_indexing to false. The floating-point type rpc_
is set in galahad_precision.h to double by default, but to float
if the preprocessor variable SINGLE is defined. Similarly, the integer
type ipc_ from galahad_precision.h is set to int by default,
but to int64_t if the preprocessor variable INTEGER_64 is defined.
/* nrekt.c */
/* Full test for the NREK C interface using C sparse matrix indexing */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "galahad_precision.h"
#include "galahad_cfunctions.h"
#include "galahad_nrek.h"
#ifdef REAL_128
#include <quadmath.h>
#endif
int main(void) {
// Derived types
void *data;
struct nrek_control_type control;
struct nrek_inform_type inform;
// Set problem data
ipc_ n = 3; // dimension of H
ipc_ H_ne = 4; // number of elements of H
ipc_ S_ne = 3; // number of elements of M
ipc_ H_dense_ne = 6; // number of elements of H
ipc_ S_dense_ne = 6; // number of elements of M
ipc_ H_row[] = {0, 1, 2, 2}; // row indices, NB lower triangle
ipc_ H_col[] = {0, 1, 2, 0};
ipc_ H_ptr[] = {0, 1, 2, 4};
ipc_ S_row[] = {0, 1, 2}; // row indices, NB lower triangle
ipc_ S_col[] = {0, 1, 2};
ipc_ S_ptr[] = {0, 1, 2, 3};
rpc_ H_val[] = {1.0, 2.0, 3.0, 4.0};
rpc_ S_val[] = {1.0, 2.0, 1.0};
rpc_ H_dense[] = {1.0, 0.0, 2.0, 4.0, 0.0, 3.0};
rpc_ S_dense[] = {1.0, 0.0, 2.0, 0.0, 0.0, 1.0};
rpc_ H_diag[] = {1.0, 0.0, 2.0};
rpc_ S_diag[] = {1.0, 2.0, 1.0};
rpc_ power = 3.0;
rpc_ weight = 1.0;
rpc_ c[] = {0.0, 2.0, 0.0};
char st = ' ';
ipc_ status;
rpc_ x[n];
char sr[3];
printf(" C sparse matrix indexing\n\n");
printf(" basic tests of storage formats\n\n");
for( ipc_ s_is=0; s_is <= 1; s_is++){ // include a scaling matrix?
for( ipc_ storage_type=1; storage_type <= 4; storage_type++){
// Initialize NREK
nrek_initialize( &data, &control, &status );
// Set user-defined control options
control.f_indexing = false; // C sparse matrix indexing
switch(storage_type){
case 1: // sparse co-ordinate storage
st = 'C';
// import the control parameters and structural data
nrek_import( &control, &data, &status, n,
"coordinate", H_ne, H_row, H_col, NULL );
if (s_is == 1) {
nrek_s_import( &data, &status, n,
"coordinate", S_ne, S_row, S_col, NULL );
}
break;
case 2: // sparse by rows
st = 'R';
// import the control parameters and structural data
nrek_import( &control, &data, &status, n,
"sparse_by_rows", H_ne, NULL, H_col, H_ptr );
if (s_is == 1) {
nrek_s_import( &data, &status, n,
"sparse_by_rows", S_ne, NULL, S_col, S_ptr );
}
break;
case 3: // dense
st = 'D';
// import the control parameters and structural data
nrek_import( &control, &data, &status, n,
"dense", H_ne, NULL, NULL, NULL );
if (s_is == 1) {
nrek_s_import( &data, &status, n,
"dense", S_ne, NULL, NULL, NULL );
}
break;
case 4: // diagonal
st = 'G';
// import the control parameters and structural data
nrek_import( &control, &data, &status, n,
"diagonal", H_ne, NULL, NULL, NULL );
if (s_is == 1) {
nrek_s_import( &data, &status, n,
"diagonal", S_ne, NULL, NULL, NULL );
}
break;
}
for( ipc_ w_is=1; w_is <= 2; w_is++){ // original or larger weight
if (w_is == 1) { // use the original weight
weight = 1.0;
}
else { // use the larger weight
weight = inform.next_weight;
control.new weight = true;
nrek_reset_control( &control, &data, &status );
}
if (w_is == 2 && s_is == 1 ) {
strcpy(sr, "S-");
}
else if (w_is == 2) {
strcpy(sr, "- ");
}
else if (s_is == 1) {
strcpy(sr, "S ");
}
else {
strcpy(sr, " ");
}
// solve the problem
switch(storage_type){
case 1: // sparse co-ordinate storage
if (s_is == 1) {
nrek_solve_problem( &data, &status, n,
H_ne, H_val, c, power, weight, x,
S_ne, S_val );
}
else {
nrek_solve_problem( &data, &status, n,
H_ne, H_val, c, power, weight, x, 0, NULL );
}
break;
case 2: // sparse by rows
if (s_is == 1) {
nrek_solve_problem( &data, &status, n,
H_ne, H_val, c, power, weight, x,
S_ne, S_val );
}
else {
nrek_solve_problem( &data, &status, n,
H_ne, H_val, c, power, weight, x, 0, NULL );
}
break;
case 3: // dense
if (s_is == 1) {
nrek_solve_problem( &data, &status, n,
H_dense_ne, H_dense, c, power, weight, x,
S_dense_ne, S_dense );
}
else {
nrek_solve_problem( &data, &status, n,
H_dense_ne, H_dense, c, power, weight, x,
0, NULL );
}
break;
case 4: // diagonal
if (s_is == 1) {
nrek_solve_problem( &data, &status, n,
n, H_diag, c, power, weight, x, n, S_diag );
}
else {
nrek_solve_problem( &data, &status, n,
n, H_diag, c, power, weight, x, 0, NULL );
}
break;
}
nrek_information( &data, &inform, &status );
#ifdef REAL_128
printf("format %c%s: NREK_solve_problem exit status = %1" d_ipc_
", f = %.2f\n", st, sr, inform.status, (double)inform.obj );
#else
printf("format %c%s: NREK_solve_problem exit status = %1" d_ipc_
", f = %.2f\n", st, sr, inform.status, inform.obj );
#endif
//printf("x: ");
//for( ipc_ i = 0; i < n+m; i++) printf("%f ", x[i]);
}
// Delete internal workspace
nrek_terminate( &data, &control, &inform );
}
}
}
This is the same example, but now fortran-style indexing is used; the code is available in $GALAHAD/src/nrek/C/nrektf.c .
/* nrektf.c */
/* Full test for the NREK C interface using Fortran sparse matrix indexing */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "galahad_precision.h"
#include "galahad_cfunctions.h"
#include "galahad_nrek.h"
#ifdef REAL_128
#include <quadmath.h>
#endif
int main(void) {
// Derived types
void *data;
struct nrek_control_type control;
struct nrek_inform_type inform;
// Set problem data
ipc_ n = 3; // dimension of H
ipc_ H_ne = 4; // number of elements of H
ipc_ S_ne = 3; // number of elements of M
ipc_ H_dense_ne = 6; // number of elements of H
ipc_ S_dense_ne = 6; // number of elements of M
ipc_ H_row[] = {1, 2, 3, 3}; // row indices, NB lower triangle
ipc_ H_col[] = {1, 2, 3, 1};
ipc_ H_ptr[] = {1, 2, 3, 5};
ipc_ S_row[] = {1, 2, 3}; // row indices, NB lower triangle
ipc_ S_col[] = {1, 2, 3};
ipc_ S_ptr[] = {1, 2, 3, 4};
rpc_ H_val[] = {1.0, 2.0, 3.0, 4.0};
rpc_ S_val[] = {1.0, 2.0, 1.0};
rpc_ H_dense[] = {1.0, 0.0, 2.0, 4.0, 0.0, 3.0};
rpc_ S_dense[] = {1.0, 0.0, 2.0, 0.0, 0.0, 1.0};
rpc_ H_diag[] = {1.0, 0.0, 2.0};
rpc_ S_diag[] = {1.0, 2.0, 1.0};
rpc_ power = 3.0;
rpc_ weight = 1.0;
rpc_ c[] = {0.0, 2.0, 0.0};
char st = ' ';
ipc_ status;
rpc_ x[n];
char sr[3];
printf(" Fortran sparse matrix indexing\n\n");
printf(" basic tests of storage formats\n\n");
for( ipc_ s_is=0; s_is <= 1; s_is++){ // include a scaling matrix?
for( ipc_ storage_type=1; storage_type <= 4; storage_type++){
// Initialize NREK
nrek_initialize( &data, &control, &status );
// Set user-defined control options
control.f_indexing = true; // Fortran sparse matrix indexing
switch(storage_type){
case 1: // sparse co-ordinate storage
st = 'C';
// import the control parameters and structural data
nrek_import( &control, &data, &status, n,
"coordinate", H_ne, H_row, H_col, NULL );
if (s_is == 1) {
nrek_s_import( &data, &status, n,
"coordinate", S_ne, S_row, S_col, NULL );
}
break;
case 2: // sparse by rows
st = 'R';
// import the control parameters and structural data
nrek_import( &control, &data, &status, n,
"sparse_by_rows", H_ne, NULL, H_col, H_ptr );
if (s_is == 1) {
nrek_s_import( &data, &status, n,
"sparse_by_rows", S_ne, NULL, S_col, S_ptr );
}
break;
case 3: // dense
st = 'D';
// import the control parameters and structural data
nrek_import( &control, &data, &status, n,
"dense", H_ne, NULL, NULL, NULL );
if (s_is == 1) {
nrek_s_import( &data, &status, n,
"dense", S_ne, NULL, NULL, NULL );
}
break;
case 4: // diagonal
st = 'G';
// import the control parameters and structural data
nrek_import( &control, &data, &status, n,
"diagonal", H_ne, NULL, NULL, NULL );
if (s_is == 1) {
nrek_s_import( &data, &status, n,
"diagonal", S_ne, NULL, NULL, NULL );
}
break;
}
for( ipc_ w_is=1; w_is <= 2; w_is++){ // original or larger weight
if (w_is == 1) { // use the original weight
weight = 1.0;
}
else { // use the larger weight
power, weight = inform.next_power, weight;
control.new_power, weight = true;
nrek_reset_control( &control, &data, &status );
}
if (w_is == 2 && s_is == 1 ) {
strcpy(sr, "S+");
}
else if (w_is == 2) {
strcpy(sr, "+ ");
}
else if (s_is == 1) {
strcpy(sr, "S ");
}
else {
strcpy(sr, " ");
}
// solve the problem
switch(storage_type){
case 1: // sparse co-ordinate storage
if (s_is == 1) {
nrek_solve_problem( &data, &status, n,
H_ne, H_val, c, power, weight, x,
S_ne, S_val );
}
else {
nrek_solve_problem( &data, &status, n,
H_ne, H_val, c, power, weight, x, 0, NULL );
}
break;
case 2: // sparse by rows
if (s_is == 1) {
nrek_solve_problem( &data, &status, n,
H_ne, H_val, c, power, weight, x,
S_ne, S_val );
}
else {
nrek_solve_problem( &data, &status, n,
H_ne, H_val, c, power, weight, x, 0, NULL );
}
break;
case 3: // dense
if (s_is == 1) {
nrek_solve_problem( &data, &status, n,
H_dense_ne, H_dense, c, power, weight, x,
S_dense_ne, S_dense );
}
else {
nrek_solve_problem( &data, &status, n,
H_dense_ne, H_dense, c, power, weight, x,
0, NULL );
}
break;
case 4: // diagonal
if (s_is == 1) {
nrek_solve_problem( &data, &status, n,
n, H_diag, c, power, weight, x, n, S_diag );
}
else {
nrek_solve_problem( &data, &status, n,
n, H_diag, c, power, weight, x, 0, NULL );
}
break;
}
nrek_information( &data, &inform, &status );
#ifdef REAL_128
printf("format %c%s: NREK_solve_problem exit status = %1" d_ipc_
", f = %.2f\n", st, sr, inform.status, (double)inform.obj );
#else
printf("format %c%s: NREK_solve_problem exit status = %1" d_ipc_
", f = %.2f\n", st, sr, inform.status, inform.obj );
#endif
//printf("x: ");
//for( ipc_ i = 0; i < n+m; i++) printf("%f ", x[i]);
}
// Delete internal workspace
nrek_terminate( &data, &control, &inform );
}
}
}