Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
Functions
async_tbb_reduction_example.cpp File Reference

Example of using uni20 coroutines with the TbbScheduler to parallelize a map–reduce computation. More...

#include <iostream>
#include <numeric>
#include <uni20/async/async.hpp>
#include <uni20/async/buffers.hpp>
#include <uni20/async/tbb_scheduler.hpp>
#include <vector>
Include dependency graph for async_tbb_reduction_example.cpp:

Functions

int main ()
 
AsyncTask square (ReadBuffer< int > in, WriteBuffer< int > out)
 
AsyncTask sum (ReadBuffer< int > a, ReadBuffer< int > b, WriteBuffer< int > out)
 

Detailed Description

Example of using uni20 coroutines with the TbbScheduler to parallelize a map–reduce computation.

This example computes the sum of squares from 1 to N using two coroutine kernels:

The computation is structured in two phases:

  1. Map stage: a parallel launch of square coroutines over all inputs
  2. Reduce stage: a binary-tree reduction using sum coroutines

The scheduler (TbbScheduler) manages coroutine resumption across threads participating in its arena, while Async<int>, ReadBuffer<int>, and WriteBuffer<int> handle dataflow and dependency tracking between tasks.

This demonstrates:

Expected output (for N=1000):

Sum of squares 1..N = 333833500

In principle the application thread can reach the last line of main() before any of the scheduled computations begin.

Function Documentation

◆ main()

int main ( )

◆ square()

AsyncTask square ( ReadBuffer< int >  in,
WriteBuffer< int >  out 
)

◆ sum()

AsyncTask sum ( ReadBuffer< int >  a,
ReadBuffer< int >  b,
WriteBuffer< int >  out 
)