Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_parallel_example.cc File Reference

Parallel functional programming utilities (ah-parallel.H): map/filter/fold/predicates/zip/sort. More...

#include <ah-parallel.H>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <chrono>
#include <numeric>
#include <random>
#include <string>
Include dependency graph for ah_parallel_example.cc:

Go to the source code of this file.

Functions

void print_header (const std::string &title)
 
void example_parallel_map ()
 
void example_parallel_filter ()
 
void example_parallel_fold ()
 
void example_parallel_predicates ()
 
void example_parallel_find ()
 
void example_parallel_aggregations ()
 
void example_parallel_sort ()
 
void example_parallel_zip ()
 
void example_variadic_zip ()
 
void example_parallel_enumerate ()
 
void example_performance_comparison ()
 
int main ()
 

Detailed Description

Parallel functional programming utilities (ah-parallel.H): map/filter/fold/predicates/zip/sort.

Overview

This example demonstrates Aleph-w's parallel functional programming helpers from ah-parallel.H. The API provides ML-style operations (map, filter, fold, predicates, etc.) that execute in parallel using an Aleph::ThreadPool.

The file is structured as a series of demos covering:

  • parallel map/filter/fold
  • parallel predicates and find
  • aggregations (sum/product/min/max)
  • parallel sort
  • parallel zip / enumerate
  • a simple parallel vs sequential performance comparison

Data model used by this example

  • Primary containers: std::vector<int>, std::vector<double>, std::vector<std::string>
  • Execution engine: ThreadPool pool(std::thread::hardware_concurrency())

Usage

./ah_parallel_example

This example has no command-line options; it runs all demos.

Algorithms and Aleph-w API

Common signature pattern:

  • the first parameter is always a ThreadPool&
  • the remaining parameters are similar to the sequential counterparts

Core operations demonstrated:

  • pmaps(pool, container, f)
  • pfilter(pool, container, pred)
  • pfoldl(pool, container, init, op)
  • pall / pexists / pnone / pcount_if
  • pfind / pfind_value
  • psum / pproduct / pmin / pmax
  • psort
  • pzip_*, penumerate_*

Complexity

Asymptotically, most operations have the same work complexity as the sequential version (e.g. O(n) for map/filter), but with wall-clock time reduced by parallelization.

Actual speedups depend on:

  • task granularity and chunking
  • CPU core count
  • memory bandwidth
  • overhead of scheduling

Pitfalls and edge cases

  • For pfoldl, the binary operator should be associative to ensure that parallel reduction is well-defined.
  • Short-circuit operations (pall, pexists, pfind) may stop early.
  • Parallel overhead can dominate for small inputs; measure before assuming gains.

References / see also

Definition in file ah_parallel_example.cc.

Function Documentation

◆ example_parallel_aggregations()

void example_parallel_aggregations ( )

◆ example_parallel_enumerate()

void example_parallel_enumerate ( )

◆ example_parallel_filter()

◆ example_parallel_find()

void example_parallel_find ( )

Definition at line 314 of file ah_parallel_example.cc.

References Aleph::maps(), Aleph::pfind(), Aleph::pfind_value(), print_header(), and rng.

Referenced by main().

◆ example_parallel_fold()

void example_parallel_fold ( )

Definition at line 222 of file ah_parallel_example.cc.

References Aleph::maps(), Aleph::pfoldl(), print_header(), Aleph::psum(), and Aleph::sum().

Referenced by main().

◆ example_parallel_map()

◆ example_parallel_predicates()

void example_parallel_predicates ( )

◆ example_parallel_sort()

void example_parallel_sort ( )

Definition at line 404 of file ah_parallel_example.cc.

References Aleph::is_sorted(), Aleph::maps(), print_header(), Aleph::psort(), and rng.

Referenced by main().

◆ example_parallel_zip()

void example_parallel_zip ( )

◆ example_performance_comparison()

◆ example_variadic_zip()

void example_variadic_zip ( )

◆ main()

◆ print_header()

void print_header ( const std::string &  title)

Definition at line 93 of file ah_parallel_example.cc.

References Aleph::maps().