/* ************************************************************************ C-DAC Tech Workshop : hyPACK-2013 October 15-18, 2013 Example 6.3: : conjugate-gradient-mpi-code-clang.c Objective : Conjugate Gradient 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 (mdatcg.inp) for Matrix A and (vdatcg.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 Header file used : cg_constants.h 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 EPSILON 1.0E-20 #define MAX_ITERATIONS 10000 /******************************************************************************/ void GetPreconditionMatrix(double **Bloc_Precond_Matrix, int NoofRows_Bloc, int NoofCols) { /*... Preconditional Martix is identity matrix .......*/ int Bloc_MatrixSize; int irow, icol, index; double *Precond_Matrix; Bloc_MatrixSize = NoofRows_Bloc*NoofCols; Precond_Matrix = (double *) malloc(Bloc_MatrixSize * sizeof(double)); index = 0; for(irow=0; irow EPSILON && Iteration < MAX_ITERATIONS); MPI_Barrier(MPI_COMM_WORLD); EndTime = MPI_Wtime(); 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",Vector_X[irow]); printf(" --------------------------------------------------- \n"); } MPI_Finalize(); }