Parallel processing of matrices by columns with MPI

1

I have a matrix (c) of 10x10 (M = 10) elements in which I divide the matrix by rows so that they are executed by 5 different processes (slaves = 5), so that each process corresponds to 2 rows of that matrix.

offset = 0;
filas = (M / esclavos);
MPI_Send(&c[offset][0], filas*M, MPI_DOUBLE, id_esclavo,0,MPI_COMM_WORLD);
offset= offset+filas;

Now I want to divide the matrix but by columns. Try changing the indexes of the matrix as follows but it does not work:

MPI_Send(&c[0][offset], filas*M, MPI_DOUBLE, id_esclavo,0,MPI_COMM_WORLD);

Do you know how to do it? Thanks

    
asked by Nico Rossello 04.01.2017 в 21:52
source

1 answer

1

The problem has to do with the way memory is organized in an array in C.

&c[offset][0] is a pointer to the first element in row offset . Then (&c[offset][0])+1 is going to be a pointer to the second element of row offset .

But &c[0][offset] is a pointer to the first element in the column offset and (&c[0][offset])+1 is not a pointer to the second element of the column offset , but is a pointer to the first element in the column offset+1 .

That is, the memory in the array is organized so that the elements of the same row are in consecutive positions in memory. Memory is organized by rows.

To be able to move the columns to MPI_Send you have to reorganize the array, in one in which the first index refers to the columns and the second to the rows. That is, elements in the same column are in consecutive memory positions.

int d[10][10];
for ( int i=0; i<10; ++i)
{
  for ( int j=0; j<10; ++j)
    d[j][i] = c[i][j];
}

And now you can invoke MPI_Send by columns.

MPI_Send(&d[offset][0], filas*M, MPI_DOUBLE, id_esclavo,0,MPI_COMM_WORLD);
    
answered by 05.01.2017 в 11:02