[hpx-users] dataflow sequential processing

2017-05-12 Thread ct clmsn
I'm doing some experiments with hpx::dataflow and would like to recursively
"chain" a dataflow for execution over a distributed vector of size_t
elements.

Sample code is provided below (apologies if it's not a perfect compile):

static void run(
  std::string nom,
  size_t v) {
  hpx::cout << v << hpx::endl;
}

HPX_PLAIN_ACTION(run, run_action);

static hpx::future run_dataflow(
  std::string nom,
  hpx::partitioned_vector &arr,
  std::vector locs,
  std::vector::iterator curr,
  std::vector::iterator end) {

hpx::future res = (curr != end) ? run_dataflow(nom, arr, locs,
std::next(curr), end) : hpx::make_ready_future();

return hpx::dataflow(
  hpx::util::unwrapped([nom, &arr, localities, &curr] () ->
hpx::future {
size_t v = *c;
return hpx::async(localities[arr.get_partition(v)], nom, v);
  }),
  std::move(res) );

}

int main(int argc, char **argv) {

... /* dvec_size is defined up here */ ...

std::vector locs = hpx::find_all_localities();

std::string nom = "arr_nom";
hpx::partitioned_vector arr(dvec_size, hpx::container_layout(locs));
arr.register_as(nom);

std::vector pos;
std::iota(
  std::begin(pos),
  std::end(pos),
  dvec_size);

hpx::future fd = run_dataflow(
  nom,
  arr,
  std::begin(pos),
  std::end(pos) );

fd.wait();

}

I've been getting a runtime errors for this code (segmentation fault) -
this is probably a semi-trivial bug or misunderstanding on my part - any
help/assistance would be appreciated. (I'm compiling against HPX with MPI)

V/r,

Chris
___
hpx-users mailing list
hpx-users@stellar.cct.lsu.edu
https://mail.cct.lsu.edu/mailman/listinfo/hpx-users


Re: [hpx-users] dataflow sequential processing

2017-05-12 Thread Hartmut Kaiser
Chris,

> I'm doing some experiments with hpx::dataflow and would like to
> recursively "chain" a dataflow for execution over a distributed vector of
> size_t elements.
> Sample code is provided below (apologies if it's not a perfect compile):
> static void run(
>   std::string nom,
>   size_t v) {
>   hpx::cout << v << hpx::endl;
> }
> 
> HPX_PLAIN_ACTION(run, run_action);
> 
> static hpx::future run_dataflow(
>   std::string nom,
>   hpx::partitioned_vector &arr,
>   std::vector locs,
>   std::vector::iterator curr,
>   std::vector::iterator end) {
> hpx::future res = (curr != end) ? run_dataflow(nom, arr, locs,
> std::next(curr), end) : hpx::make_ready_future();
> return hpx::dataflow(
>   hpx::util::unwrapped([nom, &arr, localities, &curr] () ->
> hpx::future {
> size_t v = *c;
> return hpx::async(localities[arr.get_partition(v)], nom,
> v);
>   }),
>   std::move(res) );
> 
> }
> 
> int main(int argc, char **argv) {
> ... /* dvec_size is defined up here */ ...
> 
> std::vector locs = hpx::find_all_localities();
> std::string nom = "arr_nom";
> hpx::partitioned_vector arr(dvec_size,
> hpx::container_layout(locs));
> arr.register_as(nom);
> 
> std::vector pos;
> std::iota(
>   std::begin(pos),
>   std::end(pos),
>   dvec_size);
> 
> hpx::future fd = run_dataflow(
>   nom,
>   arr,
>   std::begin(pos),
>   std::end(pos) );
> fd.wait();
> 
> }
> I've been getting a runtime errors for this code (segmentation fault) -
> this is probably a semi-trivial bug or misunderstanding on my part - any
> help/assistance would be appreciated. (I'm compiling against HPX with MPI)

The main reason for this kind of problems is that variables which are captured 
by reference into a lambda (or similar) go out of scope before the lambda 
itself is being executed:

void foo()
{
vector bar = {...};
return async([&bar]() { bar.push_back(42); });
}

foo();

In this example, the variable 'bar' is not valid anymore when the lambda (which 
is scheduled as a new thread) is eventually executed. Please note, that 
dataflow() runs the function passed to it on a new thread as well.

I'd assume that in your example the variable 'arr' goes out of scope before the 
lambda referencing it is run.

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


> 
> V/r,
> Chris

___
hpx-users mailing list
hpx-users@stellar.cct.lsu.edu
https://mail.cct.lsu.edu/mailman/listinfo/hpx-users