.. index:: pair: page; Using oneDNN with Threadpool-based Threading .. _doxid-dev_guide_threadpool: Using oneDNN with Threadpool-based Threading ============================================ When oneDNN is built with the threadpool CPU runtime (see :ref:`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 ``:ref:`include/oneapi/dnnl/dnnl_threadpool_iface.hpp ```. Below is a sample implementation based on Eigen threadpool that is also used for testing (see ``tests/test_thread.cpp``). .. ref-code-block:: cpp #include "dnnl_threadpool_iface.hpp" class threadpool : public :ref:`dnnl::threadpool_interop::threadpool_iface ` { private: // Change to Eigen::NonBlockingThreadPool if using Eigen <= 3.3.7 std::unique_ptr 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)); } int :ref:`get_num_threads `() override { return tp_->NumThreads(); } bool :ref:`get_in_parallel `() override { return tp_->CurrentThreadId() != -1; } uint64_t :ref:`get_flags `() override { return :ref:`ASYNCHRONOUS `; } void :ref:`parallel_for `( int n, const std::function &fn) override { for (int i = 0; i < n; i++) tp_->Schedule([i, n, fn]() { fn(i, n); }); } };