/* ************************************************************************** C-DAC Tech Workshop : hyPACK-2013 October 15-18, 2013 Example 6.1: : jacobi-mpi-code-clang.c Objective : Jacobi method to solve AX = b matrix system of linear equations. Input : Real Symmetric Positive definite Matrix and the real vector - Input_A and Vector_B Read files (mdatjac.inp) for Matrix A and (vdatjac.inp) for Vector b Description : Input matrix is stored in n by n format. Diagonal preconditioning matrix is used. Rowwise block striped partitioning matrix is used.Maximum iterations is given by MAX_ITERATIONS.Tolerance value is given by EPSILON. Output : The solution of Ax=b on process with Rank 0 and the number of iterations for convergence of the method. Necessary conditions : Number of Processes should be less than or equal to 8 Input Matrix Should be Square Matrix. Matrix size for Matrix A and vector size for vector b should be equally striped, that is Matrix size and Vector Size should be dividible by the number of processes used. Created : August-2013 E-mail : hpcfte@cdac.in ****************************************************************************** */ #include #include #include #include #define MAX_ITERATIONS 100 double Distance(double *X_Old, double *X_New, int n_size); 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, Iteration, GlobalRowNo; double **Matrix_A, *Input_A, *Input_B, *ARecv, *BRecv; double *X_New, *X_Old, *Bloc_X, 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 == Root) { if ((fp = fopen ("./matrix-data-jacobi.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-jacobi.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= 1.0E-24)); /* .......Output vector .....*/ if (MyRank == 0) { printf ("\n"); printf(" ------------------------------------------- \n"); printf("Results of Jacobi 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("Number of iterations = %d\n",Iteration); printf ("\n"); for(irow = 0; irow < n_size; irow++) printf("%.12lf\n",X_New[irow]); printf(" --------------------------------------------------- \n"); } MPI_Finalize(); } double Distance(double *X_Old, double *X_New, int n_size) { int index; double Sum; Sum = 0.0; for(index=0; index