3

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!

  • 2
    Think of each individual MPI process as a separate program with its own variables, address space, *etc*. If you want a variable (such as an array) in a process's memory then yes, you certainly have to allocate space for it in that process's memory. But you shouldn't, in general, allocate space for the global array in every process's memory, only space for the local part of the array. With your, quite reasonable, program design you would read the array onto process 0 then send (*message passing*) slices to each of the processes which are going to do the work. – High Performance Mark Feb 14 '14 at 21:34

2 Answers2

2

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).

Wesley Bland
  • 8,816
  • 3
  • 44
  • 59
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++.

Magnar Myrtveit
  • 2,432
  • 3
  • 30
  • 51