0

Could any help how to use MPI_Scatter to send the following matrix

float **u, **u_local;

if (rank == 0){
    u = (float**) malloc(N * size * sizeof(float*));
    for(i = 0; i < N * size; i++){
        u[i] = (float*) malloc(M * sizeof(float));
        memset(u[i], 0, M * sizeof(float));
    }
}

I wanna send u[N][M] matrix to all processes equally (u_local) N number of rows M number of columns

Thanks

1 Answers1

0

The easiest solution is to allocate memory in a linear way:

float **u, *u_stor;

if (rank == 0) {
    // Watch out for possible integer overflow while computing memory size
    u_stor = malloc(N * size * M * sizeof(float));
    for (i = 0; i < N * size; i++) {
        u[i] = &u_stor[i * M];
    }
    memset(u_stor, 0, N * size * M * sizeof(float));
}

Instead of allocating each row of u separately, this code allocates a chunk of memory as big as the whole matrix and then assigns to u[i] a pointer to the start of the i-th row in u_stor. Now rows are laid continuously in memory and a simple scatter can be used:

float **u_local, *u_local_stor;
u_local_stor = malloc(N * M * sizeof(float));
for (i = 0; i < N; i++)
    u_local[i] = &u_local_stor[i * M];

MPI_Scatter(u[0], N * M, MPI_FLOAT,
            u_local[0], N * M, MPI_FLOAT,
            0, MPI_COMM_WORLD);
Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186