avila-parallel 0.4.0

Zero-dependency parallel library with work stealing, SIMD, lock-free operations, adaptive execution, and memory-efficient algorithms
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
//! Parallel execution engine
//!
//! Core parallel execution primitives for distributing work across threads using scoped threads.

use std::thread;
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicBool, Ordering};

/// Minimum chunk size to avoid overhead (increased based on benchmarks)
/// Benchmarks showed best results with chunks >= 1024 for simple operations
const MIN_CHUNK_SIZE: usize = 1024;

/// Maximum number of chunks per thread (allows better distribution)
const MAX_CHUNKS_PER_THREAD: usize = 8;

/// Get number of CPUs
pub fn num_cpus() -> usize {
    thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1)
}

/// Get minimum chunk size (configurable via AVILA_MIN_CHUNK_SIZE env var)
pub fn get_min_chunk_size() -> usize {
    std::env::var("AVILA_MIN_CHUNK_SIZE")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(MIN_CHUNK_SIZE)
}

/// Calculate optimal chunk size based on data length and CPU count
pub fn calculate_chunk_size(len: usize, num_threads: usize) -> usize {
    let min_chunk = get_min_chunk_size();
    let max_chunks = num_threads * MAX_CHUNKS_PER_THREAD;
    let chunk_size = (len + max_chunks - 1) / max_chunks;
    chunk_size.max(min_chunk)
}

/// Execute function on each item in parallel using scoped threads
pub fn parallel_for_each<T, F>(items: &[T], f: F)
where
    T: Sync,
    F: Fn(&T) + Sync + Send,
{
    let len = items.len();
    if len == 0 {
        return;
    }

    let num_threads = num_cpus();
    let chunk_size = calculate_chunk_size(len, num_threads);

    if chunk_size >= len {
        // Too small, run sequentially
        for item in items {
            f(item);
        }
        return;
    }

    // Wrap function in Arc to share across threads
    let f = Arc::new(f);

    // Use scoped threads to avoid lifetime issues
    thread::scope(|s| {
        for chunk in items.chunks(chunk_size) {
            let f = Arc::clone(&f);
            s.spawn(move || {
                for item in chunk {
                    f(item);
                }
            });
        }
    });
}

/// Execute a map operation in parallel
pub fn parallel_map<T, R, F>(items: &[T], f: F) -> Vec<R>
where
    T: Sync,
    R: Send + 'static,
    F: Fn(&T) -> R + Sync + Send,
{
    let len = items.len();
    if len == 0 {
        return Vec::new();
    }

    let num_threads = num_cpus();
    let chunk_size = calculate_chunk_size(len, num_threads);

    if chunk_size >= len {
        // Too small, run sequentially
        return items.iter().map(&f).collect();
    }

    // Wrap function in Arc to share across threads
    let f = Arc::new(f);
    // Create vector to store (index, results) for each chunk
    let chunk_results = Arc::new(Mutex::new(Vec::new()));

    thread::scope(|s| {
        let mut start_idx = 0;
        for chunk in items.chunks(chunk_size) {
            let f = Arc::clone(&f);
            let chunk_results = Arc::clone(&chunk_results);
            let chunk_start = start_idx;
            start_idx += chunk.len();

            s.spawn(move || {
                let results: Vec<R> = chunk.iter().map(|item| f(item)).collect();
                chunk_results.lock().unwrap().push((chunk_start, results));
            });
        }
    });

    // Extract and sort results by chunk start index
    let mut sorted_chunks = Arc::try_unwrap(chunk_results)
        .unwrap_or_else(|_| panic!("Failed to unwrap Arc"))
        .into_inner()
        .unwrap_or_else(|_| panic!("Failed to acquire lock"));

    sorted_chunks.sort_by_key(|(idx, _)| *idx);

    // Flatten sorted chunks into final result
    let mut results = Vec::with_capacity(len);
    for (_, chunk) in sorted_chunks {
        results.extend(chunk);
    }
    results
}

/// Execute a filter operation in parallel
pub fn parallel_filter<T, F>(items: &[T], f: F) -> Vec<&T>
where
    T: Sync,
    F: Fn(&T) -> bool + Sync + Send,
{
    let len = items.len();
    if len == 0 {
        return Vec::new();
    }

    let num_threads = num_cpus();
    let chunk_size = calculate_chunk_size(len, num_threads);

    if chunk_size >= len {
        // Too small, run sequentially
        return items.iter().filter(|item| f(item)).collect();
    }

    // Wrap function in Arc to share across threads
    let f = Arc::new(f);
    // Shared result vector
    let results = Arc::new(Mutex::new(Vec::new()));

    thread::scope(|s| {
        for chunk in items.chunks(chunk_size) {
            let f = Arc::clone(&f);
            let results = Arc::clone(&results);
            s.spawn(move || {
                let chunk_results: Vec<&T> = chunk.iter().filter(|item| f(item)).collect();
                results.lock().unwrap().extend(chunk_results);
            });
        }
    });

    // Extract results from Arc<Mutex<>>
    Arc::try_unwrap(results)
        .unwrap_or_else(|_| panic!("Failed to unwrap Arc"))
        .into_inner()
        .unwrap_or_else(|_| panic!("Failed to acquire lock"))
}

/// Execute a reduce operation in parallel
pub fn parallel_reduce<T, F>(items: &[T], reduce_op: F) -> Option<T>
where
    T: Clone + Send + Sync,
    F: Fn(T, T) -> T + Sync + Send,
{
    let len = items.len();
    if len == 0 {
        return None;
    }

    let num_threads = num_cpus();
    let chunk_size = calculate_chunk_size(len, num_threads);

    if chunk_size >= len {
        // Too small, run sequentially
        return items.iter().cloned().reduce(|a, b| reduce_op(a, b));
    }

    // Wrap function in Arc to share across threads
    let reduce_op = Arc::new(reduce_op);
    // Collect partial results from each chunk
    let results = Arc::new(Mutex::new(Vec::new()));

    thread::scope(|s| {
        for chunk in items.chunks(chunk_size) {
            let reduce_op = Arc::clone(&reduce_op);
            let results = Arc::clone(&results);
            s.spawn(move || {
                if let Some(chunk_result) = chunk.iter().cloned().reduce(|a, b| reduce_op(a, b)) {
                    results.lock().unwrap().push(chunk_result);
                }
            });
        }
    });

    // Final reduce on partial results
    let final_results = Arc::try_unwrap(results)
        .unwrap_or_else(|_| panic!("Failed to unwrap Arc"))
        .into_inner()
        .unwrap_or_else(|_| panic!("Failed to acquire lock"));
    final_results.into_iter().reduce(|a, b| reduce_op(a, b))
}

/// Find first element that satisfies predicate (parallel with early termination)
pub fn parallel_find<T, F>(items: &[T], predicate: F) -> Option<T>
where
    T: Clone + Send + Sync,
    F: Fn(&T) -> bool + Sync + Send,
{
    let len = items.len();
    if len == 0 {
        return None;
    }

    let num_threads = num_cpus();
    let chunk_size = calculate_chunk_size(len, num_threads);

    if chunk_size >= len {
        // Too small, run sequentially
        return items.iter().find(|item| predicate(item)).cloned();
    }

    // Wrap function in Arc to share across threads
    let predicate = Arc::new(predicate);
    let result = Arc::new(Mutex::new(None));
    let found_flag = Arc::new(AtomicBool::new(false));  // Early termination flag

    thread::scope(|s| {
        for chunk in items.chunks(chunk_size) {
            let predicate = Arc::clone(&predicate);
            let result = Arc::clone(&result);
            let found_flag = Arc::clone(&found_flag);
            s.spawn(move || {
                // Early termination: skip if already found
                if found_flag.load(Ordering::Relaxed) {
                    return;
                }
                if let Some(found) = chunk.iter().find(|item| predicate(item)) {
                    found_flag.store(true, Ordering::Relaxed);
                    let mut res = result.lock().unwrap();
                    if res.is_none() {
                        *res = Some(found.clone());
                    }
                }
            });
        }
    });

    Arc::try_unwrap(result)
        .unwrap_or_else(|_| panic!("Failed to unwrap Arc"))
        .into_inner()
        .unwrap_or_else(|_| panic!("Failed to acquire lock"))
}

/// Count elements that satisfy predicate (parallel)
pub fn parallel_count<T, F>(items: &[T], predicate: F) -> usize
where
    T: Sync,
    F: Fn(&T) -> bool + Sync + Send,
{
    let len = items.len();
    if len == 0 {
        return 0;
    }

    let num_threads = num_cpus();
    let chunk_size = calculate_chunk_size(len, num_threads);

    if chunk_size >= len {
        // Too small, run sequentially
        return items.iter().filter(|item| predicate(item)).count();
    }

    // Wrap function in Arc to share across threads
    let predicate = Arc::new(predicate);
    let counts = Arc::new(Mutex::new(Vec::new()));

    thread::scope(|s| {
        for chunk in items.chunks(chunk_size) {
            let predicate = Arc::clone(&predicate);
            let counts = Arc::clone(&counts);
            s.spawn(move || {
                let count = chunk.iter().filter(|item| predicate(item)).count();
                counts.lock().unwrap().push(count);
            });
        }
    });

    Arc::try_unwrap(counts)
        .unwrap_or_else(|_| panic!("Failed to unwrap Arc"))
        .into_inner()
        .unwrap_or_else(|_| panic!("Failed to acquire lock"))
        .into_iter()
        .sum()
}

/// Partition elements based on predicate (parallel)
pub fn parallel_partition<T, F>(items: &[T], predicate: F) -> (Vec<T>, Vec<T>)
where
    T: Clone + Send + Sync,
    F: Fn(&T) -> bool + Sync + Send,
{
    let len = items.len();
    if len == 0 {
        return (Vec::new(), Vec::new());
    }

    let num_threads = num_cpus();
    let chunk_size = calculate_chunk_size(len, num_threads);

    if chunk_size >= len {
        // Too small, run sequentially
        let mut true_vec = Vec::new();
        let mut false_vec = Vec::new();
        for item in items {
            if predicate(item) {
                true_vec.push(item.clone());
            } else {
                false_vec.push(item.clone());
            }
        }
        return (true_vec, false_vec);
    }

    // Wrap function in Arc to share across threads
    let predicate = Arc::new(predicate);
    let results = Arc::new(Mutex::new(Vec::new()));

    thread::scope(|s| {
        for chunk in items.chunks(chunk_size) {
            let predicate = Arc::clone(&predicate);
            let results = Arc::clone(&results);
            s.spawn(move || {
                let mut true_vec = Vec::new();
                let mut false_vec = Vec::new();
                for item in chunk {
                    if predicate(item) {
                        true_vec.push(item.clone());
                    } else {
                        false_vec.push(item.clone());
                    }
                }
                results.lock().unwrap().push((true_vec, false_vec));
            });
        }
    });

    let chunk_results = Arc::try_unwrap(results)
        .unwrap_or_else(|_| panic!("Failed to unwrap Arc"))
        .into_inner()
        .unwrap_or_else(|_| panic!("Failed to acquire lock"));

    let mut final_true = Vec::new();
    let mut final_false = Vec::new();
    for (true_vec, false_vec) in chunk_results {
        final_true.extend(true_vec);
        final_false.extend(false_vec);
    }
    (final_true, final_false)
}

/// Sum elements in parallel
pub fn parallel_sum<T>(items: &[T]) -> T
where
    T: Clone + Send + Sync + std::iter::Sum,
{
    let len = items.len();
    if len == 0 {
        panic!("Cannot sum empty collection");
    }

    let num_threads = num_cpus();
    let chunk_size = calculate_chunk_size(len, num_threads);

    if chunk_size >= len {
        // Too small, run sequentially
        return items.iter().cloned().sum();
    }

    // Collect partial sums from each chunk
    let results = Arc::new(Mutex::new(Vec::new()));

    thread::scope(|s| {
        for chunk in items.chunks(chunk_size) {
            let results = Arc::clone(&results);
            s.spawn(move || {
                let chunk_sum: T = chunk.iter().cloned().sum();
                results.lock().unwrap().push(chunk_sum);
            });
        }
    });

    // Sum the partial results
    Arc::try_unwrap(results)
        .unwrap_or_else(|_| panic!("Failed to unwrap Arc"))
        .into_inner()
        .unwrap_or_else(|_| panic!("Failed to acquire lock"))
        .into_iter()
        .sum()
}

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

    #[test]
    fn test_parallel_for_each() {
        let data = vec![1, 2, 3, 4, 5];
        let counter = Arc::new(Mutex::new(0));

        parallel_for_each(&data, |_| {
            *counter.lock().unwrap() += 1;
        });

        assert_eq!(*counter.lock().unwrap(), 5);
    }

    #[test]
    fn test_parallel_map() {
        let data = vec![1, 2, 3, 4, 5];
        let result = parallel_map(&data, |x| x * 2);

        let mut sorted_result = result;
        sorted_result.sort();
        assert_eq!(sorted_result, vec![2, 4, 6, 8, 10]);
    }

    #[test]
    fn test_parallel_filter() {
        let data = vec![1, 2, 3, 4, 5, 6];
        let result = parallel_filter(&data, |x| *x % 2 == 0);

        let mut values: Vec<i32> = result.into_iter().map(|x| *x).collect();
        values.sort();
        assert_eq!(values, vec![2, 4, 6]);
    }

    #[test]
    fn test_parallel_reduce() {
        let data = vec![1, 2, 3, 4, 5];
        let result = parallel_reduce(&data, |a, b| a + b);
        assert_eq!(result, Some(15));
    }

    #[test]
    fn test_parallel_sum() {
        let data = vec![1, 2, 3, 4, 5];
        let result = parallel_sum(&data);
        assert_eq!(result, 15);
    }

    #[test]
    fn test_parallel_find() {
        let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        let result = parallel_find(&data, |x| *x > 5);
        assert!(result.is_some());
        assert!(result.unwrap() > 5);
    }

    #[test]
    fn test_parallel_count() {
        let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        let count = parallel_count(&data, |x| *x % 2 == 0);
        assert_eq!(count, 5);
    }

    #[test]
    fn test_parallel_partition() {
        let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        let (evens, odds) = parallel_partition(&data, |x| *x % 2 == 0);
        assert_eq!(evens.len(), 5);
        assert_eq!(odds.len(), 5);
        assert!(evens.iter().all(|x| x % 2 == 0));
        assert!(odds.iter().all(|x| x % 2 == 1));
    }
}