示例
C Interface:
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #include "kml_solver.h" int ierr; int n = 8; int nrhs = 1; // Create matrix A int ia[9] = {0, 2, 4, 6, 7, 8, 10, 12, 14}; int ja[14] = {0, 7, 1, 6, 2, 5, 3, 4, 2, 5, 1, 6, 0, 7}; double a[14] = {1.0, 2.0, -2.0, 3.0, 3.0, 4.0, -4.0, 5.0, 4.0, -6.0, 3.0, 7.0, 2.0, 8.0}; KmlSolverMatrixStore storeA; storeA.indexType = KMLSS_INDEX_INT32; storeA.valueType = KMLSS_VALUE_FP64; storeA.nRow = n; storeA.nCol = n; storeA.format = KMLSS_MATRIX_STORE_CSR; storeA.csr.rowOffset = ia; storeA.csr.colIndex = ja; storeA.csr.value = a; KmlSolverMatrixOption optA; optA.fieldMask = KMLSS_MATRIX_OPTION_TYPE; optA.type = KMLSS_MATRIX_GEN; KmlSolverMatrix *A; ierr = KmlSolverMatrixCreate(&A, &storeA, &optA); if (ierr != KMLSS_NO_ERROR) { printf("ERROR when create A: %d\n", ierr); return 1; } // Create vector b double b[8] = {3.0, 1.0, 7.0, -4.0, 5.0, -2.0, 10.0, 10.0}; KmlSolverMatrixStore storeB; storeB.indexType = KMLSS_INDEX_INT32; storeB.valueType = KMLSS_VALUE_FP64; storeB.nRow = n; storeB.nCol = nrhs; storeB.format = KMLSS_MATRIX_STORE_DENSE_COL_MAJOR; storeB.dense.value = b; storeB.dense.ld = n; KmlSolverMatrixOption optB; optB.fieldMask = KMLSS_MATRIX_OPTION_TYPE; optB.type = KMLSS_MATRIX_GEN; KmlSolverMatrix *B; ierr = KmlSolverMatrixCreate(&B, &storeB, &optB); if (ierr != KMLSS_NO_ERROR) { printf("ERROR when create b: %d\n", ierr); return 1; } // Create vector x double x[8] = {0}; KmlSolverMatrixStore storeX; storeX.indexType = KMLSS_INDEX_INT32; storeX.valueType = KMLSS_VALUE_FP64; storeX.nRow = n; storeX.nCol = nrhs; storeX.format = KMLSS_MATRIX_STORE_DENSE_COL_MAJOR; storeX.dense.value = x; storeX.dense.ld = n; KmlSolverMatrixOption optX; optX.fieldMask = KMLSS_MATRIX_OPTION_TYPE; optX.type = KMLSS_MATRIX_GEN; KmlSolverMatrix *X; ierr = KmlSolverMatrixCreate(&X, &storeX, &optX); if (ierr != KMLSS_NO_ERROR) { printf("ERROR when create x: %d\n", ierr); return 1; } // Init solver KmlDssInitOption opt; opt.fieldMask = KMLDSS_INIT_OPTION_BWR_MODE | KMLDSS_INIT_OPTION_NTHREADS; opt.bwrMode = KMLDSS_BWR_OFF; opt.nThreads = 32; KmlDssSolver *solver; ierr = KmlDssInit(&solver, &opt); if (ierr != KMLSS_NO_ERROR) { printf("ERROR in KmlDssInit: %d\n", ierr); return ierr; } // Analyze KmlDssAnalyzeOption optAnalyze; optAnalyze.fieldMask = KMLDSS_ANALYZE_OPTION_MATCHING_TYPE | KMLDSS_ANALYZE_OPTION_RDR_TYPE | KMLDSS_ANALYZE_OPTION_NTHREADS_RDR; optAnalyze.matchingType = KMLDSS_MATCHING_OFF; optAnalyze.rdrType = KMLDSS_RDR_KRDR; optAnalyze.nThreadsRdr = 8; ierr = KmlDssAnalyze(solver, A, &optAnalyze); if (ierr != KMLSS_NO_ERROR) { printf("ERROR in KmlDssAnalyze: %d\n", ierr); return ierr; } // Factorize KmlDssFactorizeOption optFact; optFact.fieldMask = KMLDSS_FACTORIZE_OPTION_PERTURBATION_THRESHOLD; optFact.perturbationThreshold = 1e-8; ierr = KmlDssFactorize(solver, A, &optFact); if (ierr != KMLSS_NO_ERROR) { printf("ERROR in KmlDssFactorize: %d\n", ierr); return ierr; } // Solve KmlDssSolveOption optSolve; optSolve.fieldMask = KMLDSS_SOLVE_OPTION_SOLVE_STAGE | KMLDSS_SOLVE_OPTION_REFINE_METHOD; optSolve.stage = KMLDSS_SOLVE_ALL; optSolve.refineMethod = KMLDSS_REFINE_OFF; ierr = KmlDssSolve(solver, B, X, &optSolve); if (ierr != KMLSS_NO_ERROR) { printf("ERROR in KmlDssSolve: %d\n", ierr); return ierr; } // Output result x printf("Result of first factorize and solve:\n"); for (int i = 0; i < n; i++) { printf("%lf ", x[i]); } printf("\n"); // Set new values of A double a1[14] = {2.0, 3.0, -3.0, 4.0, 4.0, 5.0, -5.0, 6.0, 5.0, -7.0, 4.0, 8.0, 3.0, 9.0}; KmlSolverMatrixSetValue(A, a1); // Factorize with new values ierr = KmlDssFactorize(solver, A, &optFact); if (ierr != KMLSS_NO_ERROR) { printf("ERROR in KmlDssFactorize: %d\n", ierr); return ierr; } // Set new values of B double b1[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; KmlSolverMatrixSetValue(B, b1); // Solve with new values ierr = KmlDssSolve(solver, B, X, &optSolve); if (ierr != KMLSS_NO_ERROR) { printf("ERROR in KmlDssSolve: %d\n", ierr); return ierr; } // Output new result x printf("Result of second factorize and solve:\n"); for (int i = 0; i < n; i++) { printf("%lf ", x[i]); } printf("\n"); // Query KmlDssInfo info; info.fieldMask = KMLDSS_INFO_PEAK_MEM; ierr = KmlDssQuery(solver, &info); if (ierr != KMLSS_NO_ERROR) { printf("ERROR in KmlDssQuery: %d\n", ierr); return ierr; } printf("Peak memory is %ld Byte\n", info.peakMem); // Destroy KmlDssClean(&solver); KmlSolverMatrixDestroy(&A); KmlSolverMatrixDestroy(&B); KmlSolverMatrixDestroy(&X); |
运行结果:
1 2 3 4 5 | Result of first factorize and solve: 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 Result of second factorize and solve: 0.666667 -0.100000 0.226415 -0.200000 0.166667 0.018868 0.175000 -0.111111 Peak memory is 167775740 Byte |
父主题: 求解器函数