ParallelFor

ParallelFor#

template<typename Index, typename Handler>
void YR::Parallel::ParallelFor(Index start, Index end, const Handler &handler, size_t chunkSize = -1, int workThreadSize = -1)#

ParallelFor is a function framework for parallel computing, enabling tasks to be executed in parallel across multiple threads to improve computational efficiency.

ParallelFor internally implements parallel computing through task allocation and scheduling, automatically distributing tasks to available threads.

#include "yr/parallel/parallel_for.h"

int main(int argc, char **argv)
{
    YR::Config conf;
    conf.mode = YR::Config::Mode::LOCAL_MODE;
    const int threadPoolSize = 8;
    conf.threadPoolSize = threadPoolSize;
    YR::Init(conf);
    std::vector<uint64_t> results;
    const uint32_t start = 0;
    const uint32_t end = 1000000;
    results.resize(end);
    const uint32_t chunkSize = 1000;
    const int workerNum = 4;

    auto handler = [&results](size_t start, size_t end) {
        for (size_t i = start; i < end; i++) {
            results[i] += i;
        }
    };
    YR::Parallel::ParallelFor<uint32_t>(start, end, handler, chunkSize, workerNum);

    std::vector<std::vector<int>> v(workerNum);
    auto f = [&v](int start, int end, const YR::Parallel::Context &ctx) {
        std::cout << "start: " << start << " , end: " << end << " ctx: " << ctx.id << std::endl;
        for (int i = start; i < end; i++) {
            int result = i;
            if (result) {
                v[ctx.id].emplace_back(result);
            }
        }
    };
    YR::Parallel::ParallelFor<uint32_t>(start, end, f, chunkSize, workerNum);
}

Template Parameters:
  • Index – The type of the iteration variable.

  • Handler – The type of the function to be called.

Parameters:
  • start – The starting value of the loop iteration range.

  • end – The ending value of the loop iteration range (exclusive).

  • handler – The function to be executed in the loop. The parameter list of the user-defined handler can be one of the following two types:

    1. (Index, Index)

    2. (Index, Index, const YR::Parallel::Context&) When the user’s handler uses the YR::Parallel::Context parameter, the value of context.id in the handler will be in the range [0, parallelism).

  • chunkSize – The granularity of the task.

  • workThreadSize – The number of worker threads. If set to -1 (default), it will be set to the number of threads in the thread pool plus 1.

Throws:

Exception

  1. If ParallelFor is called before initialization, an exception “Assertion IsInitialized() failed !!!” will be thrown.

  2. If the parameter list of the user-defined handler does not match the specified format, a compilation error will occur: “error: static assertion failed: handler must have 2 or 3 arguments. And arguments should be (Index, Index) or

    (Index, Index, const YR::Parallel::Context&)”.

The parameter structure is supplemented with the following explanation:

struct Context#

thread context info

Public Members

size_t id#

thread identify id