1*a9383225SVladimir Serov /*
2*a9383225SVladimir Serov     Copyright (c) 2022 Intel Corporation
3*a9383225SVladimir Serov 
4*a9383225SVladimir Serov     Licensed under the Apache License, Version 2.0 (the "License");
5*a9383225SVladimir Serov     you may not use this file except in compliance with the License.
6*a9383225SVladimir Serov     You may obtain a copy of the License at
7*a9383225SVladimir Serov 
8*a9383225SVladimir Serov         http://www.apache.org/licenses/LICENSE-2.0
9*a9383225SVladimir Serov 
10*a9383225SVladimir Serov     Unless required by applicable law or agreed to in writing, software
11*a9383225SVladimir Serov     distributed under the License is distributed on an "AS IS" BASIS,
12*a9383225SVladimir Serov     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*a9383225SVladimir Serov     See the License for the specific language governing permissions and
14*a9383225SVladimir Serov     limitations under the License.
15*a9383225SVladimir Serov */
16*a9383225SVladimir Serov 
17*a9383225SVladimir Serov /* Flow Graph Code Example for the Userguide.
18*a9383225SVladimir Serov */
19*a9383225SVladimir Serov 
20*a9383225SVladimir Serov #include <oneapi/tbb/flow_graph.h>
21*a9383225SVladimir Serov #include <vector>
22*a9383225SVladimir Serov 
23*a9383225SVladimir Serov using namespace tbb::flow;
24*a9383225SVladimir Serov 
25*a9383225SVladimir Serov //! Example shows how to set the most performant core type as the preferred one
26*a9383225SVladimir Serov //! for a graph execution.
flow_graph_attach_to_arena_1()27*a9383225SVladimir Serov static void flow_graph_attach_to_arena_1() {
28*a9383225SVladimir Serov /*begin_attach_to_arena_1*/
29*a9383225SVladimir Serov     std::vector<tbb::core_type_id> core_types = tbb::info::core_types();
30*a9383225SVladimir Serov     tbb::task_arena arena(
31*a9383225SVladimir Serov         tbb::task_arena::constraints{}.set_core_type(core_types.back())
32*a9383225SVladimir Serov     );
33*a9383225SVladimir Serov 
34*a9383225SVladimir Serov     arena.execute( [&]() {
35*a9383225SVladimir Serov         graph g;
36*a9383225SVladimir Serov         function_node< int > f( g, unlimited, []( int ) {
37*a9383225SVladimir Serov              /*the most performant core type is defined as preferred.*/
38*a9383225SVladimir Serov         } );
39*a9383225SVladimir Serov         f.try_put(1);
40*a9383225SVladimir Serov         g.wait_for_all();
41*a9383225SVladimir Serov     } );
42*a9383225SVladimir Serov /*end_attach_to_arena_1*/
43*a9383225SVladimir Serov }
44*a9383225SVladimir Serov 
45*a9383225SVladimir Serov //! Reattach existing graph to an arena with the most performant core type as
46*a9383225SVladimir Serov //! the preferred one for a work execution.
flow_graph_attach_to_arena_2()47*a9383225SVladimir Serov static void flow_graph_attach_to_arena_2() {
48*a9383225SVladimir Serov /*begin_attach_to_arena_2*/
49*a9383225SVladimir Serov     graph g;
50*a9383225SVladimir Serov     function_node< int > f( g, unlimited, []( int ) {
51*a9383225SVladimir Serov         /*the most performant core type is defined as preferred.*/
52*a9383225SVladimir Serov     } );
53*a9383225SVladimir Serov 
54*a9383225SVladimir Serov     std::vector<tbb::core_type_id> core_types = tbb::info::core_types();
55*a9383225SVladimir Serov     tbb::task_arena arena(
56*a9383225SVladimir Serov         tbb::task_arena::constraints{}.set_core_type(core_types.back())
57*a9383225SVladimir Serov     );
58*a9383225SVladimir Serov 
59*a9383225SVladimir Serov     arena.execute( [&]() {
60*a9383225SVladimir Serov         g.reset();
61*a9383225SVladimir Serov     } );
62*a9383225SVladimir Serov     f.try_put(1);
63*a9383225SVladimir Serov     g.wait_for_all();
64*a9383225SVladimir Serov /*end_attach_to_arena_2*/
65*a9383225SVladimir Serov }
66*a9383225SVladimir Serov 
main()67*a9383225SVladimir Serov int main() {
68*a9383225SVladimir Serov     flow_graph_attach_to_arena_1();
69*a9383225SVladimir Serov     flow_graph_attach_to_arena_2();
70*a9383225SVladimir Serov 
71*a9383225SVladimir Serov     return 0;
72*a9383225SVladimir Serov }
73