Split [0, total) into one chunk per thread. Chunk boundaries are rounded up to a multiple of `align` so each slice's AVX/scalar split lines up with the whole-tensor kernel's split -- otherwise an element could be computed by AVX (FMA) in one layout and the scalar tail (mul+add) in another, which differ in the last bit.
| 556 | // kernel's split -- otherwise an element could be computed by AVX (FMA) in one layout |
| 557 | // and the scalar tail (mul+add) in another, which differ in the last bit. |
| 558 | void parallel_for(size_t total, size_t align, std::function<void(size_t, size_t)> fn) |
| 559 | { |
| 560 | { |
| 561 | std::unique_lock<std::mutex> lk(m_); |
| 562 | fn_ = std::move(fn); |
| 563 | total_ = total; |
| 564 | align_ = std::max<size_t>(1, align); |
| 565 | done_count_ = 0; |
| 566 | ++gen_; |
| 567 | } |
| 568 | cv_start_.notify_all(); |
| 569 | std::unique_lock<std::mutex> lk(m_); |
| 570 | cv_done_.wait(lk, [this] { return done_count_ == n_; }); |
| 571 | } |
| 572 | |
| 573 | private: |
| 574 | void worker(size_t tid, int core) |
no test coverage detected