问题
I am dealing with parallelizing Conways' Game of Life using MPI (in c++). I have to read a (very large) matrix from input, then scatter it in slices row-wise, and then process every slice in parallel. The idea I am following is to let only one process deal with the I/O stuff. In particular, process 0 read from file and saves the initial datas into a say RxC matrix, to be scattered among the process in (R/P)xC "slice matrices". Now, when I perform the routine MPI_Scatter, the compiler complaints because the "big matrix" is allocated only in the first process. To make things work I have to allocate the big matrix in all the process, even if those remains blank. Is this ordinary, or I am doing something wrong? Is there a way to avoid allocating a blank, useless matrix for every process? Thank you guys!
回答1:
You don't need to allocate the "big matrix" everywhere, but MPI_SCATTER
does require that you allocate some memory on all of the ranks.
If you are going to scatter your data like this:
Before scatter:
rank 0 - 1 2 3 4
After scatter:
rank 0 - 1
rank 1 - 2
rank 2 - 3
rank 3 - 4
You need to allocate space for one int
on each rank (as opposed to all 4).
回答2:
You don't have to allocate the big matrix everywhere, but the big matrix variable needs to be declared everywhere. Try this:
int* big_matrix;
if(process_id == 0) {
big_matrix = (int*) malloc(big_number * sizeof(int));
// fill the big matrix with values
}
int* part_of_matrix = (int*) malloc(small_number * sizeof(int));
MPI_Scatter(big_matrix, small_number, MPI_INT, part_of_matrix, small_number, MPI_INT, 0, MPI_COMM_WORLD);
At least this is a way to do it in C. You might have to initialize the big_matrix to 0 or something in C++.
来源:https://stackoverflow.com/questions/21789067/mpi-scatter-why-do-i-have-to-allocate-memory-in-all-the-processes