Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MPI How to broadcast c++ vector?

Tags:

c++

vector

mpi

I want to broadcast C++ vector using MPI.

I am not allowed to use boost.mpi

Right now I use most upvoted answer from Vector Usage in MPI(C++) but it doesn't works..

Ok, here is the code:

// declaration of variables (ParsedData object will contain these variables)
int generators_count, intervals_count;
std::vector<float> mean_arr, variance_arr, interval_begins_arr, interval_ends_arr;
std::vector<int> amount_of_numbers_to_generate_arr;

// load data into parsed_data object
if (process_id == 0) {
  ParsedData parsed_data = load_input_data(filename);
  intervals_count = parsed_data.intervals_count;
  generators_count = parsed_data.generators_count;
  mean_arr = parsed_data.mean_arr;
  variance_arr = parsed_data.variance_arr;
  interval_begins_arr = parsed_data.interval_begins_arr;
  interval_ends_arr = parsed_data.interval_ends_arr;
  amount_of_numbers_to_generate_arr = parsed_data.amount_of_numbers_to_generate_arr;
}

// send size of vectors to all processes - that MPI code works
MPI_Bcast(&intervals_count, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&generators_count, 1, MPI_INT, 0, MPI_COMM_WORLD);

// reserve memory for vectors
if (process_id != 0) {
  mean_arr.reserve(generators_count);
  variance_arr.reserve(generators_count);
  amount_of_numbers_to_generate_arr.reserve(generators_count);

  interval_begins_arr.reserve(intervals_count);
  interval_ends_arr.reserve(intervals_count);
}

// stolen from https://stackoverflow.com/questions/2546298/vector-usage-in-mpic
// broadcast each vector, following code compiles but not works properly
MPI_Bcast(&mean_arr[0], mean_arr.size(), MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Bcast(&variance_arr[0], variance_arr.size(), MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Bcast(&interval_begins_arr[0], interval_begins_arr.size(), MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Bcast(&interval_ends_arr[0], interval_ends_arr.size(), MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Bcast(&amount_of_numbers_to_generate_arr[0], amount_of_numbers_to_generate_arr.size(), MPI_FLOAT, 0, MPI_COMM_WORLD);

When I just simply puts results on different process, I got wrong answer:

  cout << "process ID " << process_id << endl << "amount_of_numbers_to_generate_arr[0] " << amount_of_numbers_to_generate_arr[0] << endl;

Result:

using process 0
process ID 0
amount_of_numbers_to_generate_arr[0] 100000000
using process 1
process ID 1
amount_of_numbers_to_generate_arr[0] -423989576
using process 2
process ID 2
amount_of_numbers_to_generate_arr[0] 1741864632

And it should be:

using process 0
process ID 0
amount_of_numbers_to_generate_arr[0] 100000000
using process 1
process ID 1
amount_of_numbers_to_generate_arr[0] 100000000
using process 2
process ID 2
amount_of_numbers_to_generate_arr[0] 100000000

Anyway how to fix that?

Or maybe there is a another approach to do that?

like image 226
nothing-special-here Avatar asked Apr 17 '13 08:04

nothing-special-here


1 Answers

When you are "reserving memory for the vectors", you are not actually resizing the vectors. In the MPI_Bcast calls that follow, the sizes of the vectors are still 0, so no values are received.

To actually resize the vectors, call resize instead of reserve.

like image 161
Mankarse Avatar answered Oct 07 '22 00:10

Mankarse