Skip to content

Commit

Permalink
add no_std support
Browse files Browse the repository at this point in the history
Co-authored-by: Geordon Worley <vadixidav@gmail.com>
  • Loading branch information
jturner314 and vadixidav committed Dec 6, 2020
1 parent 4ac62c2 commit 5d7ae23
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 68 deletions.
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ matrix:
env:
TARGET=aarch64-unknown-linux-gnu
BUILD_ONLY=1
- rust: 1.36.0
env: TARGET=thumbv6m-none-eabi
before_script:
- rustup target add $TARGET
- set -ex
script:
- cargo rustc --target=$TARGET --manifest-path=ensure_no_std/Cargo.toml
- rust: stable
env: TARGET=thumbv6m-none-eabi
before_script:
- rustup target add $TARGET
- set -ex
script:
- cargo rustc --target=$TARGET --manifest-path=ensure_no_std/Cargo.toml
- rust: beta
env: TARGET=thumbv6m-none-eabi
before_script:
- rustup target add $TARGET
- set -ex
script:
- cargo rustc --target=$TARGET --manifest-path=ensure_no_std/Cargo.toml
- rust: nightly
env: TARGET=thumbv6m-none-eabi
before_script:
- rustup target add $TARGET
- set -ex
script:
- cargo rustc --target=$TARGET --manifest-path=ensure_no_std/Cargo.toml
env:
global:
- HOST=x86_64-unknown-linux-gnu
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ rawpointer = "0.2"
bencher = "0.1.2"
itertools = "0.8"

[features]
default = ["std"]

std = []

[profile.release]
[profile.bench]

Expand Down
47 changes: 28 additions & 19 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
extern crate matrixmultiply;
pub use matrixmultiply::sgemm;
pub use matrixmultiply::dgemm;
pub use matrixmultiply::sgemm;

#[macro_use]
extern crate bencher;


// Compute GFlop/s
// by flop / s = 2 M N K / time


benchmark_main!(mat_mul_f32, mat_mul_f64, layout_f32_032, layout_f64_032);

macro_rules! mat_mul {
Expand All @@ -20,7 +18,7 @@ macro_rules! mat_mul {
$(
pub fn $name(bench: &mut Bencher)
{
let a = vec![0.; $m * $n];
let a = vec![0.; $m * $n];
let b = vec![0.; $n * $k];
let mut c = vec![0.; $m * $k];
bench.iter(|| {
Expand All @@ -42,7 +40,7 @@ macro_rules! mat_mul {
};
}

mat_mul!{mat_mul_f32, sgemm,
mat_mul! {mat_mul_f32, sgemm,
(m004, 4, 4, 4)
(m006, 6, 6, 6)
(m008, 8, 8, 8)
Expand All @@ -61,7 +59,7 @@ mat_mul!{mat_mul_f32, sgemm,
*/
}

mat_mul!{mat_mul_f64, dgemm,
mat_mul! {mat_mul_f64, dgemm,
(m004, 4, 4, 4)
(m006, 6, 6, 6)
(m008, 8, 8, 8)
Expand All @@ -80,11 +78,13 @@ mat_mul!{mat_mul_f64, dgemm,
*/
}


// benchmarks combinations of inputs using various layouts
// row-major ("c") vs column-major ("f") experiments

enum Layout { C, F }
enum Layout {
C,
F,
}
use self::Layout::*;

impl Layout {
Expand All @@ -106,7 +106,7 @@ macro_rules! gemm_layout {

fn base(bench: &mut Bencher, al: Layout, bl: Layout, cl: Layout)
{
let a = vec![0.; $m * $m];
let a = vec![0.; $m * $m];
let b = vec![0.; $m * $m];
let mut c = vec![0.; $m * $m];
let (rsa, csa) = al.strides($m, 1);
Expand Down Expand Up @@ -149,27 +149,35 @@ macro_rules! gemm_layout {
};
}

gemm_layout!{layout_f32_032, sgemm,
gemm_layout! {layout_f32_032, sgemm,
(m032, 32)
}

gemm_layout!{layout_f64_032, dgemm,
gemm_layout! {layout_f64_032, dgemm,
(m032, 32)
}


use std::ops::{Add, Mul};

trait Z {
fn zero() -> Self;
}
impl Z for f32 { fn zero() -> Self { 0. } }
impl Z for f64 { fn zero() -> Self { 0. } }
impl Z for f32 {
fn zero() -> Self {
0.
}
}
impl Z for f64 {
fn zero() -> Self {
0.
}
}

// simple, slow, correct (hopefully) mat mul (Row Major)
#[inline(never)]
fn reference_mat_mul<A>(m: usize, k: usize, n: usize, a: &[A], b: &[A], c: &mut [A])
where A: Z + Add<Output=A> + Mul<Output=A> + Copy,
where
A: Z + Add<Output = A> + Mul<Output = A> + Copy,
{
assert!(a.len() >= m * k);
assert!(b.len() >= k * n);
Expand All @@ -179,8 +187,9 @@ fn reference_mat_mul<A>(m: usize, k: usize, n: usize, a: &[A], b: &[A], c: &mut
for j in 0..n {
unsafe {
let celt = c.get_unchecked_mut(i * m + j);
*celt = (0..k).fold(A::zero(),
move |s, x| s + *a.get_unchecked(i * k + x) * *b.get_unchecked(x * n + j));
*celt = (0..k).fold(A::zero(), move |s, x| {
s + *a.get_unchecked(i * k + x) * *b.get_unchecked(x * n + j)
});
}
}
}
Expand All @@ -194,7 +203,7 @@ macro_rules! ref_mat_mul {
$(
pub fn $name(bench: &mut Bencher)
{
let a = vec![0. as $ty; $m * $n];
let a = vec![0. as $ty; $m * $n];
let b = vec![0.; $n * $k];
let mut c = vec![0.; $m * $k];
bench.iter(|| {
Expand All @@ -207,7 +216,7 @@ macro_rules! ref_mat_mul {
benchmark_group!{ $modname, $($modname::$name),+ }
};
}
ref_mat_mul!{ref_mat_mul_f32, f32,
ref_mat_mul! {ref_mat_mul_f32, f32,
(m004, 4, 4, 4)
(m005, 5, 5, 5)
(m006, 6, 6, 6)
Expand Down
2 changes: 1 addition & 1 deletion blas-bench/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use matrixmultiply::dgemm;
extern crate bencher;
extern crate blas;

use std::os::raw::c_int;
use core::os::raw::c_int;


#[allow(non_camel_case_types)]
Expand Down
2 changes: 2 additions & 0 deletions ensure_no_std/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
15 changes: 15 additions & 0 deletions ensure_no_std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "ensure_no_std"
version = "0.1.0"
authors = ["Geordon Worley <vadixidav@gmail.com>"]
edition = "2018"
publish = false

[dependencies]
matrixmultiply = { path = "..", default-features = false }

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
16 changes: 16 additions & 0 deletions ensure_no_std/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ensure_no_std/src/main.rs
#![no_std]
#![no_main]

use core::panic::PanicInfo;

/// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
loop {}
}
3 changes: 2 additions & 1 deletion examples/usegemm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//
// Jump down to the next place where it says EXAMPLE.

extern crate core;
extern crate itertools;
extern crate matrixmultiply;

Expand All @@ -16,7 +17,7 @@ use itertools::{
enumerate,
repeat_n,
};
use std::fmt::{Display, Debug};
use core::fmt::{Display, Debug};

trait Float : Copy + Display + Debug + PartialEq {
fn zero() -> Self;
Expand Down
39 changes: 22 additions & 17 deletions src/aligned_alloc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@

use std::alloc;
use std::alloc::{Layout, handle_alloc_error};
use std::{mem, cmp};
#[cfg(not(feature = "std"))]
use alloc::alloc::{self, handle_alloc_error, Layout};
use core::{cmp, mem};
#[cfg(feature = "std")]
use std::alloc::{self, handle_alloc_error, Layout};

#[cfg(test)]
use std::ops::{Deref, DerefMut};
use core::ops::{Deref, DerefMut};
#[cfg(test)]
use std::slice;
use core::slice;

pub(crate) struct Alloc<T> { ptr: *mut T, len: usize, align: usize }
pub(crate) struct Alloc<T> {
ptr: *mut T,
len: usize,
align: usize,
}

impl<T> Alloc<T> {
#[inline]
Expand All @@ -31,7 +36,9 @@ impl<T> Alloc<T> {
}

#[cfg(test)]
pub fn init_with(mut self, elt: T) -> Alloc<T> where T: Copy
pub fn init_with(mut self, elt: T) -> Alloc<T>
where
T: Copy,
{
for elt1 in &mut self[..] {
*elt1 = elt;
Expand All @@ -40,13 +47,16 @@ impl<T> Alloc<T> {
}

#[inline]
pub fn ptr_mut(&mut self) -> *mut T { self.ptr }
pub fn ptr_mut(&mut self) -> *mut T {
self.ptr
}
}

impl<T> Drop for Alloc<T> {
fn drop(&mut self) {
unsafe {
let layout = Layout::from_size_align_unchecked(mem::size_of::<T>() * self.len, self.align);
let layout =
Layout::from_size_align_unchecked(mem::size_of::<T>() * self.len, self.align);
alloc::dealloc(self.ptr as _, layout);
}
}
Expand All @@ -56,18 +66,13 @@ impl<T> Drop for Alloc<T> {
impl<T> Deref for Alloc<T> {
type Target = [T];
fn deref(&self) -> &[T] {
unsafe {
slice::from_raw_parts(self.ptr, self.len)
}
unsafe { slice::from_raw_parts(self.ptr, self.len) }
}
}

#[cfg(test)]
impl<T> DerefMut for Alloc<T> {
fn deref_mut(&mut self) -> &mut [T] {
unsafe {
slice::from_raw_parts_mut(self.ptr, self.len)
}
unsafe { slice::from_raw_parts_mut(self.ptr, self.len) }
}
}

7 changes: 5 additions & 2 deletions src/dgemm_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use kernel::{U4, U8};
use archparam;

#[cfg(target_arch="x86")]
use std::arch::x86::*;
use core::arch::x86::*;
#[cfg(target_arch="x86_64")]
use std::arch::x86_64::*;
use core::arch::x86_64::*;
#[cfg(any(target_arch="x86", target_arch="x86_64"))]
use x86::{FusedMulAdd, AvxMulAdd, DMultiplyAdd};

Expand Down Expand Up @@ -867,6 +867,8 @@ mod tests {
mod test_arch_kernels {
use super::test_a_kernel;
use super::super::*;
#[cfg(features = "std")]
use std::println;
macro_rules! test_arch_kernels_x86 {
($($feature_name:tt, $name:ident, $kernel_ty:ty),*) => {
$(
Expand All @@ -875,6 +877,7 @@ mod tests {
if is_x86_feature_detected_!($feature_name) {
test_a_kernel::<$kernel_ty>(stringify!($name));
} else {
#[cfg(features = "std")]
println!("Skipping, host does not have feature: {:?}", $feature_name);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/gemm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::cmp::min;
use std::mem::size_of;
use std::ptr::copy_nonoverlapping;
use core::cmp::min;
use core::mem::size_of;
use core::ptr::copy_nonoverlapping;

use aligned_alloc::Alloc;

Expand Down
2 changes: 1 addition & 1 deletion src/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::ops::{AddAssign, MulAssign};
use core::ops::{AddAssign, MulAssign};

/// General matrix multiply kernel
pub trait GemmKernel {
Expand Down
Loading

0 comments on commit 5d7ae23

Please sign in to comment.