Hi,
I've wrote this simple program:
Code:
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define N 4
#define ROOT 0
int main(int argc, char **argv) {
int myrank, nprocs;
int mat[N][N] = {1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16};
int rec[N];
int i,j;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Status info;
if(nprocs!=2) MPI_Abort(MPI_COMM_WORLD,1);
MPI_Datatype column;
MPI_Type_vector(N,1,N,MPI_INT,&column);
MPI_Type_commit(&column);
if(myrank==0){
srand (time (NULL));
j=rand()%N;
printf("\nSend column %d",j);
MPI_Send(&mat[0][j],1,column,1,99,MPI_COMM_WORLD);
}
if(myrank==1){
MPI_Recv(rec,N,MPI_INT,0,99,MPI_COMM_WORLD,&info);
printf("\nColumn: ");
for(j=0;j<N;j++)
printf("\n %d",rec[j]);
}
MPI_Type_free(&column);
MPI_Finalize();
return 0;
}
Processor 1 receives the j-th column sends by Processor 0 correctly.
But, if I write:
Code:
MPI_Recv(rec,1,column,0,99,MPI_COMM_WORLD,&info);
Processor 1 receives only the element mat[0][j].
Why?