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
use super::core::{
    af_array, dim_t, AfError, Array, FloatingPoint, HasAfEnum, SparseFormat, HANDLE_ERROR,
};

use libc::{c_int, c_uint, c_void};

extern "C" {
    fn af_create_sparse_array(
        out: *mut af_array,
        nRows: dim_t,
        nCols: dim_t,
        vals: af_array,
        rowIdx: af_array,
        colIdx: af_array,
        stype: c_uint,
    ) -> c_int;

    fn af_create_sparse_array_from_ptr(
        out: *mut af_array,
        nRows: dim_t,
        nCols: dim_t,
        nNZ: dim_t,
        values: *const c_void,
        rowIdx: *const c_int,
        colIdx: *const c_int,
        aftype: c_uint,
        stype: c_uint,
        src: c_uint,
    ) -> c_int;

    fn af_create_sparse_array_from_dense(
        out: *mut af_array,
        dense: af_array,
        stype: c_uint,
    ) -> c_int;

    fn af_sparse_convert_to(out: *mut af_array, input: af_array, dstStrge: c_uint) -> c_int;

    fn af_sparse_to_dense(out: *mut af_array, sparse: af_array) -> c_int;

    fn af_sparse_get_info(
        vals: *mut af_array,
        rIdx: *mut af_array,
        cIdx: *mut af_array,
        stype: *mut c_uint,
        input: af_array,
    ) -> c_int;

    fn af_sparse_get_values(out: *mut af_array, input: af_array) -> c_int;

    fn af_sparse_get_row_idx(out: *mut af_array, input: af_array) -> c_int;

    fn af_sparse_get_col_idx(out: *mut af_array, input: af_array) -> c_int;

    fn af_sparse_get_nnz(out: *mut dim_t, input: af_array) -> c_int;

    fn af_sparse_get_storage(out: *mut c_uint, input: af_array) -> c_int;
}

/// Create sprase matrix from arrays
///
/// This function converts [Array](./struct.Array.html) of `values` into sparse array
/// of `format` sparse format using arrays `row_indices` and `col_indices`.
///
/// # Parameters
///
/// - `rows` is the number of rows in the dense matrix
/// - `cols` is the number of columns in the dense matrix
/// - `values` is the \ref af::array containing the non-zero elements
///   `of the matrix
/// - `row_indices` is the row indices for the sparse array
/// - `col_indices` is the column indices for the sparse array
/// - `format` is the storage format of the sparse array
///
/// # Return Values
///
/// Array with data in given sparse format
///
/// # Note
///
/// This function only uses references of the input arrays to create the
/// sparse data structure and does not perform deep copies.
pub fn sparse<T>(
    rows: u64,
    cols: u64,
    values: &Array<T>,
    row_indices: &Array<i32>,
    col_indices: &Array<i32>,
    format: SparseFormat,
) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
{
    unsafe {
        let mut temp: af_array = std::ptr::null_mut();
        let err_val = af_create_sparse_array(
            &mut temp as *mut af_array,
            rows as dim_t,
            cols as dim_t,
            values.get(),
            row_indices.get(),
            col_indices.get(),
            format as c_uint,
        );
        HANDLE_ERROR(AfError::from(err_val));
        temp.into()
    }
}

/// Create sprase matrix from data on host memory
///
/// This function converts host array `values` into sparse array of `format` sparse
/// format using host arrays `row_indices` and `col_indices`.
///
/// # Parameters
///
/// - `rows` is the number of rows in the dense matrix
/// - `cols` is the number of columns in the dense matrix
/// - `nzz` is the number of non zero elements in the dense matrix
/// - `values` is the \ref af::array containing the non-zero elements
///   `of the matrix
/// - `row_indices` is the row indices for the sparse array
/// - `col_indices` is the column indices for the sparse array
/// - `format` is the storage format of the sparse array
///
/// # Return Values
///
/// Array with data in given sparse format
///
/// # Note
///
/// The rules for deep copy/shallow copy/reference are the same as for creating a
/// regular [Array](./struct.Array.html).
pub fn sparse_from_host<T>(
    rows: u64,
    cols: u64,
    nzz: u64,
    values: &[T],
    row_indices: &[i32],
    col_indices: &[i32],
    format: SparseFormat,
) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
{
    let aftype = T::get_af_dtype();
    unsafe {
        let mut temp: af_array = std::ptr::null_mut();
        let err_val = af_create_sparse_array_from_ptr(
            &mut temp as *mut af_array,
            rows as dim_t,
            cols as dim_t,
            nzz as dim_t,
            values.as_ptr() as *const c_void,
            row_indices.as_ptr() as *const c_int,
            col_indices.as_ptr() as *const c_int,
            aftype as c_uint,
            format as c_uint,
            1,
        );
        HANDLE_ERROR(AfError::from(err_val));
        temp.into()
    }
}

/// Convert dense array to sparse array
///
/// # Parameters
///
/// - `dense` is the dense format array
/// - `format` is the target sparse format
///
/// # Return Values
///
/// Sparse Array
pub fn sparse_from_dense<T>(dense: &Array<T>, format: SparseFormat) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
{
    unsafe {
        let mut temp: af_array = std::ptr::null_mut();
        let err_val = af_create_sparse_array_from_dense(
            &mut temp as *mut af_array,
            dense.get(),
            format as c_uint,
        );
        HANDLE_ERROR(AfError::from(err_val));
        temp.into()
    }
}

/// Convert between sparse formats
///
/// # Parameters
///
/// - `input` is the input sparse array
/// - `format` is the target sparse format
///
/// # Return Values
///
/// Sparse Array in targe sparse format.
pub fn sparse_convert_to<T>(input: &Array<T>, format: SparseFormat) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
{
    unsafe {
        let mut temp: af_array = std::ptr::null_mut();
        let err_val =
            af_sparse_convert_to(&mut temp as *mut af_array, input.get(), format as c_uint);
        HANDLE_ERROR(AfError::from(err_val));
        temp.into()
    }
}

/// Convert sparse array to dense array
///
/// # Parameters
///
/// - `input` is the sparse array
///
/// # Return Values
///
/// Dense Array
pub fn sparse_to_dense<T>(input: &Array<T>) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
{
    unsafe {
        let mut temp: af_array = std::ptr::null_mut();
        let err_val = af_sparse_to_dense(&mut temp as *mut af_array, input.get());
        HANDLE_ERROR(AfError::from(err_val));
        temp.into()
    }
}

/// Get sparse Array information
///
/// # Parameters
///
/// - `input` is the sparse array
///
/// # Return Values
///
/// A tuple of values, row indices, column indices Arrays and SparseFormat enum.
pub fn sparse_get_info<T>(input: &Array<T>) -> (Array<T>, Array<i32>, Array<i32>, SparseFormat)
where
    T: HasAfEnum + FloatingPoint,
{
    unsafe {
        let mut val: af_array = std::ptr::null_mut();
        let mut row: af_array = std::ptr::null_mut();
        let mut col: af_array = std::ptr::null_mut();
        let mut stype: u32 = 0;
        let err_val = af_sparse_get_info(
            &mut val as *mut af_array,
            &mut row as *mut af_array,
            &mut col as *mut af_array,
            &mut stype as *mut c_uint,
            input.get(),
        );
        HANDLE_ERROR(AfError::from(err_val));
        (
            val.into(),
            row.into(),
            col.into(),
            SparseFormat::from(stype),
        )
    }
}

/// Get values of sparse Array
///
/// # Parameters
///
/// - `input` is the sparse array
///
/// # Return Values
///
/// Sparse array values
pub fn sparse_get_values<T>(input: &Array<T>) -> Array<T>
where
    T: HasAfEnum + FloatingPoint,
{
    unsafe {
        let mut val: af_array = std::ptr::null_mut();
        let err_val = af_sparse_get_values(&mut val as *mut af_array, input.get());
        HANDLE_ERROR(AfError::from(err_val));
        val.into()
    }
}

/// Get row indices Array
///
/// # Parameters
///
/// - `input` is the sparse array
///
/// # Return Values
///
/// Array with row indices values of sparse Array
pub fn sparse_get_row_indices<T>(input: &Array<T>) -> Array<i32>
where
    T: HasAfEnum + FloatingPoint,
{
    unsafe {
        let mut val: af_array = std::ptr::null_mut();
        let err_val = af_sparse_get_row_idx(&mut val as *mut af_array, input.get());
        HANDLE_ERROR(AfError::from(err_val));
        val.into()
    }
}

/// Get cololumn indices Array
///
/// # Parameters
///
/// - `input` is the sparse array
///
/// # Return Values
///
/// Array with coloumn indices values of sparse Array
pub fn sparse_get_col_indices<T>(input: &Array<T>) -> Array<i32>
where
    T: HasAfEnum + FloatingPoint,
{
    unsafe {
        let mut val: af_array = std::ptr::null_mut();
        let err_val = af_sparse_get_col_idx(&mut val as *mut af_array, input.get());
        HANDLE_ERROR(AfError::from(err_val));
        val.into()
    }
}

/// Get number of non-zero elements in sparse array
///
/// # Parameters
///
/// - `input` is the sparse array
///
/// # Return Values
///
/// Number of non-zero elements of sparse Array
pub fn sparse_get_nnz<T: HasAfEnum>(input: &Array<T>) -> i64 {
    let mut count: i64 = 0;
    unsafe {
        let err_val = af_sparse_get_nnz(&mut count as *mut dim_t, input.get());
        HANDLE_ERROR(AfError::from(err_val));
    }
    count
}

/// Get sparse format
///
/// # Parameters
///
/// - `input` is the sparse array
///
/// # Return Values
///
/// Sparse array format
pub fn sparse_get_format<T: HasAfEnum>(input: &Array<T>) -> SparseFormat {
    let mut stype: u32 = 0;
    unsafe {
        let err_val = af_sparse_get_storage(&mut stype as *mut c_uint, input.get());
        HANDLE_ERROR(AfError::from(err_val));
    }
    SparseFormat::from(stype)
}