]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/performance/fiber/skynet_shared_join.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / fiber / performance / fiber / skynet_shared_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 <cmath>
13 #include <condition_variable>
14 #include <cstddef>
15 #include <cstdint>
16 #include <cstdlib>
17 #include <deque>
18 #include <iostream>
19 #include <memory>
20 #include <mutex>
21 #include <numeric>
22 #include <vector>
23
24 #include <boost/fiber/all.hpp>
25 #include <boost/fiber/numa/pin_thread.hpp>
26 #include <boost/predef.h>
27
28 #include "barrier.hpp"
29
30 using allocator_type = boost::fibers::fixedsize_stack;
31 using channel_type = boost::fibers::buffered_channel< std::uint64_t >;
32 using clock_type = std::chrono::steady_clock;
33 using duration_type = clock_type::duration;
34 using lock_type = std::unique_lock< std::mutex >;
35 using time_point_type = clock_type::time_point;
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.emplace_back( boost::fibers::launch::dispatch,
51 std::allocator_arg, salloc,
52 skynet,
53 std::ref( salloc), std::ref( rc), sub_num, size / div, div);
54 }
55 for ( auto & f: fibers) {
56 f.join();
57 }
58 std::uint64_t sum{ 0 };
59 for ( std::size_t i = 0; i < div; ++i) {
60 sum += rc.value_pop();
61 }
62 c.push( sum);
63 }
64 }
65
66 void thread( unsigned int idx, barrier * b) {
67 boost::fibers::numa::pin_thread( idx);
68 boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >();
69 b->wait();
70 lock_type lk( mtx);
71 cnd.wait( lk, [](){ return done; });
72 BOOST_ASSERT( done);
73 }
74
75 int main() {
76 try {
77 boost::fibers::use_scheduling_algorithm< boost::fibers::algo::shared_work >();
78 unsigned int n = std::thread::hardware_concurrency();
79 barrier b( n);
80 boost::fibers::numa::pin_thread( n - 1);
81 std::size_t size{ 1000000 };
82 std::size_t div{ 10 };
83 std::vector< std::thread > threads;
84 for ( unsigned int i = 1; i < n; ++i) {
85 threads.emplace_back( thread, i - 1, & b);
86 };
87 allocator_type salloc{ 2*allocator_type::traits_type::page_size() };
88 std::uint64_t result{ 0 };
89 channel_type rc{ 2 };
90 b.wait();
91 time_point_type start{ clock_type::now() };
92 skynet( salloc, rc, 0, size, div);
93 result = rc.value_pop();
94 if ( 499999500000 != result) {
95 throw std::runtime_error("invalid result");
96 }
97 auto duration = clock_type::now() - start;
98 lock_type lk( mtx);
99 done = true;
100 lk.unlock();
101 cnd.notify_all();
102 for ( std::thread & t : threads) {
103 t.join();
104 }
105 std::cout << "duration: " << duration.count() / 1000000 << " ms" << std::endl;
106 return EXIT_SUCCESS;
107 } catch ( std::exception const& e) {
108 std::cerr << "exception: " << e.what() << std::endl;
109 } catch (...) {
110 std::cerr << "unhandled exception" << std::endl;
111 }
112 return EXIT_FAILURE;
113 }