std::experi...sform_reduce(3)
π NAME π
okay so bestie, welcome to the std::experi...sform_reduce(3) page from the C++ Standard Libary manual (yes, that spelling is literally right there at the top β not me, I promise π).
std::experimental::parallel::transform_reduce β std::experimental::parallel::transform_reduce
β¨ Synopsis β¨
Defined in header <experimental/numeric>.
Overload (1) β (parallelism TS):
template< class InputIt, class UnaryOp, class T, class BinaryOp
>
T transform_reduce( InputIt first, InputIt last,
UnaryOp unary_op, T init, BinaryOp binary_op );
Overload (2) β (parallelism TS):
template< class ExecutionPolicy,
class InputIt, class UnaryOp, class T, class BinaryOp
>
T transform_reduce( ExecutionPolicy&& policy,
InputIt first, InputIt last,
UnaryOp unary_op, T init, BinaryOp binary_op );
okay so what it does: it applies unary_op to each element in the range [first, last) and reduces the results β possibly permuted and aggregated in unspecified manner β along with the initial value init over binary_op. periodt.
The behavior is non-deterministic if binary_op is not associative or not commutative. no cap.
The behavior is undefined if unary_op or binary_op modifies any element or invalidates any iterator in [first, last). that's the danger zone, bestie π.
π Parameters π
first,lastβ the range of elements to apply the algorithm toinitβ the initial value of the generalized sumpolicyβ the execution policyunary_opβ unaryFunctionObjectthat will be applied to each element of the input range. The return type must be acceptable as input tobinary_opbinary_opβ binaryFunctionObjectthat will be applied in unspecified order to the results ofunary_op, the results of otherbinary_opandinit
π Type requirements π
InputItmust meet the requirements ofLegacyInputIterator.
π Return value π
Generalized sum of init and unary_op(*first), unary_op(*(first + 1)), ... unary_op(*(last - 1)) over binary_op, where generalized sum GSUM(op, a1, ..., aN) is defined as follows:
- if
N = 1,a1, - if
N > 1,op(GSUM(op, b1, ..., bK), GSUM(op, bM, ..., bN))whereb1, ..., bNmay be any permutation ofa1, ..., aNand1 < K + 1 = M β€ N
in other words, the results of unary_op may be grouped and arranged in arbitrary order. it's giving flexible-sum energy π
.
β‘ Complexity β‘
O(last - first) applications each of unary_op and binary_op.
π Exceptions π
- If execution of a function invoked as part of the algorithm throws an exception,
- if
policyisparallel_vector_execution_policy,std::terminateis called. - if
policyissequential_execution_policyorparallel_execution_policy, the algorithm exits with anexception_listcontaining all uncaught exceptions. If there was only one uncaught exception, the algorithm may rethrow it without wrapping inexception_list. It is unspecified how much work the algorithm will perform before returning after the first exception was encountered. - if
policyis some other type, the behavior is implementation-defined.
- if
- If the algorithm fails to allocate memory (either for itself or to construct an
exception_listwhen handling a user exception),std::bad_allocis thrown.
π¦ Notes π¦
unary_op is not applied to init.
If the range is empty, init is returned, unmodified.
- If
policyis an instance ofsequential_execution_policy, all operations are performed in the calling thread. - If
policyis an instance ofparallel_execution_policy, operations may be performed in unspecified number of threads, indeterminately sequenced with each other. - If
policyis an instance ofparallel_vector_execution_policy, execution may be both parallelized and vectorized: function body boundaries are not respected and user code may be overlapped and combined in arbitrary manner (in particular, this implies that a user-providedCallablemust not acquire a mutex to access a shared resource).
π Example π
transform_reduce can be used to parallelize std::inner_product:
// Run this code
#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple.hpp>
#include <experimental/execution_policy>
#include <experimental/numeric>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<double> xvalues(10007, 1.0), yvalues(10007, 1.0);
double result = std::experimental::parallel::transform_reduce(
std::experimental::parallel::par,
boost::iterators::make_zip_iterator(
boost::make_tuple(std::begin(xvalues),
std::begin(yvalues))),
boost::iterators::make_zip_iterator(
boost::make_tuple(std::end(xvalues), std::end(yvalues))),
[](auto r) { return boost::get<0>(r) * boost::get<1>(r); }
0.0,
std::plus<>()
);
std::cout << result << '\n';
}
Output:
10007
π See also π
accumulateβ sums up or folds a range of elements (function template)transformβ applies a function to a range of elements, storing results in a destination range (function template)reduceβ similar tostd::accumulate, except out of order (parallelism TS) (function template)
okay that's the whole page bestie π«Ά source/version: http://cppreference.com, date: 2024.06.10, page: std::experi...sform_reduce(3). mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms