Load a nonlinear program (NLP) exposed by a shared library through a plain
C interface and use it as an NLPModels.AbstractNLPModel — so any
NLPModels-compatible solver (Ipopt via NLPModelsIpopt, or any other) can
solve it in Julia.
Any library implementing the cnlp ABI works, in any source language:
test/fixtures/tinyqp.c in this repository is a complete reference
implementation in ~100 lines of plain C.
using CNLPModels, NLPModelsIpopt
CNLPModels.set_path!("/opt/models") # or the CNLPMODELS_PATH env variable
m = CNLPModel("@mymodel", 1000) # resolves libmymodel.so, instance of size 1000
# m = CNLPModel("/path/to/mymodel", 1000) # or a literal path: library file or bundle dir
res = ipopt(m)The cnlp"..." literal is the shortcut onto the path — it resolves the
name, checks that the library exists and loads, and hands back the cached
handle, directly usable as the library argument:
m = CNLPModel(cnlp"mymodel", 1000)(Or bypass the path entirely with CNLPModels.load("/path/libmymodel.so").)
Any number of model instances may coexist per library.
A library may carry more than one model, each under its own symbol prefix, each with its own schema. Name the one you want as a symbol:
m = CNLPModel("@grid", :acopf, bus, 100.0) # acopf_* inside libgrid.so
d = CNLPModel("@grid", :dcopf, bus) # dcopf_* in the same libraryThe symbol is the prefix, and it works with every library argument — an
"@name", a path, a cnlp"..." literal, or a CLib. Models in one library
share its address space and, for a library carrying a language runtime, its
single runtime copy (~80 MB, which is the reason to co-package a family of
models at all); they are otherwise independent, and instances of different
models coexist exactly as instances of one model do. Their schemas are
addressed the same way — schema_json(lib, :acopf).
Omitting the symbol is the single-model spelling, where the prefix falls back to the library name, so a one-model library needs nothing new.
Libraries built from structured data take their arguments positionally, one per schema field, in the order the library publishes them; a table is a vector of named tuples:
m = CNLPModel("@mymodel",
[(i = 1, pd = 0.4), (i = 2, pd = 0.3)], # a table
[0.9, 0.9], # an array
100.0, # a scalar
)A library that exports the cnlp device tier evaluates on the GPU, with CUDA arrays in and out, so a GPU solver runs without host round trips:
using CUDA, cuMadNLP
m = CNLPModel("@mymodel", 100_000; device = :cuda)
madnlp(m)write_solution(m, path, result) writes a solver result as a
cnlp-solution
HDF5 file (load HDF5.jl), or with the model's default writer if it names one. Multipliers
are converted to the cnlp sign convention for maximizing models. A library's own formats
are reachable by name: writers(m), write_solution(m, :matpower, path, result),
readers(lib, :acopf), CNLPModel(lib, :acopf; input = :matpower => "case.m"),
read_start(m, :matpower, path), read_parameters!(m, :name, path).
This package implements cnlp ABI 0.2:
cnlp.h is the
specification. A copy is vendored at test/fixtures/cnlp.h, where the fixture expands its
declarations so signature drift fails compilation.
Follow cnlp.h, and check yourself against test/fixtures/tinyqp.c and this package's
test suite, which compiles that file and exercises every function against closed-form
solutions. The file carries four models: tq_ (instantiated from one integer), sq_
(built from a three-field schema through the builder), fx_ (no instantiation data) and
tb_ (a table field).
If the shared library embeds a language runtime that initializes lazily on
first call and re-forwards the process-global BLAS trampoline (symptom:
Error: no BLAS/LAPACK library loaded for dgemm_() in the host afterwards),
this package repairs the host's BLAS forwarding automatically after the
library's first call; restore_blas!(lib) re-applies it at any time.
An unbundled juliac library — one linked against the standard libjulia
instead of shipping a privatized runtime copy — cannot share the host's
runtime: loaded as-is into a Julia process, its first call aborts. On Linux,
load detects this and provisions the library with a private, salted copy of
the installed Julia runtime (matching version required), patched in scratch
at load time — so a single ~2 MB library file works in-process with nothing
bundled. On other platforms the unbundled form is refused with an
explanation; compile with bundle = true there.
Sibling package: cnlpmodels —
the same consumer for Python (ctypes + numpy).