GLPK/Using the GLPK callable library

This page describes issues related to the use of GLPK as a callable library. As such this page should be primarily of interest to developers writing application programs in C or C++ and making use of the GLPK application programming interface (API).

Official documentation
The official GLPK documentation file doc/glpk.pdf contains a full reference for the GLPK API. That material is therefore not repeated here. See obtaining GLPK.

Short example
The following API example implements the linear constrained optimization model:

Production code would, of course, check the return value from function glp_simplex and react accordingly.

Linux
These instructions relate to Linux (the details for other operating systems should be similar).

Build short using your system compiler, in this case GNU GCC:

Run the resulting binary:

$ ./short OPTIMAL SOLUTION FOUND z = 0.46; x1 = 0.6; x2 = 0.2
 * 0: obj =  0.000000000e+00  infeas =  0.000e+00 (0)
 * 2: obj =  4.600000000e-01  infeas =  0.000e+00 (0)

You should obtain $$x_1 = 0.6$$ and $$x_2 = 0.2$$ as part of the terminal output from your program.

The GLPK runtime reporting format (the lines starting *) are covered in the callable library part of the official GLPK documentation and also here.

The same problem is coded in MathProg.

Submitting patches
GLPK patches are best submitted by posting them to the most appropriate of the two GLPK mailing lists. Note that the GLPK project does not maintain a web-based source code repository.

If creating source file patches, please refrain from using tabs and restrict line lengths to 72 chars.

Thread safety
GLPK is not inherently thread safe. Please consult the GLPK newsgroup archives for more information about the use of GLPK APIs under concurrent execution. In particular, see this discussion and, more latterly, this discussion.

If your problem involves multiple LPs (or can be structured thus), then consider running each LP as a separate processes. This makes sense only for multi-core hardware.

Reenterability
Andrew said that reenterability is a property of the code that allows running several instances of the same program in parallel using the only copy of that program in the memory. This property requires the program not to alter its own code and statically allocated data. See also Wikipedia definition of reentrancy.

The glpk code is reenterable except two internal routines tls_set_ptr and tls_get_ptr (see src/env/tls.c). In a reenterable version of glpk these two routines should be replaced with platform-specific ones.

The current version of tls.c is not reentrant:

Andrew suggested reentrant version:

Latest GCC compilers have storage class keyword __thread:

Visual Studio 2005 compiler has similar storage-class modifier __declspec( thread ):

This commit makes the library re-rentrant on Windows and Linux.

Deprecated GLPK APIs
A number of GLPK APIs have been recently deprecated and replaced. Unless otherwise stated, the lpx_ prefix has been replaced with a glp_ prefix. In all cases, developers should double check with the official documentation.

As of version 4.49, developers must migrate to these new APIs. The old API routines, whose names begin with lpx_, have now been removed from the GLPK API and are no longer available.

Recently added GLPK APIs
New APIs get added to GLPK from time to time. These are listed below. If appropriate, a new API may also be exposed by an associated GLPSOL command-line option in the same or a subsequent release.

Note: this information only dates back as far as release 4.31 in February 2007.