Skip to content

Commit e599c27

Browse files
committed
Merge pull request #40 from 9prady9/docs_content
Docs content
2 parents e4e20aa + 0839d4d commit e599c27

File tree

17 files changed

+1525
-35
lines changed

17 files changed

+1525
-35
lines changed

src/algorithm/mod.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ extern {
4949

5050
macro_rules! dim_reduce_func_def {
5151
($fn_name: ident, $ffi_name: ident) => (
52+
/// Reduction operation along specific dimension
53+
///
54+
/// # Parameters
55+
///
56+
/// - `input` - Input Array
57+
/// - `dim` - Dimension along which the input Array will be reduced
58+
///
59+
/// # Return Values
60+
///
61+
/// Reduced Array
5262
#[allow(unused_mut)]
5363
pub fn $fn_name(input: &Array, dim: i32) -> Result<Array, AfError> {
5464
unsafe {
@@ -95,6 +105,16 @@ dim_reduce_func_def!(diff2, af_diff2);
95105

96106
macro_rules! all_reduce_func_def {
97107
($fn_name: ident, $ffi_name: ident) => (
108+
/// Reduction operation of all values
109+
///
110+
/// # Parameters
111+
///
112+
/// `input` - Input Array
113+
///
114+
/// # Return Values
115+
///
116+
/// A tuple of reduction result. For non-complex data type Arrays, second value of tuple is
117+
/// zero.
98118
#[allow(unused_mut)]
99119
pub fn $fn_name(input: &Array) -> Result<(f64, f64), AfError> {
100120
unsafe {
@@ -141,6 +161,18 @@ all_reduce_func_def!(count_all, af_count_all);
141161

142162
macro_rules! dim_ireduce_func_def {
143163
($fn_name: ident, $ffi_name: ident) => (
164+
/// Reduction operation along specific dimension
165+
///
166+
/// # Parameters
167+
///
168+
/// - `input` - Input Array
169+
/// - `dim` - Dimension along which the input Array will be reduced
170+
///
171+
/// # Return Values
172+
///
173+
/// A tuple of Arrays: Reduced Array and Indices Array.
174+
///
175+
/// The indices Array has the index of the result element along the reduction dimension.
144176
#[allow(unused_mut)]
145177
pub fn $fn_name(input: &Array, dim: i32) -> Result<(Array, Array), AfError> {
146178
unsafe {
@@ -162,6 +194,19 @@ dim_ireduce_func_def!(imax, af_imax);
162194

163195
macro_rules! all_ireduce_func_def {
164196
($fn_name: ident, $ffi_name: ident) => (
197+
/// Reduction operation of all values
198+
///
199+
/// # Parameters
200+
///
201+
/// `input` - Input Array
202+
///
203+
/// # Return Values
204+
///
205+
/// A triplet of reduction result.
206+
///
207+
/// The second value of the tuple is zero for non-complex data type Arrays.
208+
///
209+
/// The third value of triplet is the index of result element from reduction operation.
165210
#[allow(unused_mut)]
166211
pub fn $fn_name(input: &Array) -> Result<(f64, f64, u32), AfError> {
167212
unsafe {
@@ -182,6 +227,17 @@ macro_rules! all_ireduce_func_def {
182227
all_ireduce_func_def!(imin_all, af_imin_all);
183228
all_ireduce_func_def!(imax_all, af_imax_all);
184229

230+
/// Locate the indices of non-zero elements.
231+
///
232+
/// The locations are provided by flattening the input into a linear array.
233+
///
234+
/// # Parameters
235+
///
236+
/// - `input` - Input Array
237+
///
238+
/// # Return Values
239+
///
240+
/// Array of indices where the input Array has non-zero values.
185241
#[allow(unused_mut)]
186242
pub fn locate(input: &Array) -> Result<Array, AfError> {
187243
unsafe {
@@ -194,6 +250,19 @@ pub fn locate(input: &Array) -> Result<Array, AfError> {
194250
}
195251
}
196252

253+
/// Sort the values in input Arrays
254+
///
255+
/// Sort an multidimensional Array along a given dimension
256+
///
257+
/// # Parameters
258+
///
259+
/// - `input` - Input Array
260+
/// - `dim` - Dimension along which to sort
261+
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
262+
///
263+
/// # Return Values
264+
///
265+
/// Sorted Array.
197266
#[allow(unused_mut)]
198267
pub fn sort(input: &Array, dim: u32, ascending: bool) -> Result<Array, AfError> {
199268
unsafe {
@@ -207,6 +276,21 @@ pub fn sort(input: &Array, dim: u32, ascending: bool) -> Result<Array, AfError>
207276
}
208277
}
209278

279+
/// Sort the values in input Arrays
280+
///
281+
/// # Parameters
282+
///
283+
/// - `input` - Input Array
284+
/// - `dim` - Dimension along which to sort
285+
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
286+
///
287+
/// # Return Values
288+
///
289+
/// A tuple of Arrays.
290+
///
291+
/// The first Array contains the keys based on sorted values.
292+
///
293+
/// The second Array contains the original indices of the sorted values.
210294
#[allow(unused_mut)]
211295
pub fn sort_index(input: &Array, dim: u32, ascending: bool) -> Result<(Array, Array), AfError> {
212296
unsafe {
@@ -222,6 +306,24 @@ pub fn sort_index(input: &Array, dim: u32, ascending: bool) -> Result<(Array, Ar
222306
}
223307
}
224308

309+
/// Sort the values in input Arrays
310+
///
311+
/// Sort an multidimensional Array based on keys
312+
///
313+
/// # Parameters
314+
///
315+
/// - `keys` - Array with key values
316+
/// - `vals` - Array with input values
317+
/// - `dim` - Dimension along which to sort
318+
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
319+
///
320+
/// # Return Values
321+
///
322+
/// A tuple of Arrays.
323+
///
324+
/// The first Array contains the keys based on sorted values.
325+
///
326+
/// The second Array contains the sorted values.
225327
#[allow(unused_mut)]
226328
pub fn sort_by_key(keys: &Array, vals: &Array, dim: u32,
227329
ascending: bool) -> Result<(Array, Array), AfError> {
@@ -238,6 +340,16 @@ pub fn sort_by_key(keys: &Array, vals: &Array, dim: u32,
238340
}
239341
}
240342

343+
/// Find unique values from a Set
344+
///
345+
/// # Parameters
346+
///
347+
/// - `input` - Input Array
348+
/// - `is_sorted` - is a boolean variable. If ```True`` indicates, the `input` Array is sorted.
349+
///
350+
/// # Return Values
351+
///
352+
/// An Array of unique values from the input Array.
241353
#[allow(unused_mut)]
242354
pub fn set_unique(input: &Array, is_sorted: bool) -> Result<Array, AfError> {
243355
unsafe {
@@ -251,6 +363,17 @@ pub fn set_unique(input: &Array, is_sorted: bool) -> Result<Array, AfError> {
251363
}
252364
}
253365

366+
/// Find union of two sets
367+
///
368+
/// # Parameters
369+
///
370+
/// - `first` is one of the input sets
371+
/// - `second` is the other of the input sets
372+
/// - `is_unique` is a boolean value indicates if the input sets are unique
373+
///
374+
/// # Return Values
375+
///
376+
/// An Array with union of the input sets
254377
#[allow(unused_mut)]
255378
pub fn set_union(first: &Array, second: &Array, is_unique: bool) -> Result<Array, AfError> {
256379
unsafe {
@@ -264,6 +387,17 @@ pub fn set_union(first: &Array, second: &Array, is_unique: bool) -> Result<Array
264387
}
265388
}
266389

390+
/// Find intersection of two sets
391+
///
392+
/// # Parameters
393+
///
394+
/// - `first` is one of the input sets
395+
/// - `second` is the other of the input sets
396+
/// - `is_unique` is a boolean value indicates if the input sets are unique
397+
///
398+
/// # Return Values
399+
///
400+
/// An Array with intersection of the input sets
267401
#[allow(unused_mut)]
268402
pub fn set_intersect(first: &Array, second: &Array, is_unique: bool) -> Result<Array, AfError> {
269403
unsafe {

src/arith/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ extern {
9494
fn af_isnan(out: MutAfArray, arr: AfArray) -> c_int;
9595
}
9696

97+
/// Enables use of `!` on objects of type [Array](./struct.Array.html)
9798
impl<'f> Not for &'f Array {
9899
type Output = Array;
99100

src/array.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ extern {
6464
fn af_print_array(arr: AfArray) -> c_int;
6565
}
6666

67+
/// A multidimensional data container
68+
///
69+
/// Currently, Array objects can store only data until four dimensions
6770
pub struct Array {
6871
handle: i64,
6972
}
7073

7174
macro_rules! is_func {
7275
($fn_name: ident, $ffi_fn: ident) => (
76+
/// Checks if the Array is of specific format/data type.
7377
pub fn $fn_name(&self) -> Result<bool, AfError> {
7478
unsafe {
7579
let mut ret_val: i32 = 0;
@@ -84,6 +88,14 @@ macro_rules! is_func {
8488
}
8589

8690
impl Array {
91+
/// Constructs a new Array object
92+
///
93+
/// # Examples
94+
///
95+
/// ```
96+
/// let values: &[f32] = &[1.0, 2.0, 3.0];
97+
/// let indices = Array::new(Dim4::new(&[3, 1, 1, 1]), values, Aftype::F32).unwrap();
98+
/// ```
8799
#[allow(unused_mut)]
88100
pub fn new<T>(dims: Dim4, slice: &[T], aftype: Aftype) -> Result<Array, AfError> {
89101
unsafe {
@@ -100,6 +112,7 @@ impl Array {
100112
}
101113
}
102114

115+
/// Returns the number of elements in the Array
103116
pub fn elements(&self) -> Result<i64, AfError> {
104117
unsafe {
105118
let mut ret_val: i64 = 0;
@@ -111,6 +124,7 @@ impl Array {
111124
}
112125
}
113126

127+
/// Returns the Array data type
114128
pub fn get_type(&self) -> Result<Aftype, AfError> {
115129
unsafe {
116130
let mut ret_val: u8 = 0;
@@ -122,6 +136,7 @@ impl Array {
122136
}
123137
}
124138

139+
/// Returns the dimensions of the Array
125140
pub fn dims(&self) -> Result<Dim4, AfError> {
126141
unsafe {
127142
let mut ret0: i64 = 0;
@@ -138,6 +153,7 @@ impl Array {
138153
}
139154
}
140155

156+
/// Returns the number of dimensions of the Array
141157
pub fn numdims(&self) -> Result<u32, AfError> {
142158
unsafe {
143159
let mut ret_val: u32 = 0;
@@ -149,10 +165,12 @@ impl Array {
149165
}
150166
}
151167

168+
/// Returns the native FFI handle for Rust object `Array`
152169
pub fn get(&self) -> i64 {
153170
self.handle
154171
}
155172

173+
/// Copies the data from the Array to the mutable slice `data`
156174
pub fn host<T>(&self, data: &mut [T]) -> Result<(), AfError> {
157175
unsafe {
158176
let ret_val = af_get_data_ptr(data.as_mut_ptr() as *mut c_void, self.handle as AfArray);
@@ -163,6 +181,7 @@ impl Array {
163181
}
164182
}
165183

184+
/// Evaluates any pending lazy expressions that represent the data in the Array object
166185
pub fn eval(&self) -> Result<(), AfError> {
167186
unsafe {
168187
let ret_val = af_eval(self.handle as AfArray);
@@ -173,6 +192,9 @@ impl Array {
173192
}
174193
}
175194

195+
/// Makes an copy of the Array
196+
///
197+
/// Internally, this is handled by reference counting
176198
pub fn copy(&self) -> Result<Array, AfError> {
177199
unsafe {
178200
let mut temp: i64 = 0;
@@ -198,12 +220,14 @@ impl Array {
198220
is_func!(is_bool, af_is_bool);
199221
}
200222

223+
/// Used for creating Array object from native resource id
201224
impl From<i64> for Array {
202225
fn from(t: i64) -> Array {
203226
Array {handle: t}
204227
}
205228
}
206229

230+
/// Used for incrementing the reference count of Array's native resource
207231
impl Clone for Array {
208232
fn clone(&self) -> Array {
209233
unsafe {
@@ -217,6 +241,7 @@ impl Clone for Array {
217241
}
218242
}
219243

244+
/// To free resources when Array goes out of scope
220245
impl Drop for Array {
221246
fn drop(&mut self) {
222247
unsafe {
@@ -229,6 +254,29 @@ impl Drop for Array {
229254
}
230255
}
231256

257+
/// Print data in the Array
258+
///
259+
/// # Examples
260+
///
261+
/// ```
262+
/// println!("Create a 5-by-3 matrix of random floats on the GPU");
263+
/// let a = match randu(dims, Aftype::F32) {
264+
/// Ok(value) => value,
265+
/// Err(error) => panic!("{}", error),
266+
/// };
267+
/// print(&a);
268+
/// ```
269+
///
270+
/// The sample output will look like below:
271+
///
272+
/// ```
273+
/// [5 3 1 1]
274+
/// 0.7402 0.4464 0.7762
275+
/// 0.9210 0.6673 0.2948
276+
/// 0.0390 0.1099 0.7140
277+
/// 0.9690 0.4702 0.3585
278+
/// 0.9251 0.5132 0.6814
279+
/// ```
232280
pub fn print(input: &Array) -> Result<(), AfError> {
233281
unsafe {
234282
let ret_val = af_print_array(input.get() as AfArray);

0 commit comments

Comments
 (0)