1 /* 2 Copyright (c) 2023 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 "task_emulation_layer.h" 18 19 #include <iostream> 20 #include <numeric> 21 #include <utility> 22 23 int cutoff; 24 25 long serial_fib(int n) { 26 return n < 2 ? n : serial_fib(n - 1) + serial_fib(n - 2); 27 } 28 29 struct fib_continuation : task_emulation::base_task { 30 fib_continuation(int& s) : sum(s) {} 31 32 void execute() override { 33 sum = x + y; 34 } 35 36 int x{ 0 }, y{ 0 }; 37 int& sum; 38 }; 39 40 struct fib_computation : task_emulation::base_task { 41 fib_computation(int n, int* x) : n(n), x(x) {} 42 43 void execute() override { 44 if (n < cutoff) { 45 *x = serial_fib(n); 46 } 47 else { 48 // Continuation passing 49 auto& c = *this->create_continuation<fib_continuation>(/* children_counter = */ 2, *x); 50 task_emulation::run_task(c.create_child_of_continuation<fib_computation>(n - 1, &c.x)); 51 52 // Recycling 53 this->recycle_as_child_of_continuation(c); 54 n = n - 2; 55 x = &c.y; 56 57 // Bypass is not supported by task_emulation and next_task executed directly. 58 // Howewer, the old-TBB bypass behavior can be achieved with 59 // `return task_group::defer()` (check Migration Guide). 60 // Consider submit another task if recursion call is not acceptable 61 // i.e. instead of Recycling + Direct Body call 62 // submit task_emulation::run_task(c.create_child_of_continuation<fib_computation>(n - 2, &c.y)); 63 this->operator()(); 64 } 65 } 66 67 int n; 68 int* x; 69 }; 70 71 int fibonacci(int n) { 72 int sum{}; 73 tbb::task_group tg; 74 tg.run_and_wait( 75 task_emulation::create_root_task<fib_computation>(/* for root task = */ tg, n, &sum)); 76 return sum; 77 } 78 79 template <typename F> 80 std::pair</* result */ unsigned long, /* time */ unsigned long> measure(F&& f, 81 int number, 82 unsigned long ntrial) { 83 std::vector<unsigned long> times; 84 85 unsigned long result; 86 for (unsigned long i = 0; i < ntrial; ++i) { 87 auto t1 = std::chrono::steady_clock::now(); 88 result = f(number); 89 auto t2 = std::chrono::steady_clock::now(); 90 91 auto time = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count(); 92 times.push_back(time); 93 } 94 95 return std::make_pair( 96 result, 97 static_cast<unsigned long>(std::accumulate(times.begin(), times.end(), 0) / times.size())); 98 } 99 100 int main(int argc, char* argv[]) { 101 int numbers = argc > 1 ? strtol(argv[1], nullptr, 0) : 50; 102 cutoff = argc > 2 ? strtol(argv[2], nullptr, 0) : 16; 103 unsigned long ntrial = argc > 3 ? (unsigned long)strtoul(argv[3], nullptr, 0) : 20; 104 105 auto res = measure(fibonacci, numbers, ntrial); 106 std::cout << "Fibonacci N = " << res.first << " Avg time = " << res.second << " ms" 107 << std::endl; 108 } 109