1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
/// Macro to print the current stats of ArrayFire's memory manager. /// /// `mem_info!` print 4 values: /// /// Name | Description /// -------------------------|------------------------- /// Allocated Bytes | Total number of bytes allocated by the memory manager /// Allocated Buffers | Total number of buffers allocated /// Locked (In Use) Bytes | Number of bytes that are in use by active arrays /// Locked (In Use) Buffers | Number of buffers that are in use by active arrays /// /// The `Allocated Bytes` is always a multiple of the memory step size. The /// default step size is 1024 bytes. This means when a buffer is to be /// allocated, the size is always rounded up to a multiple of the step size. /// You can use [get_mem_step_size](./fn.get_mem_step_size.html) to check the /// current step size and [set_mem_step_size](./fn.set_mem_step_size.html) to /// set a custom resolution size. /// /// The `Allocated Buffers` is the number of buffers that use up the allocated /// bytes. This includes buffers currently in scope, as well as buffers marked /// as free, ie, from arrays gone out of scope. The free buffers are available /// for use by new arrays that might be created. /// /// The `Locked Bytes` is the number of bytes in use that cannot be /// reallocated at the moment. The difference of Allocated Bytes and Locked /// Bytes is the total bytes available for reallocation. /// /// The `Locked Buffers` is the number of buffer in use that cannot be /// reallocated at the moment. The difference of Allocated Buffers and Locked /// Buffers is the number of buffers available for reallocation. /// /// # Parameters /// /// - `msg` is the message that is printed to screen before printing stats /// /// # Examples /// /// ```rust /// use arrayfire::{Dim4, device_mem_info, print, randu, mem_info}; /// /// let dims = Dim4::new(&[5, 5, 1, 1]); /// let a = randu::<f32>(dims); /// print(&a); /// mem_info!("Hello!"); /// ``` /// /// Sample Output: /// /// ```text /// AF Memory: Here /// Allocated [ Bytes | Buffers ] = [ 4096 | 4 ] /// In Use [ Bytes | Buffers ] = [ 2048 | 2 ] /// ``` #[macro_export] macro_rules! mem_info { [$msg: expr] => { { let (abytes, abuffs, lbytes, lbuffs) = $crate::device_mem_info(); println!("AF Memory: {:?}", $msg); println!("Allocated [Bytes | Buffers] = [ {} | {} ]", abytes, abuffs); println!("In Use [Bytes | Buffers] = [ {} | {} ]", lbytes, lbuffs); } }; } /// Join multiple Arrays along a given dimension /// /// All the Arrays provided to this macro should be of type `&Array` /// /// # Examples /// /// ```rust /// use arrayfire::{Dim4, join_many, print, randu}; /// /// let a = &randu::<f32>(Dim4::new(&[5, 3, 1, 1])); /// let b = &randu::<f32>(Dim4::new(&[5, 3, 1, 1])); /// let c = &randu::<f32>(Dim4::new(&[5, 3, 1, 1])); /// let d = join_many![2; a, b, c]; /// print(&d); /// ``` /// /// # Panics /// /// This macro just calls [join_many](./fn.join_many.html) function after collecting all /// the input arrays into a vector. // Using macro to implement join many wrapper #[macro_export] macro_rules! join_many { [$dim: expr; $($x:expr),+] => { { let mut temp_vec = Vec::new(); $( temp_vec.push($x); )* $crate::join_many($dim, temp_vec) } }; } /// Print given message before printing out the Array to standard output /// /// # Examples /// /// ```rust /// use arrayfire::{Dim4, print_gen, randu, af_print}; /// let dims = Dim4::new(&[3, 1, 1, 1]); /// let a = randu::<f32>(dims); /// af_print!("Create a 5-by-3 matrix of random floats on the GPU", a); /// ``` /// #[macro_export] macro_rules! af_print { [$msg: expr, $x: expr] => { { $crate::print_gen(String::from($msg), &$x, Some(4)); } }; } /// Create a dim4 object from provided dimensions /// /// The user can pass 1 or more sizes and the left over values will default to 1. #[macro_export] macro_rules! dim4 { ($dim0:expr) => { $crate::Dim4::new(&[$dim0, 1, 1, 1]) }; ($dim0:expr, $dim1:expr) => { $crate::Dim4::new(&[$dim0, $dim1, 1, 1]) }; ($dim0:expr, $dim1:expr, $dim2:expr) => { $crate::Dim4::new(&[$dim0, $dim1, $dim2, 1]) }; ($dim0:expr, $dim1:expr, $dim2:expr, $dim3:expr) => { $crate::Dim4::new(&[$dim0, $dim1, $dim2, $dim3]) }; } /// Create a sequence object /// /// If type is not provided, then the Seq will default to i32 type #[macro_export] macro_rules! seq { () => { $crate::Seq::<i32>::default() }; ($sty:ty; $start:literal : $end:literal : $step:literal) => { $crate::Seq::<$sty>::new($start, $end, $step) }; ($start:literal : $end:literal : $step:literal) => { $crate::Seq::<i32>::new($start, $end, $step) }; ($sty:ty; $start:expr , $end:expr , $step:expr) => { $crate::Seq::<$sty>::new($start, $end, $step) }; ($start:expr , $end:expr , $step:expr) => { $crate::Seq::<i32>::new($start, $end, $step) }; } /// Indexing into an existing Array /// /// This macro call with return an Array that has a view of another Array. The Array returned due to /// the indexing operation will follow copy-on-write semantics. The Array identifier taken by this /// macro is passed to the relevant internal functions as a borrowed reference. Thus, this identifier /// will be still available for futher use after the macro call. /// /// The following types of inputs are matched by this macro. /// /// - A simple Array identifier. /// - An Array with slicing info for indexing. /// - An Array with slicing info and other arrays used for indexing. /// /// Examples on how to use this macro are provided in the [tutorials book][1] /// /// [1]: http://arrayfire.org/arrayfire-rust/book/indexing.html #[macro_export] macro_rules! view { (@af_max_dims) => { 4 }; ( $array_ident:ident ) => { $array_ident.clone() }; ( $array_ident:ident [ $($start:literal : $end:literal : $step:literal),+ ] ) => { { #[allow(non_snake_case)] let AF_MAX_DIMS: usize = view!(@af_max_dims); let mut seq_vec = Vec::<$crate::Seq<i32>>::with_capacity(AF_MAX_DIMS); $( seq_vec.push($crate::seq!($start:$end:$step)); )* $crate::index(&$array_ident, &seq_vec) } }; (@set_indexer $idim:expr, $idxr:ident, $lterm:expr) => { { $idxr.set_index(&$lterm, $idim, None); } }; (@set_indexer $idim:expr, $idxr:ident, $hterm:expr, $($tterm:expr),*) => { { $idxr.set_index(&$hterm, $idim, None); view!(@set_indexer $idim + 1, $idxr, $($tterm),*); } }; ($array_ident:ident [ $($_e:expr),+ ]) => { { let mut idxrs = $crate::Indexer::default(); view!(@set_indexer 0, idxrs, $($_e),*); $crate::index_gen(&$array_ident, idxrs) } }; } /// Macro to evaluate individual Arrays or assignment operations /// /// - Evaluate on one or more Array identifiers: essentially calls [Array::eval][4] on each of those /// Array objects individually. /// /// ```rust /// use arrayfire::{dim4, eval, randu}; /// let dims = dim4!(5, 5); /// let a = randu::<f32>(dims); /// let b = a.clone(); /// let c = a.clone(); /// let d = a.clone(); /// let x = a - b; /// let y = c * d; /// eval!(&x, &y); /// ``` /// /// - Evaluate assignment operations: This is essentially syntactic sugar for modifying portions of /// Array with another Array using a combination of [Sequences][1] and/or [Array][2] objects. /// Full examples for this use case are provided in the [tutorials book][3] /// /// [1]: http://arrayfire.org/arrayfire-rust/arrayfire/struct.Seq.html /// [2]: http://arrayfire.org/arrayfire-rust/arrayfire/struct.Array.html /// [3]: http://arrayfire.org/arrayfire-rust/book/indexing.html /// [4]: http://arrayfire.org/arrayfire-rust/arrayfire/struct.Array.html#method.eval #[macro_export] macro_rules! eval { ( $l:ident [ $($lb:literal : $le:literal : $ls:literal),+ ] = $r:ident [ $($rb:literal : $re:literal : $rs:literal),+ ]) => { { #[allow(non_snake_case)] let AF_MAX_DIMS: usize = view!(@af_max_dims); let mut seq_vec = Vec::<$crate::Seq<i32>>::with_capacity(AF_MAX_DIMS); $( seq_vec.push($crate::seq!($lb:$le:$ls)); )* let mut idxrs = $crate::Indexer::default(); for i in 0..seq_vec.len() { idxrs.set_index(&seq_vec[i], i as u32, None); } let eq_rterm = $crate::view!($r[ $($rb:$re:$rs),+ ]); $crate::assign_gen(&mut $l, &idxrs, &eq_rterm); } }; ( $l:ident [ $($lb:literal : $le:literal : $ls:literal),+ ] = $r:expr ) => { { #[allow(non_snake_case)] let AF_MAX_DIMS: usize = view!(@af_max_dims); let mut seq_vec = Vec::<$crate::Seq<i32>>::with_capacity(AF_MAX_DIMS); $( seq_vec.push($crate::seq!($lb:$le:$ls)); )* let mut idxrs = $crate::Indexer::default(); for i in 0..seq_vec.len() { idxrs.set_index(&seq_vec[i], i as u32, None); } $crate::assign_gen(&mut $l, &idxrs, &$r); } }; ($lhs:ident [ $($lhs_e:expr),+ ] = $rhs:ident [ $($rhs_e:expr),+ ]) => { { let eq_rterm = $crate::view!($rhs[ $($rhs_e),+ ]); let mut idxrs = $crate::Indexer::default(); view!(@set_indexer 0, idxrs, $($lhs_e),*); $crate::assign_gen(&mut $lhs, &idxrs, &eq_rterm); } }; ($lhs:ident [ $($lhs_e:expr),+ ] = $rhs:expr) => { { let mut idxrs = $crate::Indexer::default(); view!(@set_indexer 0, idxrs, $($lhs_e),*); $crate::assign_gen(&mut $lhs, &idxrs, &$rhs); } }; [$($x:expr),+] => { { let mut temp_vec = Vec::new(); $( temp_vec.push($x); )* $crate::eval_multiple(temp_vec) } }; } /// 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),*)) }; } /// 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::<f32>($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::<f32>($crate::dim4!($($dim),*)) }; ($type:ty; $($dim:expr),+) => { $crate::randn::<$type>($crate::dim4!($($dim),*)) }; } #[cfg(test)] mod tests { use super::super::array::Array; use super::super::data::constant; use super::super::device::set_device; use super::super::index::index; use super::super::random::randu; #[test] fn dim4_construction() { let dim1d = dim4!(2); let dim2d = dim4!(2, 3); let dim3d = dim4!(2, 3, 4); let dim4d = dim4!(2, 3, 4, 2); let _dimn = dim4!(dim1d[0], dim2d[1], dim3d[2], dim4d[3]); } #[test] fn seq_construction() { let default_seq = seq!(); let _range_1_to_10_step_1 = seq!(0:9:1); let _range_1_to_10_step_1_2 = seq!(f32; 0.0:9.0:1.5); let _range_from_exprs = seq!(default_seq.begin(), default_seq.end(), default_seq.step()); let _range_from_exprs2 = seq!(f32; default_seq.begin() as f32, default_seq.end() as f32, default_seq.step() as f32); } #[test] fn seq_view() { set_device(0); let mut dim4d = dim4!(5, 3, 2, 1); dim4d[2] = 1; let a = randu::<f32>(dim4d); let seqs = &[seq!(1:3:1), seq!()]; let _sub = index(&a, seqs); } #[test] fn seq_view2() { set_device(0); // ANCHOR: seq_view2 let a = randu::<f32>(dim4!(5, 5)); let _sub = view!(a[1:3:1, 1:1:0]); // 1:1:0 means all elements along axis // ANCHOR_END: seq_view2 } #[test] fn view_macro() { set_device(0); let dims = dim4!(5, 5, 2, 1); let a = randu::<f32>(dims); let b = a.clone(); let c = a.clone(); let d = a.clone(); let e = a.clone(); let _v = view!(a); let _m = view!(c[1:3:1, 1:3:2]); let x = seq!(1:3:1); let y = seq!(1:3:2); let _u = view!(b[x, y]); let values: [u32; 3] = [1, 2, 3]; let indices = Array::new(&values, dim4!(3, 1, 1, 1)); let indices2 = Array::new(&values, dim4!(3, 1, 1, 1)); let _w = view!(d[indices, indices2]); let _z = view!(e[indices, y]); } #[test] fn eval_assign_seq_indexed_array() { set_device(0); let dims = dim4!(5, 5); let mut a = randu::<f32>(dims); //print(&a); //[5 5 1 1] // 0.6010 0.5497 0.1583 0.3636 0.6755 // 0.0278 0.2864 0.3712 0.4165 0.6105 // 0.9806 0.3410 0.3543 0.5814 0.5232 // 0.2126 0.7509 0.6450 0.8962 0.5567 // 0.0655 0.4105 0.9675 0.3712 0.7896 let b = randu::<f32>(dims); //print(&b); //[5 5 1 1] // 0.8966 0.5143 0.0123 0.7917 0.2522 // 0.0536 0.3670 0.3988 0.1654 0.9644 // 0.5775 0.3336 0.9787 0.8657 0.4711 // 0.2908 0.0363 0.2308 0.3766 0.3637 // 0.9941 0.5349 0.6244 0.7331 0.9643 let d0 = seq!(1:2:1); let d1 = seq!(1:2:1); let s0 = seq!(1:2:1); let s1 = seq!(1:2:1); eval!(a[d0, d1] = b[s0, s1]); //print(&a); //[5 5 1 1] // 0.6010 0.5497 0.1583 0.3636 0.6755 // 0.0278 0.3670 0.3988 0.4165 0.6105 // 0.9806 0.3336 0.9787 0.5814 0.5232 // 0.2126 0.7509 0.6450 0.8962 0.5567 // 0.0655 0.4105 0.9675 0.3712 0.7896 } #[test] fn eval_assign_array_to_seqd_array() { set_device(0); // ANCHOR: macro_seq_assign let mut a = randu::<f32>(dim4!(5, 5)); let b = randu::<f32>(dim4!(2, 2)); eval!(a[1:2:1, 1:2:1] = b); // ANCHOR_END: macro_seq_assign } #[test] fn macro_seq_array_assign() { set_device(0); // ANCHOR: macro_seq_array_assign let values: [f32; 3] = [1.0, 2.0, 3.0]; let indices = Array::new(&values, dim4!(3)); let seq4gen = seq!(0:2:1); let mut a = randu::<f32>(dim4!(5, 3)); let b = constant(2.0 as f32, dim4!(3, 3)); eval!(a[indices, seq4gen] = b); // ANCHOR_END: macro_seq_array_assign } #[test] fn constant_macro() { set_device(0); 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); } #[test] fn rand_macro() { set_device(0); let _ru5x5 = randu!(5, 5); let _rn5x5 = randn!(5, 5); let _ruu32_5x5 = randu!(u32; 5, 5); let _ruu8_5x5 = randu!(u8; 5, 5); } }