numrs2 0.3.1

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Cache Alignment Performance Benchmark
//!
//! Comprehensive benchmarks measuring the actual performance impact of cache alignment
//! optimizations (`#[repr(align(64))]`) on:
//! 1. False sharing elimination in parallel workloads
//! 2. SIMD load/store performance (aligned vs unaligned)
//! 3. Parallel configuration access patterns
//! 4. Cache line contention with thread scaling
//!
//! These benchmarks complement the memory_optimization_benchmark by specifically
//! isolating cache alignment effects rather than allocation reduction.
//!
//! Reference: /tmp/CACHE_ALIGNMENT_PERFORMANCE_IMPACT.md

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use numrs2::parallel_optimize::{ParallelConfig, SchedulingStrategy};
use std::hint::black_box;
use std::sync::Arc;
use std::thread;

// Import SIMD operations from SciRS2 ecosystem
use scirs2_core::ndarray::ArrayView1;
use scirs2_core::simd::reductions::{simd_max_f64, simd_min_f64, simd_sum_f64};

/// Test data size for SIMD benchmarks (must be divisible by 8 for AVX operations)
const SIMD_TEST_SIZE: usize = 1024;

/// Thread counts to test for parallel benchmarks
const THREAD_COUNTS: &[usize] = &[1, 2, 4, 8];

/// Iteration count for contention tests
const CONTENTION_ITERATIONS: usize = 10_000;

// ============================================================================
// TEST STRUCTURES: Aligned vs Unaligned
// ============================================================================

/// Aligned counter structure (occupies full cache line, prevents false sharing)
#[repr(align(64))]
struct AlignedCounter {
    value: std::sync::atomic::AtomicU64,
    _pad: [u8; 56], // Pad to 64 bytes (8 + 56 = 64)
}

impl Default for AlignedCounter {
    fn default() -> Self {
        Self {
            value: std::sync::atomic::AtomicU64::new(0),
            _pad: [0; 56],
        }
    }
}

/// Unaligned counter structure (may share cache line, causes false sharing)
#[derive(Default)]
struct UnalignedCounter {
    value: std::sync::atomic::AtomicU64,
}

/// Aligned data array for SIMD operations
#[repr(align(64))]
struct AlignedArray {
    data: [f64; SIMD_TEST_SIZE],
}

impl AlignedArray {
    fn new() -> Self {
        Self {
            data: [0.0; SIMD_TEST_SIZE],
        }
    }

    fn init_with_pattern(&mut self) {
        for (i, val) in self.data.iter_mut().enumerate() {
            *val = (i as f64) * 0.5;
        }
    }
}

/// Unaligned data array for SIMD operations
struct UnalignedArray {
    data: [f64; SIMD_TEST_SIZE],
}

impl UnalignedArray {
    fn new() -> Self {
        Self {
            data: [0.0; SIMD_TEST_SIZE],
        }
    }

    fn init_with_pattern(&mut self) {
        for (i, val) in self.data.iter_mut().enumerate() {
            *val = (i as f64) * 0.5;
        }
    }
}

// ============================================================================
// BENCHMARK 1: False Sharing in Parallel Counters
// ============================================================================

/// Benchmark parallel access to ALIGNED counters (no false sharing expected)
///
/// Expected: Linear scaling with thread count, minimal cache coherency overhead
fn bench_false_sharing_aligned(c: &mut Criterion) {
    let mut group = c.benchmark_group("false_sharing/aligned");

    for &num_threads in THREAD_COUNTS.iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(num_threads),
            &num_threads,
            |bencher, &num_threads| {
                bencher.iter(|| {
                    // Create array of aligned counters
                    let counters: Vec<AlignedCounter> = (0..num_threads)
                        .map(|_| AlignedCounter::default())
                        .collect();
                    let counters = Arc::new(counters);

                    // Spawn threads, each incrementing its own counter
                    let handles: Vec<_> = (0..num_threads)
                        .map(|thread_id| {
                            let counters = Arc::clone(&counters);
                            thread::spawn(move || {
                                for _ in 0..CONTENTION_ITERATIONS {
                                    counters[thread_id]
                                        .value
                                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                                }
                            })
                        })
                        .collect();

                    // Wait for all threads
                    for handle in handles {
                        handle.join().unwrap();
                    }

                    // Verify result
                    let sum: u64 = counters
                        .iter()
                        .map(|c| c.value.load(std::sync::atomic::Ordering::Relaxed))
                        .sum();
                    black_box(sum);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark parallel access to UNALIGNED counters (false sharing expected)
///
/// Expected: Poor scaling with thread count, high cache coherency overhead
fn bench_false_sharing_unaligned(c: &mut Criterion) {
    let mut group = c.benchmark_group("false_sharing/unaligned");

    for &num_threads in THREAD_COUNTS.iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(num_threads),
            &num_threads,
            |bencher, &num_threads| {
                bencher.iter(|| {
                    // Create array of unaligned counters (packed tightly, causing false sharing)
                    let counters: Vec<UnalignedCounter> = (0..num_threads)
                        .map(|_| UnalignedCounter::default())
                        .collect();
                    let counters = Arc::new(counters);

                    // Spawn threads, each incrementing its own counter
                    let handles: Vec<_> = (0..num_threads)
                        .map(|thread_id| {
                            let counters = Arc::clone(&counters);
                            thread::spawn(move || {
                                for _ in 0..CONTENTION_ITERATIONS {
                                    counters[thread_id]
                                        .value
                                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                                }
                            })
                        })
                        .collect();

                    // Wait for all threads
                    for handle in handles {
                        handle.join().unwrap();
                    }

                    // Verify result
                    let sum: u64 = counters
                        .iter()
                        .map(|c| c.value.load(std::sync::atomic::Ordering::Relaxed))
                        .sum();
                    black_box(sum);
                });
            },
        );
    }

    group.finish();
}

// ============================================================================
// BENCHMARK 2: SIMD Loads (Aligned vs Unaligned)
// ============================================================================

/// Benchmark SIMD operations on ALIGNED arrays
///
/// Expected: Optimal SIMD performance, single instruction per 64-byte load
fn bench_simd_aligned_loads(c: &mut Criterion) {
    let mut group = c.benchmark_group("simd_loads/aligned");
    group.throughput(Throughput::Elements(SIMD_TEST_SIZE as u64));

    let mut array = AlignedArray::new();
    array.init_with_pattern();

    group.bench_function("simd_sum", |bencher| {
        bencher.iter(|| {
            // Use SciRS2 SIMD operations (follows SCIRS2 POLICY)
            let view = ArrayView1::from(&array.data);
            let sum: f64 = simd_sum_f64(&view);
            black_box(sum);
        });
    });

    group.bench_function("simd_min", |bencher| {
        bencher.iter(|| {
            let view = ArrayView1::from(&array.data);
            let min: f64 = simd_min_f64(&view);
            black_box(min);
        });
    });

    group.bench_function("simd_max", |bencher| {
        bencher.iter(|| {
            let view = ArrayView1::from(&array.data);
            let max: f64 = simd_max_f64(&view);
            black_box(max);
        });
    });

    group.finish();
}

/// Benchmark SIMD operations on UNALIGNED arrays
///
/// Expected: Degraded SIMD performance, potential multiple micro-ops per load
fn bench_simd_unaligned_loads(c: &mut Criterion) {
    let mut group = c.benchmark_group("simd_loads/unaligned");
    group.throughput(Throughput::Elements(SIMD_TEST_SIZE as u64));

    let mut array = UnalignedArray::new();
    array.init_with_pattern();

    group.bench_function("simd_sum", |bencher| {
        bencher.iter(|| {
            let view = ArrayView1::from(&array.data);
            let sum: f64 = simd_sum_f64(&view);
            black_box(sum);
        });
    });

    group.bench_function("simd_min", |bencher| {
        bencher.iter(|| {
            let view = ArrayView1::from(&array.data);
            let min: f64 = simd_min_f64(&view);
            black_box(min);
        });
    });

    group.bench_function("simd_max", |bencher| {
        bencher.iter(|| {
            let view = ArrayView1::from(&array.data);
            let max: f64 = simd_max_f64(&view);
            black_box(max);
        });
    });

    group.finish();
}

// ============================================================================
// BENCHMARK 3: ParallelConfig Contention
// ============================================================================

/// Benchmark multiple threads reading ALIGNED ParallelConfig
///
/// Expected: Minimal contention, each thread caches config independently
fn bench_parallel_config_aligned(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_config/aligned");

    // ParallelConfig is already aligned to 64 bytes in the source code
    let config = Arc::new(ParallelConfig {
        use_parallel: true,
        min_parallel_size: 1000,
        chunk_size: 250,
        max_threads: Some(8),
        scheduling_strategy: SchedulingStrategy::Adaptive,
    });

    for &num_threads in THREAD_COUNTS.iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(num_threads),
            &num_threads,
            |bencher, &num_threads| {
                bencher.iter(|| {
                    let handles: Vec<_> = (0..num_threads)
                        .map(|_| {
                            let config = Arc::clone(&config);
                            thread::spawn(move || {
                                let mut sum = 0usize;
                                for _ in 0..CONTENTION_ITERATIONS {
                                    // Read config fields (simulating typical access pattern)
                                    sum += config.min_parallel_size;
                                    sum += config.chunk_size;
                                    if config.use_parallel {
                                        sum += 1;
                                    }
                                }
                                black_box(sum);
                            })
                        })
                        .collect();

                    for handle in handles {
                        handle.join().unwrap();
                    }
                });
            },
        );
    }

    group.finish();
}

/// Benchmark to demonstrate what would happen with UNALIGNED config
///
/// Note: We can't directly create an unaligned ParallelConfig since it's
/// already aligned in the source. This benchmark uses an unaligned struct
/// with the same size to simulate the effect.
fn bench_parallel_config_unaligned(c: &mut Criterion) {
    #[derive(Clone)]
    struct UnalignedConfig {
        use_parallel: bool,
        min_parallel_size: usize,
        chunk_size: usize,
        max_threads: Option<usize>,
        _extra: u32, // Make it similar size
    }

    let mut group = c.benchmark_group("parallel_config/unaligned");

    let config = Arc::new(UnalignedConfig {
        use_parallel: true,
        min_parallel_size: 1000,
        chunk_size: 250,
        max_threads: Some(8),
        _extra: 0,
    });

    for &num_threads in THREAD_COUNTS.iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(num_threads),
            &num_threads,
            |bencher, &num_threads| {
                bencher.iter(|| {
                    let handles: Vec<_> = (0..num_threads)
                        .map(|_| {
                            let config = Arc::clone(&config);
                            thread::spawn(move || {
                                let mut sum = 0usize;
                                for _ in 0..CONTENTION_ITERATIONS {
                                    sum += config.min_parallel_size;
                                    sum += config.chunk_size;
                                    if config.use_parallel {
                                        sum += 1;
                                    }
                                }
                                black_box(sum);
                            })
                        })
                        .collect();

                    for handle in handles {
                        handle.join().unwrap();
                    }
                });
            },
        );
    }

    group.finish();
}

// ============================================================================
// BENCHMARK 4: Cache Line Contention with Mixed Access
// ============================================================================

/// Benchmark mixed read/write access to aligned shared state
fn bench_cache_contention_aligned(c: &mut Criterion) {
    let mut group = c.benchmark_group("cache_contention/aligned");

    #[repr(align(64))]
    struct AlignedSharedState {
        counter: std::sync::atomic::AtomicU64,
        _pad: [u8; 56],
    }

    for &num_threads in THREAD_COUNTS.iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(num_threads),
            &num_threads,
            |bencher, &num_threads| {
                bencher.iter(|| {
                    let state = Arc::new(AlignedSharedState {
                        counter: std::sync::atomic::AtomicU64::new(0),
                        _pad: [0; 56],
                    });

                    let handles: Vec<_> = (0..num_threads)
                        .map(|_| {
                            let state = Arc::clone(&state);
                            thread::spawn(move || {
                                for _ in 0..CONTENTION_ITERATIONS {
                                    // Read
                                    let val =
                                        state.counter.load(std::sync::atomic::Ordering::Acquire);
                                    // Write
                                    state
                                        .counter
                                        .store(val + 1, std::sync::atomic::Ordering::Release);
                                }
                            })
                        })
                        .collect();

                    for handle in handles {
                        handle.join().unwrap();
                    }

                    let final_val = state.counter.load(std::sync::atomic::Ordering::Relaxed);
                    black_box(final_val);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark mixed read/write access to unaligned shared state
fn bench_cache_contention_unaligned(c: &mut Criterion) {
    let mut group = c.benchmark_group("cache_contention/unaligned");

    struct UnalignedSharedState {
        counter: std::sync::atomic::AtomicU64,
    }

    for &num_threads in THREAD_COUNTS.iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(num_threads),
            &num_threads,
            |bencher, &num_threads| {
                bencher.iter(|| {
                    let state = Arc::new(UnalignedSharedState {
                        counter: std::sync::atomic::AtomicU64::new(0),
                    });

                    let handles: Vec<_> = (0..num_threads)
                        .map(|_| {
                            let state = Arc::clone(&state);
                            thread::spawn(move || {
                                for _ in 0..CONTENTION_ITERATIONS {
                                    // Read
                                    let val =
                                        state.counter.load(std::sync::atomic::Ordering::Acquire);
                                    // Write
                                    state
                                        .counter
                                        .store(val + 1, std::sync::atomic::Ordering::Release);
                                }
                            })
                        })
                        .collect();

                    for handle in handles {
                        handle.join().unwrap();
                    }

                    let final_val = state.counter.load(std::sync::atomic::Ordering::Relaxed);
                    black_box(final_val);
                });
            },
        );
    }

    group.finish();
}

// ============================================================================
// BENCHMARK 5: Array Stride Access (Cache Miss Patterns)
// ============================================================================

/// Benchmark sequential access to aligned arrays (cache-friendly)
fn bench_stride_aligned_sequential(c: &mut Criterion) {
    let mut group = c.benchmark_group("stride_access/aligned_sequential");
    group.throughput(Throughput::Elements(SIMD_TEST_SIZE as u64));

    let mut array = AlignedArray::new();
    array.init_with_pattern();

    group.bench_function("sum", |bencher| {
        bencher.iter(|| {
            let sum: f64 = array.data.iter().sum();
            black_box(sum);
        });
    });

    group.finish();
}

/// Benchmark strided access to aligned arrays (potential cache misses)
fn bench_stride_aligned_strided(c: &mut Criterion) {
    let mut group = c.benchmark_group("stride_access/aligned_strided");
    group.throughput(Throughput::Elements((SIMD_TEST_SIZE / 8) as u64));

    let mut array = AlignedArray::new();
    array.init_with_pattern();

    group.bench_function("sum_stride_8", |bencher| {
        bencher.iter(|| {
            let sum: f64 = array.data.iter().step_by(8).sum();
            black_box(sum);
        });
    });

    group.finish();
}

/// Benchmark sequential access to unaligned arrays
fn bench_stride_unaligned_sequential(c: &mut Criterion) {
    let mut group = c.benchmark_group("stride_access/unaligned_sequential");
    group.throughput(Throughput::Elements(SIMD_TEST_SIZE as u64));

    let mut array = UnalignedArray::new();
    array.init_with_pattern();

    group.bench_function("sum", |bencher| {
        bencher.iter(|| {
            let sum: f64 = array.data.iter().sum();
            black_box(sum);
        });
    });

    group.finish();
}

/// Benchmark strided access to unaligned arrays
fn bench_stride_unaligned_strided(c: &mut Criterion) {
    let mut group = c.benchmark_group("stride_access/unaligned_strided");
    group.throughput(Throughput::Elements((SIMD_TEST_SIZE / 8) as u64));

    let mut array = UnalignedArray::new();
    array.init_with_pattern();

    group.bench_function("sum_stride_8", |bencher| {
        bencher.iter(|| {
            let sum: f64 = array.data.iter().step_by(8).sum();
            black_box(sum);
        });
    });

    group.finish();
}

// ============================================================================
// CRITERION CONFIGURATION
// ============================================================================

criterion_group!(
    cache_benches,
    // False sharing benchmarks
    bench_false_sharing_aligned,
    bench_false_sharing_unaligned,
    // SIMD alignment benchmarks
    bench_simd_aligned_loads,
    bench_simd_unaligned_loads,
    // ParallelConfig contention benchmarks
    bench_parallel_config_aligned,
    bench_parallel_config_unaligned,
    // Cache line contention benchmarks
    bench_cache_contention_aligned,
    bench_cache_contention_unaligned,
    // Stride access pattern benchmarks
    bench_stride_aligned_sequential,
    bench_stride_aligned_strided,
    bench_stride_unaligned_sequential,
    bench_stride_unaligned_strided,
);

criterion_main!(cache_benches);