示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | /******************************************************************************* KML_EigenSolver example codes for solving eigenvalues problem AX = BXD *******************************************************************************/ #include "kes.h" void MyMatVec4A(void *kesA, void *kesX, void *kesAX, int nRows, int nCols, int ld) { // user-defined mat-vec routine for matrix A ...... } void MyMatVec4B(void *kesB, void *kesX, void *kesBX, int nRows, int nCols, int ld) { // user-defined mat-vec routine for matrix B ...... } void MyMatVec4P(void *kesP, void *kesX, void *kesPX, int nRows, int nCols, int ld) { // user-defined mat-vec routine for preconditioner P ...... } int main (int argc, char **argv) { MPI_Init(&argc, &argv); int rank, size; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); /* set parameters */ int nRows = 1000, nCols = 100; MPI_Comm rowComm = MPI_COMM_WORLD; MPI_Comm colComm = MPI_COMM_SELF; /* create matrix, vector, and solver */ KesMatrix A, P, B; KesVector X, D; KesSolver solver; KesScalarType scalarType = KES_SCALAR_FP64; KesSolverAlgorithm solverAlgorithm = KES_LOBPCG; KesMatrixFormat matrixFormat = KES_SHELL; KesVectorFormat vectorFormat = KES_DENSE; KesMatrixCreate(&A, scalarType, matrixFormat); KesMatrixCreate(&B, scalarType, matrixFormat); KesMatrixCreate(&P, scalarType, matrixFormat); KesVectorCreate(&X, scalarType, vectorFormat); KesVectorCreate(&D, scalarType, vectorFormat); KesSolverCreate(&solver, scalarType, solverAlgorithm); KesMatrixSetDim(A, nRows * size, nRows * size); KesMatrixSetDim(B, nRows * size, nRows * size); KesVectorSetDim(X, nRows, nCols); KesVectorSetComm(X, KES_ROW, rowComm); KesVectorSetComm(X, KES_COL, colComm); KesVectorSetDim(D, nCols, 1); KesVectorSetComm(D, KES_ROW, rowComm); KesVectorSetComm(D, KES_COL, colComm); double *X_vals = new double[nRows * nCols]; double *D_vals = new double[nCols]; KesVectorDenseSetValues(X, X_vals, nRows); KesVectorDenseSetValues(D, D_vals, nCols); double *A_vals = new double[nRows * nRows]; double *B_vals = new double[nRows * nRows]; double *P_vals = new double[nRows]; KesMatrixSetShellMatVec(A, A_vals, MyMatVec4A); KesMatrixSetShellMatVec(B, B_vals, MyMatVec4B); KesMatrixSetShellMatVec(P, P_vals, MyMatVec4P); /* set solver-related parameters */ int maxIter = 100000; int maxOrthoIter = 2; double threshold = 1e-6; KesProblemType problemType = KES_STANDARD; KesDenseEigenSolverType denseEigenSolverType = KES_SCALAPACK; KesOrthoProjectorType orthoProjectorType = KES_GRAM_SCHMIDT; KesOrthogonalizerType orthogonalizerType = KES_CHOLESKY_QR; KesConvergeDetectorType convergeDetectorType = KES_RESIDUALS; KesSolverSetThreshold(solver, threshold); KesSolverSetMaxIter(solver, maxIter); KesSolverSetMaxOrthoIter(solver, maxOrthoIter); KesSolverSetProblemType(solver, problemType); KesSetDenseEigenSolverType(solver, denseEigenSolverType); KesSetConvergeDetectorType(solver, convergeDetectorType); KesSetOrthogonalizerType(solver, orthogonalizerType); KesSetOrthoProjectorType(solver, orthoProjectorType); /* run the solver */ KesRunSolver(solver, A, B, P, X, D); /* get solver information */ int nIter; int nConv; KesGetSolverConv(solver, &nConv); KesGetSolverIter(solver, &nIter); if (rank == 0) { printf(" Number of iteration steps = %d\n", nIter); printf(" Number of converged eigenvalues = %d\n", nConv); } /* compute residuals */ KesVector R; KesVectorReplicate(X, &R, true, true); KesVector normR; KesVectorReplicate(D, &normR, true, true); KesEigResidual(A, B, X, D, R); KesVectorNorm(R, normR); /* clean up */ delete[] A_vals; delete[] B_vals; delete[] P_vals; delete[] X_vals; delete[] D_vals; /* clean up internal object */ KesMatrixDestroy(A); KesMatrixDestroy(B); KesMatrixDestroy(P); KesVectorDestroy(X); KesVectorDestroy(D); KesVectorDestroy(R); KesVectorDestroy(normR); KesSolverDestroy(solver); MPI_Finalize(); return 0; } |
父主题: KML_EIGENSOLVER库函数说明