d96f8f582c1cf4c4889215bec99e57c14fc5f262
[platform/upstream/tbb.git] / examples / parallel_do / parallel_preorder / main.cpp
1 /*
2     Copyright (c) 2005-2019 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 /* Example program that shows how to use parallel_do to do parallel preorder
18    traversal of a directed acyclic graph. */
19
20 #include <cstdlib>
21 #include "tbb/task_scheduler_init.h"
22 #include "tbb/tick_count.h"
23 #include "../../common/utility/utility.h"
24 #include <iostream>
25 #include <vector>
26 #include "Graph.h"
27
28 // some forward declarations
29 class Cell;
30 void ParallelPreorderTraversal( const std::vector<Cell*>& root_set );
31
32 //------------------------------------------------------------------------
33 // Test driver
34 //------------------------------------------------------------------------
35 utility::thread_number_range threads(tbb::task_scheduler_init::default_num_threads);
36 static unsigned nodes = 1000;
37 static unsigned traversals = 500;
38 static bool SilentFlag = false;
39
40 //! Parse the command line.
41 static void ParseCommandLine( int argc, const char* argv[] ) {
42     utility::parse_cli_arguments(
43             argc,argv,
44             utility::cli_argument_pack()
45                 //"-h" option for displaying help is present implicitly
46                 .positional_arg(threads,"n-of-threads",utility::thread_number_range_desc)
47                 .positional_arg(nodes,"n-of-nodes","number of nodes in the graph.")
48                 .positional_arg(traversals,"n-of-traversals","number of times to evaluate the graph. Reduce it (e.g. to 100) to shorten example run time\n")
49                 .arg(SilentFlag,"silent","no output except elapsed time ")
50     );
51 }
52
53 int main( int argc, const char* argv[] ) {
54     try {
55         tbb::tick_count main_start = tbb::tick_count::now();
56         ParseCommandLine(argc,argv);
57
58         // Start scheduler with given number of threads.
59         for( int p=threads.first; p<=threads.last; p = threads.step(p) ) {
60             tbb::tick_count t0 = tbb::tick_count::now();
61             tbb::task_scheduler_init init(p);
62             srand(2);
63             size_t root_set_size = 0;
64             {
65                 Graph g;
66                 g.create_random_dag(nodes);
67                 std::vector<Cell*> root_set;
68                 g.get_root_set(root_set);
69                 root_set_size = root_set.size();
70                 for( unsigned int trial=0; trial<traversals; ++trial ) {
71                     ParallelPreorderTraversal(root_set);
72                 }
73             }
74             tbb::tick_count::interval_t interval = tbb::tick_count::now()-t0;
75             if (!SilentFlag){
76                 std::cout
77                     <<interval.seconds()<<" seconds using "<<p<<" threads ("<<root_set_size<<" nodes in root_set)\n";
78             }
79         }
80         utility::report_elapsed_time((tbb::tick_count::now()-main_start).seconds());
81
82         return 0;
83     }catch(std::exception& e){
84         std::cerr
85             << "unexpected error occurred. \n"
86             << "error description: "<<e.what()<<std::endl;
87         return -1;
88     }
89 }