1 | /******************************************************************************* |
2 | * Copyright 2020 Intel Corporation |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | *******************************************************************************/ |
16 | |
17 | #ifndef ONEAPI_DNNL_DNNL_THREADPOOL_IFACE_HPP |
18 | #define ONEAPI_DNNL_DNNL_THREADPOOL_IFACE_HPP |
19 | |
20 | #include <functional> |
21 | |
22 | /// @addtogroup dnnl_api |
23 | /// @{ |
24 | |
25 | namespace dnnl { |
26 | |
27 | /// @addtogroup dnnl_api_interop |
28 | /// @{ |
29 | |
30 | /// @addtogroup dnnl_api_threadpool_interop |
31 | /// @{ |
32 | |
33 | namespace threadpool_interop { |
34 | |
35 | /// Abstract threadpool interface. The users are expected to subclass this |
36 | /// interface and pass an object to the library during CPU stream creation or |
37 | /// directly in case of BLAS functions. |
38 | struct threadpool_iface { |
39 | /// Returns the number of worker threads. |
40 | virtual int get_num_threads() const = 0; |
41 | |
42 | /// Returns true if the calling thread belongs to this threadpool. |
43 | virtual bool get_in_parallel() const = 0; |
44 | |
45 | /// Submits n instances of a closure for execution in parallel: |
46 | /// |
47 | /// for (int i = 0; i < n; i++) fn(i, n); |
48 | /// |
49 | virtual void parallel_for(int n, const std::function<void(int, int)> &fn) |
50 | = 0; |
51 | |
52 | /// Returns threadpool behavior flags bit mask (see below). |
53 | virtual uint64_t get_flags() const = 0; |
54 | |
55 | /// If set, parallel_for() returns immediately and oneDNN needs implement |
56 | /// waiting for the submitted closures to finish execution on its own. |
57 | static constexpr uint64_t ASYNCHRONOUS = 1; |
58 | |
59 | virtual ~threadpool_iface() {} |
60 | }; |
61 | |
62 | } // namespace threadpool_interop |
63 | |
64 | /// @} dnnl_api_threadpool_interop |
65 | |
66 | /// @} dnnl_api_interop |
67 | |
68 | } // namespace dnnl |
69 | |
70 | /// @} dnnl_api |
71 | |
72 | #endif |
73 | |