]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/performance/fiber/skynet_join.cpp
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)
7 // based on https://github.com/atemerev/skynet from Alexander Temerev
21 #include <boost/fiber/all.hpp>
22 #include <boost/predef.h>
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
;
31 void skynet( allocator_type
& salloc
, channel_type
& c
, std::size_t num
, std::size_t size
, std::size_t div
) {
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
,
42 std::ref( salloc
), std::ref( rc
), sub_num
, size
/ div
, div
);
44 for ( auto & f
: fibers
) {
47 std::uint64_t sum
{ 0 };
48 for ( std::size_t i
= 0; i
< div
; ++i
) {
49 sum
+= rc
.value_pop();
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() };
65 allocator_type salloc
{ allocator_type::traits_type::page_size() };
67 std::uint64_t result
{ 0 };
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");
75 auto duration
= clock_type::now() - start
;
76 std::cout
<< "duration: " << duration
.count() / 1000000 << " ms" << std::endl
;
78 } catch ( std::exception
const& e
) {
79 std::cerr
<< "exception: " << e
.what() << std::endl
;
81 std::cerr
<< "unhandled exception" << std::endl
;