When oneDNN is built with the threadpool CPU runtime (see Build Options), oneDNN requires a user to implement a threadpool interface to enable the library to perform computations using multiple threads.
The threadpool interface is defined in include/dnnl_threadpool_iface.hpp
. Below is a sample implementation based on Eigen threadpool that is also used for testing (see tests/testing_threadpool.hpp
).
#include "dnnl_threadpool_iface.hpp"
private:
std::unique_ptr<Eigen::ThreadPool> tp_;
public:
explicit threadpool(int num_threads = 0) {
if (num_threads <= 0)
num_threads = (int)std::thread::hardware_concurrency();
tp_.reset(new Eigen::ThreadPool(num_threads));
}
return tp_->CurrentThreadId() != -1;
}
int n, const std::function<void(int, int)> &fn) override {
for (int i = 0; i < n; i++)
tp_->Schedule([i, n, fn]() { fn(i, n); });
}
};
Abstract threadpool interface.
Definition: dnnl_threadpool_iface.hpp:38
virtual int get_num_threads() const =0
Returns the number of worker threads.
virtual bool get_in_parallel() const =0
Returns true if the calling thread belongs to this threadpool.
virtual uint64_t get_flags() const =0
Returns threadpool behavior flags bit mask (see below).
static constexpr uint64_t ASYNCHRONOUS
If set, parallel_for() returns immediately and oneDNN needs implement waiting for the submitted closu...
Definition: dnnl_threadpool_iface.hpp:57
virtual void parallel_for(int n, const std::function< void(int, int)> &fn)=0
Submits n instances of a closure for execution in parallel: