votca 2026-dev
Loading...
Searching...
No Matches
accelerate_lapack.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2026 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Deliberately no Eigen or VOTCA eigen.h here: those pull in lapacke.h,
21// whose LAPACK prototypes clash with Accelerate's.
23
25
26// The new LAPACK interface of Accelerate needs the macOS 13.3 SDK; which
27// macOS the program then runs on is checked at run time.
28#if defined(__APPLE__) && defined(__clang__) && defined(APPLE_ACCELERATE_FOUND)
29#include <Availability.h>
30#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && \
31 __MAC_OS_X_VERSION_MAX_ALLOWED >= 130300
32#define VOTCA_XTP_ACCELERATE_LAPACK
33#endif
34#endif
35
36#ifdef VOTCA_XTP_ACCELERATE_LAPACK
37#define ACCELERATE_NEW_LAPACK
38#include <Accelerate/Accelerate.h>
39#include <algorithm>
40#include <vector>
41#endif
42
43namespace votca {
44namespace xtp {
45namespace accelerate {
46
47#ifdef VOTCA_XTP_ACCELERATE_LAPACK
48
49bool LapackAvailable() {
50 if (__builtin_available(macOS 13.3, *)) {
51 return true;
52 }
53 return false;
54}
55
56int dsyevd(char jobz, char uplo, long n, double* a, long lda, double* w) {
57 if (__builtin_available(macOS 13.3, *)) {
58 const __LAPACK_int N = static_cast<__LAPACK_int>(n);
59 const __LAPACK_int LDA = static_cast<__LAPACK_int>(std::max(1L, lda));
60 __LAPACK_int info = 0;
61 // workspace query
62 double work_query = 0.0;
63 __LAPACK_int iwork_query = 0;
64 __LAPACK_int lwork = -1;
65 __LAPACK_int liwork = -1;
66 dsyevd_(&jobz, &uplo, &N, a, &LDA, w, &work_query, &lwork, &iwork_query,
67 &liwork, &info);
68 if (info != 0) {
69 return static_cast<int>(info);
70 }
71 lwork = static_cast<__LAPACK_int>(work_query);
72 liwork = iwork_query;
73 std::vector<double> work(
74 static_cast<std::size_t>(std::max<__LAPACK_int>(1, lwork)));
75 std::vector<__LAPACK_int> iwork(
76 static_cast<std::size_t>(std::max<__LAPACK_int>(1, liwork)));
77 dsyevd_(&jobz, &uplo, &N, a, &LDA, w, work.data(), &lwork, iwork.data(),
78 &liwork, &info);
79 return static_cast<int>(info);
80 }
82}
83
84int dpotrf(char uplo, long n, double* a, long lda) {
85 if (__builtin_available(macOS 13.3, *)) {
86 const __LAPACK_int N = static_cast<__LAPACK_int>(n);
87 const __LAPACK_int LDA = static_cast<__LAPACK_int>(std::max(1L, lda));
88 __LAPACK_int info = 0;
89 dpotrf_(&uplo, &N, a, &LDA, &info);
90 return static_cast<int>(info);
91 }
93}
94
95int dpotri(char uplo, long n, double* a, long lda) {
96 if (__builtin_available(macOS 13.3, *)) {
97 const __LAPACK_int N = static_cast<__LAPACK_int>(n);
98 const __LAPACK_int LDA = static_cast<__LAPACK_int>(std::max(1L, lda));
99 __LAPACK_int info = 0;
100 dpotri_(&uplo, &N, a, &LDA, &info);
101 return static_cast<int>(info);
102 }
104}
105
106#else
107
108bool LapackAvailable() { return false; }
109int dsyevd(char, char, long, double*, long, double*) {
111}
112int dpotrf(char, long, double*, long) { return kAccelerateUnavailable; }
113int dpotri(char, long, double*, long) { return kAccelerateUnavailable; }
114
115#endif
116
117} // namespace accelerate
118} // namespace xtp
119} // namespace votca
LAPACK routines of Apple Accelerate, called directly.
bool LapackAvailable()
Whether the routines below can run here (Accelerate build, macOS 13.3+).
int dpotrf(char uplo, long n, double *a, long lda)
Cholesky factorisation of a symmetric positive definite matrix.
int dpotri(char uplo, long n, double *a, long lda)
Inverse from the Cholesky factor of dpotrf (one triangle).
int dsyevd(char jobz, char uplo, long n, double *a, long lda, double *w)
constexpr int kAccelerateUnavailable
Charge transport classes.
Definition ERIs.h:28
Provides a means for comparing floating point numbers.
Definition basebead.h:33