Skip to main content

onemkl_sys/
lib.rs

1//! Raw FFI bindings to the C interface of Intel oneAPI Math Kernel Library
2//! (oneMKL).
3//!
4//! These bindings are auto-generated by `bindgen` from the official `mkl.h`
5//! umbrella header, plus any opt-in MPI-dependent headers (`mkl_blacs.h`,
6//! `mkl_pblas.h`, `mkl_scalapack.h`, `mkl_cluster_sparse_solver.h`,
7//! `mkl_cdft.h`) selected via Cargo features.
8//!
9//! # Safety
10//!
11//! Every item exported from this crate is a raw C function or type. Callers
12//! must uphold the invariants documented by Intel's MKL reference. For a safe,
13//! idiomatic Rust API, use the `onemkl` crate instead.
14//!
15//! # Feature flags
16//!
17//! Exactly one feature must be selected from each of the following groups:
18//!
19//! - **Interface layer:** `lp64` (default) or `ilp64`
20//! - **Threading layer:** `threading-sequential` (default), `threading-intel-openmp`, or `threading-tbb`
21//! - **Linkage:** `link-dynamic` (default) or `link-static`
22//!
23//! Opt-in MPI-dependent domains: `cluster-sparse-solver`, `cdft`, `scalapack`,
24//! `blacs`, `pblas` (which implies `blacs`).
25//!
26//! # Locating the MKL install
27//!
28//! The build script searches in this order:
29//!
30//! 1. `MKLROOT` environment variable
31//! 2. `ONEMKL_SYS_INCLUDE_DIR` / `ONEMKL_SYS_LIB_DIR` (explicit override)
32//! 3. `ONEAPI_ROOT/mkl/latest`
33//! 4. Platform-standard install paths (`C:\Program Files (x86)\Intel\oneAPI`
34//!    on Windows, `/opt/intel/oneapi` elsewhere)
35
36// Standard bindgen-output allows: the bindings are generated C names.
37#![allow(non_upper_case_globals)]
38#![allow(non_camel_case_types)]
39#![allow(non_snake_case)]
40
41include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
42
43// Linux dynamic builds: the layered MKL shared libraries do not record
44// each other in DT_NEEDED, and rustc links with `--as-needed`, so a
45// final artifact that references only `mkl_intel_*` symbols loses
46// `mkl_core` and the threading layer, then dies at load with
47// `undefined symbol: mkl_blas_dgemm` (issue #3). `cargo:rustc-link-arg`
48// cannot fix this for downstream crates (it only reaches the emitting
49// crate's own artifacts) and the `as-needed` link modifier is
50// nightly-only. So reference one symbol from each library here: a
51// real symbol reference survives `--as-needed` and travels with the
52// rlib to every dependent.
53#[cfg(all(target_os = "linux", feature = "link-dynamic"))]
54mod keep_layers_needed {
55    unsafe extern "C" {
56        // Exported only by `libmkl_core` (backs the public `MKL_CBWR_Get`).
57        fn mkl_serv_cbwr_get();
58        // Exported by whichever threading layer is linked
59        // (`mkl_sequential`, `mkl_intel_thread` or `mkl_tbb_thread`).
60        fn mkl_serv_get_max_threads();
61    }
62
63    #[used]
64    static KEEP: [unsafe extern "C" fn(); 2] = [mkl_serv_cbwr_get, mkl_serv_get_max_threads];
65}
66
67// `MKL_INT`, `MKL_UINT`, and `MKL_LONG` are preprocessor `#define`s in
68// `mkl_types.h`, so bindgen inlines their expansions rather than emitting
69// Rust type aliases. We expose them here so downstream crates can refer to
70// the chosen interface width by name.
71
72/// Signed integer type used for sizes and increments throughout the MKL C
73/// interface. 32-bit under `lp64` (default), 64-bit under `ilp64`.
74#[cfg(feature = "ilp64")]
75pub type MKL_INT = ::core::ffi::c_longlong;
76/// Signed integer type used for sizes and increments throughout the MKL C
77/// interface. 32-bit under `lp64` (default), 64-bit under `ilp64`.
78#[cfg(not(feature = "ilp64"))]
79pub type MKL_INT = ::core::ffi::c_int;
80
81/// Unsigned counterpart of [`MKL_INT`].
82#[cfg(feature = "ilp64")]
83pub type MKL_UINT = ::core::ffi::c_ulonglong;
84/// Unsigned counterpart of [`MKL_INT`].
85#[cfg(not(feature = "ilp64"))]
86pub type MKL_UINT = ::core::ffi::c_uint;
87
88/// 64-bit signed integer type used in select MKL routines (CDFT, sparse
89/// solver `_64` variants, etc.). Always 64 bits regardless of interface
90/// layer.
91pub type MKL_INT64 = ::core::ffi::c_longlong;
92
93/// 64-bit unsigned integer type used in select MKL routines.
94pub type MKL_UINT64 = ::core::ffi::c_ulonglong;
95
96/// Long-width integer type. Differs by platform: `c_long` on Linux/macOS
97/// (8 bytes), `c_longlong` on Windows under ILP64, `c_long` (4 bytes) on
98/// Windows under LP64.
99#[cfg(all(windows, not(feature = "ilp64")))]
100pub type MKL_LONG = ::core::ffi::c_long;
101#[cfg(all(windows, feature = "ilp64"))]
102pub type MKL_LONG = ::core::ffi::c_longlong;
103#[cfg(not(windows))]
104pub type MKL_LONG = ::core::ffi::c_long;
105
106/// Width of [`MKL_INT`] in bytes for the selected interface layer.
107///
108/// 4 with the default `lp64` feature, 8 with `ilp64`.
109pub const MKL_INT_SIZE: usize = core::mem::size_of::<MKL_INT>();