blas-array2 0.3.0

Parameter-optional BLAS wrapper by ndarray::Array (Ix1 or Ix2).
Documentation
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
545
546
use crate::util::*;
use approx::*;
use blas_array2::blas3::gemm::GEMM;
use blas_array2::util::*;
use cblas_sys::*;
use ndarray::prelude::*;
use num_complex::*;

#[cfg(test)]
mod demonstration {
    /// Following code performs matrix multiplication out-place
    /// ```
    /// c = a * b
    /// ```
    /// Output
    /// ```
    /// [[-22.000, -28.000],
    ///  [-40.000, -52.000]]
    /// ```
    #[test]
    fn demonstration_dgemm_simple() {
        use blas_array2::prelude::*;
        use ndarray::prelude::*;
        let a = array![[1.0, 2.0, 3.0], [3.0, 4.0, 5.0]];
        let b = array![[-1.0, -2.0], [-3.0, -4.0], [-5.0, -6.0]];
        let c_out = DGEMM::default().a(a.view()).b(b.view()).run().unwrap().into_owned();
        println!("{:7.3?}", c_out);
    }

    /// Following code performs matrix multiplication in-place
    /// ```
    /// c = alpha * a * transpose(b) + beta * c
    /// where
    ///     alpha = 1.0 (by default)
    ///     beta = 1.5
    ///     a = [[1.0, 2.0, ___],
    ///          [3.0, 4.0, ___]]
    ///         (sliced by `s![.., ..2]`)
    ///     b = [[-1.0, -2.0],
    ///          [-3.0, -4.0],
    ///          [-5.0, -6.0]]
    ///     c = [[1.0, 1.0, 1.0],
    ///          [___, ___, ___],
    ///          [1.0, 1.0, 1.0]]
    ///         (Column-major, sliced by `s![0..3;2, ..]`)
    /// ```
    /// Output of `c` is
    /// ```
    /// [[-3.500,  -9.500, -15.500],
    ///  [ 1.000,   1.000,   1.000],
    ///  [-9.500, -23.500, -37.500]]
    /// ```
    #[test]
    fn demonstration_dgemm_complicated() {
        use blas_array2::prelude::*;
        use ndarray::prelude::*;
        let a = array![[1.0, 2.0, 3.0], [3.0, 4.0, 5.0]];
        let b = array![[-1.0, -2.0], [-3.0, -4.0], [-5.0, -6.0]];
        let mut c = Array::ones((3, 3).f());

        let c_out = DGEMM::default()
            .a(a.slice(s![.., ..2]))
            .b(b.view())
            .c(c.slice_mut(s![0..3;2, ..]))
            .transb('T')
            .beta(1.5)
            .run()
            .unwrap();
        // one can get the result as an owned array
        // but the result may not refer to the same memory location as `c`
        println!("{:4.3?}", c_out.into_owned());
        // this modification on `c` is actually performed in-place
        // so if `c` is pre-defined, not calling `into_owned` could be more efficient
        println!("{:4.3?}", c);
    }
}

#[cfg(test)]
mod valid_owned {
    use super::*;

    #[test]
    fn test_example() {
        type RT = <f32 as BLASFloat>::RealFloat;
        let alpha = f32::rand();
        let beta = f32::rand();
        let a_raw = random_matrix(100, 100, 'R'.into());
        let b_raw = random_matrix(100, 100, 'R'.into());
        let a_slc = slice(7, 8, 1, 1); // s![5..12, 10..18]
        let b_slc = slice(8, 9, 1, 1); // s![5..13, 10..19]

        let c_out = GEMM::default()
            .a(a_raw.slice(a_slc))
            .b(b_raw.slice(b_slc))
            .transa('N')
            .transb('N')
            .alpha(alpha)
            .beta(beta)
            .run()
            .unwrap();

        let a_naive = transpose(&a_raw.slice(a_slc), 'N'.into());
        let b_naive = transpose(&b_raw.slice(b_slc), 'N'.into());
        let c_naive = alpha * gemm(&a_naive.view(), &b_naive.view());

        if let ArrayOut2::Owned(c_out) = c_out {
            let err = (&c_naive - &c_out).mapv(|x| x.abs()).sum();
            let acc = c_naive.mapv(|x| x.abs()).sum() as RT;
            let err_div = err / acc;
            assert_abs_diff_eq!(err_div, 0.0, epsilon = 4.0 * RT::EPSILON);
        } else {
            panic!("Failed");
        }
    }

    macro_rules! test_macro {
        (
            $test_name: ident: $attr: ident,
            $F:ty,
            ($($a_slc: expr),+), ($($b_slc: expr),+),
            $a_layout: expr, $b_layout: expr,
            $a_trans: expr, $b_trans: expr
        ) => {
            #[test]
            #[$attr]
            pub fn $test_name()
            {
                type RT = <$F as BLASFloat>::RealFloat;
                let alpha = <$F>::rand();
                let beta = <$F>::rand();
                let a_raw = random_matrix(100, 100, $a_layout.into());
                let b_raw = random_matrix(100, 100, $b_layout.into());
                let a_slc = slice($($a_slc),+);
                let b_slc = slice($($b_slc),+);

                let c_out = GEMM::<$F>::default()
                    .a(a_raw.slice(a_slc))
                    .b(b_raw.slice(b_slc))
                    .transa($a_trans)
                    .transb($b_trans)
                    .alpha(alpha)
                    .beta(beta)
                    .run().unwrap();

                let a_naive = transpose(&a_raw.slice(a_slc), $a_trans.try_into().unwrap());
                let b_naive = transpose(&b_raw.slice(b_slc), $b_trans.try_into().unwrap());
                let c_naive = alpha * gemm(&a_naive.view(), &b_naive.view());

                if let ArrayOut2::Owned(c_out) = c_out {
                    let err = (&c_naive - &c_out).mapv(|x| x.abs()).sum();
                    let acc = c_naive.mapv(|x| x.abs()).sum() as RT;
                    let err_div = err / acc;
                    assert_abs_diff_eq!(err_div, 0.0, epsilon=4.0 * RT::EPSILON);
                } else {
                    panic!("Failed");
                }
            }
        };
    }

    // successful tests
    test_macro!(test_000: inline, f32, (7, 8, 1, 1), (8, 9, 1, 1), 'R', 'R', 'N', 'N');
    test_macro!(test_001: inline, f32, (7, 8, 1, 1), (8, 9, 3, 3), 'C', 'C', 'N', 'N');
    test_macro!(test_002: inline, f32, (8, 7, 3, 3), (9, 8, 1, 1), 'C', 'C', 'T', 'T');
    test_macro!(test_003: inline, f32, (8, 7, 3, 3), (9, 8, 3, 3), 'R', 'R', 'T', 'T');
    test_macro!(test_004: inline, f64, (7, 8, 3, 3), (8, 9, 1, 1), 'R', 'R', 'N', 'N');
    test_macro!(test_005: inline, f64, (7, 8, 3, 3), (8, 9, 3, 3), 'C', 'C', 'N', 'N');
    test_macro!(test_006: inline, f64, (8, 7, 1, 1), (9, 8, 1, 3), 'R', 'C', 'T', 'T');
    test_macro!(test_007: inline, f64, (8, 7, 1, 1), (9, 8, 3, 1), 'C', 'R', 'T', 'T');
    test_macro!(test_008: inline, c32, (7, 8, 1, 3), (9, 8, 1, 1), 'C', 'C', 'N', 'T');
    test_macro!(test_009: inline, c32, (7, 8, 1, 3), (9, 8, 3, 3), 'R', 'R', 'N', 'T');
    test_macro!(test_010: inline, c32, (8, 7, 3, 1), (8, 9, 1, 3), 'C', 'R', 'T', 'N');
    test_macro!(test_011: inline, c32, (8, 7, 3, 1), (8, 9, 3, 1), 'R', 'C', 'T', 'N');
    test_macro!(test_012: inline, c64, (7, 8, 3, 1), (9, 8, 1, 3), 'C', 'R', 'N', 'T');
    test_macro!(test_013: inline, c64, (7, 8, 3, 1), (9, 8, 3, 1), 'R', 'C', 'N', 'T');
    test_macro!(test_014: inline, c64, (8, 7, 1, 3), (8, 9, 1, 3), 'R', 'C', 'T', 'N');
    test_macro!(test_015: inline, c64, (8, 7, 1, 3), (8, 9, 3, 1), 'C', 'R', 'T', 'N');

    // unrecognized transpose
    test_macro!(test_100: should_panic, f32, (7, 8, 1, 1), (9, 8, 1, 3), 'R', 'R', BLASTranspose::ConjNoTrans, 'T');
    test_macro!(test_101: should_panic, f32, (7, 8, 1, 1), (9, 8, 1, 3), 'R', 'R', 'N', BLASTranspose::ConjNoTrans);

    // dimension mismatch (k)
    test_macro!(test_102: should_panic, f32, (7, 5, 1, 1), (8, 9, 1, 1), 'R', 'R', 'N', 'N');
    test_macro!(test_103: should_panic, f32, (7, 5, 1, 1), (9, 8, 1, 3), 'R', 'R', 'N', 'T');
}

#[cfg(test)]
mod valid_view {
    use super::*;

    #[test]
    fn test_example() {
        type RT = <f32 as BLASFloat>::RealFloat;
        let alpha = f32::rand();
        let beta = f32::rand();
        let a_raw = random_matrix(100, 100, 'R'.into());
        let b_raw = random_matrix(100, 100, 'R'.into());
        let mut c_raw = random_matrix(100, 100, 'C'.into());
        let a_slc = slice(7, 8, 1, 1);
        let b_slc = slice(9, 8, 3, 3);
        let c_slc = slice(7, 9, 1, 3);

        let mut c_naive = c_raw.clone();

        let c_out = GEMM::default()
            .a(a_raw.slice(a_slc))
            .b(b_raw.slice(b_slc))
            .c(c_raw.slice_mut(c_slc))
            .transa('N')
            .transb('T')
            .alpha(alpha)
            .beta(beta)
            .run()
            .unwrap();

        let a_naive = transpose(&a_raw.slice(a_slc), 'N'.into());
        let b_naive = transpose(&b_raw.slice(b_slc), 'T'.into());
        let c_assign = &(alpha * gemm(&a_naive.view(), &b_naive.view()) + beta * &c_naive.slice(c_slc));
        c_naive.slice_mut(c_slc).assign(c_assign);

        if let ArrayOut2::ViewMut(_) = c_out {
            let err = (&c_naive - &c_raw).mapv(|x| x.abs()).sum();
            let acc = c_naive.view().mapv(|x| x.abs()).sum() as RT;
            let err_div = err / acc;
            assert_abs_diff_eq!(err_div, 0.0, epsilon = 4.0 * RT::EPSILON);
        } else {
            panic!("Failed");
        }
    }

    macro_rules! test_macro {
        (
            $test_name: ident: $attr: ident,
            $F:ty,
            ($($a_slc: expr),+), ($($b_slc: expr),+), ($($c_slc: expr),+),
            $a_layout: expr, $b_layout: expr, $c_layout: expr,
            $a_trans: expr, $b_trans: expr
        ) => {
            #[test]
            #[$attr]
            pub fn $test_name()
            {
                type RT = <$F as BLASFloat>::RealFloat;
                let alpha = <$F>::rand();
                let beta = <$F>::rand();
                let a_raw = random_matrix(100, 100, $a_layout.into());
                let b_raw = random_matrix(100, 100, $b_layout.into());
                let mut c_raw = random_matrix(100, 100, $c_layout.into());
                let a_slc = slice($($a_slc),+);
                let b_slc = slice($($b_slc),+);
                let c_slc = slice($($c_slc),+);

                let mut c_naive = c_raw.clone();

                let c_out = GEMM::<$F>::default()
                    .a(a_raw.slice(a_slc))
                    .b(b_raw.slice(b_slc))
                    .c(c_raw.slice_mut(c_slc))
                    .transa($a_trans)
                    .transb($b_trans)
                    .alpha(alpha)
                    .beta(beta)
                    .run().unwrap();

                let a_naive = transpose(&a_raw.slice(a_slc), $a_trans.try_into().unwrap());
                let b_naive = transpose(&b_raw.slice(b_slc), $b_trans.try_into().unwrap());
                let c_assign = alpha * gemm(&a_naive.view(), &b_naive.view()) + beta * &c_naive.slice(c_slc);
                c_naive.slice_mut(c_slc).assign(&c_assign);

                if let ArrayOut2::ViewMut(_) = c_out {
                    let err = (&c_naive - &c_raw).mapv(|x| x.abs()).sum();
                    let acc = c_naive.view().mapv(|x| x.abs()).sum() as RT;
                    let err_div = err / acc;
                    assert_abs_diff_eq!(err_div, 0.0, epsilon=4.0 * RT::EPSILON);
                } else {
                    panic!("Failed");
                }
            }
        };
    }

    // successful tests
    test_macro!(test_000: inline, f32, (7, 8, 1, 1), (8, 9, 1, 1), (7, 9, 1, 1), 'R', 'R', 'R', 'N', 'N');
    test_macro!(test_001: inline, f32, (7, 8, 1, 1), (8, 9, 3, 3), (7, 9, 3, 3), 'C', 'C', 'C', 'N', 'N');
    test_macro!(test_002: inline, f32, (8, 7, 3, 3), (9, 8, 1, 1), (7, 9, 1, 1), 'C', 'C', 'C', 'T', 'T');
    test_macro!(test_003: inline, f32, (8, 7, 3, 3), (9, 8, 3, 3), (7, 9, 3, 3), 'R', 'R', 'R', 'T', 'T');
    test_macro!(test_004: inline, f64, (7, 8, 3, 3), (8, 9, 1, 1), (7, 9, 3, 3), 'R', 'R', 'C', 'N', 'N');
    test_macro!(test_005: inline, f64, (7, 8, 3, 3), (8, 9, 3, 3), (7, 9, 1, 1), 'C', 'C', 'R', 'N', 'N');
    test_macro!(test_006: inline, f64, (8, 7, 1, 1), (9, 8, 1, 1), (7, 9, 3, 3), 'C', 'C', 'R', 'T', 'T');
    test_macro!(test_007: inline, f64, (8, 7, 1, 1), (9, 8, 3, 3), (7, 9, 1, 1), 'R', 'R', 'C', 'T', 'T');
    test_macro!(test_008: inline, c32, (7, 8, 1, 3), (9, 8, 1, 3), (7, 9, 1, 3), 'R', 'C', 'R', 'N', 'T');
    test_macro!(test_009: inline, c32, (7, 8, 1, 3), (9, 8, 3, 1), (7, 9, 3, 1), 'C', 'R', 'C', 'N', 'T');
    test_macro!(test_010: inline, c32, (8, 7, 3, 1), (8, 9, 1, 3), (7, 9, 3, 1), 'C', 'R', 'R', 'T', 'N');
    test_macro!(test_011: inline, c32, (8, 7, 3, 1), (8, 9, 3, 1), (7, 9, 1, 3), 'R', 'C', 'C', 'T', 'N');
    test_macro!(test_012: inline, c64, (7, 8, 3, 1), (9, 8, 1, 3), (7, 9, 1, 3), 'C', 'R', 'C', 'N', 'T');
    test_macro!(test_013: inline, c64, (7, 8, 3, 1), (9, 8, 3, 1), (7, 9, 3, 1), 'R', 'C', 'R', 'N', 'T');
    test_macro!(test_014: inline, c64, (8, 7, 1, 3), (8, 9, 1, 3), (7, 9, 3, 1), 'R', 'C', 'C', 'T', 'N');
    test_macro!(test_015: inline, c64, (8, 7, 1, 3), (8, 9, 3, 1), (7, 9, 1, 3), 'C', 'R', 'R', 'T', 'N');

    // dimension mismatch (m, n)
    test_macro!(test_100: should_panic, f32, (7, 8, 1, 1), (8, 9, 1, 1), (7, 8, 1, 1), 'R', 'R', 'R', 'N', 'N');

    // zero dimension
    test_macro!(test_099: inline, f32, (0, 8, 1, 1), (8, 9, 1, 1), (0, 9, 1, 1), 'R', 'R', 'R', 'N', 'N');
    test_macro!(test_098: inline, f32, (7, 0, 1, 1), (0, 9, 1, 1), (7, 9, 1, 1), 'R', 'R', 'R', 'N', 'N');
}

#[cfg(test)]
mod valid_cblas {
    use super::*;

    #[test]
    fn test_cblas() {
        // set parameters of test configuration
        type F = c32;
        let a_slc = slice(7, 8, 1, 1);
        let b_slc = slice(8, 9, 1, 1);
        let c_slc = slice(7, 9, 1, 1);
        let a_layout = 'R';
        let b_layout = 'R';
        let c_layout = 'R';
        let transa = 'N';
        let transb = 'N';
        let cblas_layout = 'R';

        // type definition
        type FFI = <F as TestFloat>::FFIFloat;

        // data assignment
        let alpha = F::rand();
        let beta = F::rand();
        let a_raw = random_matrix::<F>(100, 100, a_layout.into());
        let b_raw = random_matrix::<F>(100, 100, b_layout.into());
        let mut c_raw = random_matrix::<F>(100, 100, c_layout.into());
        let mut c_origin = c_raw.clone();

        // cblas computation - mut
        let a_naive = ndarray_to_layout(a_raw.slice(a_slc).into_owned(), cblas_layout);
        let b_naive = ndarray_to_layout(b_raw.slice(b_slc).into_owned(), cblas_layout);
        let mut c_naive = ndarray_to_layout(c_raw.slice(c_slc).into_owned(), cblas_layout);
        let (m, n) = c_naive.dim();
        let k = if transa == 'N' { a_naive.len_of(Axis(1)) } else { a_naive.len_of(Axis(0)) };
        let lda = *a_naive.strides().iter().max().unwrap();
        let ldb = *b_naive.strides().iter().max().unwrap();
        let ldc = *c_naive.strides().iter().max().unwrap();
        unsafe {
            cblas_cgemm(
                to_cblas_layout(cblas_layout),
                to_cblas_trans(transa),
                to_cblas_trans(transb),
                m.try_into().unwrap(),
                n.try_into().unwrap(),
                k.try_into().unwrap(),
                [alpha].as_ptr() as *const FFI,
                a_naive.as_ptr() as *const FFI,
                lda.try_into().unwrap(),
                b_naive.as_ptr() as *const FFI,
                ldb.try_into().unwrap(),
                [beta].as_ptr() as *const FFI,
                c_naive.as_mut_ptr() as *mut FFI,
                ldc.try_into().unwrap(),
            );
        }

        let c_out = GEMM::<F>::default()
            .a(a_raw.slice(a_slc))
            .b(b_raw.slice(b_slc))
            .c(c_raw.slice_mut(c_slc))
            .alpha(alpha)
            .beta(beta)
            .transa(transa)
            .transb(transb)
            .run()
            .unwrap()
            .into_owned();

        check_same(&c_out.view(), &c_naive.view(), 4.0 * F::EPSILON);
        check_same(&c_raw.slice(c_slc), &c_naive.view(), 4.0 * F::EPSILON);
        c_raw.slice_mut(c_slc).fill(F::from(0.0));
        c_origin.slice_mut(c_slc).fill(F::from(0.0));
        check_same(&c_raw.view(), &c_origin.view(), 4.0 * F::EPSILON);

        // cblas computation - own
        c_naive.fill(F::from(0.0));
        unsafe {
            cblas_cgemm(
                to_cblas_layout(cblas_layout),
                to_cblas_trans(transa),
                to_cblas_trans(transb),
                m.try_into().unwrap(),
                n.try_into().unwrap(),
                k.try_into().unwrap(),
                [alpha].as_ptr() as *const FFI,
                a_naive.as_ptr() as *const FFI,
                lda.try_into().unwrap(),
                b_naive.as_ptr() as *const FFI,
                ldb.try_into().unwrap(),
                [beta].as_ptr() as *const FFI,
                c_naive.as_mut_ptr() as *mut FFI,
                ldc.try_into().unwrap(),
            );
        }

        let c_out = GEMM::<F>::default()
            .a(a_raw.slice(a_slc))
            .b(b_raw.slice(b_slc))
            .alpha(alpha)
            .beta(beta)
            .transa(transa)
            .transb(transb)
            .run()
            .unwrap()
            .into_owned();
        check_same(&c_out.view(), &c_naive.view(), 4.0 * F::EPSILON);
    }

    macro_rules! test_macro {
        (
            $test_name: ident: $attr: ident,
            $F:ty, $cblas_func: ident,
            ($($a_slc: expr),+), ($($b_slc: expr),+), ($($c_slc: expr),+),
            $a_layout: expr, $b_layout: expr, $c_layout: expr,
            $a_trans: expr, $b_trans: expr, $cblas_layout: expr
        ) => {
            #[test]
            #[$attr]
            fn $test_name() {
                // set parameters of test configuration
                type F = $F;
                let a_slc = slice($($a_slc),+);
                let b_slc = slice($($b_slc),+);
                let c_slc = slice($($c_slc),+);
                let a_layout = $a_layout;
                let b_layout = $b_layout;
                let c_layout = $c_layout;
                let transa = $a_trans;
                let transb = $b_trans;
                let cblas_layout = $cblas_layout;

                // type definition
                type FFI = <F as TestFloat>::FFIFloat;

                // data assignment
                let alpha = F::rand();
                let beta = F::rand();
                let a_raw = random_matrix::<F>(100, 100, a_layout.into());
                let b_raw = random_matrix::<F>(100, 100, b_layout.into());
                let mut c_raw = random_matrix::<F>(100, 100, c_layout.into());
                let mut c_origin = c_raw.clone();

                // cblas computation - mut
                let a_naive = ndarray_to_layout(a_raw.slice(a_slc).into_owned(), cblas_layout);
                let b_naive = ndarray_to_layout(b_raw.slice(b_slc).into_owned(), cblas_layout);
                let mut c_naive = ndarray_to_layout(c_raw.slice(c_slc).into_owned(), cblas_layout);
                let (m, n) = c_naive.dim();
                let k = if transa == 'N' { a_naive.len_of(Axis(1)) } else { a_naive.len_of(Axis(0)) };
                let lda = *a_naive.strides().iter().max().unwrap();
                let ldb = *b_naive.strides().iter().max().unwrap();
                let ldc = *c_naive.strides().iter().max().unwrap();
                unsafe {
                    $cblas_func(
                        to_cblas_layout(cblas_layout),
                        to_cblas_trans(transa),
                        to_cblas_trans(transb),
                        m.try_into().unwrap(),
                        n.try_into().unwrap(),
                        k.try_into().unwrap(),
                        [alpha].as_ptr() as *const FFI,
                        a_naive.as_ptr() as *const FFI,
                        lda.try_into().unwrap(),
                        b_naive.as_ptr() as *const FFI,
                        ldb.try_into().unwrap(),
                        [beta].as_ptr() as *const FFI,
                        c_naive.as_mut_ptr() as *mut FFI,
                        ldc.try_into().unwrap(),
                    );
                }

                let c_out = GEMM::<F>::default()
                    .a(a_raw.slice(a_slc))
                    .b(b_raw.slice(b_slc))
                    .c(c_raw.slice_mut(c_slc))
                    .alpha(alpha)
                    .beta(beta)
                    .transa(transa)
                    .transb(transb)
                    .run()
                    .unwrap()
                    .into_owned();

                check_same(&c_out.view(), &c_naive.view(), 4.0 * F::EPSILON);
                check_same(&c_raw.slice(c_slc), &c_naive.view(), 4.0 * F::EPSILON);
                c_raw.slice_mut(c_slc).fill(F::from(0.0));
                c_origin.slice_mut(c_slc).fill(F::from(0.0));
                check_same(&c_raw.view(), &c_origin.view(), 4.0 * F::EPSILON);

                // cblas computation - own
                c_naive.fill(F::from(0.0));
                unsafe {
                    $cblas_func(
                        to_cblas_layout(cblas_layout),
                        to_cblas_trans(transa),
                        to_cblas_trans(transb),
                        m.try_into().unwrap(),
                        n.try_into().unwrap(),
                        k.try_into().unwrap(),
                        [alpha].as_ptr() as *const FFI,
                        a_naive.as_ptr() as *const FFI,
                        lda.try_into().unwrap(),
                        b_naive.as_ptr() as *const FFI,
                        ldb.try_into().unwrap(),
                        [beta].as_ptr() as *const FFI,
                        c_naive.as_mut_ptr() as *mut FFI,
                        ldc.try_into().unwrap(),
                    );
                }

                let c_out = GEMM::<F>::default()
                    .a(a_raw.slice(a_slc))
                    .b(b_raw.slice(b_slc))
                    .alpha(alpha)
                    .beta(beta)
                    .transa(transa)
                    .transb(transb)
                    .run()
                    .unwrap()
                    .into_owned();
                check_same(&c_out.view(), &c_naive.view(), 4.0 * F::EPSILON);
            }
        };
    }

    test_macro!(test_000: inline, c32, cblas_cgemm, (7, 8, 1, 1), (8, 9, 1, 1), (7, 9, 1, 1), 'R', 'R', 'R', 'N', 'N', 'R');
    test_macro!(test_001: inline, c32, cblas_cgemm, (7, 8, 1, 1), (8, 9, 1, 1), (7, 9, 1, 1), 'R', 'R', 'R', 'N', 'N', 'R');
    test_macro!(test_002: inline, c32, cblas_cgemm, (7, 8, 1, 1), (8, 9, 1, 1), (7, 9, 1, 1), 'C', 'C', 'C', 'N', 'N', 'C');
    test_macro!(test_003: inline, c32, cblas_cgemm, (7, 8, 1, 1), (9, 8, 1, 1), (7, 9, 1, 1), 'R', 'R', 'R', 'N', 'T', 'R');
    test_macro!(test_004: inline, c32, cblas_cgemm, (8, 7, 1, 1), (8, 9, 1, 1), (7, 9, 1, 1), 'C', 'C', 'C', 'T', 'N', 'C');
    test_macro!(test_005: inline, c32, cblas_cgemm, (8, 7, 1, 1), (9, 8, 1, 1), (7, 9, 1, 1), 'R', 'R', 'R', 'T', 'T', 'R');
    test_macro!(test_006: inline, c32, cblas_cgemm, (8, 7, 1, 1), (9, 8, 1, 1), (7, 9, 1, 1), 'R', 'R', 'R', 'T', 'C', 'R');
    test_macro!(test_007: inline, c32, cblas_cgemm, (8, 7, 1, 1), (9, 8, 1, 1), (7, 9, 1, 1), 'R', 'R', 'R', 'T', 'C', 'R');
    test_macro!(test_008: inline, c32, cblas_cgemm, (8, 7, 1, 1), (9, 8, 1, 1), (7, 9, 1, 1), 'C', 'C', 'C', 'C', 'T', 'C');
    test_macro!(test_009: inline, c32, cblas_cgemm, (8, 7, 1, 1), (9, 8, 1, 1), (7, 9, 1, 1), 'C', 'C', 'C', 'C', 'T', 'C');
    test_macro!(test_010: inline, c32, cblas_cgemm, (8, 7, 1, 1), (9, 8, 1, 1), (7, 9, 1, 1), 'C', 'C', 'C', 'C', 'C', 'C');
    test_macro!(test_011: inline, c32, cblas_cgemm, (8, 7, 1, 1), (9, 8, 1, 1), (7, 9, 1, 1), 'C', 'C', 'C', 'C', 'C', 'C');
}