#include #include "mpi.h" /* Exercise 6.10.3, see Chap 6, p. 110 in PPMPI. by John Weatherwax Here we implement a dense matrix transpose from processor 0 to processor 1 and print. */ #define MAX_ORDER 100 typedef float LOCAL_MATRIX_T[MAX_ORDER][MAX_ORDER]; main( int argc, char* argv[] ){ int my_rank; int p; LOCAL_MATRIX_T local_A; int n; int i,j; /* declarations: */ void Send_Transpose(LOCAL_MATRIX_T local_A, int n,int my_rank); MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&p); MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); if( my_rank==0 ){ if( p != 2 ){ printf("Number of processors must be 2.\n" ); MPI_Finalize(); exit(1); } printf("Enter the size of the matrix (n x n):\n"); scanf("%d",&n); if( n > MAX_ORDER ){ printf("n is too large...exiting\n"); MPI_Finalize(); exit(1); } } MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD); if( my_rank==0 ){ /* generate matrix A */ for( i=0; i < n; i++ ){ for( j=0; j < n; j++ ){ local_A[i][j] = i+1; } } /* for debugging we print ... */ for( i=0; i < n; i++ ){ for( j=0; j < n; j++ ){ printf("%7.2f ",local_A[i][j]); } printf("\n"); } printf("\n"); } Send_Transpose(local_A,n,my_rank); if( my_rank==1 ){ /* print matrix A^T */ for( i=0; i < n; i++ ){ for( j=0; j < n; j++ ){ printf("%7.2f ",local_A[i][j]); } printf("\n"); } } MPI_Finalize(); } /**********************************************************************/ /**********************************************************************/ void Send_Transpose( LOCAL_MATRIX_T local_A /* in/out */, int n /* in */, int my_rank /* in */) { int i, j, tag; MPI_Datatype column_mpi_t; MPI_Status status; MPI_Type_vector(n,1,MAX_ORDER,MPI_FLOAT,&column_mpi_t); MPI_Type_commit(&column_mpi_t); if( my_rank==0 ){ /* I am sending...send each column */ for( j=0; j < n; j++ ){ /*tag=j;*/ tag=0; MPI_Send(&(local_A[0][j]),1,column_mpi_t,1,tag,MPI_COMM_WORLD); } }else{ /* I am recieving...assumed I am process==1 */ for( i=0; i < n; i++ ){ /*tag=i;*/ tag=0; MPI_Recv(&(local_A[i][0]),n,MPI_FLOAT,0,tag,MPI_COMM_WORLD,&status); } } } /**********************************************************************/