Parallel module

Constructs to aid task-based parallelism.

Darts includes the smallthreadpool 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 <spdlog/spdlog.h>
#define SMALL_THREADPOOL_IMPLEMENTATION
#include <smallthreadpool.h>

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

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

        // The callback is allowed to be a stateful lambda function
        [&](uint32_t begin, uint32_t end, int unit_index, int thread_index) {
            for (uint32_t i = begin; i != 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 smallthreadpool. Check the comments in smallthreadpool.h for more info.

Files

file parallel.h
parallel processing functionality