File: taskschedulertbb.cpp

package info (click to toggle)
embree 3.13.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,924 kB
  • sloc: cpp: 180,815; xml: 3,877; ansic: 2,957; python: 1,466; sh: 502; makefile: 229; csh: 42
file content (91 lines) | stat: -rw-r--r-- 2,539 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "taskschedulertbb.h"

namespace embree
{
  static bool g_tbb_threads_initialized = false;

#if TBB_INTERFACE_VERSION >= 11005
  static tbb::global_control* g_tbb_thread_control = nullptr;
#else
  static tbb::task_scheduler_init g_tbb_threads(tbb::task_scheduler_init::deferred);
#endif

  class TBBAffinity: public tbb::task_scheduler_observer
  {
  public:

    void on_scheduler_entry( bool ) {
      setAffinity(TaskScheduler::threadIndex());
    }

  } tbb_affinity;

  void TaskScheduler::create(size_t numThreads, bool set_affinity, bool start_threads)
  {
    assert(numThreads);

    /* first terminate threads in case we configured them */
    if (g_tbb_threads_initialized) {
#if TBB_INTERFACE_VERSION >= 11005
      delete g_tbb_thread_control;
      g_tbb_thread_control = nullptr;
#else
      g_tbb_threads.terminate();
#endif
      g_tbb_threads_initialized = false;
    }

    /* only set affinity if requested by the user */
#if TBB_INTERFACE_VERSION >= 9000 // affinity not properly supported by older TBB versions
    if (set_affinity)
      tbb_affinity.observe(true);
#endif

    /* now either keep default settings or configure number of threads */
    if (numThreads == std::numeric_limits<size_t>::max()) {
      numThreads = threadCount();
    }
    else {
      g_tbb_threads_initialized = true;
      const size_t max_concurrency = threadCount();
      if (numThreads > max_concurrency) numThreads = max_concurrency;
#if TBB_INTERFACE_VERSION >= 11005
      g_tbb_thread_control = new tbb::global_control(tbb::global_control::max_allowed_parallelism,numThreads);
#else
      g_tbb_threads.initialize(int(numThreads));
#endif
    }

    /* start worker threads */
    if (start_threads)
    {
      BarrierSys barrier(numThreads);
      tbb::parallel_for(size_t(0), size_t(numThreads), size_t(1), [&] ( size_t i ) {
          barrier.wait();
        });
    }
  }

  void TaskScheduler::destroy()
  {
#if TBB_INTERFACE_VERSION >= 9000 // affinity not properly supported by older TBB versions
    // Stop observe to prevent calling on_scheduler_entry
    // when static objects are already destroyed.
    if (tbb_affinity.is_observing())
      tbb_affinity.observe(false);
#endif

    if (g_tbb_threads_initialized) {
#if TBB_INTERFACE_VERSION >= 11005
      delete g_tbb_thread_control;
      g_tbb_thread_control = nullptr;
#else
      g_tbb_threads.terminate();
#endif
      g_tbb_threads_initialized = false;
    }
  }
}