Chris,

> I hit an issue this week while working with parallel::for_each over
> partitioned_vectors. parallel::for_each works well over std::vector types
> but, when attempting to perform a parallel::for_each over
> partitioned_vectors, gcc complains that the lambda function I'm supplying
> isn't a structure with an overloaded operator().

That's one of the unfortunate limitations of C++ we run into. The function 
object used with partitioned_vector has to be serializable as it is sent to all 
the partitions, possibly on another locality. However, C++ does not allow for 
us to serialize lambdas, even more so if the lambda has a non-empty capture 
clause.

The work around for this (and I really hope the committee will find a way to 
fix this while adding reflection capabilities to the language) is to use 
explicit function objects:

struct pass_through
{
    int& value;

    int operator()(int) const
    {
        return value;
    }

    template <typename Archive>
    void serialize(Archive& ar, unsigned)
    {
        ar & value;
    }
};

std::vector<int> vec(100);
int value = 100;
hpx::parallel::transform(
  hpx::parallel::execution::par,
  std::begin(vec),
  std::end(vec),
  std::begin(vec),
  pass_through{value}
);

If the function object has no members (it is empty, i.e. the corresponding 
lambda would have no captures) you can omit the serialize() function as we 
generate that for you.

HTH
Regards Hartmut
---------------
http://boost-spirit.com
http://stellar.cct.lsu.edu


> The code below works well:
> std::vector<int> vec(100);
> int value = 100;
> hpx::parallel::transform(
>   hpx::parallel::execution::par,
>   std::begin(vec),
>   std::end(vec),
>   std::begin(vec),
>   [&value](int val) {
>     return value;
>   });
> 
> hpx::parallel::for_each(
>   hpx::parallel::execution::par,
>   std::begin(vec),
>   std::end(vec),
>   [](int val) {
>     hpx::cout << val << hpx::endl;
>   });
> The code below fails to compile:
> 
> hpx::partitioned_vector<int> vec(100,
> hpx::container_layout(hpx::find_all_localities()));
> vec.register_as("intvectors");
> 
> int value = 100;
> 
> hpx::parallel::transform(
>   hpx::parallel::execution::par,
>   std::begin(vec),
>   std::end(vec),
>   std::begin(vec),
>   [&value](int val) {
>     return value;
>   });
> 
> hpx::parallel::for_each(
>   hpx::parallel::execution::par,
>   std::begin(vec),
>   std::end(vec),
>   [](int val) {
>     hpx::cout << val << hpx::endl;
>   });
> gcc throws a compiler error on the line where the for_each is supplied the
> lambda. The error implies that parallel::for_each requires/expects a data
> type similar to std::plus<int>() with an overloaded operator().
> Did I make an error, find a new error, or is this an edge case?
> V/r,
> Chris


_______________________________________________
hpx-users mailing list
[email protected]
https://mail.cct.lsu.edu/mailman/listinfo/hpx-users

Reply via email to