Skip to content
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

(WIP) Add no_std support #46

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dist: trusty

matrix:
include:
- rust: 1.28.0
- rust: 1.36.0
env:
TARGET=x86_64-unknown-linux-gnu
- rust: stable
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.7.11"

[features]
default = ["std"]

std = []

[profile.release]
[profile.bench]

Expand Down
2 changes: 1 addition & 1 deletion benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ gemm_layout!{layout_f64_032, dgemm,
}


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

trait Z {
fn zero() -> Self;
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
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
9 changes: 4 additions & 5 deletions src/aligned_alloc.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

use std::alloc;
use std::alloc::{Layout, handle_alloc_error};
use std::{mem, cmp};
use alloc::alloc::{self, Layout, handle_alloc_error};
use core::{mem, cmp};

#[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 }

Expand Down
6 changes: 4 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 @@ -812,6 +812,7 @@ unsafe fn at(ptr: *const T, i: usize) -> T {
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
use aligned_alloc::Alloc;

fn aligned_alloc<T>(elt: T, n: usize) -> Alloc<T> where T: Copy
Expand Down Expand Up @@ -864,6 +865,7 @@ mod tests {
mod test_arch_kernels {
use super::test_a_kernel;
use super::super::*;
use std::println;
macro_rules! test_arch_kernels_x86 {
($($feature_name:tt, $name:ident, $kernel_ty:ty),*) => {
$(
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
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,38 @@
//! - `avx`
//! - `sse2`
//!
//! ## Features
//!
//! This crate can be used without the standard library (`#![no_std]`) by
//! disabling the default `std` feature. To do so, use this in your
//! `Cargo.toml`:
//!
//! ```toml
//! matrixmultiply = { version = "0.2", default-features = false }
//! ```
//!
//! Runtime CPU feature detection is available only when `std` is enabled.
//! Without the `std` feature, the crate uses special CPU features only if they
//! are enabled at compile time. (To enable CPU features at compile time, pass
//! the relevant
//! [`target-cpu`](https://doc.rust-lang.org/rustc/codegen-options/index.html#target-cpu)
//! or
//! [`target-feature`](https://doc.rust-lang.org/rustc/codegen-options/index.html#target-feature)
//! option to `rustc`.)
//!
//! ## Other Notes
//!
//! The functions in this crate are thread safe, as long as the destination
//! matrix is distinct.

#![doc(html_root_url = "https://docs.rs/matrixmultiply/0.2/")]

#![no_std]
#[cfg(feature = "std")]
extern crate std;

extern crate alloc;

extern crate rawpointer;

#[macro_use] mod debugmacros;
Expand Down
6 changes: 4 additions & 2 deletions src/sgemm_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ 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, SMultiplyAdd};

Expand Down Expand Up @@ -499,6 +499,7 @@ unsafe fn at(ptr: *const T, i: usize) -> T {
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
use aligned_alloc::Alloc;

fn aligned_alloc<T>(elt: T, n: usize) -> Alloc<T> where T: Copy
Expand Down Expand Up @@ -551,6 +552,7 @@ mod tests {
mod test_arch_kernels {
use super::test_a_kernel;
use super::super::*;
use std::println;
macro_rules! test_arch_kernels_x86 {
($($feature_name:tt, $name:ident, $kernel_ty:ty),*) => {
$(
Expand Down
2 changes: 1 addition & 1 deletion src/util.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::cmp::min;
use core::cmp::min;

pub struct RangeChunk { i: usize, n: usize, chunk: usize }

Expand Down
26 changes: 19 additions & 7 deletions src/x86/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ macro_rules! compile_env_matches_or_is_empty {
}

macro_rules! is_x86_feature_detected_ {
($name:tt) => {
// for testing purposes, we can make sure only one specific feature
// is enabled by setting MMTEST_FEATURE=featurename (all others
// disabled). This does not force it to be detected, it must also be.
compile_env_matches_or_is_empty!("MMTEST_FEATURE", $name) && is_x86_feature_detected!($name)
}
($name:tt) => {{
#[cfg(feature="std")]
{
// For testing purposes, we can make sure only one specific feature
// is enabled by setting MMTEST_FEATURE=featurename (all others
// disabled). This does not force it to be detected, it must also be.
compile_env_matches_or_is_empty!("MMTEST_FEATURE", $name) && std::is_x86_feature_detected!($name)
}
#[cfg(not(feature="std"))]
{
// For testing purposes, we can make sure only one specific feature
// is enabled by setting MMTEST_FEATURE=featurename (all others
// disabled). This does not force it to be detected, it must also
// be. In the `no_std` case, the `is_86_feature_detected` macro is
// not available, so we have to fall back to checking whether the
// feature is enabled at compile-time.
compile_env_matches_or_is_empty!("MMTEST_FEATURE", $name) && cfg!(target_feature=$name)
}
}};
}

4 changes: 2 additions & 2 deletions src/x86/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#[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::*;

#[macro_use]
mod macros;
Expand Down
3 changes: 2 additions & 1 deletion tests/sgemm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern crate core;
extern crate itertools;
extern crate matrixmultiply;

Expand All @@ -9,7 +10,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