arrayfire 3.3.2

ArrayFire is a high performance software library for parallel computing with an easy-to-use API. Its array based function set makes parallel programming simple. ArrayFire's multiple backends (CUDA, OpenCL and native CPU) make it platform independent and highly portable. A few lines of code in ArrayFire can replace dozens of lines of parallel computing code, saving you valuable time and lowering development costs. This crate provides Rust bindings for ArrayFire library.
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
extern crate libc;

use array::Array;
use defines::AfError;
use error::HANDLE_ERROR;
use self::libc::{c_int, c_uint, c_double};

type MutAfArray = *mut self::libc::c_longlong;
type MutDouble  = *mut self::libc::c_double;
type MutUint    = *mut self::libc::c_uint;
type AfArray    = self::libc::c_longlong;

#[allow(dead_code)]
extern {
    fn af_sum(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_sum_nan(out: MutAfArray, input: AfArray, dim: c_int, nanval: c_double) -> c_int;
    fn af_product(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_product_nan(out: MutAfArray, input: AfArray, dim: c_int, val: c_double) -> c_int;
    fn af_min(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_max(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_all_true(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_any_true(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_count(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_sum_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_sum_nan_all(r: MutDouble, i: MutDouble, input: AfArray, val: c_double) -> c_int;
    fn af_product_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_product_nan_all(r: MutDouble, i: MutDouble, input: AfArray, val: c_double) -> c_int;
    fn af_min_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_max_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_all_true_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_any_true_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_count_all(r: MutDouble, i: MutDouble, input: AfArray) -> c_int;
    fn af_imin(out: MutAfArray, idx: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_imax(out: MutAfArray, idx: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_imin_all(r: MutDouble, i: MutDouble, idx: MutUint, input: AfArray) -> c_int;
    fn af_imax_all(r: MutDouble, i: MutDouble, idx: MutUint, input: AfArray) -> c_int;
    fn af_accum(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_where(out: MutAfArray, input: AfArray) -> c_int;
    fn af_diff1(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_diff2(out: MutAfArray, input: AfArray, dim: c_int) -> c_int;
    fn af_sort(out: MutAfArray, input: AfArray, dim: c_uint, ascend: c_int) -> c_int;
    fn af_sort_index(o: MutAfArray, i: MutAfArray, inp: AfArray, d: c_uint, a: c_int) -> c_int;
    fn af_set_unique(out: MutAfArray, input: AfArray, is_sorted: c_int) -> c_int;
    fn af_set_union(out: MutAfArray, first: AfArray, second: AfArray, is_unq: c_int) -> c_int;
    fn af_set_intersect(out: MutAfArray, one: AfArray, two: AfArray, is_unq: c_int) -> c_int;

    fn af_sort_by_key(out_keys: MutAfArray, out_vals: MutAfArray,
                      in_keys: AfArray, in_vals: AfArray, dim: c_uint, ascend: c_int) -> c_int;
}

macro_rules! dim_reduce_func_def {
    ($fn_name: ident, $ffi_name: ident) => (
        /// Reduction operation along specific dimension
        ///
        /// # Parameters
        ///
        /// - `input` - Input Array
        /// - `dim` - Dimension along which the input Array will be reduced
        ///
        /// # Return Values
        ///
        /// Reduced Array
        #[allow(unused_mut)]
        pub fn $fn_name(input: &Array, dim: i32) -> Array {
            unsafe {
                let mut temp: i64 = 0;
                let err_val = $ffi_name(&mut temp as MutAfArray,
                                        input.get() as AfArray, dim as c_int);
                HANDLE_ERROR(AfError::from(err_val));
                Array::from(temp)
            }
        }
    )
}

dim_reduce_func_def!(sum, af_sum);
dim_reduce_func_def!(product, af_product);
dim_reduce_func_def!(min, af_min);
dim_reduce_func_def!(max, af_max);
dim_reduce_func_def!(all_true, af_all_true);
dim_reduce_func_def!(any_true, af_any_true);
dim_reduce_func_def!(count, af_count);
dim_reduce_func_def!(accum, af_accum);
dim_reduce_func_def!(diff1, af_diff1);
dim_reduce_func_def!(diff2, af_diff2);

/// Reduction operation along specific dimension
///
/// Sum values of the `input` Array along `dim` dimension after replacing any `NAN` values in the
/// Array with `nanval` value.
///
/// # Parameters
///
/// - `input` is the input Array
/// - `dim` is reduction dimension
/// - `nanval` is value with which all the `NAN` values of Array are replaced with
///
/// # Return Values
///
/// Reduced Array
pub fn sum_nan(input: &Array, dim: i32, nanval: f64) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_sum_nan(&mut temp as MutAfArray, input.get() as AfArray,
                                 dim as c_int, nanval as c_double);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Reduction operation along specific dimension
///
/// Compute product of the values of the `input` Array along `dim` dimension after replacing any `NAN` values in the Array with `nanval` value.
///
/// # Parameters
///
/// - `input` is the input Array
/// - `dim` is reduction dimension
/// - `nanval` is value with which all the `NAN` values of Array are replaced with
///
/// # Return Values
///
/// Reduced Array
pub fn product_nan(input: &Array, dim: i32, nanval: f64) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_product_nan(&mut temp as MutAfArray, input.get() as AfArray,
                                     dim as c_int, nanval as c_double);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

macro_rules! all_reduce_func_def {
    ($fn_name: ident, $ffi_name: ident) => (
        /// Reduction operation of all values
        ///
        /// # Parameters
        ///
        /// - `input` is the input Array
        ///
        /// # Return Values
        ///
        /// A tuple of reduction result. For non-complex data type Arrays, second value of tuple is
        /// zero.
        #[allow(unused_mut)]
        pub fn $fn_name(input: &Array) -> (f64, f64) {
            unsafe {
                let mut real: f64 = 0.0;
                let mut imag: f64 = 0.0;
                let err_val = $ffi_name(&mut real as MutDouble, &mut imag as MutDouble,
                                        input.get() as AfArray);
                HANDLE_ERROR(AfError::from(err_val));
                (real, imag)
            }
        }
    )
}

all_reduce_func_def!(sum_all, af_sum_all);
all_reduce_func_def!(product_all, af_product_all);
all_reduce_func_def!(min_all, af_min_all);
all_reduce_func_def!(max_all, af_max_all);
all_reduce_func_def!(all_true_all, af_all_true_all);
all_reduce_func_def!(any_true_all, af_any_true_all);
all_reduce_func_def!(count_all, af_count_all);

/// Reduction operation of all values
///
/// Sum all the values of the `input` Array after replacing any `NAN` values with `val`.
///
/// # Parameters
///
/// - `input` is the input Array
/// - `val` is the val that replaces all `NAN` values of the Array before reduction operation is
/// performed.
///
/// # Return Values
///
/// A tuple of reduction result. For non-complex data type Arrays, second value of tuple is
/// zero.
pub fn sum_nan_all(input: &Array, val: f64) -> (f64, f64) {
    unsafe {
        let mut real: f64 = 0.0;
        let mut imag: f64 = 0.0;
        let err_val = af_sum_nan_all(&mut real as MutDouble, &mut imag as MutDouble,
                                     input.get() as AfArray, val as c_double);
        HANDLE_ERROR(AfError::from(err_val));
        (real, imag)
    }
}

/// Reduction operation of all values
///
/// Compute the product of all the values of the `input` Array after replacing any `NAN` values with `val`.
///
/// # Parameters
///
/// - `input` is the input Array
/// - `val` is the val that replaces all `NAN` values of the Array before reduction operation is
/// performed.
///
/// # Return Values
///
/// A tuple of reduction result. For non-complex data type Arrays, second value of tuple is
/// zero.
pub fn product_nan_all(input: &Array, val: f64) -> (f64, f64) {
    unsafe {
        let mut real: f64 = 0.0;
        let mut imag: f64 = 0.0;
        let err_val = af_product_nan_all(&mut real as MutDouble, &mut imag as MutDouble,
                                         input.get() as AfArray, val as c_double);
        HANDLE_ERROR(AfError::from(err_val));
        (real, imag)
    }
}

macro_rules! dim_ireduce_func_def {
    ($fn_name: ident, $ffi_name: ident) => (
        /// Reduction operation along specific dimension
        ///
        /// # Parameters
        ///
        /// - `input` - Input Array
        /// - `dim` - Dimension along which the input Array will be reduced
        ///
        /// # Return Values
        ///
        /// A tuple of Arrays: Reduced Array and Indices Array.
        ///
        /// The indices Array has the index of the result element along the reduction dimension.
        #[allow(unused_mut)]
        pub fn $fn_name(input: &Array, dim: i32) -> (Array, Array) {
            unsafe {
                let mut temp: i64 = 0;
                let mut idx: i64 = 0;
                let err_val = $ffi_name(&mut temp as MutAfArray, &mut idx as MutAfArray,
                                        input.get() as AfArray, dim as c_int);
                HANDLE_ERROR(AfError::from(err_val));
                (Array::from(temp), Array::from(idx))
            }
        }
    )
}

dim_ireduce_func_def!(imin, af_imin);
dim_ireduce_func_def!(imax, af_imax);

macro_rules! all_ireduce_func_def {
    ($fn_name: ident, $ffi_name: ident) => (
        /// Reduction operation of all values
        ///
        /// # Parameters
        ///
        /// `input` - Input Array
        ///
        /// # Return Values
        ///
        /// A triplet of reduction result.
        ///
        /// The second value of the tuple is zero for non-complex data type Arrays.
        ///
        /// The third value of triplet is the index of result element from reduction operation.
        #[allow(unused_mut)]
        pub fn $fn_name(input: &Array) -> (f64, f64, u32) {
            unsafe {
                let mut real: f64 = 0.0;
                let mut imag: f64 = 0.0;
                let mut temp: u32 = 0;
                let err_val = $ffi_name(&mut real as MutDouble, &mut imag as MutDouble,
                                        &mut temp as MutUint, input.get() as AfArray);
                HANDLE_ERROR(AfError::from(err_val));
                (real, imag, temp)
            }
        }
    )
}

all_ireduce_func_def!(imin_all, af_imin_all);
all_ireduce_func_def!(imax_all, af_imax_all);

/// Locate the indices of non-zero elements.
///
/// The locations are provided by flattening the input into a linear array.
///
/// # Parameters
///
/// - `input` - Input Array
///
/// # Return Values
///
/// Array of indices where the input Array has non-zero values.
#[allow(unused_mut)]
pub fn locate(input: &Array) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_where(&mut temp as MutAfArray, input.get() as AfArray);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Sort the values in input Arrays
///
/// Sort an multidimensional Array along a given dimension
///
/// # Parameters
///
/// - `input` - Input Array
/// - `dim` - Dimension along which to sort
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
///
/// # Return Values
///
/// Sorted Array.
#[allow(unused_mut)]
pub fn sort(input: &Array, dim: u32, ascending: bool) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_sort(&mut temp as MutAfArray, input.get() as AfArray,
                              dim as c_uint, ascending as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Sort the values in input Arrays
///
/// # Parameters
///
/// - `input` - Input Array
/// - `dim` - Dimension along which to sort
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
///
/// # Return Values
///
/// A tuple of Arrays.
///
/// The first Array contains the keys based on sorted values.
///
/// The second Array contains the original indices of the sorted values.
#[allow(unused_mut)]
pub fn sort_index(input: &Array, dim: u32, ascending: bool) -> (Array, Array) {
    unsafe {
        let mut temp: i64 = 0;
        let mut idx: i64 = 0;
        let err_val = af_sort_index(&mut temp as MutAfArray, &mut idx as MutAfArray,
                                    input.get() as AfArray,
                                    dim as c_uint, ascending as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        (Array::from(temp), Array::from(idx))
    }
}

/// Sort the values in input Arrays
///
/// Sort an multidimensional Array based on keys
///
/// # Parameters
///
/// - `keys` - Array with key values
/// - `vals` - Array with input values
/// - `dim` - Dimension along which to sort
/// - `ascending` - Sorted output will have ascending values if ```True``` and descending order otherwise.
///
/// # Return Values
///
/// A tuple of Arrays.
///
/// The first Array contains the keys based on sorted values.
///
/// The second Array contains the sorted values.
#[allow(unused_mut)]
pub fn sort_by_key(keys: &Array, vals: &Array, dim: u32,
                   ascending: bool) -> (Array, Array) {
    unsafe {
        let mut temp: i64 = 0;
        let mut temp2: i64 = 0;
        let err_val = af_sort_by_key(&mut temp as MutAfArray, &mut temp2 as MutAfArray,
                                     keys.get() as AfArray, vals.get() as AfArray,
                                     dim as c_uint, ascending as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        (Array::from(temp), Array::from(temp2))
    }
}

/// Find unique values from a Set
///
/// # Parameters
///
/// - `input` - Input Array
/// - `is_sorted` - is a boolean variable. If ```True`` indicates, the `input` Array is sorted.
///
/// # Return Values
///
/// An Array of unique values from the input Array.
#[allow(unused_mut)]
pub fn set_unique(input: &Array, is_sorted: bool) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_set_unique(&mut temp as MutAfArray, input.get() as AfArray,
                                    is_sorted as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Find union of two sets
///
/// # Parameters
///
/// - `first` is one of the input sets
/// - `second` is the other of the input sets
/// - `is_unique` is a boolean value indicates if the input sets are unique
///
/// # Return Values
///
/// An Array with union of the input sets
#[allow(unused_mut)]
pub fn set_union(first: &Array, second: &Array, is_unique: bool) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_set_union(&mut temp as MutAfArray, first.get() as AfArray,
                                   second.get() as AfArray, is_unique as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}

/// Find intersection of two sets
///
/// # Parameters
///
/// - `first` is one of the input sets
/// - `second` is the other of the input sets
/// - `is_unique` is a boolean value indicates if the input sets are unique
///
/// # Return Values
///
/// An Array with intersection of the input sets
#[allow(unused_mut)]
pub fn set_intersect(first: &Array, second: &Array, is_unique: bool) -> Array {
    unsafe {
        let mut temp: i64 = 0;
        let err_val = af_set_intersect(&mut temp as MutAfArray, first.get() as AfArray,
                                       second.get() as AfArray, is_unique as c_int);
        HANDLE_ERROR(AfError::from(err_val));
        Array::from(temp)
    }
}