Skip to content

Commit 666e019

Browse files
committed
Documentation for Array object and all enums
1 parent d08ac2b commit 666e019

File tree

2 files changed

+109
-32
lines changed

2 files changed

+109
-32
lines changed

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);

src/defines.rs

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,44 @@ use std::fmt::Error as FmtError;
55
#[repr(C)]
66
#[derive(Clone, Copy, Debug)]
77
pub enum AfError {
8-
///
98
/// The function returned successfully
10-
///
119
SUCCESS = 0,
1210
// 100-199 Errors in environment
13-
///
1411
/// The system or device ran out of memory
15-
///
1612
ERR_NO_MEM = 101,
17-
///
1813
/// There was an error in the device driver
19-
///
2014
ERR_DRIVER = 102,
21-
///
2215
/// There was an error with the runtime environment
23-
///
2416
ERR_RUNTIME = 103,
2517
// 200-299 Errors in input parameters
26-
///
2718
/// The input array is not a valid af_array object
28-
///
2919
ERR_INVALID_ARRAY = 201,
30-
///
3120
/// One of the function arguments is incorrect
32-
///
3321
ERR_ARG = 202,
34-
///
3522
/// The size is incorrect
36-
///
3723
ERR_SIZE = 203,
38-
///
3924
/// The type is not suppported by this function
40-
///
4125
ERR_TYPE = 204,
42-
///
4326
/// The type of the input arrays are not compatible
44-
///
4527
ERR_DIFF_TYPE = 205,
46-
///
4728
/// Function does not support GFOR / batch mode
48-
///
4929
ERR_BATCH = 207,
5030
// 300-399 Errors for missing software features
51-
///
5231
/// The option is not supported
53-
///
5432
ERR_NOT_SUPPORTED = 301,
55-
///
5633
/// This build of ArrayFire does not support this feature
57-
///
5834
ERR_NOT_CONFIGURED = 302,
5935
// 400-499 Errors for missing hardware features
60-
///
6136
/// This device does not support double
62-
///
6337
ERR_NO_DBL = 401,
64-
///
6538
/// This build of ArrayFire was not built with graphics or this device does
6639
/// not support graphics
67-
///
6840
ERR_NO_GFX = 402,
6941
// 900-999 Errors from upstream libraries and runtimes
70-
///
7142
/// There was an internal error either in ArrayFire or in a project
7243
/// upstream
73-
///
7444
ERR_INTERNAL = 998,
75-
///
7645
/// Unknown Error
77-
///
7846
ERR_UNKNOWN = 999
7947
}
8048

@@ -109,107 +77,168 @@ impl Error for AfError {
10977

11078
#[derive(Copy, Clone)]
11179
pub enum Aftype {
80+
/// 32 bit float
11281
F32 = 0,
82+
/// 32 bit complex float
11383
C32 = 1,
84+
/// 64 bit float
11485
F64 = 2,
86+
/// 64 bit complex float
11587
C64 = 3,
88+
/// 8 bit boolean
11689
B8 = 4,
90+
/// 32 bit signed integer
11791
S32 = 5,
92+
/// 32 bit unsigned integer
11893
U32 = 6,
94+
/// 8 bit unsigned integer
11995
U8 = 7,
96+
/// 64 bit signed integer
12097
S64 = 8,
98+
/// 64 bit unsigned integer
12199
U64 = 9,
122100
}
123101

124102
#[derive(Copy, Clone)]
125103
pub enum InterpType {
104+
/// Nearest Neighbor interpolation method
126105
NEAREST = 0,
106+
/// Linear interpolation method
127107
LINEAR = 1,
108+
/// Bilinear interpolation method
128109
BILINEAR= 2,
110+
/// Cubic interpolation method
129111
CUBIC = 3,
130112
}
131113

132114
#[derive(Copy, Clone)]
133115
pub enum BorderType {
116+
/// Pad using zeros
134117
ZERO = 0,
118+
/// Pad using mirrored values along border
135119
SYMMETRIC = 1,
136120
}
137121

138122
#[derive(Copy, Clone)]
139123
pub enum Connectivity {
124+
/// North-East-South-West (N-E-S-W) connectivity from given pixel/point
140125
FOUR = 4,
126+
/// N-NE-E-SE-S-SW-W-NW connectivity from given pixel/point
141127
EIGHT = 8
142128
}
143129

144130
#[derive(Copy, Clone)]
145131
pub enum ConvMode {
132+
/// Default convolution mode where output size is same as input size
146133
DEFAULT = 0,
134+
/// Output of convolution is expanded based on signal and filter sizes
147135
EXPAND = 1,
148136
}
149137

150138
#[derive(Copy, Clone)]
151139
pub enum ConvDomain {
140+
/// ArrayFire chooses whether the convolution will be in spatial domain or frequency domain
152141
AUTO = 0,
142+
/// Convoltion in spatial domain
153143
SPATIAL = 1,
144+
/// Convolution in frequency domain
154145
FREQUENCY= 2,
155146
}
156147

157148
#[derive(Copy, Clone)]
158149
pub enum MatchType {
150+
/// Sum of Absolute Differences
159151
SAD = 0,
152+
/// Zero-mean Sum of Absolute Differences
160153
ZSAD= 1,
154+
/// Locally scaled Sum of Absolute Differences
161155
LSAD= 2,
156+
/// Sum of Squared Differences
162157
SSD = 3,
158+
/// Zero-mean Sum of Squared Differences
163159
ZSSD= 4,
160+
/// Localy scaled Sum of Squared Differences
164161
LSSD= 5,
162+
/// Normalized Cross Correlation
165163
NCC = 6,
164+
/// Zero-mean Normalized Cross Correlation
166165
ZNCC= 7,
166+
/// Sum of Hamming Distances
167167
SHD = 8,
168168
}
169169

170170
#[derive(Copy, Clone)]
171171
pub enum ColorSpace {
172+
/// Grayscale color space
172173
GRAY = 0,
174+
/// Red-Green-Blue color space
173175
RGB = 1,
176+
/// Hue-Saturation-value color space
174177
HSV = 2,
175178
}
176179

177180
#[derive(Copy, Clone)]
178181
pub enum MatProp {
182+
/// Default (no-op)
179183
NONE,
184+
/// Data needs to be transposed
180185
TRANS,
186+
/// Data needs to be conjugate transposed
181187
CTRANS,
188+
/// Matrix is upper triangular
182189
UPPER,
190+
/// Matrix is lower triangular
183191
LOWER,
192+
/// Matrix diagonal has unitary values
184193
DIAGUNIT,
194+
/// Matrix is symmetric
185195
SYM,
196+
/// Matrix is positive definite
186197
POSDEF,
198+
/// Matrix is orthogonal
187199
ORTHOG,
200+
/// Matrix is tri-diagonal
188201
TRIDIAG,
202+
/// Matrix is block-diagonal
189203
BLOCKDIAG,
190204
}
191205

192206
#[allow(non_camel_case_types)]
193207
#[derive(Copy, Clone)]
194208
pub enum NormType {
209+
/// Treats input as a vector and return sum of absolute values
195210
VECTOR_1 = 0,
211+
/// Treats input as vector and return max of absolute values
196212
VECTOR_INF = 1,
213+
/// Treats input as vector and returns euclidean norm
197214
VECTOR_2 = 2,
215+
/// Treats input as vector and returns the p-norm
198216
VECTOR_P = 3,
217+
/// Return the max of column sums
199218
MATRIX_1 = 4,
219+
/// Return the max of row sums
200220
MATRIX_INF = 5,
221+
/// Returns the max singular value (Currently not supported)
201222
MATRIX_2 = 6,
223+
/// Returns Lpq-norm
202224
MATRIX_L_PQ = 7,
203225
}
204226

205227
#[repr(C)]
206228
#[derive(Copy, Clone)]
207229
pub enum ColorMap {
230+
/// Default color map is grayscale range [0-1]
208231
DEFAULT = 0,
232+
/// Visible spectrum color map
209233
SPECTRUM= 1,
234+
/// Colors
210235
COLORS = 2,
236+
/// Red hue map
211237
RED = 3,
238+
/// Mood color map
212239
MOOD = 4,
240+
/// Heat color map
213241
HEAT = 5,
242+
/// Blue hue map
214243
BLUE = 6,
215244
}

0 commit comments

Comments
 (0)