Skip to content

intel-mkl-sys crate #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"intel-mkl-src",
"intel-mkl-sys",
"intel-mkl-tool",
]
17 changes: 17 additions & 0 deletions intel-mkl-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "intel-mkl-sys"
version = "2019.5.0"
authors = ["Toshiki Teramura <[email protected]>"]
edition = "2018"

[dependencies.intel-mkl-src]
path = "../intel-mkl-src"

[dev-dependencies]
criterion = "0.3"
rand = "0.7"
approx = "0.3"

[[bench]]
name = "cos"
harness = false
65 changes: 65 additions & 0 deletions intel-mkl-sys/benches/cos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#[macro_use]
extern crate criterion;

use criterion::Criterion;
use rand::distributions::{Distribution, Uniform};

fn criterion_benchmark(c: &mut Criterion) {
// f32
for &n in &[100, 1000, 10000] {
let in_ = {
let mut rng = rand::thread_rng();
let between = Uniform::from(0.0..2.0 * std::f32::consts::PI);
let mut buf = vec![0.0; n];
for val in buf.iter_mut() {
*val = between.sample(&mut rng);
}
buf
};

let mut out = vec![0.0_f32; n];
c.bench_function(&format!("cos32_n{}", n), |b| {
b.iter(|| {
for i in 0..n {
out[i] = in_[i].cos();
}
})
});
c.bench_function(&format!("vcos32_n{}", n), |b| {
b.iter(|| unsafe {
intel_mkl_sys::vsCos(n as i32, in_.as_ptr(), out.as_mut_ptr());
})
});
}

// f64
for &n in &[100, 1000, 10000] {
let in_ = {
let mut rng = rand::thread_rng();
let between = Uniform::from(0.0..2.0 * std::f64::consts::PI);
let mut buf = vec![0.0; n];
for val in buf.iter_mut() {
*val = between.sample(&mut rng);
}
buf
};

let mut out = vec![0.0_f64; n];
c.bench_function(&format!("cos64_n{}", n), |b| {
b.iter(|| {
for i in 0..n {
out[i] = in_[i].cos();
}
})
});

c.bench_function(&format!("vcos64_n{}", n), |b| {
b.iter(|| unsafe {
intel_mkl_sys::vdCos(n as i32, in_.as_ptr(), out.as_mut_ptr());
})
});
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
8 changes: 8 additions & 0 deletions intel-mkl-sys/bindgen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -uex

bindgen \
--use-core \
--with-derive-{default,eq,hash,ord} \
wrapper.h \
> src/mkl.rs
66 changes: 66 additions & 0 deletions intel-mkl-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Rust binding to Intel-MKL including
//!
//! - [Vector Mathematical Functions (mkl_vml.h)](https://software.intel.com/en-us/onemkl-developer-reference-c-vector-mathematical-functions)
//! - [Statistical Functions (mkl_vsl.h)](https://software.intel.com/en-us/onemkl-developer-reference-c-statistical-functions)
//!
//! Other parts of Intel-MKL is served via
//!
//! - [blas-sys](https://crates.io/crates/blas-sys)
//! - [lapack-sys](https://crates.io/crates/lapack-sys)
//! - [lapacke-sys](https://crates.io/crates/lapacke-sys)
//! - [fftw-sys](https://crates.io/crates/fftw-sys)
//!
#![allow(
improper_ctypes,
non_upper_case_globals,
non_camel_case_types,
non_snake_case
)]

extern crate intel_mkl_src;

include!("mkl.rs");

// Test linking
#[cfg(test)]
mod tests {
use super::*;
use approx::ulps_eq;
use rand::distributions::{Distribution, Uniform};
use std::ffi::c_void;

fn gen_rand_array(n: usize) -> Vec<f64> {
let mut rng = rand::thread_rng();
let between = Uniform::from(0.0..2.0 * std::f64::consts::PI);
let mut buf = vec![0.0; n];
for val in buf.iter_mut() {
*val = between.sample(&mut rng);
}
buf
}

#[test]
fn cos() {
let n = 1024;
let a = gen_rand_array(n);
let mut b = vec![0.0_f64; n];
unsafe {
vdCos(n as i32, a.as_ptr(), b.as_mut_ptr());
}
for i in 0..n {
ulps_eq!(b[i], a[i].cos(), max_ulps = 4, epsilon = std::f64::EPSILON);
}
}

#[test]
fn new_stream() {
let mut stream: *mut c_void = std::ptr::null_mut();
unsafe {
vslNewStream(
&mut stream as *mut *mut c_void,
VSL_BRNG_MT19937 as i32,
777,
);
}
}
}
Loading