Generates wrappers for Math Kernel Library routines
Installation
The package relies on modernc.org/cc/v4 as AST parser, which is a pure go implementation. The binary can be installed with go install
:
go install github.com/fardream/gen-mkl-wrapper@latest
Wrappers
Math Kernel Library by Intel is widely used library of common mathematical routines, which provides support for various BLAS and LAPACK routines and many many more.
Due to its root in Fortran and C, many routines' names contain the type its operates on, for example, the Cholesky Decomposition is
lapack_int LAPACKE_spotrf (int matrix_layout , char uplo , lapack_int n , float * a , lapack_int lda );
lapack_int LAPACKE_dpotrf (int matrix_layout , char uplo , lapack_int n , double * a , lapack_int lda );
For C++ or rust, it may be desired to dispatch the method based on the type. This becomes extremely handy when implementing something based on MKL for both 32-bit and 64-bit float point number.
In C++, below is valid
lapack_int LAPACKE_potrf (int matrix_layout , char uplo , lapack_int n , float * a , lapack_int lda );
lapack_int LAPACKE_potrf (int matrix_layout , char uplo , lapack_int n , double * a , lapack_int lda );
Similarly in rust, the below is valid
pub trait MKLRoutines {
fn LAPACKE_potrf(matrix_layout: i32, uplo: i8, n: i32, a: *mut Self, lda: i32) -> i32;
}
impl MKLRoutines for f64 {
fn LAPACKE_potrf(matrix_layout: i32, uplo: i8, n: i32, a: *mut Self, lda: i32) -> i32 {
unsafe {
LAPACKE_dpotrf(matrix_layout, uplo, n, a, lda)
}
}
}
impl MKLRoutines for f32 {
fn LAPACKE_potrf(matrix_layout: i32, uplo: i8, n: i32, a: *mut Self, lda: i32) -> i32 {
unsafe {
LAPACKE_spotrf(matrix_layout, uplo, n, a, lda)
}
}
}
This simply binary does just the above - provided with a list of routines names, it will generate the rust trait or C++ polymorphic functions to those routines.
Those generated functions can be called from template/generic functions
template <typename T>
void SomeFunctionForFloatPoint(T* x) {
LAPACKE_potrf(LAPACK_ROW_MAJOR, 'U', n, x, lda);
}
or
pub fn some_function_for_float<T: MKLRoutines>(x:&mut [T]) {
unsafe { T::LAPACKE_potrf(LAPACK_ROW_MAJOR as i32, 'U' as i8, n x.as_mut_ptr(), lda) };
}