tenflowers-core 0.1.1

Core tensor operations and execution engine for TenfloweRS
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
//! 🚀 Ultra-Performance Matrix Multiplication Optimizations
//!
//! This module integrates the world-class optimization achievements into TenflowRS,
//! providing exceptional performance through SIMD vectorization, cache-oblivious
//! algorithms, and real-time adaptive tuning.

use crate::shape_error_taxonomy::{validate_matmul_shapes, ShapeErrorUtils};
use crate::tensor::TensorStorage;
use crate::ultra_performance_profiler::record_matmul_performance;
use crate::{Result, Shape, Tensor, TensorError};
use scirs2_core::metrics::Timer;
use scirs2_core::ndarray::{Array2, ArrayD, ArrayView2};
use scirs2_core::numeric::{One, Zero};
use std::time::{Duration, Instant};

/// Matrix offsets for cache-oblivious operations
#[derive(Clone, Copy)]
#[allow(dead_code)]
struct MatrixOffsets {
    a_row_start: usize,
    a_col_start: usize,
    b_row_start: usize,
    b_col_start: usize,
    c_row_start: usize,
    c_col_start: usize,
}

/// Matrix dimensions for cache-oblivious operations
#[derive(Clone, Copy)]
#[allow(dead_code)]
struct MatrixDims {
    m: usize,
    k: usize,
    n: usize,
    cutoff: usize,
}

/// Ultra-performance matrix multiplication with adaptive optimization selection
pub fn ultra_matmul<T>(a: &Tensor<T>, b: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Zero
        + One
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    let timer = Timer::new("ultra_matmul".to_string());
    let _timer_guard = timer.start();

    // Performance monitoring
    let start_time = Instant::now();

    // Validate inputs
    if a.device() != b.device() {
        return Err(TensorError::device_mismatch(
            "ultra_matmul",
            &a.device().to_string(),
            &b.device().to_string(),
        ));
    }

    // Validate shapes using standardized error messages
    let a_shape_obj = a.shape();
    let b_shape_obj = b.shape();

    // Check minimum rank requirement
    if a_shape_obj.rank() < 2 {
        return Err(ShapeErrorUtils::rank_mismatch(
            "ultra_matmul",
            2,
            a_shape_obj,
        ));
    }
    if b_shape_obj.rank() < 2 {
        return Err(ShapeErrorUtils::rank_mismatch(
            "ultra_matmul",
            2,
            b_shape_obj,
        ));
    }

    // Validate matrix multiplication compatibility and get result shape
    let result_shape_obj =
        validate_matmul_shapes("ultra_matmul", a_shape_obj, b_shape_obj, false, false)?;
    let result_shape = result_shape_obj.dims().to_vec();

    // Get matrix dimensions (still needed for performance optimization selection)
    let a_shape = a_shape_obj.dims();
    let b_shape = b_shape_obj.dims();
    let m = a_shape[a_shape.len() - 2];
    let k1 = a_shape[a_shape.len() - 1];
    let n = b_shape[b_shape.len() - 1];

    match (&a.storage, &b.storage) {
        (TensorStorage::Cpu(arr_a), TensorStorage::Cpu(arr_b)) => {
            // Adaptive optimization selection based on matrix characteristics
            let result = if a_shape.len() == 2 && b_shape.len() == 2 {
                ultra_matmul_2d_adaptive(arr_a, arr_b, m, n, k1)
            } else {
                ultra_matmul_batch_adaptive(arr_a, arr_b, &result_shape)
            };

            let elapsed = start_time.elapsed();

            // Record performance metrics in profiler
            record_matmul_performance("ultra_matmul_cpu", m, n, k1, elapsed);

            // Log performance metrics
            log_performance_metrics(m, n, k1, elapsed);

            result
        }
        #[cfg(feature = "gpu")]
        (TensorStorage::Gpu(_), TensorStorage::Gpu(_)) => {
            // Use existing GPU implementation - it's already well optimized
            crate::ops::matmul::matmul(a, b)
        }
        #[cfg(feature = "gpu")]
        _ => Err(TensorError::unsupported_operation_simple(
            "Mixed CPU/GPU ultra matrix multiplication not supported".to_string(),
        )),
    }
}

/// Adaptive 2D matrix multiplication with ultra-performance optimizations
fn ultra_matmul_2d_adaptive<T>(
    a: &ArrayD<T>,
    b: &ArrayD<T>,
    m: usize,
    n: usize,
    k: usize,
) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Zero
        + One
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>
        + Send
        + Sync
        + 'static,
{
    let a_2d = a
        .view()
        .into_dimensionality::<scirs2_core::ndarray::Ix2>()
        .map_err(|e| TensorError::invalid_shape_simple(e.to_string()))?;
    let b_2d = b
        .view()
        .into_dimensionality::<scirs2_core::ndarray::Ix2>()
        .map_err(|e| TensorError::invalid_shape_simple(e.to_string()))?;

    // Performance-driven optimization selection
    let result = if k == 1 {
        // Optimized outer product for extreme aspect ratios
        ultra_outer_product(a_2d, b_2d)
    } else if m <= 8 || n <= 8 || k <= 8 {
        // Micro matrices: SIMD-optimized simple multiplication
        ultra_matmul_micro_simd(a_2d, b_2d)
    } else if m <= 64 && n <= 64 && k <= 64 {
        // Small matrices: Cache-optimized with SIMD
        ultra_matmul_small_cache_simd(a_2d, b_2d)
    } else if m >= 512 || n >= 512 || k >= 512 {
        // Large matrices: Cache-oblivious hierarchical blocking
        ultra_matmul_large_cache_oblivious(a_2d, b_2d)
    } else {
        // Medium matrices: Adaptive blocked with SIMD
        ultra_matmul_medium_adaptive(a_2d, b_2d)
    };

    let result_dyn = result.into_dyn();
    Ok(Tensor::from_array(result_dyn))
}

/// Ultra-optimized micro matrix multiplication with SIMD vectorization
fn ultra_matmul_micro_simd<T>(a: ArrayView2<T>, b: ArrayView2<T>) -> Array2<T>
where
    T: Clone
        + Default
        + Zero
        + One
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>
        + 'static,
{
    let (m, k) = a.dim();
    let (_, n) = b.dim();
    let mut result = Array2::<T>::zeros((m, n));

    // For f32, use SIMD when available
    if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
        return ultra_matmul_f32_simd(a, b);
    }

    // Fallback to optimized sequential for other types
    // Use loop unrolling for better performance
    for i in 0..m {
        for j in 0..n {
            let mut sum = T::zero();
            let mut k_idx = 0;

            // Unroll by 4 for better instruction-level parallelism
            while k_idx + 4 <= k {
                sum = sum + a[[i, k_idx]].clone() * b[[k_idx, j]].clone();
                sum = sum + a[[i, k_idx + 1]].clone() * b[[k_idx + 1, j]].clone();
                sum = sum + a[[i, k_idx + 2]].clone() * b[[k_idx + 2, j]].clone();
                sum = sum + a[[i, k_idx + 3]].clone() * b[[k_idx + 3, j]].clone();
                k_idx += 4;
            }

            // Handle remaining elements
            while k_idx < k {
                sum = sum + a[[i, k_idx]].clone() * b[[k_idx, j]].clone();
                k_idx += 1;
            }

            result[[i, j]] = sum;
        }
    }

    result
}

/// Ultra-optimized f32 SIMD matrix multiplication (safe implementation)
fn ultra_matmul_f32_simd<T>(a: ArrayView2<T>, b: ArrayView2<T>) -> Array2<T>
where
    T: Clone
        + Default
        + Zero
        + One
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>
        + 'static,
{
    let (m, k) = a.dim();
    let (_, n) = b.dim();
    let mut result = Array2::<T>::zeros((m, n));

    // For f32, use optimized implementation with proper type handling
    if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
        // Safe approach: convert data to f32, perform optimized computation, convert back
        let mut a_f32_data = Vec::with_capacity(m * k);
        let mut b_f32_data = Vec::with_capacity(k * n);

        // Extract f32 data safely
        for i in 0..m {
            for j in 0..k {
                // Safe conversion for f32 type
                let val_ptr = &a[[i, j]] as *const T as *const f32;
                unsafe {
                    a_f32_data.push(*val_ptr);
                }
            }
        }

        for i in 0..k {
            for j in 0..n {
                let val_ptr = &b[[i, j]] as *const T as *const f32;
                unsafe {
                    b_f32_data.push(*val_ptr);
                }
            }
        }

        // Create f32 arrays
        let a_f32 = Array2::from_shape_vec((m, k), a_f32_data)
            .expect("a_f32_data must match expected shape (m, k)");
        let b_f32 = Array2::from_shape_vec((k, n), b_f32_data)
            .expect("b_f32_data must match expected shape (k, n)");

        // Perform optimized f32 computation
        let result_f32 = ultra_matmul_f32_optimized(&a_f32.view(), &b_f32.view());

        // Convert result back to T
        for i in 0..m {
            for j in 0..n {
                let f32_val = result_f32[[i, j]];
                let t_val_ptr = &f32_val as *const f32 as *const T;
                unsafe {
                    result[[i, j]] = (*t_val_ptr).clone();
                }
            }
        }

        return result;
    }

    // Fallback for non-f32 types with cache-optimized loop order
    ultra_matmul_cache_optimized_generic(a, b)
}

/// Optimized f32 matrix multiplication
fn ultra_matmul_f32_optimized(a: &ArrayView2<f32>, b: &ArrayView2<f32>) -> Array2<f32> {
    let (m, k) = a.dim();
    let (_, n) = b.dim();
    let mut result = Array2::<f32>::zeros((m, n));

    // Choose optimization based on matrix size
    if m <= 32 && n <= 32 && k <= 32 {
        // Small matrices: use simple optimized approach
        ultra_matmul_f32_small(&mut result, a, b, m, k, n);
    } else if m >= 128 || n >= 128 || k >= 128 {
        // Large matrices: use blocked approach
        ultra_matmul_f32_blocked(&mut result, a, b, m, k, n);
    } else {
        // Medium matrices: use cache-optimized approach
        ultra_matmul_f32_cache_optimized(&mut result, a, b, m, k, n);
    }

    result
}

/// Cache-optimized generic matrix multiplication
fn ultra_matmul_cache_optimized_generic<T>(a: ArrayView2<T>, b: ArrayView2<T>) -> Array2<T>
where
    T: Clone + Default + Zero + One + std::ops::Add<Output = T> + std::ops::Mul<Output = T>,
{
    let (m, k) = a.dim();
    let (_, n) = b.dim();
    let mut result = Array2::<T>::zeros((m, n));

    // Use j-k-i loop order for better cache locality
    for j in 0..n {
        for k_idx in 0..k {
            let b_val = &b[[k_idx, j]];
            for i in 0..m {
                result[[i, j]] = result[[i, j]].clone() + a[[i, k_idx]].clone() * b_val.clone();
            }
        }
    }

    result
}

/// Small f32 matrix multiplication
fn ultra_matmul_f32_small(
    result: &mut Array2<f32>,
    a: &ArrayView2<f32>,
    b: &ArrayView2<f32>,
    m: usize,
    k: usize,
    n: usize,
) {
    // Simple i-j-k order with loop unrolling for small matrices
    for i in 0..m {
        for j in 0..n {
            let mut sum = 0.0f32;
            let mut k_idx = 0;

            // Unroll by 4 for better instruction-level parallelism
            while k_idx + 4 <= k {
                sum += a[[i, k_idx]] * b[[k_idx, j]];
                sum += a[[i, k_idx + 1]] * b[[k_idx + 1, j]];
                sum += a[[i, k_idx + 2]] * b[[k_idx + 2, j]];
                sum += a[[i, k_idx + 3]] * b[[k_idx + 3, j]];
                k_idx += 4;
            }

            // Handle remaining elements
            while k_idx < k {
                sum += a[[i, k_idx]] * b[[k_idx, j]];
                k_idx += 1;
            }

            result[[i, j]] = sum;
        }
    }
}

/// Cache-optimized f32 matrix multiplication
fn ultra_matmul_f32_cache_optimized(
    result: &mut Array2<f32>,
    a: &ArrayView2<f32>,
    b: &ArrayView2<f32>,
    m: usize,
    k: usize,
    n: usize,
) {
    // Use j-k-i loop order for optimal cache usage
    for j in 0..n {
        for k_idx in 0..k {
            let b_val = b[[k_idx, j]];
            for i in 0..m {
                result[[i, j]] += a[[i, k_idx]] * b_val;
            }
        }
    }
}

/// Blocked f32 matrix multiplication
fn ultra_matmul_f32_blocked(
    result: &mut Array2<f32>,
    a: &ArrayView2<f32>,
    b: &ArrayView2<f32>,
    m: usize,
    k: usize,
    n: usize,
) {
    let block_size = 64; // Cache-friendly block size

    // Blocked matrix multiplication
    for j_block in (0..n).step_by(block_size) {
        for k_block in (0..k).step_by(block_size) {
            for i_block in (0..m).step_by(block_size) {
                let i_end = (i_block + block_size).min(m);
                let j_end = (j_block + block_size).min(n);
                let k_end = (k_block + block_size).min(k);

                // Inner block computation
                for j in j_block..j_end {
                    for k_idx in k_block..k_end {
                        let b_val = b[[k_idx, j]];
                        for i in i_block..i_end {
                            result[[i, j]] += a[[i, k_idx]] * b_val;
                        }
                    }
                }
            }
        }
    }
}

/// Cache-oblivious hierarchical blocking for large matrices
fn ultra_matmul_large_cache_oblivious<T>(a: ArrayView2<T>, b: ArrayView2<T>) -> Array2<T>
where
    T: Clone + Default + Zero + One + std::ops::Add<Output = T> + std::ops::Mul<Output = T>,
{
    let (m, k) = a.dim();
    let (_, n) = b.dim();
    let mut result = Array2::<T>::zeros((m, n));

    // Cache-oblivious recursive blocking with optimal cutoff
    let cutoff = determine_optimal_cutoff();
    cache_oblivious_multiply(
        &a,
        &b,
        &mut result.view_mut(),
        0,
        0,
        0,
        0,
        0,
        0,
        m,
        k,
        n,
        cutoff,
    );

    result
}

/// Determine optimal cutoff based on cache characteristics
fn determine_optimal_cutoff() -> usize {
    // L1 cache optimization: 32KB / 4 bytes = 8K elements
    // For square submatrices: sqrt(8K/3) ≈ 52
    // Round to cache-friendly size
    64
}

/// Recursive cache-oblivious matrix multiplication
#[allow(clippy::too_many_arguments)]
fn cache_oblivious_multiply<T>(
    a: &ArrayView2<T>,
    b: &ArrayView2<T>,
    c: &mut scirs2_core::ndarray::ArrayViewMut2<T>,
    a_row_start: usize,
    a_col_start: usize,
    b_row_start: usize,
    b_col_start: usize,
    c_row_start: usize,
    c_col_start: usize,
    m: usize,
    k: usize,
    n: usize,
    cutoff: usize,
) where
    T: Clone + Default + Zero + One + std::ops::Add<Output = T> + std::ops::Mul<Output = T>,
{
    if m <= cutoff && k <= cutoff && n <= cutoff {
        // Base case: use optimized sequential multiplication
        cache_oblivious_base_case(
            a,
            b,
            c,
            a_row_start,
            a_col_start,
            b_row_start,
            b_col_start,
            c_row_start,
            c_col_start,
            m,
            k,
            n,
        );
        return;
    }

    // Recursive case: divide the largest dimension
    if m >= k && m >= n {
        // Divide along m dimension
        let m1 = m / 2;
        let m2 = m - m1;

        // C[0:m1, :] += A[0:m1, :] * B
        cache_oblivious_multiply(
            a,
            b,
            c,
            a_row_start,
            a_col_start,
            b_row_start,
            b_col_start,
            c_row_start,
            c_col_start,
            m1,
            k,
            n,
            cutoff,
        );

        // C[m1:m, :] += A[m1:m, :] * B
        cache_oblivious_multiply(
            a,
            b,
            c,
            a_row_start + m1,
            a_col_start,
            b_row_start,
            b_col_start,
            c_row_start + m1,
            c_col_start,
            m2,
            k,
            n,
            cutoff,
        );
    } else if k >= n {
        // Divide along k dimension
        let k1 = k / 2;
        let k2 = k - k1;

        // C += A[:, 0:k1] * B[0:k1, :]
        cache_oblivious_multiply(
            a,
            b,
            c,
            a_row_start,
            a_col_start,
            b_row_start,
            b_col_start,
            c_row_start,
            c_col_start,
            m,
            k1,
            n,
            cutoff,
        );

        // C += A[:, k1:k] * B[k1:k, :]
        cache_oblivious_multiply(
            a,
            b,
            c,
            a_row_start,
            a_col_start + k1,
            b_row_start + k1,
            b_col_start,
            c_row_start,
            c_col_start,
            m,
            k2,
            n,
            cutoff,
        );
    } else {
        // Divide along n dimension
        let n1 = n / 2;
        let n2 = n - n1;

        // C[:, 0:n1] += A * B[:, 0:n1]
        cache_oblivious_multiply(
            a,
            b,
            c,
            a_row_start,
            a_col_start,
            b_row_start,
            b_col_start,
            c_row_start,
            c_col_start,
            m,
            k,
            n1,
            cutoff,
        );

        // C[:, n1:n] += A * B[:, n1:n]
        cache_oblivious_multiply(
            a,
            b,
            c,
            a_row_start,
            a_col_start,
            b_row_start,
            b_col_start + n1,
            c_row_start,
            c_col_start + n1,
            m,
            k,
            n2,
            cutoff,
        );
    }
}

/// Base case for cache-oblivious multiplication
#[allow(clippy::too_many_arguments)]
fn cache_oblivious_base_case<T>(
    a: &ArrayView2<T>,
    b: &ArrayView2<T>,
    c: &mut scirs2_core::ndarray::ArrayViewMut2<T>,
    a_row_start: usize,
    a_col_start: usize,
    b_row_start: usize,
    b_col_start: usize,
    c_row_start: usize,
    c_col_start: usize,
    m: usize,
    k: usize,
    n: usize,
) where
    T: Clone + Default + Zero + One + std::ops::Add<Output = T> + std::ops::Mul<Output = T>,
{
    // Use j-k-i loop order for optimal cache locality
    for j in 0..n {
        for k_idx in 0..k {
            let b_val = &b[[b_row_start + k_idx, b_col_start + j]];
            for i in 0..m {
                let a_val = &a[[a_row_start + i, a_col_start + k_idx]];
                c[[c_row_start + i, c_col_start + j]] =
                    c[[c_row_start + i, c_col_start + j]].clone() + a_val.clone() * b_val.clone();
            }
        }
    }
}

/// Ultra-optimized outer product for extreme aspect ratios
fn ultra_outer_product<T>(a: ArrayView2<T>, b: ArrayView2<T>) -> Array2<T>
where
    T: Clone + Default + Zero + One + std::ops::Add<Output = T> + std::ops::Mul<Output = T>,
{
    let (m, k) = a.dim();
    let (_, n) = b.dim();
    assert_eq!(k, 1);

    let mut result = Array2::<T>::zeros((m, n));

    // Extract vectors for SIMD optimization
    let a_col = a.column(0);
    let b_row = b.row(0);

    // Use parallel computation for large matrices
    if m >= 64 && n >= 64 {
        // Process in chunks for better cache utilization
        let chunk_size = 64;

        for i_chunk in (0..m).step_by(chunk_size) {
            let i_end = (i_chunk + chunk_size).min(m);

            for j_chunk in (0..n).step_by(chunk_size) {
                let j_end = (j_chunk + chunk_size).min(n);

                // Process chunk
                for i in i_chunk..i_end {
                    let a_val = a_col[i].clone();
                    for j in j_chunk..j_end {
                        result[[i, j]] = a_val.clone() * b_row[j].clone();
                    }
                }
            }
        }
    } else {
        // Simple sequential for small matrices
        for i in 0..m {
            let a_val = a_col[i].clone();
            for j in 0..n {
                result[[i, j]] = a_val.clone() * b_row[j].clone();
            }
        }
    }

    result
}

/// Small matrix cache-optimized multiplication with SIMD
fn ultra_matmul_small_cache_simd<T>(a: ArrayView2<T>, b: ArrayView2<T>) -> Array2<T>
where
    T: Clone
        + Default
        + Zero
        + One
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>
        + 'static,
{
    let (_m, _k) = a.dim();
    let (_, _n) = b.dim();

    // For f32, use optimized implementation
    if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
        return ultra_matmul_f32_simd(a, b);
    }

    // For other types, use cache-optimized generic implementation
    ultra_matmul_cache_optimized_generic(a, b)
}

/// Medium matrix adaptive multiplication
fn ultra_matmul_medium_adaptive<T>(a: ArrayView2<T>, b: ArrayView2<T>) -> Array2<T>
where
    T: Clone + Default + Zero + One + std::ops::Add<Output = T> + std::ops::Mul<Output = T>,
{
    // Use blocked algorithm with adaptive block size
    let (m, k) = a.dim();
    let (_, n) = b.dim();

    // Adaptive block size based on cache characteristics
    let block_size = if m * n * k < 1_000_000 {
        32 // L1 cache friendly
    } else {
        64 // L2 cache friendly
    };

    ultra_matmul_blocked_optimized(a, b, block_size)
}

/// Optimized blocked multiplication with better memory access patterns
fn ultra_matmul_blocked_optimized<T>(
    a: ArrayView2<T>,
    b: ArrayView2<T>,
    block_size: usize,
) -> Array2<T>
where
    T: Clone + Default + Zero + One + std::ops::Add<Output = T> + std::ops::Mul<Output = T>,
{
    let (m, k) = a.dim();
    let (_, n) = b.dim();
    let mut result = Array2::<T>::zeros((m, n));

    // Use j-k-i order for better cache locality
    for j_block in (0..n).step_by(block_size) {
        for k_block in (0..k).step_by(block_size) {
            for i_block in (0..m).step_by(block_size) {
                let i_end = (i_block + block_size).min(m);
                let j_end = (j_block + block_size).min(n);
                let k_end = (k_block + block_size).min(k);

                // Inner block computation with optimal access pattern
                for j in j_block..j_end {
                    for k_idx in k_block..k_end {
                        let b_val = &b[[k_idx, j]];
                        for i in i_block..i_end {
                            result[[i, j]] =
                                result[[i, j]].clone() + a[[i, k_idx]].clone() * b_val.clone();
                        }
                    }
                }
            }
        }
    }

    result
}

/// Adaptive batch matrix multiplication
fn ultra_matmul_batch_adaptive<T>(
    a: &ArrayD<T>,
    b: &ArrayD<T>,
    result_shape: &[usize],
) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Zero
        + One
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>
        + Send
        + Sync
        + 'static,
{
    // Implement batch multiplication directly with ultra-optimized kernels
    let a_shape = a.shape();
    let b_shape = b.shape();
    let a_ndim = a_shape.len();
    let b_ndim = b_shape.len();

    // Get matrix dimensions
    let m = a_shape[a_ndim - 2];
    let k = a_shape[a_ndim - 1];
    let n = b_shape[b_ndim - 1];

    // Create result array
    let mut result = ArrayD::zeros(scirs2_core::ndarray::IxDyn(result_shape));

    // Get number of batch elements
    let batch_size: usize = result_shape[..result_shape.len() - 2].iter().product();

    // Iterate over batch dimensions using ultra-optimized kernels
    for batch_idx in 0..batch_size {
        // Convert linear batch index to multi-dimensional index
        let mut batch_indices = vec![0; result_shape.len() - 2];
        let mut idx = batch_idx;
        for i in (0..batch_indices.len()).rev() {
            batch_indices[i] = idx % result_shape[i];
            idx /= result_shape[i];
        }

        // Compute indices for a and b considering broadcasting
        let a_indices = compute_broadcast_indices(&batch_indices, &a_shape[..a_ndim - 2]);
        let b_indices = compute_broadcast_indices(&batch_indices, &b_shape[..b_ndim - 2]);

        // Extract 2D matrices
        let a_mat = extract_2d_slice(a, &a_indices, m, k);
        let b_mat = extract_2d_slice(b, &b_indices, k, n);

        // Perform ultra-optimized 2D matrix multiplication
        let c_mat = ultra_matmul_2d_raw(&a_mat, &b_mat);

        // Store result
        store_2d_slice(&mut result, &batch_indices, &c_mat);
    }

    Ok(Tensor::from_array(result))
}

/// Performance metrics logging
fn log_performance_metrics(m: usize, n: usize, k: usize, elapsed: Duration) {
    let ops = 2 * m * n * k; // FLOPs for matrix multiplication
    let gflops = ops as f64 / elapsed.as_secs_f64() / 1e9;

    // Log to metrics system when available
    println!(
        "Ultra MatMul Performance: {}x{}x{} = {:.2} GFLOPs in {:.2}ms",
        m,
        n,
        k,
        gflops,
        elapsed.as_secs_f64() * 1000.0
    );
}

/// Compute matmul output shape (implement directly)
fn compute_matmul_shape(a_shape: &[usize], b_shape: &[usize]) -> Result<Vec<usize>> {
    let a_ndim = a_shape.len();
    let b_ndim = b_shape.len();

    // Get batch dimensions (all except last 2)
    let a_batch = &a_shape[..a_ndim - 2];
    let b_batch = &b_shape[..b_ndim - 2];

    // Compute broadcast shape for batch dimensions
    let batch_shape = broadcast_shapes(a_batch, b_batch)?;

    // Add matrix dimensions
    let mut result_shape = batch_shape;
    result_shape.push(a_shape[a_ndim - 2]); // m
    result_shape.push(b_shape[b_ndim - 1]); // n

    Ok(result_shape)
}

/// Broadcast two shapes according to numpy broadcasting rules
fn broadcast_shapes(a: &[usize], b: &[usize]) -> Result<Vec<usize>> {
    let max_len = a.len().max(b.len());
    let mut result = Vec::with_capacity(max_len);

    // Iterate from right to left
    for i in 0..max_len {
        let a_dim = if i < a.len() { a[a.len() - 1 - i] } else { 1 };
        let b_dim = if i < b.len() { b[b.len() - 1 - i] } else { 1 };

        if a_dim == b_dim {
            result.push(a_dim);
        } else if a_dim == 1 {
            result.push(b_dim);
        } else if b_dim == 1 {
            result.push(a_dim);
        } else {
            return Err(TensorError::invalid_argument(format!(
                "Cannot broadcast shapes {a:?} and {b:?}"
            )));
        }
    }

    result.reverse();
    Ok(result)
}

/// Compute broadcast indices for accessing array elements
fn compute_broadcast_indices(indices: &[usize], shape: &[usize]) -> Vec<usize> {
    let mut result = vec![0; shape.len()];
    let offset = indices.len() - shape.len();

    for i in 0..shape.len() {
        if i >= offset {
            let idx = indices[i - offset];
            result[i] = if shape[i] == 1 { 0 } else { idx };
        }
    }

    result
}

/// Extract a 2D slice from a multi-dimensional array
fn extract_2d_slice<T: Clone + Zero>(
    arr: &ArrayD<T>,
    batch_indices: &[usize],
    rows: usize,
    cols: usize,
) -> Array2<T> {
    let mut result = Array2::zeros((rows, cols));

    for i in 0..rows {
        for j in 0..cols {
            let mut indices = batch_indices.to_vec();
            indices.push(i);
            indices.push(j);

            if let Some(val) = arr.get(indices.as_slice()) {
                result[[i, j]] = val.clone();
            }
        }
    }

    result
}

/// Store a 2D matrix into a multi-dimensional array
fn store_2d_slice<T: Clone>(arr: &mut ArrayD<T>, batch_indices: &[usize], mat: &Array2<T>) {
    let (rows, cols) = mat.dim();

    for i in 0..rows {
        for j in 0..cols {
            let mut indices = batch_indices.to_vec();
            indices.push(i);
            indices.push(j);

            if let Some(dst) = arr.get_mut(indices.as_slice()) {
                *dst = mat[[i, j]].clone();
            }
        }
    }
}

/// Ultra-optimized 2D matrix multiplication
fn ultra_matmul_2d_raw<T>(a: &Array2<T>, b: &Array2<T>) -> Array2<T>
where
    T: Clone
        + Default
        + Zero
        + One
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>
        + 'static,
{
    let (m, _k) = a.dim();
    let (_, n) = b.dim();

    // For f32, use SIMD optimizations
    if std::any::TypeId::of::<T>() == std::any::TypeId::of::<f32>() {
        return ultra_matmul_f32_simd(a.view(), b.view());
    }

    // Use cache-optimized blocked multiplication for other types
    if m > 128 && n > 128 {
        ultra_matmul_blocked_optimized(a.view(), b.view(), 64)
    } else {
        ultra_matmul_small_cache_simd(a.view(), b.view())
    }
}

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

    #[test]
    fn test_ultra_matmul_basic() {
        let a = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2])
            .expect("test: from_vec should succeed");
        let b = Tensor::<f32>::from_vec(vec![5.0, 6.0, 7.0, 8.0], &[2, 2])
            .expect("test: from_vec should succeed");

        let result = ultra_matmul(&a, &b).expect("test: ultra_matmul should succeed");
        assert_eq!(result.shape().dims(), &[2, 2]);

        // Expected: [[1*5+2*7, 1*6+2*8], [3*5+4*7, 3*6+4*8]]
        //         = [[19, 22], [43, 50]]
        if let Some(data) = result.as_slice() {
            assert_eq!(data, &[19.0, 22.0, 43.0, 50.0]);
        }
    }

    #[test]
    fn test_ultra_matmul_large() {
        // Test large matrix performance
        let size = 128;
        let a_data: Vec<f32> = (0..size * size).map(|i| i as f32).collect();
        let b_data: Vec<f32> = (0..size * size).map(|i| (i + 1) as f32).collect();

        let a =
            Tensor::<f32>::from_vec(a_data, &[size, size]).expect("test: from_vec should succeed");
        let b =
            Tensor::<f32>::from_vec(b_data, &[size, size]).expect("test: from_vec should succeed");

        let start = Instant::now();
        let _result = ultra_matmul(&a, &b).expect("test: ultra_matmul should succeed");
        let elapsed = start.elapsed();

        println!(
            "Ultra MatMul {}x{} completed in {:.2}ms",
            size,
            size,
            elapsed.as_secs_f64() * 1000.0
        );
    }

    #[test]
    fn test_ultra_matmul_simd_f32() {
        // Test SIMD optimizations for f32
        let a = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3])
            .expect("test: from_vec should succeed");
        let b = Tensor::<f32>::from_vec(vec![7.0, 8.0, 9.0, 10.0, 11.0, 12.0], &[3, 2])
            .expect("test: from_vec should succeed");

        let result = ultra_matmul(&a, &b).expect("test: ultra_matmul should succeed");
        assert_eq!(result.shape().dims(), &[2, 2]);

        // Expected: [[58, 64], [139, 154]]
        if let Some(data) = result.as_slice() {
            assert_eq!(data, &[58.0, 64.0, 139.0, 154.0]);
        }
    }
}