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
13 #include <condition_variable>
24 #include <boost/fiber/all.hpp>
25 #include <boost/fiber/numa/pin_thread.hpp>
26 #include <boost/predef.h>
28 #include "barrier.hpp"
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
;
37 static bool done
= false;
38 static std::mutex mtx
{};
39 static boost::fibers::condition_variable_any cnd
{};
42 void skynet( allocator_type
& salloc
, channel_type
& c
, std::size_t num
, std::size_t size
, std::size_t div
) {
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
,
53 std::ref( salloc
), std::ref( rc
), sub_num
, size
/ div
, div
);
55 for ( auto & f
: fibers
) {
58 std::uint64_t sum
{ 0 };
59 for ( std::size_t i
= 0; i
< div
; ++i
) {
60 sum
+= rc
.value_pop();
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
>();
71 cnd
.wait( lk
, [](){ return done
; });
77 boost::fibers::use_scheduling_algorithm
< boost::fibers::algo::shared_work
>();
78 unsigned int n
= std::thread::hardware_concurrency();
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
);
87 allocator_type salloc
{ 2*allocator_type::traits_type::page_size() };
88 std::uint64_t result
{ 0 };
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");
97 auto duration
= clock_type::now() - start
;
102 for ( std::thread
& t
: threads
) {
105 std::cout
<< "duration: " << duration
.count() / 1000000 << " ms" << std::endl
;
107 } catch ( std::exception
const& e
) {
108 std::cerr
<< "exception: " << e
.what() << std::endl
;
110 std::cerr
<< "unhandled exception" << std::endl
;