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