]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/example/parallel_accumulate.cpp
1 // Copyright (C) 2014 Vicente Botet
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/config.hpp>
8 #define BOOST_THREAD_VERSION 4
9 #define BOOST_THREAD_PROVIDES_EXECUTORS
10 #define BOOST_THREAD_USES_LOG_THREAD_ID
11 #define BOOST_THREAD_QUEUE_DEPRECATE_OLD
12 #if ! defined BOOST_NO_CXX11_DECLTYPE
13 #define BOOST_RESULT_OF_USE_DECLTYPE
16 #include <boost/thread/executors/basic_thread_pool.hpp>
17 #include <boost/thread/future.hpp>
25 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
27 template<typename Iterator
,typename T
>
28 struct accumulate_block
30 //typedef T result_type;
31 T
operator()(Iterator first
,Iterator last
)
33 return std::accumulate(first
,last
,T());
37 template<typename Iterator
,typename T
>
38 T
parallel_accumulate(Iterator first
,Iterator last
,T init
)
40 unsigned long const length
=static_cast<unsigned long>(std::distance(first
,last
));
45 unsigned long const block_size
=25;
46 unsigned long const num_blocks
=(length
+block_size
-1)/block_size
;
48 boost::csbl::vector
<boost::future
<T
> > futures(num_blocks
-1);
49 boost::basic_thread_pool pool
;
51 Iterator block_start
=first
;
52 for(unsigned long i
=0;i
<(num_blocks
-1);++i
)
54 Iterator block_end
=block_start
;
55 std::advance(block_end
,block_size
);
56 futures
[i
]=boost::async(pool
, accumulate_block
<Iterator
,T
>(), block_start
, block_end
);
57 block_start
=block_end
;
59 T last_result
=accumulate_block
<Iterator
,T
>()(block_start
,last
);
61 for(unsigned long i
=0;i
<(num_blocks
-1);++i
)
63 result
+=futures
[i
].get();
65 result
+= last_result
;
77 for (int i
=0; i
<s
;++i
)
79 int r
= parallel_accumulate(vec
.begin(), vec
.end(),0);
80 std::cout
<< r
<< std::endl
;
83 catch (std::exception
& ex
)
85 std::cout
<< "ERROR= " << ex
.what() << "" << std::endl
;
90 std::cout
<< " ERROR= exception thrown" << std::endl
;
97 ///#warning "This compiler doesn't supports variadics"