I want to send in the most easy way the anti-diagonal elements of a NXN matrix from the root Process to the other process. Sadly at the moment i can not test my codes because the compute nodes are down. Can someone check my simple code?
I am not sure if i send the anti-diag elements of A correctly. And will the anti-diagonal elements land in the receive buffer B?
#include "mpi.h"
#include <stdio.h>
int main(int argc, char** argv){
MPI_Init(&argc,&argv);
int size, rank;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
int N=4;
double A[N][N];
double B[N];
MPI_Datatype antidiag;
int* blockleng=(int*)malloc(N*sizeof(int));
int* displace=(int*)malloc(N*sizeof(int));
for(int i=0; i<N; ++i){
blockleng[i]=1;
displace[i] = (i+1)*(N-1);
}
MPI_Type_indexed(N,blockleng,displace, MPI_DOUBLE,antidiag);
MPI_Type_commit(&antidiag);
MPI_Status status;
if(rank==0){
A= {
{1.0,5.0,9.0,13.0},
{2.0,6.0,10.5,14.5},
{3.0,7.2,11.0,15.0},
{4.0,8.0,12.0,16.0}
};
MPI_Send(A,1,antidiag,1,100,MPI_COMM_WORLD);
}
if(rank==1){
MPI_Recv(B,1,antidiag,0,100,MPI_COMM_WORLD,status);
}
MPI_Type_free(&antidiag);
MPI_Finalize();
return 0;
}