I am having a problem with a little test program I am developing to help with integration into our main program. I want to be able to "extract" a sub-matrix from a larger 2D matrix, and do it as efficiently as possible w.r.t. the amount of MPI overhead.
Please see the attached code for a simple example. I am creating a 4x4 matrix, and I want to extract a 2x2 matrix from it, on another processor. In this case, I want to extract the 2x2 that is in the top-left corner. This is giving me bogus results, and may be because i am declaring the type wrong. If you can get a version working, I'd much apprecaite it.
Thanks in advance!
#include <stdlib.h>
#include <mpi.h>
#include <stdio.h>
int main(int argc, char **argv){
double u[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
double *recv_buff;
int size, rank;
MPI_Datatype matrix;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Type_vector(4,2,4, MPI_DOUBLE, &matrix);
if(rank ==0){
for (i = 0; i < 4; i++){
for (j = 0; j<4; j++){
printf("u[%i][%j]=%4.3f\n", u[i][j]);
}
}
MPI_Send(u[0][0],1,matrix, 1,0, MPI_COMM_WORD);
}
if (rank == 1){
recv_buff = malloc(sizeof(double *) * 2);
for (i = 0; i<2; i++)
recv_buff[i] = malloc(sizeof(double) * 2);
MPI_Recv(recv_buff[0][0], 1, matrix, 0,0,MPI_COMM_WORLD, &status);
for (i = 0; i < 2; i++){
for (j = 0; j<2; j++){
printf("recv_buff[%i][%j]=%4.3f\n", recv_buff[i][j]);
}
}
}
}
|