3 namespace threading_serial {
5 using namespace threading_common;
10 typename Result = std::result_of_t<Fn && (Args && ...)>>
12 std::promise<Result> pr;
13 if constexpr (std::is_same<void, Result>::value) {
14 fn(std::forward<Args>(
args)...);
17 pr.set_value(fn(std::forward<Args>(
args)...));
19 return pr.get_future();
33 template <
typename Int,
typename Body,
typename Partitioner = auto_partitioner>
36 const Partitioner& p = Partitioner()) {
40 start_entry = range.
begin(),
41 stop_entry = range.
end(),
42 stride = (range.
size() + worker_count - 1) / worker_count;
43 i < worker_count && start_entry < stop_entry;
44 ++i, start_entry += stride) {
45 const auto end_entry = std::min(start_entry + stride, stop_entry);
52 template <
typename Index,
typename Function,
typename Partitioner = auto_partitioner>
56 const Partitioner& p = Partitioner()) {
60 for (
auto i = r.
begin(), e = r.
end(); i < e; i++) {
69 template <
typename Int,
75 const Value& identity,
76 const RealBody& real_body,
77 const Reduction& reduction,
78 const Partitioner& p = Partitioner()) {
80 std::vector<Value> worker_threads;
81 worker_threads.reserve(worker_count);
84 start_entry = range.
begin(),
85 stop_entry = range.
end(),
86 stride = (range.
size() + worker_count - 1) / worker_count;
87 i < worker_count && start_entry < stop_entry;
88 ++i, start_entry += stride) {
89 const auto end_entry = std::min(start_entry + stride, stop_entry);
91 worker_threads.emplace_back(
95 for (
auto& child : worker_threads) {
96 v = reduction(v, child);
size_type size() const
Size of the range.
future< Result > async(Fn &&fn, Args &&...args)
A range over which to iterate.
Value parallel_reduce(const blocked_range< Int > &range, const Value &identity, const RealBody &real_body, const Reduction &reduction, const Partitioner &p=Partitioner())
Parallel iteration with reduction.
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
const_iterator end() const
One past last value in range.
const_iterator begin() const
Beginning of range.