scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! Hardware-specific SIMD optimizations
//!
//! This module provides optimized implementations for specific CPU architectures
//! including Intel AVX/AVX2/AVX-512 and ARM Neon instruction sets.

use crate::error::{LinalgError, LinalgResult};
use scirs2_core::ndarray::{s, Array1, Array2, ArrayView1, ArrayView2};
use scirs2_core::numeric::{Float, NumAssign, One, Zero};
use scirs2_core::parallel_ops::*;

/// Hardware capabilities detection
#[derive(Debug, Clone, Copy)]
pub struct HardwareCapabilities {
    pub has_avx: bool,
    pub has_avx2: bool,
    pub has_avx512: bool,
    pub has_fma: bool,
    pub has_neon: bool,
}

impl HardwareCapabilities {
    /// Detect available hardware features
    pub fn detect() -> Self {
        HardwareCapabilities {
            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
            has_avx: is_x86_feature_detected!("avx"),
            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
            has_avx: false,

            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
            has_avx2: is_x86_feature_detected!("avx2"),
            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
            has_avx2: false,

            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
            has_avx512: is_x86_feature_detected!("avx512f"),
            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
            has_avx512: false,

            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
            has_fma: is_x86_feature_detected!("fma"),
            #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
            has_fma: false,

            has_neon: cfg!(target_arch = "aarch64"),
        }
    }

    /// Get the optimal vector width for the current hardware
    pub fn optimal_vector_width(&self) -> usize {
        if self.has_avx512 {
            64 // AVX-512 uses 512-bit vectors (64 bytes)
        } else if self.has_avx2 || self.has_avx {
            32 // AVX/AVX2 uses 256-bit vectors (32 bytes)
        } else {
            16 // ARM Neon and SSE both use 128-bit vectors (16 bytes)
        }
    }
}

/// Hardware-optimized dot product computation
#[allow(dead_code)]
pub fn hardware_optimized_dot<F>(
    x: &ArrayView1<F>,
    y: &ArrayView1<F>,
    capabilities: &HardwareCapabilities,
) -> LinalgResult<F>
where
    F: Float + NumAssign + Zero + Send + Sync + 'static,
{
    if x.len() != y.len() {
        return Err(LinalgError::ShapeError(format!(
            "Vector lengths must match: {} vs {}",
            x.len(),
            y.len()
        )));
    }

    let n = x.len();
    let mut result = F::zero();

    // Choose optimization based on available hardware and type
    if std::any::TypeId::of::<F>() == std::any::TypeId::of::<f64>() {
        if capabilities.has_avx2 && n >= 16 {
            #[cfg(target_arch = "x86_64")]
            unsafe {
                let raw_result =
                    avx2_dot_f64(x.as_ptr() as *const f64, y.as_ptr() as *const f64, n)?;
                result = F::from(raw_result).unwrap_or(F::zero());
            }
        } else if capabilities.has_neon && cfg!(target_arch = "aarch64") && n >= 8 {
            #[cfg(target_arch = "aarch64")]
            unsafe {
                let raw_result =
                    neon_dot_f64(x.as_ptr() as *const f64, y.as_ptr() as *const f64, n)?;
                result = F::from(raw_result).unwrap_or(F::zero());
            }
        } else {
            // Fallback to standard implementation
            for i in 0..n {
                result += x[i] * y[i];
            }
        }
    } else if std::any::TypeId::of::<F>() == std::any::TypeId::of::<f32>() {
        if capabilities.has_avx2 && n >= 32 {
            #[cfg(target_arch = "x86_64")]
            unsafe {
                let raw_result =
                    avx2_dot_f32(x.as_ptr() as *const f32, y.as_ptr() as *const f32, n)?;
                result = F::from(raw_result).unwrap_or(F::zero());
            }
        } else if capabilities.has_neon && cfg!(target_arch = "aarch64") && n >= 16 {
            #[cfg(target_arch = "aarch64")]
            unsafe {
                let raw_result =
                    neon_dot_f32(x.as_ptr() as *const f32, y.as_ptr() as *const f32, n)?;
                result = F::from(raw_result).unwrap_or(F::zero());
            }
        } else {
            // Fallback to standard implementation
            for i in 0..n {
                result += x[i] * y[i];
            }
        }
    } else {
        // Fallback for other types
        for i in 0..n {
            result += x[i] * y[i];
        }
    }

    Ok(result)
}

/// Hardware-optimized matrix-vector multiplication
#[allow(dead_code)]
pub fn hardware_optimized_matvec<F>(
    a: &ArrayView2<F>,
    x: &ArrayView1<F>,
    capabilities: &HardwareCapabilities,
) -> LinalgResult<Array1<F>>
where
    F: Float + NumAssign + Zero + Send + Sync + 'static,
{
    let (m, n) = a.dim();

    if x.len() != n {
        return Err(LinalgError::ShapeError(format!(
            "Matrix columns ({}) must match vector length ({})",
            n,
            x.len()
        )));
    }

    let mut result = Array1::zeros(m);

    // Choose optimization based on available hardware
    if std::any::TypeId::of::<F>() == std::any::TypeId::of::<f64>() {
        if capabilities.has_avx2 {
            #[cfg(target_arch = "x86_64")]
            unsafe {
                avx2_matvec_f64(
                    a.as_ptr() as *const f64,
                    x.as_ptr() as *const f64,
                    result.as_mut_ptr() as *mut f64,
                    m,
                    n,
                    a.strides()[0],
                )?;
            }
        } else if capabilities.has_neon && cfg!(target_arch = "aarch64") {
            #[cfg(target_arch = "aarch64")]
            unsafe {
                neon_matvec_f64(
                    a.as_ptr() as *const f64,
                    x.as_ptr() as *const f64,
                    result.as_mut_ptr() as *mut f64,
                    m,
                    n,
                    a.strides()[0],
                )?;
            }
        } else {
            // Fallback to standard implementation
            return standard_matvec(a, x);
        }
    } else if std::any::TypeId::of::<F>() == std::any::TypeId::of::<f32>() {
        if capabilities.has_avx2 {
            #[cfg(target_arch = "x86_64")]
            unsafe {
                avx2_matvec_f32(
                    a.as_ptr() as *const f32,
                    x.as_ptr() as *const f32,
                    result.as_mut_ptr() as *mut f32,
                    m,
                    n,
                    a.strides()[0],
                )?;
            }
        } else if capabilities.has_neon && cfg!(target_arch = "aarch64") {
            #[cfg(target_arch = "aarch64")]
            unsafe {
                neon_matvec_f32(
                    a.as_ptr() as *const f32,
                    x.as_ptr() as *const f32,
                    result.as_mut_ptr() as *mut f32,
                    m,
                    n,
                    a.strides()[0],
                )?;
            }
        } else {
            // Fallback to standard implementation
            return standard_matvec(a, x);
        }
    } else {
        // Fallback for other types
        return standard_matvec(a, x);
    }

    Ok(result)
}

/// Standard fallback matrix-vector multiplication
#[allow(dead_code)]
fn standard_matvec<F>(a: &ArrayView2<F>, x: &ArrayView1<F>) -> LinalgResult<Array1<F>>
where
    F: Float + NumAssign + Zero,
{
    let (m, n) = a.dim();
    let mut result = Array1::zeros(m);

    for i in 0..m {
        let mut sum = F::zero();
        for j in 0..n {
            sum += a[[i, j]] * x[j];
        }
        result[i] = sum;
    }

    Ok(result)
}

/// AVX2-optimized matrix-vector multiplication for f64
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2", enable = "fma")]
unsafe fn avx2_matvec_f64(
    a_ptr: *const f64,
    x_ptr: *const f64,
    y_ptr: *mut f64,
    m: usize,
    n: usize,
    a_stride: isize,
) -> LinalgResult<()> {
    use std::arch::x86_64::*;

    const BLOCK_SIZE: usize = 4; // AVX2 can process 4 f64 values at once

    for i in 0..m {
        let row_ptr = a_ptr.offset(i as isize * a_stride);
        let mut sum = _mm256_setzero_pd();

        // Process 4 elements at a time
        let mut j = 0;
        while j + BLOCK_SIZE <= n {
            let a_vec = _mm256_loadu_pd(row_ptr.add(j));
            let x_vec = _mm256_loadu_pd(x_ptr.add(j));
            sum = _mm256_fmadd_pd(a_vec, x_vec, sum);
            j += BLOCK_SIZE;
        }

        // Horizontal sum of the 4 elements in sum
        let sum_high = _mm256_extractf128_pd(sum, 1);
        let sum_low = _mm256_castpd256_pd128(sum);
        let sum_quad = _mm_add_pd(sum_low, sum_high);
        let sum_dual = _mm_hadd_pd(sum_quad, sum_quad);
        let mut result = _mm_cvtsd_f64(sum_dual);

        // Handle remaining elements
        while j < n {
            result += *row_ptr.add(j) * *x_ptr.add(j);
            j += 1;
        }

        *y_ptr.add(i) = result;
    }

    Ok(())
}

/// AVX2-optimized matrix-vector multiplication for f32
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2", enable = "fma")]
unsafe fn avx2_matvec_f32(
    a_ptr: *const f32,
    x_ptr: *const f32,
    y_ptr: *mut f32,
    m: usize,
    n: usize,
    a_stride: isize,
) -> LinalgResult<()> {
    use std::arch::x86_64::*;

    const BLOCK_SIZE: usize = 8; // AVX2 can process 8 f32 values at once

    for i in 0..m {
        let row_ptr = a_ptr.offset(i as isize * a_stride);
        let mut sum = _mm256_setzero_ps();

        // Process 8 elements at a time
        let mut j = 0;
        while j + BLOCK_SIZE <= n {
            let a_vec = _mm256_loadu_ps(row_ptr.add(j));
            let x_vec = _mm256_loadu_ps(x_ptr.add(j));
            sum = _mm256_fmadd_ps(a_vec, x_vec, sum);
            j += BLOCK_SIZE;
        }

        // Horizontal sum of the 8 elements in sum
        let sum_high = _mm256_extractf128_ps(sum, 1);
        let sum_low = _mm256_castps256_ps128(sum);
        let sum_quad = _mm_add_ps(sum_low, sum_high);
        let sum_dual = _mm_hadd_ps(sum_quad, sum_quad);
        let sum_final = _mm_hadd_ps(sum_dual, sum_dual);
        let mut result = _mm_cvtss_f32(sum_final);

        // Handle remaining elements
        while j < n {
            result += *row_ptr.add(j) * *x_ptr.add(j);
            j += 1;
        }

        *y_ptr.add(i) = result;
    }

    Ok(())
}

/// ARM Neon-optimized matrix-vector multiplication for f64
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
unsafe fn neon_matvec_f64(
    a_ptr: *const f64,
    x_ptr: *const f64,
    y_ptr: *mut f64,
    m: usize,
    n: usize,
    a_stride: isize,
) -> LinalgResult<()> {
    use std::arch::aarch64::*;

    const BLOCK_SIZE: usize = 2; // Neon can process 2 f64 values at once

    for i in 0..m {
        let row_ptr = a_ptr.offset(i as isize * a_stride);
        let mut sum = vdupq_n_f64(0.0);

        // Process 2 elements at a time
        let mut j = 0;
        while j + BLOCK_SIZE <= n {
            let a_vec = vld1q_f64(row_ptr.add(j));
            let x_vec = vld1q_f64(x_ptr.add(j));
            sum = vfmaq_f64(sum, a_vec, x_vec);
            j += BLOCK_SIZE;
        }

        // Horizontal sum
        let mut result = vaddvq_f64(sum);

        // Handle remaining elements
        while j < n {
            result += *row_ptr.add(j) * *x_ptr.add(j);
            j += 1;
        }

        *y_ptr.add(i) = result;
    }

    Ok(())
}

/// ARM Neon-optimized matrix-vector multiplication for f32
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
unsafe fn neon_matvec_f32(
    a_ptr: *const f32,
    x_ptr: *const f32,
    y_ptr: *mut f32,
    m: usize,
    n: usize,
    a_stride: isize,
) -> LinalgResult<()> {
    use std::arch::aarch64::*;

    const BLOCK_SIZE: usize = 4; // Neon can process 4 f32 values at once

    for i in 0..m {
        let row_ptr = a_ptr.offset(i as isize * a_stride);
        let mut sum = vdupq_n_f32(0.0);

        // Process 4 elements at a time
        let mut j = 0;
        while j + BLOCK_SIZE <= n {
            let a_vec = vld1q_f32(row_ptr.add(j));
            let x_vec = vld1q_f32(x_ptr.add(j));
            sum = vfmaq_f32(sum, a_vec, x_vec);
            j += BLOCK_SIZE;
        }

        // Horizontal sum
        let mut result = vaddvq_f32(sum);

        // Handle remaining elements
        while j < n {
            result += *row_ptr.add(j) * *x_ptr.add(j);
            j += 1;
        }

        *y_ptr.add(i) = result;
    }

    Ok(())
}

/// Standard fallback dot product
#[allow(dead_code)]
fn standard_dot<F>(x: &ArrayView1<F>, y: &ArrayView1<F>) -> F
where
    F: Float + NumAssign + Zero,
{
    let mut result = F::zero();
    for (a, b) in x.iter().zip(y.iter()) {
        result += *a * *b;
    }
    result
}

/// AVX2-optimized dot product for f64
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2", enable = "fma")]
unsafe fn avx2_dot_f64(_x_ptr: *const f64, yptr: *const f64, n: usize) -> LinalgResult<f64> {
    use std::arch::x86_64::*;

    const BLOCK_SIZE: usize = 4;
    let mut sum = _mm256_setzero_pd();

    // Process 4 elements at a time
    let mut i = 0;
    while i + BLOCK_SIZE <= n {
        let x_vec = _mm256_loadu_pd(_x_ptr.add(i));
        let y_vec = _mm256_loadu_pd(yptr.add(i));
        sum = _mm256_fmadd_pd(x_vec, y_vec, sum);
        i += BLOCK_SIZE;
    }

    // Horizontal sum
    let sum_high = _mm256_extractf128_pd(sum, 1);
    let sum_low = _mm256_castpd256_pd128(sum);
    let sum_quad = _mm_add_pd(sum_low, sum_high);
    let sum_dual = _mm_hadd_pd(sum_quad, sum_quad);
    let mut result = _mm_cvtsd_f64(sum_dual);

    // Handle remaining elements
    while i < n {
        result += *_x_ptr.add(i) * *yptr.add(i);
        i += 1;
    }

    Ok(result)
}

/// AVX2-optimized dot product for f32
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2", enable = "fma")]
unsafe fn avx2_dot_f32(_x_ptr: *const f32, yptr: *const f32, n: usize) -> LinalgResult<f32> {
    use std::arch::x86_64::*;

    const BLOCK_SIZE: usize = 8;
    let mut sum = _mm256_setzero_ps();

    // Process 8 elements at a time
    let mut i = 0;
    while i + BLOCK_SIZE <= n {
        let x_vec = _mm256_loadu_ps(_x_ptr.add(i));
        let y_vec = _mm256_loadu_ps(yptr.add(i));
        sum = _mm256_fmadd_ps(x_vec, y_vec, sum);
        i += BLOCK_SIZE;
    }

    // Horizontal sum
    let sum_high = _mm256_extractf128_ps(sum, 1);
    let sum_low = _mm256_castps256_ps128(sum);
    let sum_quad = _mm_add_ps(sum_low, sum_high);
    let sum_dual = _mm_hadd_ps(sum_quad, sum_quad);
    let sum_final = _mm_hadd_ps(sum_dual, sum_dual);
    let mut result = _mm_cvtss_f32(sum_final);

    // Handle remaining elements
    while i < n {
        result += *_x_ptr.add(i) * *yptr.add(i);
        i += 1;
    }

    Ok(result)
}

/// ARM Neon-optimized dot product for f64
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
unsafe fn neon_dot_f64(_x_ptr: *const f64, yptr: *const f64, n: usize) -> LinalgResult<f64> {
    use std::arch::aarch64::*;

    const BLOCK_SIZE: usize = 2;
    let mut sum = vdupq_n_f64(0.0);

    // Process 2 elements at a time
    let mut i = 0;
    while i + BLOCK_SIZE <= n {
        let x_vec = vld1q_f64(_x_ptr.add(i));
        let y_vec = vld1q_f64(yptr.add(i));
        sum = vfmaq_f64(sum, x_vec, y_vec);
        i += BLOCK_SIZE;
    }

    // Horizontal sum
    let mut result = vaddvq_f64(sum);

    // Handle remaining elements
    while i < n {
        result += *_x_ptr.add(i) * *yptr.add(i);
        i += 1;
    }

    Ok(result)
}

/// ARM Neon-optimized dot product for f32
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
unsafe fn neon_dot_f32(_x_ptr: *const f32, yptr: *const f32, n: usize) -> LinalgResult<f32> {
    use std::arch::aarch64::*;

    const BLOCK_SIZE: usize = 4;
    let mut sum = vdupq_n_f32(0.0);

    // Process 4 elements at a time
    let mut i = 0;
    while i + BLOCK_SIZE <= n {
        let x_vec = vld1q_f32(_x_ptr.add(i));
        let y_vec = vld1q_f32(yptr.add(i));
        sum = vfmaq_f32(sum, x_vec, y_vec);
        i += BLOCK_SIZE;
    }

    // Horizontal sum
    let mut result = vaddvq_f32(sum);

    // Handle remaining elements
    while i < n {
        result += *_x_ptr.add(i) * *yptr.add(i);
        i += 1;
    }

    Ok(result)
}

/// SIMD-Parallel hybrid matrix multiplication for large matrices
///
/// This function combines SIMD vectorization with parallel processing
/// to achieve optimal performance for large matrix operations.
#[allow(dead_code)]
pub fn simd_parallel_gemm<F>(
    a: &ArrayView2<F>,
    b: &ArrayView2<F>,
    workers: usize,
    capabilities: &HardwareCapabilities,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Zero + One + Send + Sync + 'static,
{
    let (m, k) = a.dim();
    let (k2, n) = b.dim();

    if k != k2 {
        return Err(LinalgError::ShapeError(format!(
            "Matrix dimensions incompatible: {m}x{k} * {k2}x{n}"
        )));
    }

    // For small matrices, use sequential SIMD
    if m * n < 10000 || workers == 1 {
        return hardware_optimized_gemm(a, b, capabilities);
    }

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

    // Determine optimal block size based on hardware capabilities
    let simd_width = capabilities.optimal_vector_width();
    let cache_blocksize = if capabilities.has_avx2 {
        256 // Medium blocks for AVX2
    } else {
        128 // Smaller blocks for SSE/Neon
    };

    // Parallel block processing using scirs2_core parallel operations
    let chunks: Vec<(usize, usize)> = (0..workers)
        .map(|worker_id| {
            let rows_per_worker = m.div_ceil(workers);
            let start_row = worker_id * rows_per_worker;
            let end_row = ((worker_id + 1) * rows_per_worker).min(m);
            (start_row, end_row)
        })
        .filter(|(start, end)| start < end)
        .collect();

    let results: Vec<Array2<F>> = parallel_map(&chunks, |(start_row, end_row)| {
        let a_block = a.slice(s![*start_row..*end_row, ..]);
        hardware_optimized_gemm_block(&a_block, b, capabilities, cache_blocksize, simd_width)
            .expect("Operation failed")
    });

    // Assemble results
    for (i, (start_row, _)) in chunks.iter().enumerate() {
        let block_result = &results[i];
        for (row_idx, row) in block_result
            .axis_iter(scirs2_core::ndarray::Axis(0))
            .enumerate()
        {
            for (col_idx, &val) in row.iter().enumerate() {
                result[[start_row + row_idx, col_idx]] = val;
            }
        }
    }

    Ok(result)
}

/// Hardware-optimized GEMM for sequential execution
#[allow(dead_code)]
fn hardware_optimized_gemm<F>(
    a: &ArrayView2<F>,
    b: &ArrayView2<F>,
    capabilities: &HardwareCapabilities,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Zero + Send + Sync + 'static,
{
    let (m, k) = a.dim();
    let (_, n) = b.dim();

    let mut result = Array2::zeros((m, n));
    let cache_blocksize = capabilities.optimal_vector_width() * 4;

    // Blocked GEMM with SIMD optimization
    for ii in (0..m).step_by(cache_blocksize) {
        for jj in (0..n).step_by(cache_blocksize) {
            for kk in (0..k).step_by(cache_blocksize) {
                let i_end = (ii + cache_blocksize).min(m);
                let j_end = (jj + cache_blocksize).min(n);
                let k_end = (kk + cache_blocksize).min(k);

                // Process each block using SIMD operations
                for i in ii..i_end {
                    for j in jj..j_end {
                        let a_row = a.slice(s![i, kk..k_end]);
                        let b_col = b.slice(s![kk..k_end, j]);

                        // Use hardware-optimized dot product
                        let dot_result = hardware_optimized_dot(&a_row, &b_col, capabilities)?;
                        result[[i, j]] += dot_result;
                    }
                }
            }
        }
    }

    Ok(result)
}

/// Hardware-optimized block GEMM for worker threads
#[allow(dead_code)]
fn hardware_optimized_gemm_block<F>(
    a: &ArrayView2<F>,
    b: &ArrayView2<F>,
    capabilities: &HardwareCapabilities,
    cache_blocksize: usize,
    _simd_width: usize,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Zero + Send + Sync + 'static,
{
    let (m, k) = a.dim();
    let (_, n) = b.dim();

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

    // Optimized block multiplication
    for ii in (0..m).step_by(cache_blocksize) {
        for jj in (0..n).step_by(cache_blocksize) {
            for kk in (0..k).step_by(cache_blocksize) {
                let i_end = (ii + cache_blocksize).min(m);
                let j_end = (jj + cache_blocksize).min(n);
                let k_end = (kk + cache_blocksize).min(k);

                for i in ii..i_end {
                    for j in jj..j_end {
                        let a_row = a.slice(s![i, kk..k_end]);
                        let b_col = b.slice(s![kk..k_end, j]);

                        let dot_result = hardware_optimized_dot(&a_row, &b_col, capabilities)?;
                        result[[i, j]] += dot_result;
                    }
                }
            }
        }
    }

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_hardware_capabilities_detection() {
        let caps = HardwareCapabilities::detect();
        // Just ensure it doesn't panic and returns reasonable values
        assert!(caps.optimal_vector_width() >= 16);
        assert!(caps.optimal_vector_width() <= 64);
    }

    #[test]
    fn test_hardware_optimized_dot_product() {
        let caps = HardwareCapabilities::detect();
        let x = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let y = array![8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0];

        let result = hardware_optimized_dot(&x.view(), &y.view(), &caps).expect("Operation failed");
        let expected = 120.0; // 1*8 + 2*7 + 3*6 + 4*5 + 5*4 + 6*3 + 7*2 + 8*1

        assert_abs_diff_eq!(result, expected, epsilon = 1e-10);
    }

    #[test]
    fn test_hardware_optimized_matvec() {
        let caps = HardwareCapabilities::detect();
        let a = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let x = array![1.0, 2.0, 3.0];

        let result =
            hardware_optimized_matvec(&a.view(), &x.view(), &caps).expect("Operation failed");
        let expected = array![14.0, 32.0]; // [1*1+2*2+3*3, 4*1+5*2+6*3]

        for (actual, expected) in result.iter().zip(expected.iter()) {
            assert_abs_diff_eq!(*actual, *expected, epsilon = 1e-10);
        }
    }
}