numrs2 0.3.2

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
//! Parallel Algorithms Performance Benchmarks
//!
//! Comprehensive benchmarks for parallel algorithm implementations:
//! - Parallel map, reduce, filter, sort operations
//! - Thread scaling analysis (1, 2, 4, 8 threads)
//! - Work distribution and load balancing
//! - Sequential vs parallel comparison
//! - Strong and weak scaling efficiency

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use numrs2::parallel::parallel_algorithms::{ParallelArrayOps, ParallelConfig};
use std::hint::black_box;

/// Array sizes for testing
const SIZES: &[usize] = &[10_000, 100_000, 1_000_000, 10_000_000];

/// Thread counts for scaling tests
const THREAD_COUNTS: &[usize] = &[1, 2, 4, 8];

/// Create parallel configuration with specified thread count
fn create_config(num_threads: usize) -> ParallelConfig {
    ParallelConfig {
        num_threads: Some(num_threads),
        parallel_threshold: 1000,
        block_size: 64,
        numa_aware: false,
        chunk_size: 256,
    }
}

/// Benchmark parallel map operation with different thread counts
fn bench_parallel_map_scaling(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_map_scaling");

    for size in [100_000, 1_000_000, 10_000_000].iter() {
        group.throughput(Throughput::Elements(*size as u64));

        let input: Vec<f64> = (0..*size).map(|i| i as f64).collect();

        for &num_threads in THREAD_COUNTS.iter() {
            let config = create_config(num_threads);
            let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");
            let mut output = vec![0.0f64; *size];

            group.bench_with_input(
                BenchmarkId::new("threads", format!("{}t_{}", num_threads, size)),
                &num_threads,
                |bencher, _| {
                    bencher.iter(|| {
                        // Complex computation: sqrt + sin + cos
                        ops.parallel_map(&input, &mut output, |x| x.sqrt().sin() + x.cos())
                            .expect("parallel_map should succeed");
                        black_box(&output);
                    });
                },
            );
        }
    }

    group.finish();
}

/// Benchmark parallel reduce operation
fn bench_parallel_reduce(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_reduce");

    for size in SIZES.iter() {
        group.throughput(Throughput::Elements(*size as u64));

        let data: Vec<f64> = (0..*size).map(|i| i as f64).collect();

        // Sum reduction
        for &num_threads in THREAD_COUNTS.iter() {
            let config = create_config(num_threads);
            let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

            group.bench_with_input(
                BenchmarkId::new("sum", format!("{}t_{}", num_threads, size)),
                &num_threads,
                |bencher, _| {
                    bencher.iter(|| {
                        let result = ops
                            .parallel_reduce(&data, 0.0, |a, b| a + b)
                            .expect("parallel_reduce should succeed");
                        black_box(result);
                    });
                },
            );
        }

        // Product reduction (limited size to avoid overflow)
        if *size <= 100_000 {
            for &num_threads in THREAD_COUNTS.iter() {
                let config = create_config(num_threads);
                let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

                // Use small values to avoid overflow
                let small_data: Vec<f64> = (0..*size).map(|i| 1.0 + (i as f64) * 0.00001).collect();

                group.bench_with_input(
                    BenchmarkId::new("product", format!("{}t_{}", num_threads, size)),
                    &num_threads,
                    |bencher, _| {
                        bencher.iter(|| {
                            let result = ops
                                .parallel_reduce(&small_data, 1.0, |a, b| a * b)
                                .expect("parallel_reduce should succeed");
                            black_box(result);
                        });
                    },
                );
            }
        }
    }

    group.finish();
}

/// Benchmark parallel filter with different selectivity rates
fn bench_parallel_filter(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_filter");

    for size in [100_000, 1_000_000].iter() {
        group.throughput(Throughput::Elements(*size as u64));

        let data: Vec<i32> = (0..*size).collect();

        // Different selectivity rates
        let selectivities = [
            ("10pct", 10), // 10% pass rate
            ("50pct", 50), // 50% pass rate
            ("90pct", 90), // 90% pass rate
        ];

        for (name, threshold) in selectivities.iter() {
            for &num_threads in THREAD_COUNTS.iter() {
                let config = create_config(num_threads);
                let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

                group.bench_with_input(
                    BenchmarkId::new(*name, format!("{}t_{}", num_threads, size)),
                    &num_threads,
                    |bencher, _| {
                        bencher.iter(|| {
                            let result = ops
                                .parallel_filter(&data, |&x| (x % 100) < *threshold)
                                .expect("parallel_filter should succeed");
                            black_box(result);
                        });
                    },
                );
            }
        }
    }

    group.finish();
}

/// Benchmark parallel sort
fn bench_parallel_sort(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_sort");
    group.sample_size(10); // Reduce sample size for expensive operations

    for size in [10_000, 100_000, 1_000_000].iter() {
        group.throughput(Throughput::Elements(*size as u64));

        // Test on different data patterns
        let patterns = [
            (
                "random",
                (0..*size).map(|i| i * 7919 % *size).collect::<Vec<_>>(),
            ),
            ("sorted", (0..*size).collect::<Vec<_>>()),
            ("reverse", (0..*size).rev().collect::<Vec<_>>()),
        ];

        for (pattern_name, pattern_data) in patterns.iter() {
            for &num_threads in THREAD_COUNTS.iter() {
                let config = create_config(num_threads);
                let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

                group.bench_with_input(
                    BenchmarkId::new(*pattern_name, format!("{}t_{}", num_threads, size)),
                    &num_threads,
                    |bencher, _| {
                        bencher.iter(|| {
                            let mut data = pattern_data.clone();
                            ops.parallel_sort(&mut data)
                                .expect("parallel_sort should succeed");
                            black_box(data);
                        });
                    },
                );
            }
        }
    }

    group.finish();
}

/// Benchmark parallel map-reduce operation
fn bench_parallel_map_reduce(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_map_reduce");

    for size in SIZES.iter() {
        group.throughput(Throughput::Elements(*size as u64));

        let data: Vec<f64> = (0..*size).map(|i| i as f64).collect();

        for &num_threads in THREAD_COUNTS.iter() {
            let config = create_config(num_threads);
            let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

            group.bench_with_input(
                BenchmarkId::new("sqrt_sum", format!("{}t_{}", num_threads, size)),
                &num_threads,
                |bencher, _| {
                    bencher.iter(|| {
                        // Map: compute sqrt, Reduce: sum
                        let result = ops
                            .parallel_map_reduce(&data, |x| x.sqrt(), |a, b| a + b, 0.0)
                            .expect("parallel_map_reduce should succeed");
                        black_box(result);
                    });
                },
            );
        }
    }

    group.finish();
}

/// Benchmark parallel prefix sum (scan)
fn bench_parallel_prefix_sum(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_prefix_sum");

    for size in [10_000, 100_000, 1_000_000].iter() {
        group.throughput(Throughput::Elements(*size as u64));

        let data: Vec<f64> = (0..*size).map(|i| i as f64).collect();

        for &num_threads in THREAD_COUNTS.iter() {
            let config = create_config(num_threads);
            let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");
            let mut result = vec![0.0f64; *size];

            group.bench_with_input(
                BenchmarkId::new("prefix_sum", format!("{}t_{}", num_threads, size)),
                &num_threads,
                |bencher, _| {
                    bencher.iter(|| {
                        ops.parallel_prefix_sum(&data, &mut result)
                            .expect("parallel_prefix_sum should succeed");
                        black_box(&result);
                    });
                },
            );
        }
    }

    group.finish();
}

/// Benchmark strong scaling efficiency
/// Fixed problem size, vary thread count
fn bench_strong_scaling(c: &mut Criterion) {
    let mut group = c.benchmark_group("strong_scaling");

    let size = 10_000_000;
    group.throughput(Throughput::Elements(size as u64));

    let data: Vec<f64> = (0..size).map(|i| i as f64).collect();

    // Measure baseline (1 thread)
    let baseline_config = create_config(1);
    let baseline_ops =
        ParallelArrayOps::new(baseline_config).expect("Failed to create parallel ops");
    let mut baseline_output = vec![0.0f64; size];

    group.bench_function("baseline_1thread", |bencher| {
        bencher.iter(|| {
            baseline_ops
                .parallel_map(&data, &mut baseline_output, |x| x.sqrt().sin())
                .expect("parallel_map should succeed");
            black_box(&baseline_output);
        });
    });

    // Measure with multiple threads
    for &num_threads in &[2, 4, 8] {
        let config = create_config(num_threads);
        let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");
        let mut output = vec![0.0f64; size];

        group.bench_with_input(
            BenchmarkId::new("threads", num_threads),
            &num_threads,
            |bencher, _| {
                bencher.iter(|| {
                    ops.parallel_map(&data, &mut output, |x| x.sqrt().sin())
                        .expect("parallel_map should succeed");
                    black_box(&output);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark weak scaling efficiency
/// Problem size increases proportionally with thread count
fn bench_weak_scaling(c: &mut Criterion) {
    let mut group = c.benchmark_group("weak_scaling");

    let base_size = 1_000_000; // Size per thread

    for &num_threads in THREAD_COUNTS.iter() {
        let size = base_size * num_threads;
        group.throughput(Throughput::Elements(size as u64));

        let data: Vec<f64> = (0..size).map(|i| i as f64).collect();
        let config = create_config(num_threads);
        let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");
        let mut output = vec![0.0f64; size];

        group.bench_with_input(
            BenchmarkId::new("size_per_thread", format!("{}t_{}elem", num_threads, size)),
            &num_threads,
            |bencher, _| {
                bencher.iter(|| {
                    ops.parallel_map(&data, &mut output, |x| x.sqrt().sin())
                        .expect("parallel_map should succeed");
                    black_box(&output);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark binary operations with different thread counts
fn bench_parallel_binary_op(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_binary_op");

    for size in [100_000, 1_000_000, 10_000_000].iter() {
        group.throughput(Throughput::Elements(*size as u64));

        let a: Vec<f64> = (0..*size).map(|i| i as f64).collect();
        let b: Vec<f64> = (0..*size).map(|i| (i as f64) * 2.0).collect();

        for &num_threads in THREAD_COUNTS.iter() {
            let config = create_config(num_threads);
            let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");
            let mut result = vec![0.0f64; *size];

            group.bench_with_input(
                BenchmarkId::new("add", format!("{}t_{}", num_threads, size)),
                &num_threads,
                |bencher, _| {
                    bencher.iter(|| {
                        ops.parallel_binary_op(&a, &b, &mut result, |x, y| x + y)
                            .expect("parallel_binary_op should succeed");
                        black_box(&result);
                    });
                },
            );

            group.bench_with_input(
                BenchmarkId::new("mul", format!("{}t_{}", num_threads, size)),
                &num_threads,
                |bencher, _| {
                    bencher.iter(|| {
                        ops.parallel_binary_op(&a, &b, &mut result, |x, y| x * y)
                            .expect("parallel_binary_op should succeed");
                        black_box(&result);
                    });
                },
            );
        }
    }

    group.finish();
}

/// Benchmark work distribution with irregular workloads
fn bench_irregular_workload(c: &mut Criterion) {
    let mut group = c.benchmark_group("irregular_workload");

    let size = 100_000;
    group.throughput(Throughput::Elements(size as u64));

    // Create irregular workload: some elements require more computation
    let data: Vec<f64> = (0..size).map(|i| i as f64).collect();

    for &num_threads in THREAD_COUNTS.iter() {
        let config = create_config(num_threads);
        let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");
        let mut output = vec![0.0f64; size];

        group.bench_with_input(
            BenchmarkId::new("variable_work", format!("{}t", num_threads)),
            &num_threads,
            |bencher, _| {
                bencher.iter(|| {
                    ops.parallel_map(&data, &mut output, |x| {
                        // Irregular work: more iterations for larger indices
                        let iterations = ((x % 100.0) as usize) + 1;
                        let mut result = x;
                        for _ in 0..iterations {
                            result = result.sqrt() + 0.1;
                        }
                        result
                    })
                    .expect("parallel_map should succeed");
                    black_box(&output);
                });
            },
        );
    }

    group.finish();
}

criterion_group!(
    benches,
    bench_parallel_map_scaling,
    bench_parallel_reduce,
    bench_parallel_filter,
    bench_parallel_sort,
    bench_parallel_map_reduce,
    bench_parallel_prefix_sum,
    bench_strong_scaling,
    bench_weak_scaling,
    bench_parallel_binary_op,
    bench_irregular_workload,
);
criterion_main!(benches);