rstsr_lapack_ffi/lapack/
mod.rs1#![allow(non_snake_case)]
2#![allow(non_camel_case_types)]
3#![allow(clippy::missing_safety_doc)]
4#![allow(clippy::type_complexity)]
5#![allow(clippy::too_many_arguments)]
6
7pub const MOD_NAME: &str = module_path!();
8pub const LIB_NAME: &str = "LAPACK"; pub const LIB_NAME_SHOW: &str = "LAPACK"; pub const LIB_NAME_LINK: &str = "lapack"; #[cfg(feature = "dynamic_loading")]
13mod dynamic_loading_specific {
14 use super::*;
15 use libloading::Library;
16 use std::fmt::Debug;
17 use std::sync::OnceLock;
18
19 fn get_lib_candidates() -> Vec<String> {
20 use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
21 let mut candidates = vec![];
22
23 for paths in [format!("RSTSR_DYLOAD_{LIB_NAME}").as_str(), "RSTSR_DYLOAD"] {
25 if let Ok(path) = std::env::var(paths) {
26 candidates.extend(path.split(":").map(|s| s.to_string()).collect::<Vec<_>>());
27 }
28 }
29
30 let int_type = if cfg!(feature = "ilp64") { "64" } else { "32" };
32 let int_name = if cfg!(feature = "ilp64") { "ilp64" } else { "lp64" };
33
34 candidates.extend(vec![
35 format!("{DLL_PREFIX}lapack{int_type}{DLL_SUFFIX}"),
36 format!("{DLL_PREFIX}lapack_{int_type}{DLL_SUFFIX}"),
37 format!("{DLL_PREFIX}lapack-{int_type}{DLL_SUFFIX}"),
38 format!("{DLL_PREFIX}lapack_{int_name}{DLL_SUFFIX}"),
39 format!("{DLL_PREFIX}lapack-{int_name}{DLL_SUFFIX}"),
40 format!("{DLL_PREFIX}lapack{DLL_SUFFIX}"),
41 format!("{DLL_PREFIX}lapacke{int_type}{DLL_SUFFIX}"),
42 format!("{DLL_PREFIX}lapacke_{int_type}{DLL_SUFFIX}"),
43 format!("{DLL_PREFIX}lapacke-{int_type}{DLL_SUFFIX}"),
44 format!("{DLL_PREFIX}lapacke_{int_name}{DLL_SUFFIX}"),
45 format!("{DLL_PREFIX}lapacke-{int_name}{DLL_SUFFIX}"),
46 format!("{DLL_PREFIX}lapacke{DLL_SUFFIX}"),
47 ]);
48 candidates
49 }
50
51 fn check_lib_loaded(lib: &DyLoadLib) -> bool {
52 lib.dsyevd_.is_some()
53 }
54
55 fn panic_no_lib_found<S: Debug>(candidates: &[S]) -> ! {
56 panic!(
57 r#"
58This happens in module `{MOD_NAME}`.
59Unable to dynamically load the {LIB_NAME_SHOW} (`{LIB_NAME_LINK}`) shared library.
60Candidates: {candidates:#?}
61
62Please check
63- if dynamic-loading is not desired, please disable the `dynamic_loading` feature in your `Cargo.toml` (by something like --no-default-features).
64- if you want to provide custom {LIB_NAME_SHOW} library, use environment variable `RSTSR_DYLOAD_{LIB_NAME}` or `RSTSR_DYLOAD` to specify the path to the library.
65- if `lib{LIB_NAME_LINK}.so` (linux) or `lib{LIB_NAME_LINK}.dylib` (macOS) or `lib{LIB_NAME_LINK}.dll` (Windows) is installed on your system.
66- if `LD_LIBRARY_PATH` (linux) or `DYLD_LIBRARY_PATH` (macOS) or `PATH` (Windows) environment variable is set correctly (any path that's visible to linker).
67- this crate does not use things like `LD_PRELOAD` or `DYLD_INSERT_LIBRARIES` to load the library.
68- this crate does not support static linking of libraries when dynamic-loading.
69"#
70 )
71 }
72
73 fn panic_condition_not_met<S: Debug>(candidates: &[S]) -> ! {
74 panic!(
75 r#"
76This happens in module `{MOD_NAME}`.
77Unable to dynamically load the {LIB_NAME_SHOW} (`{LIB_NAME_LINK}`) shared library, due to condition unfulfilled.
78Condition: `dsyevd_` not found.
79Found libraries: {candidates:#?}
80
81Please check
82- if dynamic-loading is not desired, please disable the `dynamic_loading` feature in your `Cargo.toml` (by something like --no-default-features).
83- if you want to provide custom {LIB_NAME_SHOW} library, use environment variable `RSTSR_DYLOAD_{LIB_NAME}` or `RSTSR_DYLOAD` to specify the path to the library.
84- sequence of libraries matters: `RSTSR_DYLOAD_{LIB_NAME}` will be tried first, then `RSTSR_DYLOAD`, then system dynamic library search paths.
85"#
86 )
87 }
88
89 pub unsafe fn dyload_lib() -> &'static DyLoadLib {
90 static LIB: OnceLock<DyLoadLib> = OnceLock::new();
91
92 LIB.get_or_init(|| {
93 let candidates = get_lib_candidates();
94 let (mut libraries, mut libraries_path) = (vec![], vec![]);
95 for candidate in &candidates {
96 if let Ok(l) = Library::new(candidate) {
97 libraries.push(l);
98 libraries_path.push(candidate.to_string());
99 }
100 }
101 let lib = DyLoadLib::new(libraries, libraries_path);
102 if lib.__libraries.is_empty() {
103 panic_no_lib_found(&candidates);
104 }
105 if !check_lib_loaded(&lib) {
106 panic_condition_not_met(&lib.__libraries_path);
107 }
108 lib
109 })
110 }
111}
112
113#[cfg(feature = "dynamic_loading")]
114pub use dynamic_loading_specific::*;
115
116pub(crate) mod ffi_base;
119pub use ffi_base::*;
120
121#[cfg(not(feature = "dynamic_loading"))]
122pub(crate) mod ffi_extern;
123#[cfg(not(feature = "dynamic_loading"))]
124pub use ffi_extern::*;
125
126#[cfg(feature = "dynamic_loading")]
127pub(crate) mod dyload_compatible;
128#[cfg(feature = "dynamic_loading")]
129pub(crate) mod dyload_initializer;
130#[cfg(feature = "dynamic_loading")]
131pub(crate) mod dyload_struct;
132
133#[cfg(feature = "dynamic_loading")]
134pub use dyload_compatible::*;
135#[cfg(feature = "dynamic_loading")]
136pub use dyload_struct::*;
137
138#[test]
141fn playground() {
142 let time = std::time::Instant::now();
144 let l = unsafe { dyload_lib() };
145 println!("Time taken to load LAPACK library: {:?}", time.elapsed());
146 println!("Loaded LAPACK library: {:?}", l.__libraries);
147 println!("Loaded LAPACK library: {:?}", l.dsyevd_);
148
149 let mut a: Vec<f64> = vec![2.0, 1.0, 3.0, 4.0];
151 let uplo = 'L';
152 let n = 2;
153 let lda = 2;
154 let mut info = 0;
155 unsafe {
156 dpotrf_(&uplo as *const _ as *const _, &n, a.as_mut_ptr(), &lda, &mut info);
157 }
158 println!("{a:?}");
159 assert_eq!(info, 0);
160}