1 /*
2     Copyright (c) 2022 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 /* Flow Graph Code Example for the Userguide.
18 */
19 
20 //! Enable extended task_arena constraints feature for supporting Intel Hybrid Technology
21 //! and Intel Hyper-Threading Technology.
22 #define TBB_PREVIEW_TASK_ARENA_CONSTRAINTS_EXTENSION 1
23 
24 #include <oneapi/tbb/flow_graph.h>
25 #include <vector>
26 
27 using namespace tbb::flow;
28 
29 //! Example shows how to set the most performant core type as the preferred one
30 //! for a graph execution.
31 static void flow_graph_attach_to_arena_1() {
32 /*begin_attach_to_arena_1*/
33     std::vector<tbb::core_type_id> core_types = tbb::info::core_types();
34     tbb::task_arena arena(
35         tbb::task_arena::constraints{}.set_core_type(core_types.back())
36     );
37 
38     arena.execute( [&]() {
39         graph g;
40         function_node< int > f( g, unlimited, []( int ) {
41              /*the most performant core type is defined as preferred.*/
42         } );
43         f.try_put(1);
44         g.wait_for_all();
45     } );
46 /*end_attach_to_arena_1*/
47 }
48 
49 //! Reattach existing graph to an arena with the most performant core type as
50 //! the preferred one for a work execution.
51 static void flow_graph_attach_to_arena_2() {
52 /*begin_attach_to_arena_2*/
53     graph g;
54     function_node< int > f( g, unlimited, []( int ) {
55         /*the most performant core type is defined as preferred.*/
56     } );
57 
58     std::vector<tbb::core_type_id> core_types = tbb::info::core_types();
59     tbb::task_arena arena(
60         tbb::task_arena::constraints{}.set_core_type(core_types.back())
61     );
62 
63     arena.execute( [&]() {
64         g.reset();
65     } );
66     f.try_put(1);
67     g.wait_for_all();
68 /*end_attach_to_arena_2*/
69 }
70 
71 int main() {
72     flow_graph_attach_to_arena_1();
73     flow_graph_attach_to_arena_2();
74 
75     return 0;
76 }
77