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