Template Function concore::v1::conc_for(It, It, const UnaryFunction&, const task_group&, partition_hints)

Function Documentation

template<typename It, typename UnaryFunction>
void concore::v1::conc_for(It first, It last, const UnaryFunction &f, const task_group &grp, partition_hints hints)

A concurrent for algorithm.

If there are no dependencies between the iterations of a for loop, then those iterations can be run in parallel. This function attempts to parallelize these iterations. On a machine that has a very large number of cores, this can execute each iteration on a different core.

Parameters
  • first: Iterator pointing to the first element in a collection

  • last: Iterator pointing to the last element in a collection (1 past the end)

  • f: Functor to apply to each element of the collection

  • grp: Group in which to execute the tasks

  • hints: Hints that may be passed to the

  • work: The work to be applied to be executed for the elements

Template Parameters
  • It: The type of the iterator to use

  • UnaryFunction: The type of function to be applied for each element

  • WorkType: The type of a work object to be used (when not using f)

This ensure that the given work/functor is called exactly once for each element from the given sequence. But the call may happen on different threads.

The function does not return until all the iterations are executed. (It may execute other non-related tasks while waiting for the conc_for tasks to complete).

This generates internal tasks by spawning and waiting for those tasks to complete. If the user spawns other tasks during the execution of an iteration, those tasks would also be waited on. This can be a method of generating more work in the concurrent for loop.

One can cancel the execution of the tasks by passing a task_group in, and canceling that task_group.

One can also provide hints to the implementation to fine-tune the algorithms to better fit the data it operates on. Please note however that the implementation may completely ignore all the hints it was provided.

There are two forms of this function: one that uses a functor, and one that takes a work as parameter. The version with the work given as argument may be faster in certain cases in which, between iterators, we can store temporary data.

The work structure given to the function must have the following structure:

struct GenericWorkType {
    using iterator = my_iterator_type;
    void exec(my_iterator_type first, my_iterator_type last) { ... }
};

This work will be called for various chunks from the input. The iterator type defined in the given work must support basic random-iterator operations, but without dereference. That is: difference, incrementing, and addition with an integer. The work objects must be copyable.

In the case that no work is given, the algorithm expects either input iterators, or integral types.

Warning

If the iterations are not completely independent, this results in undefined behavior.

See

partition_hints, partition_method, task_group