]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/phoenix/example/parallel_for.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2007 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
8 #include <boost/phoenix.hpp>
12 typedef void result_type
;
14 template <typename Init
, typename Cond
, typename Step
, typename Do
, typename Context
>
26 boost::phoenix::eval(init
, ctx
);
27 boost::phoenix::eval(cond
, ctx
);
28 boost::phoenix::eval(step
, ctx
)
31 boost::phoenix::eval(do_
, ctx
);
37 ////////////////////////////////////////////////////////////////////////////////
38 // Define new custom expression
39 BOOST_PHOENIX_DEFINE_EXPRESSION(
41 , (boost::phoenix::meta_grammar
) // Cond
42 (boost::phoenix::meta_grammar
) // Init
43 (boost::phoenix::meta_grammar
) // Step
44 (boost::phoenix::meta_grammar
) // Do
47 namespace boost
{ namespace phoenix
50 struct default_actions::when
< ::rule::omp_for
>
51 : boost::phoenix::call
< ::omp_for_eval
>
55 template <typename Init
, typename Cond
, typename Step
>
58 omp_for_gen(Init
const& init
, Cond
const& cond
, Step
const& step
)
59 : init(init
), cond(cond
), step(step
) {}
61 template <typename Do
>
62 typename
result_of::make_omp_for
<Init
, Cond
, Step
, Do
>::type
const
63 operator[](Do
const& do_
) const
65 return make_omp_for(init
, cond
, step
, do_
);
73 template <typename Init
, typename Cond
, typename Step
>
75 omp_for_gen
<Init
, Cond
, Step
> const
76 omp_for(Init
const& init
, Cond
const& cond
, Step
const& step
)
78 return omp_for_gen
<Init
, Cond
, Step
>(init
, cond
, step
);
80 ////////////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////////////
84 // Define new evaluation scheme
86 struct parallel_actions
88 template <typename Rule
>
90 : boost::phoenix::default_actions::when
<Rule
>
95 struct parallel_actions::when
<boost::phoenix::rule::for_
>
96 : boost::phoenix::call
<omp_for_eval
>
99 // Doing the same as actor<Expr>::operator
100 template <typename Expr
, typename A0
, typename A1
, typename A2
>
101 typename
boost::phoenix::result_of::eval
<
103 , typename
boost::phoenix::result_of::make_context
<
104 typename
boost::phoenix::result_of::make_env
<
113 parallel_eval(Expr
& expr
, A0
& a0
, A1
& a1
, A2
& a2
)
115 Expr
const * this_
= boost::addressof(expr
);
117 boost::phoenix::eval(
119 , boost::phoenix::make_context(
120 boost::phoenix::make_env(this_
, a0
, a1
, a2
)
126 // changing evaluation mechanism on the fly
127 BOOST_PHOENIX_DEFINE_EXPRESSION(
129 , (boost::phoenix::meta_grammar
)
132 namespace boost
{ namespace phoenix
135 struct default_actions::when
< ::rule::parallel
>
139 , functional::make_context(
143 , unused()//mpl::void_()
149 template <typename Expr
>
150 typename
result_of::make_parallel
<Expr
>::type
151 parallel(Expr
const & expr
)
153 return make_parallel(expr
);
155 ////////////////////////////////////////////////////////////////////////////////
163 using boost::phoenix::arg_names::_1
;
164 using boost::phoenix::arg_names::_2
;
165 using boost::phoenix::arg_names::_3
;
166 using boost::phoenix::local_names::_a
;
167 using boost::phoenix::local_names::_b
;
168 using boost::phoenix::local_names::_c
;
169 using boost::phoenix::let
;
170 using boost::phoenix::bind
;
171 using boost::phoenix::lambda
;
172 using boost::phoenix::nothing
;
177 std::vector
<int> a(NUM
, 1);
178 std::vector
<int> b(NUM
, 2);
179 std::vector
<int> c(NUM
, 0);
182 let(_a
= begin(_1
), _b
= begin(_2
), _c
= begin(_3
))
184 for_(nothing
, _a
!= end(_1
), (++_a
, ++_b
, ++_c
))
189 , std::cout
<< accumulate(_3
, 0) << "\n"
194 std::vector
<int> a(NUM
, 1);
195 std::vector
<int> b(NUM
, 2);
196 std::vector
<int> c(NUM
, 0);
199 let(_a
= begin(_1
), _b
= begin(_2
), _c
= begin(_3
))
201 omp_for(nothing
, _a
!= end(_1
), (++_a
, ++_b
, ++_c
))
205 , std::cout
<< accumulate(_3
, 0) << "\n"
211 std::vector
<int> a(NUM
, 1);
212 std::vector
<int> b(NUM
, 2);
213 std::vector
<int> c(NUM
, 0);
216 let(_a
= begin(_1
), _b
= begin(_2
), _c
= begin(_3
))
218 for_(nothing
, _a
!= end(_1
), (++_a
, ++_b
, ++_c
))
222 , std::cout
<< accumulate(_3
, 0) << "\n"
228 std::vector
<int> a(NUM
, 1);
229 std::vector
<int> b(NUM
, 2);
230 std::vector
<int> c(NUM
, 0);
233 let(_a
= begin(_1
), _b
= begin(_2
), _c
= begin(_3
))
236 for_(nothing
, _a
!= end(_1
), (++_a
, ++_b
, ++_c
))
242 , std::cout
<< accumulate(_3
, 0) << "\n"