onemkl_sys/lib.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Raw FFI bindings to the C interface of Intel oneAPI Math Kernel Library
3//! (oneMKL).
4//!
5//! These bindings are auto-generated by `bindgen` from the official `mkl.h`
6//! umbrella header, plus any opt-in MPI-dependent headers (`mkl_blacs.h`,
7//! `mkl_pblas.h`, `mkl_scalapack.h`, `mkl_cluster_sparse_solver.h`,
8//! `mkl_cdft.h`) selected via Cargo features.
9//!
10//! # Safety
11//!
12//! Every item exported from this crate is a raw C function or type. Callers
13//! must uphold the invariants documented by Intel's MKL reference. For a safe,
14//! idiomatic Rust API, use the `onemkl` crate instead.
15//!
16//! # Feature flags
17//!
18//! Exactly one feature must be selected from each of the following groups:
19//!
20//! - **Interface layer:** `lp64` (default) or `ilp64`
21//! - **Threading layer:** `threading-sequential` (default), `threading-intel-openmp`, or `threading-tbb`
22//! - **Linkage:** `link-dynamic` (default) or `link-static`
23//!
24//! Opt-in MPI-dependent domains: `cluster-sparse-solver`, `cdft`, `scalapack`,
25//! `blacs`, `pblas` (which implies `blacs`).
26//!
27//! # Locating the MKL install
28//!
29//! The build script searches in this order:
30//!
31//! 1. `MKLROOT` environment variable
32//! 2. `ONEMKL_SYS_INCLUDE_DIR` / `ONEMKL_SYS_LIB_DIR` (explicit override)
33//! 3. `ONEAPI_ROOT/mkl/latest`
34//! 4. Platform-standard install paths (`C:\Program Files (x86)\Intel\oneAPI`
35//! on Windows, `/opt/intel/oneapi` elsewhere)
36
37// Standard bindgen-output allows: the bindings are generated C names.
38#![allow(non_upper_case_globals)]
39#![allow(non_camel_case_types)]
40#![allow(non_snake_case)]
41
42include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
43
44// Linux dynamic builds: the layered MKL shared libraries do not record
45// each other in DT_NEEDED, and rustc links with `--as-needed`, so a
46// final artifact that references only `mkl_intel_*` symbols loses
47// `mkl_core` and the threading layer, then dies at load with
48// `undefined symbol: mkl_blas_dgemm` (issue #3). `cargo:rustc-link-arg`
49// cannot fix this for downstream crates (it only reaches the emitting
50// crate's own artifacts) and the `as-needed` link modifier is
51// nightly-only. So reference one symbol from each library here: a
52// real symbol reference survives `--as-needed` and travels with the
53// rlib to every dependent.
54#[cfg(all(target_os = "linux", feature = "link-dynamic"))]
55mod keep_layers_needed {
56 unsafe extern "C" {
57 // Exported only by `libmkl_core` (backs the public `MKL_CBWR_Get`).
58 fn mkl_serv_cbwr_get();
59 // Exported by whichever threading layer is linked
60 // (`mkl_sequential`, `mkl_intel_thread` or `mkl_tbb_thread`).
61 fn mkl_serv_get_max_threads();
62 }
63
64 #[used]
65 static KEEP: [unsafe extern "C" fn(); 2] = [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>();