5#ifndef LINE_UTIL_LAPACK_H
6#define LINE_UTIL_LAPACK_H
47#ifdef LINE_MP_HAVE_LAPACK
49#ifdef LINE_MP_LAPACK_DLOPEN
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);
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);
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*,
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*,
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*,
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*);
91#ifdef LINE_MP_LAPACK_DLOPEN
96 void* handle =
nullptr;
100inline const Library& library() {
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,
108 "/System/Library/Frameworks/Accelerate.framework/Accelerate",
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) {
125 if (h !=
nullptr) dlclose(h);
126 if (!lib.tried.empty()) lib.tried +=
", ";
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");
143 void* p = dlsym(lib.handle, symbol);
145 throw ExternalError(ErrorKind{
"LAPACK"},
146 std::string(
"the loaded LAPACK has no ") + symbol +
147 "; set LINE_LAPACK_LIBRARY to a complete LAPACK");
149 return reinterpret_cast<Fn
>(p);
154#define LINE_LAPACK_FN(name) \
155 static const name##_t fn = detail::resolve<name##_t>(#name "_"); \
158#define LINE_LAPACK_FN(name) return &::name##_
166inline bool available() {
167#ifdef LINE_MP_LAPACK_DLOPEN
168 return detail::library().handle !=
nullptr;
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); }
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);
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);
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);
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);
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);
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.