Parallel module

Constructs to aid task-based parallelism.

Darts includes the nanothread library, which offers a convenient cross-platform interface for task parallelism. The library efficiently distributes work that you split into tasks across a thread pool.

One of the core constructs is the parallel for loop. Here's an example of how to use it:

#include <darts/parallel.h>
#include <darts/common.h>

int main(int, char **) {
    int result[100];

    // Call the provided lambda function 100 times with blocks of size 1
    parallel_for(
        blocked_range<uint32_t>(0   // begin,
                                100 // end,
                                1   // block_size
                                ),

        // The callback is allowed to be a stateful lambda function
        [&](blocked_range<uint32_t> range) {
            for (uint32_t i = range.begin(); i != range.end(); ++i) {
                spdlog::info("Worker thread {} is starting to process work unit {}\n",
                             pool_thread_id(), i);

                // Write to variables defined in the caller's frame
                result[i] = i;
            }
        }
    );
}

There are a number of other constructs provided by nanothread, and you can check out the project for full documenation on the project's GitHub page.

Darts additionally provides the for_each_thread function, allowing you to execute a function once for each thread in the thread pool, and a Barrier thread-coordination mechanism class.

Classes

class Barrier
Implements a simple barrier thread-coordination mechanism using a condition variable and mutex.

Functions

void for_each_thread(std::function<void(void)> func, Pool* pool = nullptr)
Run a function once on each thread in thread Pool pool.

Function documentation

void for_each_thread(std::function<void(void)> func, Pool* pool = nullptr)

Run a function once on each thread in thread Pool pool.