File: README.md

package info (click to toggle)
ptl 2.3.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,296 kB
  • sloc: cpp: 8,195; python: 246; sh: 7; makefile: 3
file content (80 lines) | stat: -rw-r--r-- 1,952 bytes parent folder | download | duplicates (4)
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
# Parallel Tasking Library (PTL)
Lightweight C++11 multithreading tasking system featuring thread-pool, task-groups, and lock-free task queue

## Basic Interface

```cpp
#include "PTL/PTL.hh"

#include <cassert>

inline long
fibonacci(long n)
{
    return (n < 2) ? n : (fibonacci(n - 1) + fibonacci(n - 2));
}

int main()
{
    bool use_tbb     = false;
    auto num_threads = 4;
    auto run_manager = PTL::TaskRunManager(use_tbb);

    run_manager.Initialize(num_threads);

    auto* task_manager = run_manager.GetTaskManager();

    // add a task via the task manager
    auto baz = task_manager->async<long>(fibonacci, 40);

    // functor to combine results
    auto join = [](long& lhs, long rhs) { return lhs += rhs; };

    // create a task group for 10 fibonacci calculations
    PTL::TaskGroup<long> foo(join);
    for(uint64_t i = 0; i < 10; ++i)
        foo.exec(fibonacci, 40);

    // create a task group for 10 fibonacci calculations
    PTL::TaskGroup<void> bar{};

    long ret_bar = 0;
    auto run     = [&ret_bar](long n) { ret_bar += fibonacci(n); };
    for(uint64_t i = 0; i < 10; ++i)
        bar.exec(run, 40);

    auto ret_baz = baz->get();
    auto ret_foo = foo.join();
    bar.join();

    assert(ret_baz * 10 == ret_foo);
    assert(ret_baz * 10 == ret_bar);
    assert(ret_foo == ret_bar);
}
```

## Explicit Thread-Pool

Using `PTL::TaskRunManager` is not necessary with task-groups.
You can create new thread-pools directly and pass them to task-groups:

```cpp
long example()
{
    // create a new thread-pool explicitly
    PTL::ThreadPool tp(4);

    // combines results
    auto join = [](long& lhs, long rhs) { return lhs += rhs; };

    // specify thread-pool explicitly
    PTL::TaskGroup<long> foo(join, &tp);

    for(int i = 0; i < 10; ++i)
        foo.exec(fibonacci, 40);

    // blocks until tasks in group are completed
    // thread-pool is destroyed after function returns
    return foo.get();
}
```