Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
Parallel

Growing collection of utilities for parallel computing. More...

Classes

class  sourcemeta::core::ParallelThreadError

Functions

template<typename Iterator, typename Callback>
requires std::input_iterator<Iterator> && std::copyable<Iterator> && std::invocable<Callback, std::iter_reference_t<Iterator>,
std::size_t, std::size_t>
auto sourcemeta::core::parallel_for_each (Iterator first, Iterator last, Callback &&callback, const std::size_t parallelism=std::thread::hardware_concurrency(), const std::size_t stack_size_bytes=0) -> void

Detailed Description

Growing collection of utilities for parallel computing.

This functionality is included as follows:

#include <sourcemeta/core/parallel.h>

Class Documentation

◆ sourcemeta::core::ParallelThreadError

class sourcemeta::core::ParallelThreadError

An error that represents a worker thread the platform refused to create

Inheritance diagram for sourcemeta::core::ParallelThreadError:
sourcemeta::core::Error

Public Member Functions

 ParallelThreadError (const StaticString message) noexcept
 Construct an error with the given message.
Public Member Functions inherited from sourcemeta::core::Error
auto message () const noexcept -> StaticString

Additional Inherited Members

Protected Member Functions inherited from sourcemeta::core::Error
 Error (const StaticString message) noexcept
 Construct the error given its message.

Function Documentation

◆ parallel_for_each()

template<typename Iterator, typename Callback>
requires std::input_iterator<Iterator> && std::copyable<Iterator> && std::invocable<Callback, std::iter_reference_t<Iterator>,
std::size_t, std::size_t>
auto sourcemeta::core::parallel_for_each ( Iterator first,
Iterator last,
Callback && callback,
const std::size_t parallelism = std::thread::hardware_concurrency(),
const std::size_t stack_size_bytes = 0 ) -> void

Process a collection in parallel. By default, parallelism uses the available number of cores. Setting parallelism to zero runs sequentially. When worker threads are created, a stack size of zero uses the platform default, and an unsupported stack size throws. Sequential execution uses the calling thread's stack. Single-pass iterators run sequentially and report an effective parallelism of one to the callback. For example:

#include <sourcemeta/core/parallel.h>
#include <vector>
#include <mutex>
#include <iostream>
std::vector<std::size_t> input;
for (std::size_t index = 0; index < 20; index++) {
input.push_back(index);
}
std::mutex mutex;
std::vector<std::size_t> result;
input.cbegin(), input.cend(),
[&mutex, &result](const auto value,
const auto parallelism,
const auto cursor) {
std::scoped_lock lock{mutex};
result.push_back(value);
std::cerr << "Processing " << cursor
<< " with parallelism " << parallelism << "\n";
});
auto parallel_for_each(Iterator first, Iterator last, Callback &&callback, const std::size_t parallelism=std::thread::hardware_concurrency(), const std::size_t stack_size_bytes=0) -> void
Definition parallel_for_each.h:93