1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/*=============================================================================
Copyright (c) 2001-2007 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include <boost/phoenix.hpp>
struct omp_for_eval
{
typedef void result_type;
template <typename Init, typename Cond, typename Step, typename Do, typename Context>
result_type
operator()(
Init const& init
, Cond const& cond
, Step const& step
, Do const& do_
, Context & ctx
) const
{
#pragma omp parallel
for(
boost::phoenix::eval(init, ctx);
boost::phoenix::eval(cond, ctx);
boost::phoenix::eval(step, ctx)
)
{
boost::phoenix::eval(do_, ctx);
}
}
};
////////////////////////////////////////////////////////////////////////////////
// Define new custom expression
BOOST_PHOENIX_DEFINE_EXPRESSION(
(omp_for)
, (boost::phoenix::meta_grammar) // Cond
(boost::phoenix::meta_grammar) // Init
(boost::phoenix::meta_grammar) // Step
(boost::phoenix::meta_grammar) // Do
)
namespace boost { namespace phoenix
{
template <>
struct default_actions::when< ::rule::omp_for>
: boost::phoenix::call< ::omp_for_eval>
{};
}}
template <typename Init, typename Cond, typename Step>
struct omp_for_gen
{
omp_for_gen(Init const& init, Cond const& cond, Step const& step)
: init(init), cond(cond), step(step) {}
template <typename Do>
typename result_of::make_omp_for<Init, Cond, Step, Do>::type const
operator[](Do const& do_) const
{
return make_omp_for(init, cond, step, do_);
}
Init init;
Cond cond;
Step step;
};
template <typename Init, typename Cond, typename Step>
inline
omp_for_gen<Init, Cond, Step> const
omp_for(Init const& init, Cond const& cond, Step const& step)
{
return omp_for_gen<Init, Cond, Step>(init, cond, step);
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Define new evaluation scheme
struct parallel_actions
{
template <typename Rule>
struct when
: boost::phoenix::default_actions::when<Rule>
{};
};
template <>
struct parallel_actions::when<boost::phoenix::rule::for_>
: boost::phoenix::call<omp_for_eval>
{};
// Doing the same as actor<Expr>::operator
template <typename Expr, typename A0, typename A1, typename A2>
typename boost::phoenix::result_of::eval<
Expr const &
, typename boost::phoenix::result_of::make_context<
typename boost::phoenix::result_of::make_env<
Expr const *
, A0 &
, A1 &
, A2 &
>::type
, parallel_actions
>::type
>::type
parallel_eval(Expr & expr, A0 & a0, A1 & a1, A2 & a2)
{
Expr const * this_ = boost::addressof(expr);
return
boost::phoenix::eval(
expr
, boost::phoenix::make_context(
boost::phoenix::make_env(this_, a0, a1, a2)
, parallel_actions()
)
);
}
// changing evaluation mechanism on the fly
BOOST_PHOENIX_DEFINE_EXPRESSION(
(parallel)
, (boost::phoenix::meta_grammar)
)
namespace boost { namespace phoenix
{
template <>
struct default_actions::when< ::rule::parallel>
: proto::call<
evaluator(
proto::_child0
, functional::make_context(
_env
, parallel_actions()
)
, unused()//mpl::void_()
)
>
{};
}}
template <typename Expr>
typename result_of::make_parallel<Expr>::type
parallel(Expr const & expr)
{
return make_parallel(expr);
}
////////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <iostream>
int main()
{
using boost::phoenix::arg_names::_1;
using boost::phoenix::arg_names::_2;
using boost::phoenix::arg_names::_3;
using boost::phoenix::local_names::_a;
using boost::phoenix::local_names::_b;
using boost::phoenix::local_names::_c;
using boost::phoenix::let;
using boost::phoenix::bind;
using boost::phoenix::lambda;
using boost::phoenix::nothing;
const int NUM = 1;
{
std::vector<int> a(NUM, 1);
std::vector<int> b(NUM, 2);
std::vector<int> c(NUM, 0);
(
let(_a = begin(_1), _b = begin(_2), _c = begin(_3))
[
for_(nothing, _a != end(_1), (++_a, ++_b, ++_c))
[
*_c = *_a + *_b
]
]
, std::cout << accumulate(_3, 0) << "\n"
)(a, b, c);
}
{
std::vector<int> a(NUM, 1);
std::vector<int> b(NUM, 2);
std::vector<int> c(NUM, 0);
(
let(_a = begin(_1), _b = begin(_2), _c = begin(_3))
[
omp_for(nothing, _a != end(_1), (++_a, ++_b, ++_c))
[
*_c = *_a + *_b
]
, std::cout << accumulate(_3, 0) << "\n"
]
)(a, b, c);
}
{
std::vector<int> a(NUM, 1);
std::vector<int> b(NUM, 2);
std::vector<int> c(NUM, 0);
parallel_eval(
let(_a = begin(_1), _b = begin(_2), _c = begin(_3))
[
for_(nothing, _a != end(_1), (++_a, ++_b, ++_c))
[
*_c = *_a + *_b
]
, std::cout << accumulate(_3, 0) << "\n"
]
, a, b, c);
}
{
std::vector<int> a(NUM, 1);
std::vector<int> b(NUM, 2);
std::vector<int> c(NUM, 0);
(
let(_a = begin(_1), _b = begin(_2), _c = begin(_3))
[
parallel(
for_(nothing, _a != end(_1), (++_a, ++_b, ++_c))
[
*_c = *_a + *_b
]
)
]
, std::cout << accumulate(_3, 0) << "\n"
)(a, b, c);
}
}
|