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#![allow(non_upper_case_globals)]
37#![allow(non_camel_case_types)]
38#![allow(non_snake_case)]
39#![allow(dead_code)]
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] =
65        [mkl_serv_cbwr_get, mkl_serv_get_max_threads];
66}
67
68// `MKL_INT`, `MKL_UINT`, and `MKL_LONG` are preprocessor `#define`s in
69// `mkl_types.h`, so bindgen inlines their expansions rather than emitting
70// Rust type aliases. We expose them here so downstream crates can refer to
71// the chosen interface width by name.
72
73/// Signed integer type used for sizes and increments throughout the MKL C
74/// interface. 32-bit under `lp64` (default), 64-bit under `ilp64`.
75#[cfg(feature = "ilp64")]
76pub type MKL_INT = ::core::ffi::c_longlong;
77/// Signed integer type used for sizes and increments throughout the MKL C
78/// interface. 32-bit under `lp64` (default), 64-bit under `ilp64`.
79#[cfg(not(feature = "ilp64"))]
80pub type MKL_INT = ::core::ffi::c_int;
81
82/// Unsigned counterpart of [`MKL_INT`].
83#[cfg(feature = "ilp64")]
84pub type MKL_UINT = ::core::ffi::c_ulonglong;
85/// Unsigned counterpart of [`MKL_INT`].
86#[cfg(not(feature = "ilp64"))]
87pub type MKL_UINT = ::core::ffi::c_uint;
88
89/// 64-bit signed integer type used in select MKL routines (CDFT, sparse
90/// solver `_64` variants, etc.). Always 64 bits regardless of interface
91/// layer.
92pub type MKL_INT64 = ::core::ffi::c_longlong;
93
94/// 64-bit unsigned integer type used in select MKL routines.
95pub type MKL_UINT64 = ::core::ffi::c_ulonglong;
96
97/// Long-width integer type. Differs by platform: `c_long` on Linux/macOS
98/// (8 bytes), `c_longlong` on Windows under ILP64, `c_long` (4 bytes) on
99/// Windows under LP64.
100#[cfg(all(windows, not(feature = "ilp64")))]
101pub type MKL_LONG = ::core::ffi::c_long;
102#[cfg(all(windows, feature = "ilp64"))]
103pub type MKL_LONG = ::core::ffi::c_longlong;
104#[cfg(not(windows))]
105pub type MKL_LONG = ::core::ffi::c_long;
106
107/// Width of [`MKL_INT`] in bytes for the selected interface layer.
108///
109/// 4 with the default `lp64` feature, 8 with `ilp64`.
110pub const MKL_INT_SIZE: usize = core::mem::size_of::<MKL_INT>();