Debian Codegraph @

third-party/tbb/rfcs/proposed/numa_support/README.md

raw
1 # NUMA support
2
3 ## Introduction
4
5 In Non-Uniform Memory Access (NUMA) systems, the cost of memory accesses depends on the
6 *nearness* of the processor to the memory resource on which the accessed data resides.
7 While oneTBB has core support that enables developers to tune for Non-Uniform Memory
8 Access (NUMA) systems, we believe this support can be simplified and improved to provide
9 an improved user experience.
10
11 This RFC acts as an umbrella for sub-proposals that address four areas for improvement:
12
13 1. improved reliability of HWLOC-dependent topology and pinning support in,
14 2. addition of a NUMA-aware allocation,
15 3. simplified approaches to associate task distribution with data placement and
16 4. where possible, improved out-of-the-box performance for high-level oneTBB features.
17
18 We expect that this draft proposal will spawn sub-proposals that will progress
19 independently based on feedback and prioritization of the suggested features.
20
21 The features for NUMA tuning already available in the oneTBB 1.3 specification include:
22
23 - Functions in the `tbb::info` namespace **[info_namespace]**
24 - `std::vector<numa_node_id> numa_nodes()`
25 - `int default_concurrency(numa_node_id id = oneapi::tbb::task_arena::automatic)`
26 - `tbb::task_arena::constraints` in **[scheduler.task_arena]**
27
28 Below is the example based on existing oneTBB documentation that demonstrates the use of these APIs
29 to pin threads to different arenas to each of the NUMA nodes available on a system, submit work
30 across those `task_arena` objects and into associated `task_group` objects, and then wait for work
31 again using both the `task_arena` and `task_group` objects.
32
33 void constrain_for_numa_nodes() {
34 std::vector<tbb::numa_node_id> numa_nodes = tbb::info::numa_nodes();
35 std::vector<tbb::task_arena> arenas(numa_nodes.size());
36 std::vector<tbb::task_group> task_groups(numa_nodes.size());
37
38 // initialize each arena, each constrained to a different NUMA node
39 for (int i = 0; i < numa_nodes.size(); i++)
40 arenas[i].initialize(tbb::task_arena::constraints(numa_nodes[i]), 0);
41
42 // enqueue work to all but the first arena, using the task_group to track work
43 // by using defer, the task_group reference count is incremented immediately
44 for (int i = 1; i < numa_nodes.size(); i++)
45 arenas[i].enqueue(
46 task_groups[i].defer([] {
47 tbb::parallel_for(0, N, [](int j) { f(w); });
48 })
49 );
50
51 // directly execute the work to completion in the remaining arena
52 arenas[0].execute([] {
53 tbb::parallel_for(0, N, [](int j) { f(w); });
54 });
55
56 // join the other arenas to wait on their task_groups
57 for (int i = 1; i < numa_nodes.size(); i++)
58 arenas[i].execute([&task_groups, i] { task_groups[i].wait(); });
59 }
60
61 ### The need for application-specific knowledge
62
63 In general when tuning a parallel application for NUMA systems, the goal is to expose sufficient
64 parallelism while minimizing (or at least controlling) data access and communication costs. The
65 tradeoffs involved in this tuning often rely on application-specific knowledge.
66
67 In particular, NUMA tuning typically involves:
68
69 1. Understanding the overall application problem and its use of algorithms and data containers
70 2. Placement/allocation of data container objects onto memory resources
71 3. Distribution of tasks to hardware resources that optimize for data placement
72
73 As shown in the previous example, the oneTBB 1.3 specification only provides low-level
74 support for NUMA optimization. The `tbb::info` namespace provides topology discovery. And the
75 combination of `task_arena`, `task_arena::constraints` and `task_group` provide a mechanism for
76 placing tasks onto specific processors. There is no high-level support for memory allocation
77 or placement, or for guiding the task distribution of algorithms.
78
79 ### Issues that should be resolved in the oneTBB library
80
81 **The behavior of existing features is not always predictable.** There is a note in
82 section **[info_namespace]** of the oneTBB specification that describes
83 the function `std::vector<numa_node_id> numa_nodes()`, "If error occurs during system topology
84 parsing, returns vector containing single element that equals to `task_arena::automatic`."
85
86 In practice, the error can occurs because HWLOC is not detected on the system. While the
87 oneTBB documentation states in several places that HWLOC is required for NUMA support and
88 even provides guidance on
89 [how to check for HWLOC](https://www.intel.com/content/www/us/en/docs/onetbb/get-started-guide/2021-12/next-steps.html),
90 the inability to resolve HWLOC at runtime silently returns a default of `task_arena::automatic`. This
91 default does not pin threads to NUMA nodes. It is too easy to write code similar to the preceding
92 example and be unaware that a HWLOC installation error (or lack of HWLOC) has undone all your effort.
93
94 **Getting good performance using these tools requires notable manual coding effort by users.** As we
95 can see in the preceding example, if we want to spread work across the NUMA nodes in
96 a system we might need to query the topology using functions in the `tbb::info` namespace, create
97 one `task_arena` per NUMA node, along with one `task_group` per NUMA node, and then add an
98 extra loop that iterates over these `task_arena` and `task_group` objects to execute the
99 work on the desired NUMA nodes. We also need to handle all container allocations using OS-specific
100 APIs (or behaviors, such as first-touch) to allocator or place them on the appropriate NUMA nodes.
101
102 **The out-of-the-box performance of the generic TBB APIs on NUMA systems is not good enough.**
103 Should the oneTBB library do anything special by default if the system is a NUMA system? Or should
104 regular random stealing distribute the work across all of the cores, regardless of which NUMA first
105 touched the data?
106
107 Is it reasonable for a developer to expect that a series of loops, such as the ones that follow, will
108 try to create a NUMA-friendly distribution of tasks so that accesses to the same elements of `b` and `c`
109 in the two loops are from the same NUMA nodes? Or is this too much to expect without providing hints?
110
111 tbb::parallel_for(0, N,
112 [](int i) {
113 b[i] = f(i);
114 c[i] = g(i);
115 });
116
117 tbb::parallel_for(0, N,
118 [](int i) {
119 a[i] = b[i] + c[i];
120 });
121
122 ## Possible Sub-Proposals
123
124 ### Increased availability of NUMA support
125
126 See [sub-RFC for increased availability of NUMA API](tbbbind-link-static-hwloc.org)
127
128
129 ### Add NUMA-constrained arenas
130
131 See [sub-RFC for creation and use of NUMA-constrained arenas](numa-arenas-creation-and-use.org)
132
133 ### NUMA-aware allocation
134
135 Define allocators or other features that simplify the process of allocating or placing data onto
136 specific NUMA nodes.
137
138 ### Simplified approaches to associate task distribution with data placement
139
140 As discussed earlier, NUMA-aware allocation is just the first step in optimizing for NUMA architectures.
141 We also need to deliver mechanisms to guide task distribution so that tasks are executed on execution
142 resources that are near to the data they access. oneTBB already provides low-level support through
143 `tbb::info` and `tbb::task_arena`, but we should up-level this support into the high-level algorithms,
144 flow graph and containers where appropriate.
145
146 ### Improved out-of-the-box performance for high-level oneTBB features.
147
148 For high-level oneTBB features that are modified to provide improved NUMA support, we can try to
149 align default behaviors for those features with user-expectations when used on NUMA systems.
150
151 ## Open Questions
152
153 1. Do we need simplified support, or are users that want NUMA support in oneTBB
154 willing to, or perhaps even prefer, to manage the details manually?
155 2. Is it reasonable to expect good out-of-the-box performance on NUMA systems
156 without user hints or guidance.