MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
parallel.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ALGORITHM_PARALLEL_HPP__
2#define MSTL_CORE_ALGORITHM_PARALLEL_HPP__
3
10
14
20
35template <typename Iterator, typename BinaryOperation, typename Result,
36 size_t Threshhold = 10, enable_if_t<is_ranges_input_iter_v<Iterator>, int> = 0>
37void reduce(Iterator first, Iterator last, BinaryOperation op, Result& res) {
38 const size_t dist = _MSTL distance(first, last);
39 if (dist <= Threshhold) {
40 for (Iterator it = first; it != last; ++it)
41 res = op(res, *it);
42 }
43 else {
44 Iterator mid = _MSTL next(first, dist / 2);
45 Result l_res = res, r_res = res;
47 _MSTL reduce(first, mid, op, l_res);
48 r_thd.join();
49 res = op(l_res, r_res);
50 }
51}
52
69template <typename Iterator, typename UnaryOperation, typename BinaryOp, typename Result,
70 size_t Threshhold = 10, enable_if_t<is_ranges_input_iter_v<Iterator>, int> = 0>
71void transform_reduce(Iterator first, Iterator last, UnaryOperation transform, BinaryOp reduce, Result& res) {
72 const size_t dist = _MSTL distance(first, last);
73 if (dist <= Threshhold) {
74 for (Iterator it = first; it != last; ++it)
75 res = reduce(res, transform(*it));
76 }
77 else {
78 Iterator mid = _MSTL next(first, dist / 2);
79 Result l_res = _MSTL initialize<Result>(), r_res = _MSTL initialize<Result>();
81 mid, last, transform, reduce, _MSTL ref(r_res));
82 _MSTL transform_reduce(first, mid, transform, reduce, l_res);
83 r_thd.join();
84 res = reduce(res, reduce(l_res, r_res));
85 }
86}
87 // ParallelAlgorithms
89
91#endif // MSTL_CORE_ALGORITHM_PARALLEL_HPP__
线程类
constexpr Iterator next(Iterator iter, iter_difference_t< Iterator > n=1)
获取迭代器的后一个位置
constexpr iter_difference_t< Iterator > distance(Iterator first, Iterator last)
计算两个迭代器之间的距离
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
void reduce(Iterator first, Iterator last, BinaryOperation op, Result &res)
并行归约操作
void transform_reduce(Iterator first, Iterator last, UnaryOperation transform, BinaryOp reduce, Result &res)
并行变换归约操作
MSTL_NODISCARD constexpr reference_wrapper< T > ref(T &val) noexcept
创建引用包装器
constexpr Iterator2 transform(Iterator1 first, Iterator1 last, Iterator2 result, UnaryOperation op) noexcept(noexcept(++first) &&noexcept(++result) &&noexcept(*result=op(*first)))
对范围元素应用一元变换
constexpr T initialize() noexcept(is_nothrow_default_constructible< T >::value)
返回类型T的默认初始化值
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
MSTL迭代器操作算法
MSTL线程支持