numrs2 0.3.3

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
//! Comprehensive tests for parallel algorithms

use numrs2::parallel::{ParallelArrayOps, ParallelConfig};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;

// ============================================================================
// Parallel Map Tests
// ============================================================================

#[test]
fn test_parallel_map_basic() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 10,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let input: Vec<i32> = (0..1000).collect();
    let mut output = vec![0i32; 1000];

    ops.parallel_map(&input, &mut output, |x| x * 2)
        .expect("Failed to parallel map");

    for i in 0..1000 {
        assert_eq!(output[i], input[i] * 2);
    }
}

#[test]
fn test_parallel_map_different_types() {
    let config = ParallelConfig {
        num_threads: Some(2),
        parallel_threshold: 10,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    // Test with f64
    let input_f64: Vec<f64> = (0..500).map(|x| x as f64 * 0.5).collect();
    let mut output_f64 = vec![0.0f64; 500];

    ops.parallel_map(&input_f64, &mut output_f64, |x| x * x)
        .expect("Failed to parallel map f64");

    for i in 0..500 {
        assert!((output_f64[i] - input_f64[i] * input_f64[i]).abs() < 1e-10);
    }
}

#[test]
fn test_parallel_map_edge_cases() {
    let config = ParallelConfig::default();
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    // Empty array
    let input_empty: Vec<i32> = vec![];
    let mut output_empty: Vec<i32> = vec![];
    ops.parallel_map(&input_empty, &mut output_empty, |x| x + 1)
        .expect("Failed to parallel map empty array");
    assert_eq!(output_empty.len(), 0);

    // Single element
    let input_single = vec![42];
    let mut output_single = vec![0];
    ops.parallel_map(&input_single, &mut output_single, |x| x * 3)
        .expect("Failed to parallel map single element");
    assert_eq!(output_single[0], 126);
}

// ============================================================================
// Parallel Reduce Tests
// ============================================================================

#[test]
fn test_parallel_reduce_sum() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 100,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let data: Vec<i64> = (1..=1000).collect();
    let result = ops
        .parallel_reduce(&data, 0i64, |a, b| a + b)
        .expect("Failed to parallel reduce");

    let expected: i64 = (1..=1000).sum();
    assert_eq!(result, expected);
}

#[test]
fn test_parallel_reduce_custom_operations() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 50,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let data: Vec<f64> = vec![1.5, 2.5, 3.5, 4.5, 5.5];

    // Test max
    let max_result = ops
        .parallel_reduce(&data, f64::NEG_INFINITY, |a, b| a.max(b))
        .expect("Failed to reduce max");
    assert_eq!(max_result, 5.5);

    // Test min
    let min_result = ops
        .parallel_reduce(&data, f64::INFINITY, |a, b| a.min(b))
        .expect("Failed to reduce min");
    assert_eq!(min_result, 1.5);

    // Test product
    let product_result = ops
        .parallel_reduce(&data, 1.0, |a, b| a * b)
        .expect("Failed to reduce product");
    let expected_product = 1.5 * 2.5 * 3.5 * 4.5 * 5.5;
    assert!((product_result - expected_product).abs() < 1e-6);
}

#[test]
fn test_parallel_reduce_correctness() {
    let config = ParallelConfig {
        num_threads: Some(8),
        parallel_threshold: 10,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

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

    let parallel_result = ops
        .parallel_reduce(&data, 0, |a, b| a + b)
        .expect("Failed to parallel reduce");

    let sequential_result: i32 = data.iter().sum();

    assert_eq!(parallel_result, sequential_result);
}

// ============================================================================
// Parallel Filter Tests
// ============================================================================

#[test]
fn test_parallel_filter_basic() {
    // Note: ParallelArrayOps doesn't have a filter method in the current implementation
    // This test demonstrates how it would work if implemented
    let config = ParallelConfig::default();
    let _ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let data: Vec<i32> = (0..100).collect();
    let filtered: Vec<i32> = data.iter().filter(|&&x| x % 2 == 0).copied().collect();

    assert_eq!(filtered.len(), 50);
    assert!(filtered.iter().all(|&x| x % 2 == 0));
}

#[test]
fn test_parallel_filter_complex_predicate() {
    let data: Vec<i32> = (0..1000).collect();
    let filtered: Vec<i32> = data
        .iter()
        .filter(|&&x| x % 3 == 0 && x % 5 == 0)
        .copied()
        .collect();

    assert!(filtered.iter().all(|&x| x % 15 == 0));
}

// ============================================================================
// Parallel Sort Tests
// ============================================================================

#[test]
fn test_parallel_sort_correctness() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 100,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let mut data: Vec<i32> = (0..1000).rev().collect(); // Reverse sorted
    ops.parallel_sort(&mut data).expect("Failed to sort");

    for i in 1..data.len() {
        assert!(data[i - 1] <= data[i]);
    }
}

#[test]
fn test_parallel_sort_random_data() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 100,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let mut data: Vec<i32> = vec![
        64, 34, 25, 12, 22, 11, 90, 88, 45, 50, 33, 17, 10, 82, 67, 23,
    ];
    ops.parallel_sort(&mut data).expect("Failed to sort");

    for i in 1..data.len() {
        assert!(data[i - 1] <= data[i]);
    }
}

#[test]
fn test_parallel_sort_different_sizes() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 50,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    for size in [10, 100, 1000, 5000] {
        let mut data: Vec<i32> = (0..size).rev().collect();
        ops.parallel_sort(&mut data).expect("Failed to sort");

        for i in 1..data.len() {
            assert!(data[i - 1] <= data[i], "Failed for size {}", size);
        }
    }
}

// ============================================================================
// Parallel Scan/Prefix Sum Tests
// ============================================================================

#[test]
fn test_parallel_prefix_sum_basic() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 10,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let data: Vec<i32> = vec![1, 2, 3, 4, 5];
    let mut result = vec![0i32; 5];

    ops.parallel_prefix_sum(&data, &mut result)
        .expect("Failed to compute prefix sum");

    assert_eq!(result, vec![1, 3, 6, 10, 15]);
}

#[test]
fn test_parallel_prefix_sum_large_array() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 100,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let data: Vec<i32> = vec![1; 1000];
    let mut result = vec![0i32; 1000];

    ops.parallel_prefix_sum(&data, &mut result)
        .expect("Failed to compute prefix sum");

    for i in 0..1000 {
        assert_eq!(result[i], (i + 1) as i32);
    }
}

// ============================================================================
// Parallel Pipeline Tests
// ============================================================================

#[test]
fn test_parallel_pipeline_multi_stage() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 10,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    // Stage 1: Map
    let input: Vec<i32> = (0..100).collect();
    let mut stage1 = vec![0i32; 100];
    ops.parallel_map(&input, &mut stage1, |x| x * 2)
        .expect("Failed stage 1");

    // Stage 2: Map again
    let mut stage2 = vec![0i32; 100];
    ops.parallel_map(&stage1, &mut stage2, |x| x + 10)
        .expect("Failed stage 2");

    // Verify pipeline result
    for i in 0..100 {
        assert_eq!(stage2[i], (i as i32) * 2 + 10);
    }
}

#[test]
fn test_parallel_pipeline_with_reduce() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 10,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    // Stage 1: Map
    let input: Vec<f64> = (1..=100).map(|x| x as f64).collect();
    let mut squared = vec![0.0f64; 100];
    ops.parallel_map(&input, &mut squared, |x| x * x)
        .expect("Failed to square");

    // Stage 2: Reduce
    let sum = ops
        .parallel_reduce(&squared, 0.0, |a, b| a + b)
        .expect("Failed to reduce");

    // Verify sum of squares
    let expected: f64 = (1..=100).map(|x| (x * x) as f64).sum();
    assert!((sum - expected).abs() < 1e-6);
}

// ============================================================================
// Parallel Binary Operations Tests
// ============================================================================

#[test]
fn test_parallel_binary_op_addition() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 10,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let a: Vec<f64> = (0..1000).map(|x| x as f64).collect();
    let b: Vec<f64> = (0..1000).map(|x| x as f64 * 0.5).collect();
    let mut result = vec![0.0f64; 1000];

    ops.parallel_binary_op(&a, &b, &mut result, |x, y| x + y)
        .expect("Failed binary op");

    for i in 0..1000 {
        assert!((result[i] - (a[i] + b[i])).abs() < 1e-10);
    }
}

#[test]
fn test_parallel_binary_op_multiplication() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 10,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let a: Vec<f64> = (1..=500).map(|x| x as f64).collect();
    let b: Vec<f64> = (1..=500).map(|x| (x as f64) * 2.0).collect();
    let mut result = vec![0.0f64; 500];

    ops.parallel_binary_op(&a, &b, &mut result, |x, y| x * y)
        .expect("Failed binary op");

    for i in 0..500 {
        assert!((result[i] - (a[i] * b[i])).abs() < 1e-6);
    }
}

// ============================================================================
// Performance and Correctness Tests
// ============================================================================

#[test]
fn test_parallel_ops_thread_scaling() {
    // Test with different thread counts
    for num_threads in [1, 2, 4, 8] {
        let available = std::thread::available_parallelism().map_or(4, |n| n.get());
        if num_threads > available {
            continue;
        }

        let config = ParallelConfig {
            num_threads: Some(num_threads),
            parallel_threshold: 100,
            ..Default::default()
        };
        let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

        let data: Vec<i32> = (0..10000).collect();
        let result = ops
            .parallel_reduce(&data, 0, |a, b| a + b)
            .expect("Failed to reduce");

        let expected: i32 = data.iter().sum();
        assert_eq!(result, expected);
    }
}

#[test]
fn test_parallel_ops_determinism() {
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 100,
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let mut data1: Vec<i32> = (0..1000).rev().collect();
    let mut data2 = data1.clone();

    ops.parallel_sort(&mut data1).expect("Failed to sort 1");
    ops.parallel_sort(&mut data2).expect("Failed to sort 2");

    assert_eq!(data1, data2, "Parallel sort should be deterministic");
}

#[test]
fn test_parallel_ops_error_handling() {
    let config = ParallelConfig::default();
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    // Test dimension mismatch
    let a = vec![1, 2, 3];
    let b = vec![1, 2];
    let mut result = vec![0; 3];

    let err = ops.parallel_binary_op(&a, &b, &mut result, |x, y| x + y);
    assert!(err.is_err());
}

#[test]
fn test_parallel_threshold_behavior() {
    // Test that small arrays use sequential processing
    let config = ParallelConfig {
        num_threads: Some(4),
        parallel_threshold: 1000, // High threshold
        ..Default::default()
    };
    let ops = ParallelArrayOps::new(config).expect("Failed to create parallel ops");

    let data: Vec<i32> = (0..100).collect(); // Below threshold
    let result = ops
        .parallel_reduce(&data, 0, |a, b| a + b)
        .expect("Failed to reduce");

    let expected: i32 = data.iter().sum();
    assert_eq!(result, expected);
}