Skip to content

No_std support for ndarray #864

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 22 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ bench = false
test = true

[dependencies]
num-integer = "0.1.39"
num-integer = { version = "0.1.39", default-features = false }
num-traits = { version = "0.2", default-features = false }
num-complex = { version = "0.3", default-features = false }

rayon = { version = "1.0.3", optional = true }

approx = { version = "0.4", optional = true }
approx = { version = "0.4", optional = true , default-features = false }

# Use via the `blas` crate feature!
cblas-sys = { version = "0.1.4", optional = true, default-features = false }
blas-src = { version = "0.6.1", optional = true, default-features = false }

matrixmultiply = { version = "0.2.0" }
matrixmultiply = { version = "0.2.0", default-features = false}
serde = { version = "1.0", optional = true }
rawpointer = { version = "0.2" }

Expand All @@ -66,7 +66,7 @@ test = ["test-blas-openblas-sys"]
# This feature is used for docs
docs = ["approx", "serde", "rayon"]

std = ["num-traits/std"]
std = ["num-traits/std", "matrixmultiply/std"]

[profile.release]
[profile.bench]
Expand Down
3 changes: 1 addition & 2 deletions examples/convo.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![allow(unused)]
extern crate ndarray;
extern crate num_traits;

use num_traits::Float;
use ndarray::Float;

use ndarray::prelude::*;

Expand Down
1 change: 1 addition & 0 deletions src/array_approx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ where
#[cfg(test)]
mod tests {
use crate::prelude::*;
use alloc::vec;
use approx::{
assert_abs_diff_eq, assert_abs_diff_ne, assert_relative_eq, assert_relative_ne,
assert_ulps_eq, assert_ulps_ne,
Expand Down
2 changes: 2 additions & 0 deletions src/array_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};

use std::fmt;
use std::marker::PhantomData;
use alloc::format;
use alloc::vec::Vec;

use crate::imp_prelude::*;

Expand Down
3 changes: 3 additions & 0 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use super::{ArrayBase, ArrayView, Axis, Data, Dimension, NdProducer};
use crate::aliases::{Ix1, IxDyn};
use std::fmt;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

/// Default threshold, below this element count, we don't ellipsize
const ARRAY_MANY_ELEMENT_LIMIT: usize = 500;
Expand Down
1 change: 1 addition & 0 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::iter::FromIterator;
use std::iter::IntoIterator;
use std::mem;
use std::ops::{Index, IndexMut};
use alloc::vec::Vec;

use crate::imp_prelude::*;
use crate::iter::{Iter, IterMut};
Expand Down
5 changes: 3 additions & 2 deletions src/data_repr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

use std::mem;
use std::mem::ManuallyDrop;
use std::ptr::NonNull;
use std::slice;
use alloc::slice;
use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use crate::extension::nonnull;

/// Array's representation.
Expand Down
3 changes: 2 additions & 1 deletion src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use rawpointer::PointerExt;
use std::mem::{self, size_of};
use std::ptr::NonNull;
use std::sync::Arc;
use alloc::sync::Arc;
use alloc::vec::Vec;

use crate::{
ArrayBase, CowRepr, Dimension, OwnedArcRepr, OwnedRepr, RawViewRepr, ViewRepr,
Expand Down
1 change: 1 addition & 0 deletions src/dimension/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use num_traits::Zero;
use std::ops::{Index, IndexMut};
use alloc::vec::Vec;

use crate::{Dim, Dimension, Ix, Ix1, IxDyn, IxDynImpl};

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

use std::fmt;

use std::fmt;
use super::Dimension;
use super::IntoDimension;
use crate::itertools::zip;
Expand Down
1 change: 1 addition & 0 deletions src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use std::fmt::Debug;
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use std::ops::{Index, IndexMut};
use alloc::vec::Vec;

use super::axes_of;
use super::conversion::Convert;
Expand Down
4 changes: 3 additions & 1 deletion src/dimension/dynindeximpl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::imp_prelude::*;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut, Index, IndexMut};

use alloc::vec;
use alloc::boxed::Box;
use alloc::vec::Vec;
const CAP: usize = 4;

/// T is usize or isize
Expand Down
6 changes: 3 additions & 3 deletions src/dimension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ mod test {

#[test]
fn slice_indexing_uncommon_strides() {
let v: Vec<_> = (0..12).collect();
let v: alloc::vec::Vec<_> = (0..12).collect();
let dim = (2, 3, 2).into_dimension();
let strides = (1, 2, 6).into_dimension();
assert!(super::can_index_slice(&v, &dim, &strides).is_ok());
Expand Down Expand Up @@ -784,7 +784,7 @@ mod test {
}

quickcheck! {
fn can_index_slice_not_custom_same_as_can_index_slice(data: Vec<u8>, dim: Vec<usize>) -> bool {
fn can_index_slice_not_custom_same_as_can_index_slice(data: alloc::vec::Vec<u8>, dim: alloc::vec::Vec<usize>) -> bool {
let dim = IxDyn(&dim);
let result = can_index_slice_not_custom(data.len(), &dim);
if dim.size_checked().is_none() {
Expand Down Expand Up @@ -871,7 +871,7 @@ mod test {
let (min2, max2) = (cmp::min(first2, last2), cmp::max(first2, last2));

// Naively determine if the sequences intersect.
let seq1: Vec<_> = (0..len1)
let seq1: alloc::vec::Vec<_> = (0..len1)
.map(|n| first1 + step1 * n)
.collect();
let intersects = (0..len2)
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use super::Dimension;
#[cfg(feature = "std")]
use std::error::Error;
use std::fmt;

Expand Down Expand Up @@ -69,6 +70,7 @@ impl PartialEq for ShapeError {
}
}

#[cfg(feature = "std")]
impl Error for ShapeError {}

impl fmt::Display for ShapeError {
Expand Down
1 change: 1 addition & 0 deletions src/extension/nonnull.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ptr::NonNull;
use alloc::vec::Vec;

/// Return a NonNull<T> pointer to the vector's data
pub(crate) fn nonnull_from_vec_data<T>(v: &mut Vec<T>) -> NonNull<T> {
Expand Down
4 changes: 3 additions & 1 deletion src/free_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// except according to those terms.

use std::mem::{forget, size_of};
use std::slice;
use alloc::slice;
use alloc::vec;
use alloc::vec::Vec;

use crate::imp_prelude::*;
use crate::{dimension, ArcArray1, ArcArray2};
Expand Down
1 change: 1 addition & 0 deletions src/geomspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![cfg(feature = "std")]
use num_traits::Float;

/// An iterator of a sequence of geometrically spaced floats.
Expand Down
1 change: 1 addition & 0 deletions src/impl_1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// except according to those terms.

//! Methods for one-dimensional arrays.
use alloc::vec::Vec;
use crate::imp_prelude::*;

/// # Methods For 1-D Arrays
Expand Down
12 changes: 10 additions & 2 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

#![allow(clippy::match_wild_err_arm)]

use num_traits::{Float, One, Zero};
use crate::Float;
use num_traits::{One, Zero};
use std::mem::MaybeUninit;
use alloc::vec;
use alloc::vec::Vec;

use crate::dimension;
use crate::error::{self, ShapeError};
Expand All @@ -23,7 +26,10 @@ use crate::indexes;
use crate::indices;
use crate::iterators::{to_vec, to_vec_mapped};
use crate::StrideShape;
use crate::{geomspace, linspace, logspace};
use crate::linspace;
#[cfg(feature = "std")]
use crate::{geomspace, logspace};


/// # Constructor Methods for Owned Arrays
///
Expand Down Expand Up @@ -112,6 +118,7 @@ where
/// assert_abs_diff_eq!(array, arr1(&[-1e3, -1e2, -1e1, -1e0]));
/// # }
/// ```
#[cfg(feature = "std")]
pub fn logspace(base: A, start: A, end: A, n: usize) -> Self
where
A: Float,
Expand Down Expand Up @@ -146,6 +153,7 @@ where
/// #
/// # example().unwrap();
/// ```
#[cfg(feature = "std")]
pub fn geomspace(start: A, end: A, n: usize) -> Option<Self>
where
A: Float,
Expand Down
5 changes: 3 additions & 2 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
// except according to those terms.

use std::ptr as std_ptr;
use std::slice;

use alloc::slice;
use alloc::vec;
use alloc::vec::Vec;
use rawpointer::PointerExt;

use crate::imp_prelude::*;
Expand Down
1 change: 1 addition & 0 deletions src/impl_owned_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

use alloc::vec::Vec;
use crate::imp_prelude::*;

/// Methods specific to `Array0`.
Expand Down
2 changes: 1 addition & 1 deletion src/impl_views/conversions.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::slice;
use alloc::slice;

use crate::imp_prelude::*;

Expand Down
4 changes: 2 additions & 2 deletions src/iterators/macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Send and Sync
// All the iterators are thread safe the same way the slice's iterator are

// read-only iterators use Sync => Send rules, same as `std::slice::Iter`.
// read-only iterators use Sync => Send rules, same as `alloc::slice::Iter`.
macro_rules! send_sync_read_only {
($name:ident) => {
unsafe impl<'a, A, D> Send for $name<'a, A, D>
Expand All @@ -19,7 +19,7 @@ macro_rules! send_sync_read_only {
};
}

// read-write iterators use Send => Send rules, same as `std::slice::IterMut`.
// read-write iterators use Send => Send rules, same as `alloc::slice::IterMut`.
macro_rules! send_sync_read_write {
($name:ident) => {
unsafe impl<'a, A, D> Send for $name<'a, A, D>
Expand Down
10 changes: 7 additions & 3 deletions src/iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod windows;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::ptr;
use alloc::vec::Vec;

use crate::Ix1;

Expand Down Expand Up @@ -1446,10 +1447,13 @@ pub unsafe trait TrustedIterator {}

use crate::indexes::IndicesIterF;
use crate::iter::IndicesIter;
use crate::{geomspace::Geomspace, linspace::Linspace, logspace::Logspace};

unsafe impl<F> TrustedIterator for Geomspace<F> {}
use crate::linspace::Linspace;
unsafe impl<F> TrustedIterator for Linspace<F> {}
#[cfg(feature = "std")]
use crate::{geomspace::Geomspace, logspace::Logspace};
#[cfg(feature = "std")]
unsafe impl<F> TrustedIterator for Geomspace<F> {}
#[cfg(feature = "std")]
unsafe impl<F> TrustedIterator for Logspace<F> {}
unsafe impl<'a, A, D> TrustedIterator for Iter<'a, A, D> {}
unsafe impl<'a, A, D> TrustedIterator for IterMut<'a, A, D> {}
Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
clippy::deref_addrof,
clippy::unreadable_literal
)]
#![no_std]

//! The `ndarray` crate provides an *n*-dimensional container for general elements
//! and for numerics.
Expand Down Expand Up @@ -109,6 +110,19 @@
//! For conversion between `ndarray`, [`nalgebra`](https://crates.io/crates/nalgebra) and
//! [`image`](https://crates.io/crates/image) check out [`nshare`](https://crates.io/crates/nshare).


extern crate alloc;

#[cfg(feature = "std")]
extern crate std;
#[cfg(not(feature = "std"))]
extern crate core as std;

#[cfg(feature = "std")]
pub use num_traits::Float;
#[cfg(not(feature = "std"))]
pub use num_traits::float::FloatCore as Float;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line must be deleted. Then we follow the consequence of that for not(feature="std").

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need a creative solution, then we can "redirect" to support only f32, f64 on no-std (not general FloatCore!). Then we can feature-safely expand our methods bounded on A: Float to support the full A: Float range with std.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for my lack of experience. I will change it.
I think it is cool to expand for FloatCore under no_std, but if we don’t use libm, we might have to implement exp() and ln() functions ourselves. I doubt about that.


#[cfg(feature = "blas")]
extern crate blas_src;
#[cfg(feature = "blas")]
Expand All @@ -118,7 +132,7 @@ extern crate cblas_sys;
pub mod doc;

use std::marker::PhantomData;
use std::sync::Arc;
use alloc::sync::Arc;

pub use crate::dimension::dim::*;
pub use crate::dimension::{Axis, AxisDescription, Dimension, IntoDimension, RemoveAxis};
Expand All @@ -135,7 +149,7 @@ use crate::iterators::{ElementsBase, ElementsBaseMut, Iter, IterMut, Lanes, Lane
pub use crate::arraytraits::AsArray;
#[cfg(feature = "std")]
pub use crate::linalg_traits::NdFloat;
pub use crate::linalg_traits::LinalgScalar;
pub use crate::linalg_traits::LinalgScalar;

pub use crate::stacking::{concatenate, stack, stack_new_axis};

Expand All @@ -161,7 +175,6 @@ mod data_repr;
mod data_traits;

pub use crate::aliases::*;

#[allow(deprecated)]
pub use crate::data_traits::{
Data, DataClone, DataMut, DataOwned, DataShared, RawData, RawDataClone, RawDataMut,
Expand Down
1 change: 1 addition & 0 deletions src/linalg/impl_linalg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::numeric_util;
use crate::{LinalgScalar, Zip};

use std::any::TypeId;
use alloc::vec::Vec;

#[cfg(feature = "blas")]
use std::cmp;
Expand Down
2 changes: 1 addition & 1 deletion src/linspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use num_traits::Float;
use crate::Float;

/// An iterator of a sequence of evenly spaced floats.
///
Expand Down
Loading