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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
use super::Context;
use crate::ffi::*;
use crate::{Error, API};

impl API {
    // TODO: cublasIsamax_v2 x 4
    // TODO: cublasIsamin_v2 x 4

    // TODO: cublasSasum_v2 x 4

    /// Compute the sum of magnitudes of the provided vector elements.
    ///
    /// `x`: pointer to input vector.
    /// `result`: pointer to output scalar.
    /// `n`: number of elements to compute sum over (should not be greater than `x`).
    /// `stride`: offset from one input element to the next. Defaults to `1`.
    pub fn asum(
        context: &Context,
        x: *mut f32,
        result: *mut f32,
        n: i32,
        stride: Option<i32>,
    ) -> Result<(), Error> {
        let stride_x = stride.unwrap_or(1);
        unsafe { Self::ffi_sasum(*context.id_c(), n, x, stride_x, result) }
    }

    unsafe fn ffi_sasum(
        handle: cublasHandle_t,
        n: i32,
        x: *mut f32,
        incx: i32,
        result: *mut f32,
    ) -> Result<(), Error> {
        match cublasSasum_v2(handle, n, x, incx, result) {
            cublasStatus_t::CUBLAS_STATUS_SUCCESS => Ok(()),
            cublasStatus_t::CUBLAS_STATUS_NOT_INITIALIZED => Err(Error::NotInitialized),
            cublasStatus_t::CUBLAS_STATUS_ALLOC_FAILED => Err(Error::AllocFailed),
            cublasStatus_t::CUBLAS_STATUS_ARCH_MISMATCH => Err(Error::ArchMismatch),
            cublasStatus_t::CUBLAS_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed),
            status => Err(Error::Unknown(
                "Unable to calculate sum of x.",
                status as i32 as u64,
            )),
        }
    }

    // TODO: cublasSaxpy_v2 x 4

    /// Computes a vector-scalar product and adds the result to a vector.
    ///
    /// `alpha`: pointer to input scalar.
    /// `x`: pointer to input vector.
    /// `y`: pointer to output vector.
    /// `n`: number of elements to use for operation (should not be greater than number of elements in `x` or `y`).
    /// `stride_x`: offset from one element in x to the next. Defaults to `1`.
    /// `stride_y`: offset from one element in y to the next. Defaults to `1`.
    pub fn axpy(
        context: &Context,
        alpha: *mut f32,
        x: *mut f32,
        y: *mut f32,
        n: i32,
        stride_x: Option<i32>,
        stride_y: Option<i32>,
    ) -> Result<(), Error> {
        let stride_x = stride_x.unwrap_or(1);
        let stride_y = stride_y.unwrap_or(1);
        unsafe { Self::ffi_saxpy(*context.id_c(), n, alpha, x, stride_x, y, stride_y) }
    }

    unsafe fn ffi_saxpy(
        handle: cublasHandle_t,
        n: i32,
        alpha: *mut f32,
        x: *mut f32,
        incx: i32,
        y: *mut f32,
        incy: i32,
    ) -> Result<(), Error> {
        match cublasSaxpy_v2(handle, n, alpha, x, incx, y, incy) {
            cublasStatus_t::CUBLAS_STATUS_SUCCESS => Ok(()),
            cublasStatus_t::CUBLAS_STATUS_NOT_INITIALIZED => Err(Error::NotInitialized),
            cublasStatus_t::CUBLAS_STATUS_ARCH_MISMATCH => Err(Error::ArchMismatch),
            cublasStatus_t::CUBLAS_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed),
            status => Err(Error::Unknown(
                "Unable to calculate axpy (alpha * x + y).",
                status as i32 as u64,
            )),
        }
    }

    // TODO: cublasScopy_v2 x 4

    /// Copies a vector into another vector.
    ///
    /// `x`: pointer to input vector.
    /// `y`: pointer to output vector.
    /// `n`: number of elements to use for operation (should not be greater than number of elements in `x` or `y`).
    /// `stride_x`: offset from one element in x to the next. Defaults to `1`.
    /// `stride_y`: offset from one element in y to the next. Defaults to `1`.
    pub fn copy(
        context: &Context,
        x: *mut f32,
        y: *mut f32,
        n: i32,
        stride_x: Option<i32>,
        stride_y: Option<i32>,
    ) -> Result<(), Error> {
        let stride_x = stride_x.unwrap_or(1);
        let stride_y = stride_y.unwrap_or(1);
        unsafe { Self::ffi_scopy(*context.id_c(), n, x, stride_x, y, stride_y) }
    }

    unsafe fn ffi_scopy(
        handle: cublasHandle_t,
        n: i32,
        x: *mut f32,
        incx: i32,
        y: *mut f32,
        incy: i32,
    ) -> Result<(), Error> {
        match cublasScopy_v2(handle, n, x, incx, y, incy) {
            cublasStatus_t::CUBLAS_STATUS_SUCCESS => Ok(()),
            cublasStatus_t::CUBLAS_STATUS_NOT_INITIALIZED => Err(Error::NotInitialized),
            cublasStatus_t::CUBLAS_STATUS_ARCH_MISMATCH => Err(Error::ArchMismatch),
            cublasStatus_t::CUBLAS_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed),
            status => Err(Error::Unknown(
                "Unable to calculate copy from x to y.",
                status as i32 as u64,
            )),
        }
    }

    // TODO: cublasSdot_v2 x 6

    /// TODO: DOC
    pub fn dot(
        context: &Context,
        x: *mut f32,
        y: *mut f32,
        result: *mut f32,
        n: i32,
        stride_x: Option<i32>,
        stride_y: Option<i32>,
    ) -> Result<(), Error> {
        let stride_x = stride_x.unwrap_or(1);
        let stride_y = stride_y.unwrap_or(1);
        unsafe { Self::ffi_sdot(*context.id_c(), n, x, stride_x, y, stride_y, result) }
    }

    unsafe fn ffi_sdot(
        handle: cublasHandle_t,
        n: i32,
        x: *mut f32,
        incx: i32,
        y: *mut f32,
        incy: i32,
        result: *mut f32,
    ) -> Result<(), Error> {
        match cublasSdot_v2(handle, n, x, incx, y, incy, result) {
            cublasStatus_t::CUBLAS_STATUS_SUCCESS => Ok(()),
            cublasStatus_t::CUBLAS_STATUS_NOT_INITIALIZED => Err(Error::NotInitialized),
            cublasStatus_t::CUBLAS_STATUS_ARCH_MISMATCH => Err(Error::ArchMismatch),
            cublasStatus_t::CUBLAS_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed),
            status => Err(Error::Unknown(
                "Unable to calculate dot product of x and y.",
                status as i32 as u64,
            )),
        }
    }

    // TODO: cublasSnrm2_v2 x 4

    /// TODO: DOC
    pub fn nrm2(
        context: &Context,
        x: *mut f32,
        result: *mut f32,
        n: i32,
        stride_x: Option<i32>,
    ) -> Result<(), Error> {
        let stride_x = stride_x.unwrap_or(1);
        unsafe { Self::ffi_snrm2(*context.id_c(), n, x, stride_x, result) }
    }

    unsafe fn ffi_snrm2(
        handle: cublasHandle_t,
        n: i32,
        x: *mut f32,
        incx: i32,
        result: *mut f32,
    ) -> Result<(), Error> {
        match cublasSnrm2_v2(handle, n, x, incx, result) {
            cublasStatus_t::CUBLAS_STATUS_SUCCESS => Ok(()),
            cublasStatus_t::CUBLAS_STATUS_NOT_INITIALIZED => Err(Error::NotInitialized),
            cublasStatus_t::CUBLAS_STATUS_ALLOC_FAILED => {
                dbg!("Alloc failed");
                Err(Error::AllocFailed)
            }
            cublasStatus_t::CUBLAS_STATUS_ARCH_MISMATCH => Err(Error::ArchMismatch),
            cublasStatus_t::CUBLAS_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed),
            status => {
                dbg!("Unknown!");
                Err(Error::Unknown(
                    "Unable to calculate the euclidian norm of x.",
                    status as i32 as u64,
                ))
            }
        }
    }

    // TODO: cublasSrot_v2 x 6
    // TODO: cublasSrotg_v2 x 4
    // TODO: cublasSrotm_v2 x 2
    // TODO: cublasSrotmg_v2 x 2

    // TODO: cublasSscal_v2 x 6

    /// TODO: DOC
    pub fn scal(
        context: &Context,
        alpha: *mut f32,
        x: *mut f32,
        n: i32,
        stride_x: Option<i32>,
    ) -> Result<(), Error> {
        let stride_x = stride_x.unwrap_or(1);
        unsafe { Self::ffi_sscal(*context.id_c(), n, alpha, x, stride_x) }
    }

    unsafe fn ffi_sscal(
        handle: cublasHandle_t,
        n: i32,
        alpha: *mut f32,
        x: *mut f32,
        incx: i32,
    ) -> Result<(), Error> {
        match cublasSscal_v2(handle, n, alpha, x, incx) {
            cublasStatus_t::CUBLAS_STATUS_SUCCESS => Ok(()),
            cublasStatus_t::CUBLAS_STATUS_NOT_INITIALIZED => Err(Error::NotInitialized),
            cublasStatus_t::CUBLAS_STATUS_ARCH_MISMATCH => Err(Error::ArchMismatch),
            cublasStatus_t::CUBLAS_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed),
            status => Err(Error::Unknown(
                "Unable to scale the vector x.",
                status as i32 as u64,
            )),
        }
    }

    // TODO: cublasSswap_v2 x 4

    /// TODO: DOC
    pub fn swap(
        context: &Context,
        x: *mut f32,
        y: *mut f32,
        n: i32,
        stride_x: Option<i32>,
        stride_y: Option<i32>,
    ) -> Result<(), Error> {
        let stride_x = stride_x.unwrap_or(1);
        let stride_y = stride_y.unwrap_or(1);
        unsafe { Self::ffi_sswap(*context.id_c(), n, x, stride_x, y, stride_y) }
    }

    unsafe fn ffi_sswap(
        handle: cublasHandle_t,
        n: i32,
        x: *mut f32,
        incx: i32,
        y: *mut f32,
        incy: i32,
    ) -> Result<(), Error> {
        match cublasSswap_v2(handle, n, x, incx, y, incy) {
            cublasStatus_t::CUBLAS_STATUS_SUCCESS => Ok(()),
            cublasStatus_t::CUBLAS_STATUS_NOT_INITIALIZED => Err(Error::NotInitialized),
            cublasStatus_t::CUBLAS_STATUS_ARCH_MISMATCH => Err(Error::ArchMismatch),
            cublasStatus_t::CUBLAS_STATUS_EXECUTION_FAILED => Err(Error::ExecutionFailed),
            status => Err(Error::Unknown(
                "Unable to swap vector x and y.",
                status as i32 as u64,
            )),
        }
    }
}

#[cfg(test)]
mod test {
    use crate::api::context::Context;
    use crate::api::enums::PointerMode;
    use crate::chore::*;
    use crate::co::tensor::SharedTensor;
    use crate::API;

    #[test]
    fn use_cuda_memory_for_asum() {
        test_setup();

        let native = get_native_backend();
        let cuda = get_cuda_backend();

        // set up input
        let n = 20i32;
        let val = 2f32;
        let x = filled_tensor(&native, n as usize, val);

        // set up result
        let mut result = SharedTensor::<f32>::new(&vec![1]);

        {
            let cuda_mem = x.read(cuda.device()).unwrap();
            let cuda_mem_result = result.write_only(cuda.device()).unwrap();
            let mut ctx = Context::new().unwrap();
            ctx.set_pointer_mode(PointerMode::Device).unwrap();
            unsafe {
                let x_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem.id_c());
                let res_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_result.id_c());
                API::ffi_sasum(*ctx.id_c(), n, x_addr, 1, res_addr).unwrap();
            }
        }

        let native_res = result.read(native.device()).unwrap();
        assert_eq!(&[40f32], native_res.as_slice::<f32>());

        test_teardown();
    }

    #[test]
    fn use_cuda_memory_for_axpy() {
        test_setup();

        let native = get_native_backend();
        let cuda = get_cuda_backend();

        // set up alpha
        let alpha = filled_tensor(&native, 1, 1.5f32);

        // set up x
        let n = 5i32;
        let val = 2f32;
        let x = filled_tensor(&native, n as usize, val);

        // set up y
        let val = 4f32;
        let mut y = filled_tensor(&native, n as usize, val);

        {
            let cuda_mem_alpha = alpha.read(cuda.device()).unwrap();
            let cuda_mem_x = x.read(cuda.device()).unwrap();
            let cuda_mem_y = y.read_write(cuda.device()).unwrap();
            let mut ctx = Context::new().unwrap();
            ctx.set_pointer_mode(PointerMode::Device).unwrap();
            unsafe {
                let alpha_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_alpha.id_c());
                let x_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_x.id_c());
                let y_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_y.id_c());
                API::ffi_saxpy(*ctx.id_c(), n, alpha_addr, x_addr, 1, y_addr, 1).unwrap();
            }
        }

        let native_y = y.read(native.device()).unwrap();
        assert_eq!(&[7f32, 7f32, 7f32, 7f32, 7f32], native_y.as_slice::<f32>());

        test_teardown();
    }

    #[test]
    fn use_cuda_memory_for_copy() {
        test_setup();

        let native = get_native_backend();
        let cuda = get_cuda_backend();

        // set up x
        let n = 5i32;
        let val = 2f32;
        let x = filled_tensor(&native, n as usize, val);

        // set up y
        let val = 4f32;
        let mut y = filled_tensor(&native, n as usize, val);

        {
            let cuda_mem_x = x.read(cuda.device()).unwrap();
            let cuda_mem_y = y.write_only(cuda.device()).unwrap();
            let mut ctx = Context::new().unwrap();
            ctx.set_pointer_mode(PointerMode::Device).unwrap();
            unsafe {
                let x_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_x.id_c());
                let y_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_y.id_c());
                API::ffi_scopy(*ctx.id_c(), n, x_addr, 1, y_addr, 1).unwrap();
            }
        }

        let native_y = y.read(native.device()).unwrap();
        assert_eq!(&[2f32, 2f32, 2f32, 2f32, 2f32], native_y.as_slice::<f32>());

        test_teardown();
    }

    #[test]
    fn use_cuda_memory_for_dot() {
        test_setup();

        let native = get_native_backend();
        let cuda = get_cuda_backend();

        // set up x
        let n = 5i32;
        let val = 2f32;
        let x = filled_tensor(&native, n as usize, val);

        // set up y
        let val = 4f32;
        let y = filled_tensor(&native, n as usize, val);

        // set up result
        let mut result = SharedTensor::<f32>::new(&vec![1]);

        {
            let cuda_mem_x = x.read(cuda.device()).unwrap();
            let cuda_mem_y = y.read(cuda.device()).unwrap();
            let cuda_mem_result = result.write_only(cuda.device()).unwrap();
            let mut ctx = Context::new().unwrap();
            ctx.set_pointer_mode(PointerMode::Device).unwrap();
            unsafe {
                let x_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_x.id_c());
                let y_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_y.id_c());
                let result_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_result.id_c());
                API::ffi_sdot(*ctx.id_c(), n, x_addr, 1, y_addr, 1, result_addr).unwrap();
            }
        }

        let native_result = result.read(native.device()).unwrap();
        assert_eq!(&[40f32], native_result.as_slice::<f32>());

        test_teardown();
    }

    #[test]
    fn use_cuda_memory_for_nrm2() {
        test_setup();

        let native = get_native_backend();
        let cuda = get_cuda_backend();

        // set up x
        let n = 3i32;
        let val = 2f32;
        let mut x = filled_tensor(&native, n as usize, val);
        write_to_memory(x.write_only(native.device()).unwrap(), &[2f32, 2f32, 1f32]);

        // set up result
        let mut result = SharedTensor::<f32>::new(&vec![1]);

        {
            let cuda_mem_x = x.read(cuda.device()).unwrap();
            let cuda_mem_result = result.write_only(cuda.device()).unwrap();
            let mut ctx = Context::new().unwrap();
            ctx.set_pointer_mode(PointerMode::Device).unwrap();
            unsafe {
                let x_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_x.id_c());
                let result_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_result.id_c());
                API::ffi_snrm2(*ctx.id_c(), n, x_addr, 1, result_addr).unwrap();
            }
        }

        let native_result = result.read(native.device()).unwrap();
        assert_eq!(&[3f32], native_result.as_slice::<f32>());

        test_teardown();
    }

    #[test]
    fn use_cuda_memory_for_scal() {
        test_setup();

        let native = get_native_backend();
        let cuda = get_cuda_backend();

        // set up alpha
        let alpha = filled_tensor(&native, 1, 2.5f32);

        // set up x
        let n = 3i32;
        let val = 2f32;
        let mut x = filled_tensor(&native, n as usize, val);

        {
            let cuda_mem_alpha = alpha.read(cuda.device()).unwrap();
            let cuda_mem_x = x.read_write(cuda.device()).unwrap();
            let mut ctx = Context::new().unwrap();
            ctx.set_pointer_mode(PointerMode::Device).unwrap();
            unsafe {
                let alpha_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_alpha.id_c());
                let x_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_x.id_c());
                API::ffi_sscal(*ctx.id_c(), n, alpha_addr, x_addr, 1).unwrap();
            }
        }

        let native_x = x.read(native.device()).unwrap();
        assert_eq!(&[5f32, 5f32, 5f32], native_x.as_slice::<f32>());

        test_teardown();
    }

    #[test]
    fn use_cuda_memory_for_swap() {
        test_setup();

        let native = get_native_backend();
        let cuda = get_cuda_backend();

        // set up x
        let n = 5i32;
        let val = 2f32;
        let mut x = filled_tensor(&native, n as usize, val);

        // set up y
        let val = 4f32;
        let mut y = filled_tensor(&native, n as usize, val);

        {
            let cuda_mem_x = x.read_write(cuda.device()).unwrap();
            let cuda_mem_y = y.read_write(cuda.device()).unwrap();
            let mut ctx = Context::new().unwrap();
            ctx.set_pointer_mode(PointerMode::Device).unwrap();
            unsafe {
                let x_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_x.id_c());
                let y_addr = ::std::mem::transmute::<u64, *mut f32>(*cuda_mem_y.id_c());
                API::ffi_sswap(*ctx.id_c(), n, x_addr, 1, y_addr, 1).unwrap();
            }
        }

        let native_x = x.read(native.device()).unwrap();
        assert_eq!(&[4f32, 4f32, 4f32, 4f32, 4f32], native_x.as_slice::<f32>());

        let native_y = y.read(native.device()).unwrap();
        assert_eq!(&[2f32, 2f32, 2f32, 2f32, 2f32], native_y.as_slice::<f32>());

        test_teardown();
    }
}