/* ***************************************************************** C-DAC Tech Workshop : hyPACK-2013 October 15-18, 2013 Example 6.2 : (gauss-elmn-mpi-code-clang.c) Objective : To solve the system of Linear Equations using Gaussian Elimination without pivoting on 'p' processors. Input : Read files (mdatgaus.inp) for Matrix A and (vdatgaus.inp) for Vector b Output : The solution of matrix system of linear equations Ax=b on processor 0. Description : Input matrix is stored in n by n format. Columnwise cyclic distribution of input matrix is used for partitioning of the matrix. Necessary conditions : Number of Processes should be less than or equal to 8.Matrix size for Matrix A and vector size for vector b should be equally striped, that is Matrix size and Vector Size should be divisible by Number of processes used. Created : August-2013 E-mail : hpcfte@cdac.in ******************************************************************** */ #include #include #include #include main(int argc, char** argv) { /* .......Variables Initialisation ......*/ MPI_Status status; int n_size, NoofRows_Bloc, NoofRows, NoofCols; int Numprocs, MyRank, Root = 0; int irow, jrow, icol, index, ColofPivot, neigh_proc; double **Matrix_A, *Input_A, *Input_B, *ARecv, *BRecv; double *Output, Pivot; double *X_buffer, *Y_buffer; double *Buffer_Pivot, *Buffer_bksub; double tmp; FILE *fp; /* ........MPI Initialisation .......*/ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &MyRank); MPI_Comm_size(MPI_COMM_WORLD, &Numprocs); /* .......Read the Input file ......*/ if(MyRank == 0) { if ((fp = fopen ("./matrix-data-gauss.inp", "r")) == NULL) { printf("Can't open input matrix file"); exit(-1); } fscanf(fp, "%d %d", &NoofRows,&NoofCols); n_size=NoofRows; /* ...Allocate memory and read data .....*/ Matrix_A = (double **) malloc(n_size*sizeof(double *)); for(irow = 0; irow < n_size; irow++){ Matrix_A[irow] = (double *) malloc(n_size * sizeof(double)); for(icol = 0; icol < n_size; icol++) fscanf(fp, "%lf", &Matrix_A[irow][icol]); } fclose(fp); if ((fp = fopen ("./vector-data-gauss.inp", "r")) == NULL){ printf("Can't open input vector file"); exit(-1); } fscanf(fp, "%d", &NoofRows); n_size=NoofRows; Input_B = (double *)malloc(n_size*sizeof(double)); for (irow = 0; irow= 0; irow--) { for (icol = NoofRows_Bloc-1;icol >= 0; icol--) { /* ... Pick up starting Index .....*/ index = (int) Buffer_bksub[icol]; Y_buffer[irow] -= Buffer_bksub[NoofRows_Bloc+icol] * ARecv[irow*n_size+index]; } } } for (irow = NoofRows_Bloc-1; irow >= 0; irow--) { index = MyRank*NoofRows_Bloc+irow; Buffer_bksub[irow] = (double) index; Buffer_bksub[NoofRows_Bloc+irow] = X_buffer[irow] = Y_buffer[irow]; for (jrow = irow-1; jrow >= 0; jrow--) Y_buffer[jrow] -= X_buffer[irow] * ARecv[jrow*n_size + index]; } /*.... Send to all lower processes...*/ for (neigh_proc = 0; neigh_proc < MyRank; neigh_proc++) MPI_Send(Buffer_bksub, 2*NoofRows_Bloc, MPI_DOUBLE, neigh_proc, MyRank, MPI_COMM_WORLD); /*.... Gather the result on the processor 0 ....*/ Output = (double *) malloc (n_size * sizeof(double)); MPI_Gather(X_buffer, NoofRows_Bloc, MPI_DOUBLE, Output, NoofRows_Bloc, MPI_DOUBLE, 0, MPI_COMM_WORLD); /* .......Output vector .....*/ if (MyRank == 0) { printf ("\n"); printf(" ------------------------------------------- \n"); printf("Results of Gaussian Elimination Method on processor %d: \n", MyRank); printf ("\n"); printf("Matrix Input_A \n"); printf ("\n"); for (irow = 0; irow < n_size; irow++) { for (icol = 0; icol < n_size; icol++) printf("%.3lf ", Matrix_A[irow][icol]); printf("\n"); } printf ("\n"); printf("Matrix Input_B \n"); printf("\n"); for (irow = 0; irow < n_size; irow++) { printf("%.3lf\n", Input_B[irow]); } printf ("\n"); printf("Solution vector \n"); printf ("\n"); for(irow = 0; irow < n_size; irow++) printf("%.3lf\n",Output[irow]); printf(" --------------------------------------------------- \n"); } MPI_Finalize(); }