]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/performance/fiber/skynet_join.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / fiber / performance / fiber / skynet_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 <cstddef>
14 #include <cstdint>
15 #include <cstdlib>
16 #include <iostream>
17 #include <memory>
18 #include <numeric>
19 #include <vector>
20
21 #include <boost/fiber/all.hpp>
22 #include <boost/predef.h>
23
24 using allocator_type = boost::fibers::fixedsize_stack;
25 using channel_type = boost::fibers::buffered_channel< std::uint64_t >;
26 using clock_type = std::chrono::steady_clock;
27 using duration_type = clock_type::duration;
28 using time_point_type = clock_type::time_point;
29
30 // microbenchmark
31 void skynet( allocator_type & salloc, channel_type & c, std::size_t num, std::size_t size, std::size_t div) {
32 if ( 1 == size) {
33 c.push( num);
34 } else {
35 channel_type rc{ 16 };
36 std::vector< boost::fibers::fiber > fibers;
37 for ( std::size_t i = 0; i < div; ++i) {
38 auto sub_num = num + i * size / div;
39 fibers.emplace_back( boost::fibers::launch::dispatch,
40 std::allocator_arg, salloc,
41 skynet,
42 std::ref( salloc), std::ref( rc), sub_num, size / div, div);
43 }
44 for ( auto & f: fibers) {
45 f.join();
46 }
47 std::uint64_t sum{ 0 };
48 for ( std::size_t i = 0; i < div; ++i) {
49 sum += rc.value_pop();
50 }
51 c.push( sum);
52 }
53 }
54
55 int main() {
56 try {
57 std::size_t size{ 1000000 };
58 std::size_t div{ 10 };
59 // Windows 10 and FreeBSD require a fiber stack of 8kb
60 // otherwise the stack gets exhausted
61 // stack requirements must be checked for other OS too
62 #if BOOST_OS_WINDOWS || BOOST_OS_BSD
63 allocator_type salloc{ 2*allocator_type::traits_type::page_size() };
64 #else
65 allocator_type salloc{ allocator_type::traits_type::page_size() };
66 #endif
67 std::uint64_t result{ 0 };
68 channel_type rc{ 2 };
69 time_point_type start{ clock_type::now() };
70 skynet( salloc, rc, 0, size, div);
71 result = rc.value_pop();
72 if ( 499999500000 != result) {
73 throw std::runtime_error("invalid result");
74 }
75 auto duration = clock_type::now() - start;
76 std::cout << "duration: " << duration.count() / 1000000 << " ms" << std::endl;
77 return EXIT_SUCCESS;
78 } catch ( std::exception const& e) {
79 std::cerr << "exception: " << e.what() << std::endl;
80 } catch (...) {
81 std::cerr << "unhandled exception" << std::endl;
82 }
83 return EXIT_FAILURE;
84 }