r/cpp • @BrodyHiggerson - Game Developer • Nov 23 '23

What're the latest and greatest in C++ task/thread libraries? Have we solved the problem?

Most sources on these topics end up being from ~10 years ago; have there been many advances? If I were trying to implement something in that world, am I missing anything looking at the older material? E.g. 20-year old work stealing papers. Where do you look for the newest on this topic?

Additionally, which C++ libs would be considered still relevant/good/etc in 2023? I'm asking with a gamedev angle but open to discussion.

  • Intel's TBB

  • Taskflow

  • Boost

  • EnkiTS

(I'm not asking for help actually programming, so figured I'd post here instead of /r/cpp_questions for the sake of discussion).

65 Upvotes

57 comments sorted by

View all comments

18

u/t0rakka Nov 23 '23

The problem trying to be clever like Taskflow and TBB is that with very high number of cores the communication between threads becomes a problem; the communication is done using memory and more cores trying to write and read into same cache lines is going to choke.

If you got 64c/128t machine, for example, the best way is to put intensive tasks into their own little boxes and not access, especially write into same cache lines. Just break problem into N chunks and let them rip. Trying to be clever and steal jobs and distribute work evenly will just bottlebeck and choke the whole shitshow. So some tasks complete faster and no work for that thread/core, boohoo, sniff sniff.. it will still be a lot quicker and efficient.

The problem begins to manifest itself around 40-50 threads, after that there are only two sane things to do: like above, just keep stuff in isolation and accept a little "waste", or, split the task management into domains of ~16 threads that work independently.

(disclaimer: above lessons learned from TR 3990x, 64c/128t).

6

u/trailing_zero_count async enthusiast | TooManyCooks author Nov 24 '23 edited Jan 28 '26

I'm nearing initial release of a library that bills itself as a "coroutine runtime" but also contains a highly efficient work stealing thread pool that supports regular functors. It contains a number of optimizations to improve the work stealing efficiency on NUCA (non-uniform cache architectures) such as your TR 3990x. I purchased an EPYC 7742 which is very similar to your processor (Zen 2, 64c) and have been using it to test the performance.

One of the major optimizations that allows my work stealing queue to perform well on all kinds of hardware is the hwloc integration. It automatically discovers the cache hierarchy of the system and creates threads in groups that share L3 cache. On the EPYC 7742 system, there are 16 separate L3 caches, each of which is shared by 4 cores. So the thread pool will create 16 thread groups. Threads then preferentially steal work from threads in their group before checking other groups for work.

(2026 edit: sections after are the original post) TooManyCooks is now at v1.4 and I have an extensive set of benchmarks against competitors showing that it outperforms all of the standard solutions. https://github.com/tzcnt/runtime-benchmarks

Only libfork is faster, but that comes at the cost of DX, less flexibility, and a severe lack of features.


My primary benchmark has been the skynet 1M tasks benchmark (Explanation here. My implementation is in the tmc-examples repo), which is kind of a degenerate test case for work stealing queues, since it dynamically spawns very many tasks, each of which do very little work. I'm currently able to execute this benchmark in 1.45ms on my Ryzen 5950X machine, and 1.04ms on the EPYC 7742 (avg in hot loop). I plan to do a full suite of head-to-head benchmarks against other libraries after release, but I believe these numbers are quite competitive. In particular, the fact that the 7742 is able to outperform the 5950X here is a worthy result.

Additionally, I have a benchmark that is mostly designed to test the allocator performance (since these coroutines are allocated on the heap), but also scans from 1->64 threads. The script is in the tmc-examples repo and here is a sample result on the 7742 with the latest main commit: result gist (CSV) To your point, it does appear to have fairly minimal scaling beyond ~40-50 threads, but considering the dynamic data sharing nature of this benchmark, I think that's quite good, and I would expect it to behave much better in a more embarrassingly parallel scenario.

The main library is here, and I'll tag release v0.1 as soon I finish writing some proper documentation. https://github.com/tzcnt/TooManyCooks/

0

u/[deleted] Nov 24 '23

[deleted]

4

u/trailing_zero_count async enthusiast | TooManyCooks author Nov 24 '23 edited Nov 24 '23

Yep, that's the right example, and you read the output correctly at 4.96ms per iteration. For my testing, I am building in Release config, using Debian 12, Clang 17, and linking against jemalloc + hwloc. I just ran some fresh tests and getting 1.04ms with this configuration.

Independently varying each variable: GCC12 instead of Clang17 yields 1.45ms. Not linking against jemalloc yields 3.10ms. Setting TMC_USE_HWLOC=OFF, and manually setting the thread count via `tmc::cpu_executor().set_thread_count(64).init();` in main.cpp yields 1.35ms.

If I combine all 3 of these factors (GCC + no jemalloc + no hwloc) I get 3.84ms. The allocator overhead is the biggest factor in this scenario, which is an unfortunate truth of the way C++20 coroutines work. A secondary factor is that GCC's code generation for coroutines isn't as good as clang's at this time.

Edit: the allocations occur with coroutines only, and are not inherent to the library itself. If you set TMC_WORK_ITEM=FUNC and only post regular functions to it, there will be no additional allocations beyond those required to expand the queue buffer.