0

I am trying to find a proper way to collect values returned from slaves to the master in parallel programming. I had asked similar question earlier on how to divide work to calculate pixel of mandelbrot. I got the answer to how to send the work but am still struggling on how to gather the data and plot it as pixel.

node 0: (master)

node 1:(slave)

v[0]  = {2,3,4,5,67,86,56,5} // core 0 holds value of 8 threads of this core
v[1]  = {12,21,4,3,54,65,6,5,4} // core 1 holds value of 9 threads of this core
v[2]  = {1,3,4,54,6,5,65,7,4}  //core 2 holds value of 9 threads of this core

node 2:(slave)

v[0]  = {2,3,4,5,67,86,56,5} // core 0
v[1]  = {12,21,4,3,54,65,6,5,4} // core 1 
v[2]  = {1,3,4,54,6,5,65,7,4}  //core 2

node 3:(slave)

v[0]  = {2,3,4,5,67,86,56,5} // core 0
v[1]  = {12,21,4,3,54,65,6,5,4} // core 1 
v[2]  = {1,3,4,54,6,5,65,7,4}  //core 2

so when master want those value should the slave append the vector and send or is there any other better way to pass values to the master?

Community
  • 1
  • 1
solti
  • 4,339
  • 3
  • 31
  • 51
  • Similar question:- http://stackoverflow.com/questions/11284441/gathering-data-from-different-node-using-mpi-and-c – solti Apr 17 '13 at 21:40
  • but i am not using MPI – solti Apr 17 '13 at 22:24
  • I think i got my answer from this( http://www.omarflorez.info/index.php?id=parallel-mandelbrot-set ) website,although its MPI I can get a hint on how to do it .. – solti Apr 17 '13 at 22:59

1 Answers1

1

If you're using the C++11 thread library (or Boost.Thread) then what you probably want is a std::future. They can be obtained in one of three ways:

  1. By passing a std::promise to a thread and having it set the value.
  2. By using a std::packaged_task.
  3. By calling std::async.

Here's an example using std::async:

some_return_type my_work_function( some_input_type const & );

some_input_type inputs_to_slave_threads[] = { /* ... */ };

std::future< some_return_type >
  // launch the slaves, letting the OS decide whether to spawn new threads
  slave_0_future = std::async( my_work_function, std::ref( inputs_to_slave_threads[0] ) ),
  slave_1_future = std::async( my_work_function, std::ref( inputs_to_slave_threads[1] ) ),
  // ...
  slave_N_future = std::async( my_work_function, std::ref( inputs_to_slave_threads[N] ) );

some_return_type
  // block until results are ready
  result_of_slave_0 = slave_0_future.get(),
  result_of_slave_1 = slave_1_future.get(),
  // ...
  result_of_slave_N = slave_N_future.get();

process_results_of_slaves( result_of_slave_0, ..., result_of_slave_N );
Andrew Durward
  • 3,771
  • 1
  • 19
  • 30