/proc/self/cwd/external/ducc/google/threading.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2023 The TensorFlow Authors. All Rights Reserved. |
2 | | |
3 | | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | you may not use this file except in compliance with the License. |
5 | | You may obtain a copy of the License at |
6 | | |
7 | | http://www.apache.org/licenses/LICENSE-2.0 |
8 | | |
9 | | Unless required by applicable law or agreed to in writing, software |
10 | | distributed under the License is distributed on an "AS IS" BASIS, |
11 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | See the License for the specific language governing permissions and |
13 | | limitations under the License. |
14 | | ==============================================================================*/ |
15 | | #include "ducc/google/threading.h" |
16 | | |
17 | | #include <thread> |
18 | | #include <utility> |
19 | | |
20 | | #include "ducc/src/ducc0/infra/threading.h" |
21 | | #include "unsupported/Eigen/CXX11/ThreadPool" |
22 | | |
23 | | namespace ducc0 { |
24 | | |
25 | | namespace google { |
26 | | |
27 | | namespace { |
28 | | |
29 | | // Default shared global pool. It is created on first use. |
30 | 0 | EigenThreadPool* GetGlobalThreadPoolSingleton() { |
31 | 0 | static Eigen::ThreadPool* eigen_pool = |
32 | 0 | new Eigen::ThreadPool(std::thread::hardware_concurrency()); |
33 | 0 | static EigenThreadPool* pool = new EigenThreadPool(*eigen_pool); |
34 | 0 | return pool; |
35 | 0 | } |
36 | | |
37 | | // Thread-local active pool for current execution. |
38 | 0 | ducc0::detail_threading::thread_pool*& GetActiveThreadPoolSingleton() { |
39 | 0 | thread_local thread_pool* active_pool = nullptr; |
40 | 0 | return active_pool; |
41 | 0 | } |
42 | | |
43 | | } // namespace |
44 | | } // namespace google |
45 | | |
46 | | // Implementations required by ducc0. |
47 | | namespace detail_threading { |
48 | | |
49 | | // Missing variable used by DUCC threading.cc. |
50 | | thread_local bool in_parallel_region = false; |
51 | | |
52 | 0 | thread_pool* set_active_pool(thread_pool* new_pool) { |
53 | 0 | return std::exchange(ducc0::google::GetActiveThreadPoolSingleton(), new_pool); |
54 | 0 | } |
55 | | |
56 | 0 | thread_pool* get_active_pool() { |
57 | 0 | thread_pool* pool = google::GetActiveThreadPoolSingleton(); |
58 | 0 | if (pool == nullptr) { |
59 | | // Set to use a global pool. This may trigger threadpool creation. |
60 | | // Since the active pool is thread-local, this is thread-safe. |
61 | 0 | pool = google::GetGlobalThreadPoolSingleton(); |
62 | 0 | set_active_pool(pool); |
63 | 0 | } |
64 | 0 | return pool; |
65 | 0 | } |
66 | | |
67 | | } // namespace detail_threading |
68 | | } // namespace ducc0 |