Blender V5.3
task_scheduler.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_lazy_threading.hh"
14#include "BLI_simd.hh"
15#include "BLI_task_c.hh"
16#include "BLI_threads.hh"
17
18#ifdef WITH_TBB
19/* Need to include at least one header to get the version define. */
20# include <tbb/blocked_range.h>
21# include <tbb/task_arena.h>
22# include <tbb/task_scheduler_observer.h>
23# if TBB_INTERFACE_VERSION_MAJOR >= 10
24# include <tbb/global_control.h>
25# define WITH_TBB_GLOBAL_CONTROL
26# endif
27#endif
28
29namespace blender {
30
31/* Task Scheduler */
32
34#ifdef WITH_TBB_GLOBAL_CONTROL
35static tbb::global_control *task_scheduler_global_control = nullptr;
36#endif
37
38#ifdef WITH_TBB
46class FlushDenormalsObserver : public tbb::task_scheduler_observer {
47 public:
48 FlushDenormalsObserver()
49 {
50 this->observe(true);
51 }
52
53 ~FlushDenormalsObserver() override
54 {
55 this->observe(false);
56 }
57
58 void on_scheduler_entry(bool /*is_worker*/) override
59 {
61 }
62};
63
64static void ensure_flush_denormals_observer()
65{
66 static FlushDenormalsObserver observer;
67}
68#endif
69
70void BLI_task_scheduler_init(const bool use_flush_denormals_to_zero)
71{
72 if (use_flush_denormals_to_zero) {
73 /* The main thread is not observed by the task scheduler, set it directly. */
75
76#ifdef WITH_TBB
77 ensure_flush_denormals_observer();
78#endif
79 }
80
81#ifdef WITH_TBB_GLOBAL_CONTROL
82 const int threads_override_num = BLI_system_num_threads_override_get();
83
84 if (threads_override_num > 0) {
85 /* Override number of threads. This settings is used within the lifetime
86 * of tbb::global_control, so we allocate it on the heap. */
87 task_scheduler_global_control = MEM_new<tbb::global_control>(
88 __func__, tbb::global_control::max_allowed_parallelism, threads_override_num);
89 task_scheduler_num_threads = threads_override_num;
90 }
91 else {
92 /* Let TBB choose the number of threads. For (legacy) code that calls
93 * BLI_task_scheduler_num_threads() we provide the system thread count.
94 * Ideally such code should be rewritten not to use the number of threads
95 * at all. */
97 }
98#else
100#endif
101}
102
104{
105#ifdef WITH_TBB_GLOBAL_CONTROL
106 MEM_delete(task_scheduler_global_control);
107#endif
108}
109
114
115void BLI_task_isolate(void (*func)(void *userdata), void *userdata)
116{
117#ifdef WITH_TBB
119 tbb::this_task_arena::isolate([&] { func(userdata); });
120#else
121 func(userdata);
122#endif
123}
124
125} // namespace blender
#define SIMD_SET_FLUSH_TO_ZERO
Definition BLI_simd.hh:91
Read Blender memory allocation and freeing.
BMesh Iterators.
int BLI_task_scheduler_num_threads()
void BLI_task_scheduler_exit()
static int task_scheduler_num_threads
int BLI_system_thread_count()
Definition threads.cc:258
void BLI_task_isolate(void(*func)(void *userdata), void *userdata)
int BLI_system_num_threads_override_get()
Definition threads.cc:299
void BLI_task_scheduler_init(bool use_flush_denormals_to_zero=true)