LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
lapack.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_UTIL_LAPACK_H
6#define LINE_UTIL_LAPACK_H
7
8/**
9 * @file
10 * @ingroup line_util
11 * The five LAPACK routines the port calls, behind one indirection.
12 *
13 * Every LAPACK call in the tree goes through `line::lapack::<routine>` rather
14 * than the Fortran symbol, so that HOW the library is reached is decided here
15 * and nowhere else. There are two ways, chosen at configure time:
16 *
17 * - LINE_MP_LAPACK_DLOPEN (the default): the library is NOT linked. The first
18 * call loads it with dlopen and resolves the routine with dlsym. A published
19 * binary (`common/ldes`, `common/line-cli`, the wheel's `bin/ldes`) then
20 * carries no DT_NEEDED entry for liblapack.so.3, so it STARTS on a machine
21 * that has no LAPACK at all, and only a computation that actually reaches one
22 * of these routines refuses, by name, with the install hint. Before
23 * 2026-11-02 the link-time dependency made such a machine fail at exec
24 * (`error while loading shared libraries: liblapack.so.3`, exit 127) for
25 * every model, including the ones that never touch LAPACK.
26 *
27 * This is also the licensing answer. Every distro LAPACK is gfortran-built,
28 * so it reaches libquadmath (LGPL-2.1) through libgfortran; linking it
29 * statically was removed on 2026-11-01 for exactly that reason. Loading the
30 * USER'S OWN copy at run time links nothing of it into the binary.
31 *
32 * - otherwise: the Fortran symbols are declared and linked directly, as before.
33 * r/capi uses this, because R supplies its own LAPACK at link time.
34 *
35 * Candidates, first hit wins: $LINE_LAPACK_LIBRARY (a path or soname), then
36 * liblapack.so.3 (whatever the distro's alternatives point at, which is what a
37 * linked build would have used), liblapack.so, libopenblas.so.0, libopenblas.so,
38 * libflexiblas.so.3; on macOS the Accelerate framework, then liblapack.dylib.
39 * The library is opened RTLD_LOCAL so its symbols cannot interpose on anything
40 * else in the process.
41 */
42
43#include <string>
44
45#include "line/util/error.h"
46
47#ifdef LINE_MP_HAVE_LAPACK
48
49#ifdef LINE_MP_LAPACK_DLOPEN
50#include <dlfcn.h>
51#include <cstdlib>
52#include <mutex>
53#else
54extern "C" {
55void dgeev_(const char* jobvl, const char* jobvr, const int* n, double* a, const int* lda,
56 double* wr, double* wi, double* vl, const int* ldvl, double* vr, const int* ldvr,
57 double* work, const int* lwork, int* info);
58void dgesvd_(const char* jobu, const char* jobvt, const int* m, const int* n, double* a,
59 const int* lda, double* s, double* u, const int* ldu, double* vt, const int* ldvt,
60 double* work, const int* lwork, int* info);
61void dgees_(const char* jobvs, const char* sort, int (*select)(const double*, const double*),
62 const int* n, double* a, const int* lda, int* sdim, double* wr, double* wi, double* vs,
63 const int* ldvs, double* work, const int* lwork, int* bwork, int* info);
64void dtrexc_(const char* compq, const int* n, double* t, const int* ldt, double* q, const int* ldq,
65 int* ifst, int* ilst, double* work, int* info);
66/** Quasi-triangular Sylvester solve, the core of Bartels-Stewart. */
67void dtrsyl_(const char* trana, const char* tranb, const int* isgn, const int* m, const int* n,
68 const double* a, const int* lda, const double* b, const int* ldb, double* c,
69 const int* ldc, double* scale, int* info);
70}
71#endif
72
73namespace line {
74namespace lapack {
75
76using dgeev_t = void (*)(const char*, const char*, const int*, double*, const int*, double*,
77 double*, double*, const int*, double*, const int*, double*, const int*,
78 int*);
79using dgesvd_t = void (*)(const char*, const char*, const int*, const int*, double*, const int*,
80 double*, double*, const int*, double*, const int*, double*, const int*,
81 int*);
82using dgees_t = void (*)(const char*, const char*, int (*)(const double*, const double*),
83 const int*, double*, const int*, int*, double*, double*, double*,
84 const int*, double*, const int*, int*, int*);
85using dtrexc_t = void (*)(const char*, const int*, double*, const int*, double*, const int*, int*,
86 int*, double*, int*);
87using dtrsyl_t = void (*)(const char*, const char*, const int*, const int*, const int*,
88 const double*, const int*, const double*, const int*, double*,
89 const int*, double*, int*);
90
91#ifdef LINE_MP_LAPACK_DLOPEN
92namespace detail {
93
94/** The opened library, or nullptr with `tried` naming every candidate that failed. */
95struct Library {
96 void* handle = nullptr;
97 std::string tried;
98};
99
100inline const Library& library() {
101 static Library lib;
102 static std::once_flag once;
103 std::call_once(once, [] {
104 const char* env = std::getenv("LINE_LAPACK_LIBRARY");
105 const char* const candidates[] = {
106 (env != nullptr && env[0] != '\0') ? env : nullptr,
107#ifdef __APPLE__
108 "/System/Library/Frameworks/Accelerate.framework/Accelerate",
109 "liblapack.dylib",
110#else
111 "liblapack.so.3",
112 "liblapack.so",
113 "libopenblas.so.0",
114 "libopenblas.so",
115 "libflexiblas.so.3",
116#endif
117 };
118 for (const char* name : candidates) {
119 if (name == nullptr) continue;
120 void* h = dlopen(name, RTLD_NOW | RTLD_LOCAL);
121 if (h != nullptr && dlsym(h, "dgeev_") != nullptr) {
122 lib.handle = h;
123 return;
124 }
125 if (h != nullptr) dlclose(h);
126 if (!lib.tried.empty()) lib.tried += ", ";
127 lib.tried += name;
128 }
129 });
130 return lib;
131}
132
133/** Resolve one routine, or refuse by name with what to install. */
134template <class Fn>
135inline Fn resolve(const char* symbol) {
136 const Library& lib = library();
137 if (lib.handle == nullptr) {
138 throw ExternalError(ErrorKind{"LAPACK"},
139 std::string(symbol) + " requires LAPACK at run time and none could be loaded (tried " +
140 lib.tried + "). Install liblapack3 or libopenblas0 (apt), lapack (dnf), or set "
141 "LINE_LAPACK_LIBRARY to the library's path");
142 }
143 void* p = dlsym(lib.handle, symbol);
144 if (p == nullptr) {
145 throw ExternalError(ErrorKind{"LAPACK"},
146 std::string("the loaded LAPACK has no ") + symbol +
147 "; set LINE_LAPACK_LIBRARY to a complete LAPACK");
148 }
149 return reinterpret_cast<Fn>(p);
150}
151
152} // namespace detail
153
154#define LINE_LAPACK_FN(name) \
155 static const name##_t fn = detail::resolve<name##_t>(#name "_"); \
156 return fn
157#else
158#define LINE_LAPACK_FN(name) return &::name##_
159#endif
160
161/**
162 * Whether a LAPACK can be reached at all, for the one caller that has a
163 * non-LAPACK fallback (cache_miss_rmf's null space) and must take it on a
164 * machine without the library, exactly as a build configured without LAPACK does.
165 */
166inline bool available() {
167#ifdef LINE_MP_LAPACK_DLOPEN
168 return detail::library().handle != nullptr;
169#else
170 return true;
171#endif
172}
173
174inline dgeev_t dgeev_fn() { LINE_LAPACK_FN(dgeev); }
175inline dgesvd_t dgesvd_fn() { LINE_LAPACK_FN(dgesvd); }
176inline dgees_t dgees_fn() { LINE_LAPACK_FN(dgees); }
177inline dtrexc_t dtrexc_fn() { LINE_LAPACK_FN(dtrexc); }
178inline dtrsyl_t dtrsyl_fn() { LINE_LAPACK_FN(dtrsyl); }
179
180#undef LINE_LAPACK_FN
181
182inline void dgeev(const char* jobvl, const char* jobvr, const int* n, double* a, const int* lda,
183 double* wr, double* wi, double* vl, const int* ldvl, double* vr, const int* ldvr,
184 double* work, const int* lwork, int* info) {
185 dgeev_fn()(jobvl, jobvr, n, a, lda, wr, wi, vl, ldvl, vr, ldvr, work, lwork, info);
186}
187inline void dgesvd(const char* jobu, const char* jobvt, const int* m, const int* n, double* a,
188 const int* lda, double* s, double* u, const int* ldu, double* vt,
189 const int* ldvt, double* work, const int* lwork, int* info) {
190 dgesvd_fn()(jobu, jobvt, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, info);
191}
192inline void dgees(const char* jobvs, const char* sort,
193 int (*select)(const double*, const double*), const int* n, double* a,
194 const int* lda, int* sdim, double* wr, double* wi, double* vs, const int* ldvs,
195 double* work, const int* lwork, int* bwork, int* info) {
196 dgees_fn()(jobvs, sort, select, n, a, lda, sdim, wr, wi, vs, ldvs, work, lwork, bwork, info);
197}
198inline void dtrexc(const char* compq, const int* n, double* t, const int* ldt, double* q,
199 const int* ldq, int* ifst, int* ilst, double* work, int* info) {
200 dtrexc_fn()(compq, n, t, ldt, q, ldq, ifst, ilst, work, info);
201}
202inline void dtrsyl(const char* trana, const char* tranb, const int* isgn, const int* m,
203 const int* n, const double* a, const int* lda, const double* b, const int* ldb,
204 double* c, const int* ldc, double* scale, int* info) {
205 dtrsyl_fn()(trana, tranb, isgn, m, n, a, lda, b, ldb, c, ldc, scale, info);
206}
207
208} // namespace lapack
209} // namespace line
210
211#endif // LINE_MP_HAVE_LAPACK
212
213#endif // LINE_UTIL_LAPACK_H
The exception types the port throws.
std::string name(const Element *el, const std::string &key="name")
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51