From f207e009f6742f2603c4612d38f9f886bafe23be Mon Sep 17 00:00:00 2001 From: pradeep Date: Fri, 11 Sep 2020 18:08:44 +0530 Subject: [PATCH 1/3] Add macro based syntactic sugar for creating constant arrays The syntax takes from the vec macro of rust standard library where the literal/expr followed by a ";" gives the value with which array is to be filled. A sequence of comma separated literals/exprs after ";" give the target shape of array. --- src/core/macros.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/core/macros.rs b/src/core/macros.rs index 69c08a8d5..b280e1a36 100644 --- a/src/core/macros.rs +++ b/src/core/macros.rs @@ -298,6 +298,25 @@ macro_rules! eval { }; } +/// Create an array of given shape filled with a single value a.k.a constant array +/// +/// # Examples +/// +/// ```rust +/// # use arrayfire::{constant}; +/// let _zeros_1d = constant!(0.0f32; 10); +/// let _ones_3d = constant!(1u32; 3, 3, 3); +/// +/// let dim = 10; +/// let mix_shape = constant!(42.0f32; dim, 10); +/// ``` +#[macro_export] +macro_rules! constant { + ($value:expr; $($dim:expr),+) => { + $crate::constant($value, $crate::dim4!($($dim),*)) + }; +} + #[cfg(test)] mod tests { use super::super::array::Array; @@ -425,4 +444,15 @@ mod tests { eval!(a[indices, seq4gen] = b); // ANCHOR_END: macro_seq_array_assign } + + #[test] + fn constant_macro() { + let _zeros_1d = constant!(0.0f32; 10); + let _zeros_2d = constant!(0.0f64; 5, 5); + let _ones_3d = constant!(1u32; 3, 3, 3); + let _twos_4d = constant!(2u16; 2, 2, 2, 2); + + let dim = 10; + let _mix_shape = constant!(42.0f32; dim, 10); + } } From 94f281bb6dd2dcf993924f83f5c32c994e7ce839 Mon Sep 17 00:00:00 2001 From: pradeep Date: Fri, 11 Sep 2020 18:24:27 +0530 Subject: [PATCH 2/3] Add random number generator macros for ease of use --- src/core/macros.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/core/macros.rs b/src/core/macros.rs index b280e1a36..c06018a1f 100644 --- a/src/core/macros.rs +++ b/src/core/macros.rs @@ -317,6 +317,38 @@ macro_rules! constant { }; } +/// Create an array of given shape sampled from uniform distribution +/// +/// If no type argument is specified, the data type defaults to 32 bit floats. +/// +/// # Examples +/// +/// ```rust +/// # use arrayfire::{randu}; +/// let mat10x10 = randu!(10, 10); +/// ``` +#[macro_export] +macro_rules! randu { + ($($dim:expr),+) => { $crate::randu::($crate::dim4!($($dim),*)) }; + ($type:ty; $($dim:expr),+) => { $crate::randu::<$type>($crate::dim4!($($dim),*)) }; +} + +/// Create an array of given shape sampled from normal distribution +/// +/// If no type argument is specified, the data type defaults to 32 bit floats. +/// +/// # Examples +/// +/// ```rust +/// # use arrayfire::{randn}; +/// let mat10x10 = randn!(10, 10); +/// ``` +#[macro_export] +macro_rules! randn { + ($($dim:expr),+) => { $crate::randn::($crate::dim4!($($dim),*)) }; + ($type:ty; $($dim:expr),+) => { $crate::randn::<$type>($crate::dim4!($($dim),*)) }; +} + #[cfg(test)] mod tests { use super::super::array::Array; @@ -455,4 +487,12 @@ mod tests { let dim = 10; let _mix_shape = constant!(42.0f32; dim, 10); } + + #[test] + fn rand_macro() { + let _ru5x5 = randu!(5, 5); + let _rn5x5 = randn!(5, 5); + let _ruu32_5x5 = randu!(u32; 5, 5); + let _ruu8_5x5 = randu!(u8; 5, 5); + } } From 8c7780ba9c6361bab450d72e23096794f10c7220 Mon Sep 17 00:00:00 2001 From: pradeep Date: Sun, 13 Sep 2020 16:12:53 +0530 Subject: [PATCH 3/3] Add troubleshooting section to README --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index d33bb61fc..e13021a19 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,14 @@ Create a 5-by-3 matrix of random floats on the GPU ... ``` +### Troubleshooting + +If the build command fails with undefined references errors even after taking care of environment +variables, we recommend doing a `cargo clean` and re-running `cargo build` or `cargo test`. + +You can also use some environment variables mentioned in our [book][23], such as `AF_PRINT_ERRORS` +to print more elaborate error messages to console. + ## Acknowledgements The ArrayFire library is written by developers at [ArrayFire][14] LLC with [contributions][15] @@ -118,3 +126,4 @@ the DARPA SBIR Program Office. [20]: https://img.shields.io/badge/arrayfire-google--groups-orange [21]: http://arrayfire.org/arrayfire-rust/book/index.html [22]: https://img.shields.io/badge/arrayfire-mdbook-073763?logo=readthedocs +[23]: http://arrayfire.org/arrayfire-rust/book/configuring_arrayfire_environment.html