Skip to content

Commit 58cbd1d

Browse files
authored
Merge pull request #26 from rust-math/intel-mkl-sys
intel-mkl-sys crate
2 parents d39afdd + 1759f8a commit 58cbd1d

File tree

7 files changed

+13847
-0
lines changed

7 files changed

+13847
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
33
"intel-mkl-src",
4+
"intel-mkl-sys",
45
"intel-mkl-tool",
56
]

intel-mkl-sys/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "intel-mkl-sys"
3+
version = "2019.5.0"
4+
authors = ["Toshiki Teramura <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies.intel-mkl-src]
8+
path = "../intel-mkl-src"
9+
10+
[dev-dependencies]
11+
criterion = "0.3"
12+
rand = "0.7"
13+
approx = "0.3"
14+
15+
[[bench]]
16+
name = "cos"
17+
harness = false

intel-mkl-sys/benches/cos.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#[macro_use]
2+
extern crate criterion;
3+
4+
use criterion::Criterion;
5+
use rand::distributions::{Distribution, Uniform};
6+
7+
fn criterion_benchmark(c: &mut Criterion) {
8+
// f32
9+
for &n in &[100, 1000, 10000] {
10+
let in_ = {
11+
let mut rng = rand::thread_rng();
12+
let between = Uniform::from(0.0..2.0 * std::f32::consts::PI);
13+
let mut buf = vec![0.0; n];
14+
for val in buf.iter_mut() {
15+
*val = between.sample(&mut rng);
16+
}
17+
buf
18+
};
19+
20+
let mut out = vec![0.0_f32; n];
21+
c.bench_function(&format!("cos32_n{}", n), |b| {
22+
b.iter(|| {
23+
for i in 0..n {
24+
out[i] = in_[i].cos();
25+
}
26+
})
27+
});
28+
c.bench_function(&format!("vcos32_n{}", n), |b| {
29+
b.iter(|| unsafe {
30+
intel_mkl_sys::vsCos(n as i32, in_.as_ptr(), out.as_mut_ptr());
31+
})
32+
});
33+
}
34+
35+
// f64
36+
for &n in &[100, 1000, 10000] {
37+
let in_ = {
38+
let mut rng = rand::thread_rng();
39+
let between = Uniform::from(0.0..2.0 * std::f64::consts::PI);
40+
let mut buf = vec![0.0; n];
41+
for val in buf.iter_mut() {
42+
*val = between.sample(&mut rng);
43+
}
44+
buf
45+
};
46+
47+
let mut out = vec![0.0_f64; n];
48+
c.bench_function(&format!("cos64_n{}", n), |b| {
49+
b.iter(|| {
50+
for i in 0..n {
51+
out[i] = in_[i].cos();
52+
}
53+
})
54+
});
55+
56+
c.bench_function(&format!("vcos64_n{}", n), |b| {
57+
b.iter(|| unsafe {
58+
intel_mkl_sys::vdCos(n as i32, in_.as_ptr(), out.as_mut_ptr());
59+
})
60+
});
61+
}
62+
}
63+
64+
criterion_group!(benches, criterion_benchmark);
65+
criterion_main!(benches);

intel-mkl-sys/bindgen.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -uex
3+
4+
bindgen \
5+
--use-core \
6+
--with-derive-{default,eq,hash,ord} \
7+
wrapper.h \
8+
> src/mkl.rs

intel-mkl-sys/src/lib.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Rust binding to Intel-MKL including
2+
//!
3+
//! - [Vector Mathematical Functions (mkl_vml.h)](https://software.intel.com/en-us/onemkl-developer-reference-c-vector-mathematical-functions)
4+
//! - [Statistical Functions (mkl_vsl.h)](https://software.intel.com/en-us/onemkl-developer-reference-c-statistical-functions)
5+
//!
6+
//! Other parts of Intel-MKL is served via
7+
//!
8+
//! - [blas-sys](https://crates.io/crates/blas-sys)
9+
//! - [lapack-sys](https://crates.io/crates/lapack-sys)
10+
//! - [lapacke-sys](https://crates.io/crates/lapacke-sys)
11+
//! - [fftw-sys](https://crates.io/crates/fftw-sys)
12+
//!
13+
#![allow(
14+
improper_ctypes,
15+
non_upper_case_globals,
16+
non_camel_case_types,
17+
non_snake_case
18+
)]
19+
20+
extern crate intel_mkl_src;
21+
22+
include!("mkl.rs");
23+
24+
// Test linking
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
use approx::ulps_eq;
29+
use rand::distributions::{Distribution, Uniform};
30+
use std::ffi::c_void;
31+
32+
fn gen_rand_array(n: usize) -> Vec<f64> {
33+
let mut rng = rand::thread_rng();
34+
let between = Uniform::from(0.0..2.0 * std::f64::consts::PI);
35+
let mut buf = vec![0.0; n];
36+
for val in buf.iter_mut() {
37+
*val = between.sample(&mut rng);
38+
}
39+
buf
40+
}
41+
42+
#[test]
43+
fn cos() {
44+
let n = 1024;
45+
let a = gen_rand_array(n);
46+
let mut b = vec![0.0_f64; n];
47+
unsafe {
48+
vdCos(n as i32, a.as_ptr(), b.as_mut_ptr());
49+
}
50+
for i in 0..n {
51+
ulps_eq!(b[i], a[i].cos(), max_ulps = 4, epsilon = std::f64::EPSILON);
52+
}
53+
}
54+
55+
#[test]
56+
fn new_stream() {
57+
let mut stream: *mut c_void = std::ptr::null_mut();
58+
unsafe {
59+
vslNewStream(
60+
&mut stream as *mut *mut c_void,
61+
VSL_BRNG_MT19937 as i32,
62+
777,
63+
);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)