MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / ParallelForImpl

Method ParallelForImpl

oneflow/core/thread/thread_runtime.h:73–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

71class OfRuntime final : public RuntimeBase {
72 private:
73 void ParallelForImpl(int64_t begin, int64_t end, const CallableT& func, size_t num_threads,
74 size_t grain_size) override {
75 if (unlikely(pthread_fork::IsForkedSubProcess()) || Singleton<ThreadPool>::Get() == nullptr) {
76 return SeqFor(begin, end, func);
77 }
78 const size_t num_elements = end - begin;
79 num_threads = std::min(num_elements, num_threads);
80 BalancedSplitter bs(num_elements, num_threads);
81 BlockingCounter bc(num_threads);
82
83 FOR_RANGE(size_t, range_id, 0, num_threads) {
84 Singleton<ThreadPool>::Get()->AddWork([&bc, &bs, range_id, func] {
85 const size_t begin_ = bs.At(range_id).begin();
86 const size_t end_ = bs.At(range_id).end();
87 SeqFor(begin_, end_, func);
88 bc.Decrease();
89 });
90 }
91 // buzy loop wait.
92 bc.WaitForeverUntilCntEqualZero();
93 }
94};
95
96#if WITH_TBB

Callers

nothing calls this directly

Calls 4

IsForkedSubProcessFunction · 0.85
GetFunction · 0.85
SeqForFunction · 0.85

Tested by

no test coverage detected