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 #include <cstdlib> 18 #include <cstdio> 19 #include <cstring> 20 #include <cctype> 21 22 #include <utility> 23 #include <iostream> 24 #include <sstream> 25 26 #include "oneapi/tbb/tick_count.h" 27 28 #include "common/utility/utility.hpp" 29 30 #include "primes.hpp" 31 32 struct RunOptions { 33 //! NumberType of threads to use. 34 utility::thread_number_range threads; 35 //whether to suppress additional output 36 bool silentFlag; 37 // 38 NumberType n; 39 //! Grain size parameter 40 NumberType grainSize; 41 // number of time to repeat calculation 42 NumberType repeatNumber; 43 44 RunOptions(utility::thread_number_range threads_, 45 NumberType grainSize_, 46 NumberType n_, 47 bool silentFlag_, 48 NumberType repeatNumber_) 49 : threads(threads_), 50 silentFlag(silentFlag_), 51 n(n_), 52 grainSize(grainSize_), 53 repeatNumber(repeatNumber_) {} 54 }; 55 56 //! Parse the command line. 57 static RunOptions ParseCommandLine(int argc, char* argv[]) { 58 utility::thread_number_range threads( 59 utility::get_default_num_threads, 0, utility::get_default_num_threads()); 60 NumberType grainSize = 1000; 61 bool silent = false; 62 NumberType number = 100000000; 63 NumberType repeatNumber = 1; 64 65 utility::parse_cli_arguments( 66 argc, 67 argv, 68 utility::cli_argument_pack() 69 //"-h" option for displaying help is present implicitly 70 .positional_arg(threads, "n-of-threads", utility::thread_number_range_desc) 71 .positional_arg(number, 72 "number", 73 "upper bound of range to search primes in, must be a positive integer") 74 .positional_arg(grainSize, "grain-size", "must be a positive integer") 75 .positional_arg( 76 repeatNumber, 77 "n-of-repeats", 78 "repeat the calculation this number of times, must be a positive integer") 79 .arg(silent, "silent", "no output except elapsed time")); 80 81 RunOptions options(threads, grainSize, number, silent, repeatNumber); 82 return options; 83 } 84 85 int main(int argc, char* argv[]) { 86 oneapi::tbb::tick_count mainBeginMark = oneapi::tbb::tick_count::now(); 87 RunOptions options = ParseCommandLine(argc, argv); 88 89 // Try different numbers of threads 90 for (int p = options.threads.first; p <= options.threads.last; p = options.threads.step(p)) { 91 for (NumberType i = 0; i < options.repeatNumber; ++i) { 92 oneapi::tbb::tick_count iterationBeginMark = oneapi::tbb::tick_count::now(); 93 NumberType count = 0; 94 NumberType n = options.n; 95 if (p == 0) { 96 count = SerialCountPrimes(n); 97 } 98 else { 99 NumberType grainSize = options.grainSize; 100 count = ParallelCountPrimes(n, p, grainSize); 101 } 102 oneapi::tbb::tick_count iterationEndMark = oneapi::tbb::tick_count::now(); 103 if (!options.silentFlag) { 104 std::cout << "#primes from [2.." << options.n << "] = " << count << " (" 105 << (iterationEndMark - iterationBeginMark).seconds() << " sec with "; 106 if (0 != p) 107 std::cout << p << "-way parallelism"; 108 else 109 std::cout << "serial code"; 110 std::cout << ")\n"; 111 } 112 } 113 } 114 utility::report_elapsed_time((oneapi::tbb::tick_count::now() - mainBeginMark).seconds()); 115 return 0; 116 } 117