#include #include "mpi.h" /* Exercise 6.10.1 pt a, see Chap 6, p. 109 in PPMPI. by John Weathewax Here we input/create a matrix distributed by block columns 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 local_n; /* declarations: */ void Gen_matrix(LOCAL_MATRIX_T local_A, int local_n, int n, int my_rank); void Print_matrix(char* title,LOCAL_MATRIX_T local_A,int local_n,int n,int my_rank,int p); MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&p); MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); if( my_rank==0 ){ 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"); exit(1); } } MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD); local_n = n/p; /* Fill the matrix/vector in each process: */ Gen_matrix(local_A,local_n,n,my_rank); /* Print the matrix/vector from each process: */ Print_matrix("The matrix A is: ",local_A,local_n,n,my_rank,p); MPI_Finalize(); } /**********************************************************************/ void Print_matrix( char* title /* in */, LOCAL_MATRIX_T local_A /* in */, int local_n /* in */, int n /* in */, int my_rank /* in */, int p /* in */) { MPI_Datatype block_rows_t; int i, j, bri, bci, pi, start_row, tag; float blk_row_recv[MAX_ORDER][MAX_ORDER]; float blk_rows[MAX_ORDER][MAX_ORDER]; MPI_Status status; MPI_Type_vector(local_n,local_n,MAX_ORDER,MPI_FLOAT,&block_rows_t); MPI_Type_commit(&block_rows_t); /* Send all the local matrices by block rows: */ for( i = 0; i < p; i++ ){ start_row = local_n*i; /* tag = i + my_rank; */ tag = 0; MPI_Send(&(local_A[start_row][0]),1,block_rows_t,0,tag,MPI_COMM_WORLD); } if( my_rank == 0 ){ printf("%s\n", title); for( bri=0; bri < p; bri++ ){ /* loop over block rows... */ for( bci=0; bci < p; bci++ ){ /* loop over block cols... */ /* tag = bri + bci; */ tag = 0; MPI_Recv(blk_row_recv,1,block_rows_t,bci,tag,MPI_COMM_WORLD,&status); /* Concatonate into a full block rows array */ for( i=0; i < local_n; i++ ){ for( j=0; j < local_n; j++ ){ blk_rows[i][j+local_n*bci] = blk_row_recv[i][j]; } } } /* end block cols loop ... */ for( i=0; i < local_n; i++ ){ /* Print the elements to the screen... */ for( j=0; j < n; j++ ){ printf("%f ",blk_rows[i][j]); } printf("\n"); } } /* end block row loop */ } } /* Print_matrix */ /**********************************************************************/ /* Fill local matrix by block columns */ void Gen_matrix( LOCAL_MATRIX_T local_A /* out */, int local_n /* in */, int n /* in */, int my_rank /* in */) { int i, j; for (i = 0; i < n; i++) for (j = 0; j < local_n; j++) local_A[i][j] = my_rank; }