Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / fiber / performance / fiber / skynet_stealing_join.cpp
1
2 //          Copyright Oliver Kowalke 2015.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6
7 // based on https://github.com/atemerev/skynet from Alexander Temerev 
8
9 #include <algorithm>
10 #include <cassert>
11 #include <chrono>
12 #include <condition_variable>
13 #include <cstddef>
14 #include <cstdint>
15 #include <cstdlib>
16 #include <queue>
17 #include <iostream>
18 #include <memory>
19 #include <mutex>
20 #include <numeric>
21 #include <random>
22 #include <sstream>
23 #include <vector>
24
25 #include <boost/fiber/all.hpp>
26
27 #include "barrier.hpp"
28 #include "bind/bind_processor.hpp"
29
30 using clock_type = std::chrono::steady_clock;
31 using duration_type = clock_type::duration;
32 using time_point_type = clock_type::time_point;
33 using channel_type = boost::fibers::buffered_channel< std::uint64_t >;
34 using allocator_type = boost::fibers::fixedsize_stack;
35 using lock_type = std::unique_lock< std::mutex >;
36
37 static bool done = false;
38 static std::mutex mtx{};
39 static boost::fibers::condition_variable_any cnd{};
40
41 // microbenchmark
42 void skynet( allocator_type & salloc, channel_type & c, std::size_t num, std::size_t size, std::size_t div) {
43     if ( 1 == size) {
44         c.push( num);
45     } else {
46         channel_type rc{ 16 };
47         std::vector< boost::fibers::fiber > fibers;
48         for ( std::size_t i = 0; i < div; ++i) {
49             auto sub_num = num + i * size / div;
50             fibers.push_back(
51                 boost::fibers::fiber{ boost::fibers::launch::dispatch,
52                                   std::allocator_arg, salloc,
53                                   skynet,
54                                   std::ref( salloc), std::ref( rc), sub_num, size / div, div });
55         }
56         std::uint64_t sum{ 0 };
57         for ( std::size_t i = 0; i < div; ++i) {
58             sum += rc.value_pop();
59         }
60         for ( auto & f: fibers) {
61             f.join();
62         }
63         c.push( sum);
64     }
65 }
66
67 void thread( unsigned int max_idx, unsigned int idx, barrier * b) {
68     bind_to_processor( idx);
69     boost::fibers::use_scheduling_algorithm< boost::fibers::algo::work_stealing >( max_idx, idx);
70     b->wait();
71     lock_type lk( mtx);
72     cnd.wait( lk, [](){ return done; });
73     BOOST_ASSERT( done);
74 }
75
76 int main() {
77     try {
78         unsigned int cpus = std::thread::hardware_concurrency();
79         barrier b( cpus);
80         unsigned int max_idx = cpus - 1;
81         boost::fibers::use_scheduling_algorithm< boost::fibers::algo::work_stealing >( max_idx, max_idx);
82         bind_to_processor( max_idx);
83         std::size_t size{ 1000000 };
84         std::size_t div{ 10 };
85         std::vector< std::thread > threads;
86         for ( unsigned int idx = 0; idx < max_idx; ++idx) {
87             threads.push_back( std::thread( thread, max_idx, idx, & b) );
88         };
89         allocator_type salloc{ allocator_type::traits_type::page_size() };
90         std::uint64_t result{ 0 };
91         duration_type duration{ duration_type::zero() };
92         channel_type rc{ 2 };
93         b.wait();
94         time_point_type start{ clock_type::now() };
95         skynet( salloc, rc, 0, size, div);
96         result = rc.value_pop();
97         duration = clock_type::now() - start;
98         std::cout << "Result: " << result << " in " << duration.count() / 1000000 << " ms" << std::endl;
99         lock_type lk( mtx);
100         done = true;
101         lk.unlock();
102         cnd.notify_all();
103         for ( std::thread & t : threads) {
104             t.join();
105         }
106         std::cout << "done." << std::endl;
107         return EXIT_SUCCESS;
108     } catch ( std::exception const& e) {
109         std::cerr << "exception: " << e.what() << std::endl;
110     } catch (...) {
111         std::cerr << "unhandled exception" << std::endl;
112     }
113         return EXIT_FAILURE;
114 }