|
| 1 | +#include <string> |
| 2 | +#include <iostream> |
| 3 | +#include <iomanip> |
| 4 | + |
| 5 | +#include <resolve/matrix/Coo.hpp> |
| 6 | +#include <resolve/matrix/Csr.hpp> |
| 7 | +#include <resolve/matrix/Csc.hpp> |
| 8 | +#include <resolve/vector/Vector.hpp> |
| 9 | +#include <resolve/matrix/io.hpp> |
| 10 | +#include <resolve/matrix/MatrixHandler.hpp> |
| 11 | +#include <resolve/vector/VectorHandler.hpp> |
| 12 | +#include <resolve/LinSolverDirectKLU.hpp> |
| 13 | +#include <resolve/LinSolverDirectRocSolverRf.hpp> |
| 14 | +#include <resolve/workspace/LinAlgWorkspace.hpp> |
| 15 | + |
| 16 | +using namespace ReSolve::constants; |
| 17 | + |
| 18 | +int main(int argc, char *argv[] ) |
| 19 | +{ |
| 20 | + // Use the same data types as those you specified in ReSolve build. |
| 21 | + using index_type = ReSolve::index_type; |
| 22 | + using real_type = ReSolve::real_type; |
| 23 | + using vector_type = ReSolve::vector::Vector; |
| 24 | + |
| 25 | + (void) argc; // TODO: Check if the number of input parameters is correct. |
| 26 | + std::string matrixFileName = argv[1]; |
| 27 | + std::string rhsFileName = argv[2]; |
| 28 | + |
| 29 | + index_type numSystems = atoi(argv[3]); |
| 30 | + std::cout<<"Family mtx file name: "<< matrixFileName << ", total number of matrices: "<<numSystems<<std::endl; |
| 31 | + std::cout<<"Family rhs file name: "<< rhsFileName << ", total number of RHSes: " << numSystems<<std::endl; |
| 32 | + |
| 33 | + std::string fileId; |
| 34 | + std::string rhsId; |
| 35 | + std::string matrixFileNameFull; |
| 36 | + std::string rhsFileNameFull; |
| 37 | + |
| 38 | + ReSolve::matrix::Coo* A_coo; |
| 39 | + ReSolve::matrix::Csr* A; |
| 40 | + |
| 41 | + ReSolve::LinAlgWorkspaceHIP* workspace_HIP = new ReSolve::LinAlgWorkspaceHIP; |
| 42 | + workspace_HIP->initializeHandles(); |
| 43 | + ReSolve::MatrixHandler* matrix_handler = new ReSolve::MatrixHandler(workspace_HIP); |
| 44 | + ReSolve::VectorHandler* vector_handler = new ReSolve::VectorHandler(workspace_HIP); |
| 45 | + real_type* rhs = nullptr; |
| 46 | + real_type* x = nullptr; |
| 47 | + |
| 48 | + vector_type* vec_rhs; |
| 49 | + vector_type* vec_x; |
| 50 | + vector_type* vec_r; |
| 51 | + |
| 52 | + ReSolve::LinSolverDirectKLU* KLU = new ReSolve::LinSolverDirectKLU; |
| 53 | + ReSolve::LinSolverDirectRocSolverRf* Rf = new ReSolve::LinSolverDirectRocSolverRf(workspace_HIP); |
| 54 | + |
| 55 | + real_type res_nrm; |
| 56 | + real_type b_nrm; |
| 57 | + |
| 58 | + for (int i = 0; i < numSystems; ++i) |
| 59 | + { |
| 60 | + index_type j = 4 + i * 2; |
| 61 | + fileId = argv[j]; |
| 62 | + rhsId = argv[j + 1]; |
| 63 | + |
| 64 | + matrixFileNameFull = ""; |
| 65 | + rhsFileNameFull = ""; |
| 66 | + |
| 67 | + // Read matrix first |
| 68 | + matrixFileNameFull = matrixFileName + fileId + ".mtx"; |
| 69 | + rhsFileNameFull = rhsFileName + rhsId + ".mtx"; |
| 70 | + std::cout << std::endl << std::endl << std::endl; |
| 71 | + std::cout << "========================================================================================================================"<<std::endl; |
| 72 | + std::cout << "Reading: " << matrixFileNameFull << std::endl; |
| 73 | + std::cout << "========================================================================================================================"<<std::endl; |
| 74 | + std::cout << std::endl; |
| 75 | + // Read first matrix |
| 76 | + std::ifstream mat_file(matrixFileNameFull); |
| 77 | + if(!mat_file.is_open()) |
| 78 | + { |
| 79 | + std::cout << "Failed to open file " << matrixFileNameFull << "\n"; |
| 80 | + return -1; |
| 81 | + } |
| 82 | + std::ifstream rhs_file(rhsFileNameFull); |
| 83 | + if(!rhs_file.is_open()) |
| 84 | + { |
| 85 | + std::cout << "Failed to open file " << rhsFileNameFull << "\n"; |
| 86 | + return -1; |
| 87 | + } |
| 88 | + if (i == 0) { |
| 89 | + A_coo = ReSolve::io::readMatrixFromFile(mat_file); |
| 90 | + A = new ReSolve::matrix::Csr(A_coo->getNumRows(), |
| 91 | + A_coo->getNumColumns(), |
| 92 | + A_coo->getNnz(), |
| 93 | + A_coo->symmetric(), |
| 94 | + A_coo->expanded()); |
| 95 | + |
| 96 | + rhs = ReSolve::io::readRhsFromFile(rhs_file); |
| 97 | + x = new real_type[A->getNumRows()]; |
| 98 | + vec_rhs = new vector_type(A->getNumRows()); |
| 99 | + vec_x = new vector_type(A->getNumRows()); |
| 100 | + vec_r = new vector_type(A->getNumRows()); |
| 101 | + } |
| 102 | + else { |
| 103 | + ReSolve::io::readAndUpdateMatrix(mat_file, A_coo); |
| 104 | + ReSolve::io::readAndUpdateRhs(rhs_file, &rhs); |
| 105 | + } |
| 106 | + std::cout<<"Finished reading the matrix and rhs, size: "<<A->getNumRows()<<" x "<<A->getNumColumns()<< ", nnz: "<< A->getNnz()<< ", symmetric? "<<A->symmetric()<< ", Expanded? "<<A->expanded()<<std::endl; |
| 107 | + mat_file.close(); |
| 108 | + rhs_file.close(); |
| 109 | + |
| 110 | + //Now convert to CSR. |
| 111 | + if (i < 2) { |
| 112 | + matrix_handler->coo2csr(A_coo, A, "cpu"); |
| 113 | + vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::HOST); |
| 114 | + vec_rhs->setDataUpdated(ReSolve::memory::HOST); |
| 115 | + } else { |
| 116 | + matrix_handler->coo2csr(A_coo, A, "hip"); |
| 117 | + vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE); |
| 118 | + } |
| 119 | + std::cout<<"COO to CSR completed. Expanded NNZ: "<< A->getNnzExpanded()<<std::endl; |
| 120 | + //Now call direct solver |
| 121 | + if (i == 0) { |
| 122 | + KLU->setupParameters(1, 0.1, false); |
| 123 | + } |
| 124 | + int status; |
| 125 | + if (i < 2){ |
| 126 | + KLU->setup(A); |
| 127 | + status = KLU->analyze(); |
| 128 | + std::cout<<"KLU analysis status: "<<status<<std::endl; |
| 129 | + status = KLU->factorize(); |
| 130 | + std::cout<<"KLU factorization status: "<<status<<std::endl; |
| 131 | + status = KLU->solve(vec_rhs, vec_x); |
| 132 | + std::cout<<"KLU solve status: "<<status<<std::endl; |
| 133 | + if (i == 1) { |
| 134 | + ReSolve::matrix::Csc* L = (ReSolve::matrix::Csc*) KLU->getLFactor(); |
| 135 | + ReSolve::matrix::Csc* U = (ReSolve::matrix::Csc*) KLU->getUFactor(); |
| 136 | + index_type* P = KLU->getPOrdering(); |
| 137 | + index_type* Q = KLU->getQOrdering(); |
| 138 | + vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE); |
| 139 | + Rf->setup(A, L, U, P, Q, vec_rhs); |
| 140 | + Rf->refactorize(); |
| 141 | + } |
| 142 | + } else { |
| 143 | + std::cout<<"Using rocsolver rf"<<std::endl; |
| 144 | + status = Rf->refactorize(); |
| 145 | + std::cout<<"rocsolver rf refactorization status: "<<status<<std::endl; |
| 146 | + status = Rf->solve(vec_rhs, vec_x); |
| 147 | + std::cout<<"rocsolver rf solve status: "<<status<<std::endl; |
| 148 | + } |
| 149 | + vec_r->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE); |
| 150 | + |
| 151 | + matrix_handler->setValuesChanged(true, "hip"); |
| 152 | + |
| 153 | + matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", "hip"); |
| 154 | + res_nrm = sqrt(vector_handler->dot(vec_r, vec_r, "hip")); |
| 155 | + b_nrm = sqrt(vector_handler->dot(vec_rhs, vec_rhs, "hip")); |
| 156 | + std::cout << "\t 2-Norm of the residual: " |
| 157 | + << std::scientific << std::setprecision(16) |
| 158 | + << res_nrm/b_nrm << "\n"; |
| 159 | + if (!isnan(res_nrm)) { |
| 160 | + if (res_nrm/b_nrm > 1e-7 ) { |
| 161 | + std::cout << "\n \t !!! ALERT !!! Residual norm is too large; redoing KLU symbolic and numeric factorization. !!! ALERT !!! \n \n"; |
| 162 | + |
| 163 | + KLU->setup(A); |
| 164 | + status = KLU->analyze(); |
| 165 | + std::cout<<"KLU analysis status: "<<status<<std::endl; |
| 166 | + status = KLU->factorize(); |
| 167 | + std::cout<<"KLU factorization status: "<<status<<std::endl; |
| 168 | + status = KLU->solve(vec_rhs, vec_x); |
| 169 | + std::cout<<"KLU solve status: "<<status<<std::endl; |
| 170 | + |
| 171 | + vec_rhs->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE); |
| 172 | + vec_r->update(rhs, ReSolve::memory::HOST, ReSolve::memory::DEVICE); |
| 173 | + |
| 174 | + matrix_handler->setValuesChanged(true, "hip"); |
| 175 | + |
| 176 | + matrix_handler->matvec(A, vec_x, vec_r, &ONE, &MINUSONE,"csr", "hip"); |
| 177 | + res_nrm = sqrt(vector_handler->dot(vec_r, vec_r, "hip")); |
| 178 | + |
| 179 | + std::cout<<"\t New residual norm: " |
| 180 | + << std::scientific << std::setprecision(16) |
| 181 | + << res_nrm/b_nrm << "\n"; |
| 182 | + |
| 183 | + |
| 184 | + ReSolve::matrix::Csc* L = (ReSolve::matrix::Csc*) KLU->getLFactor(); |
| 185 | + ReSolve::matrix::Csc* U = (ReSolve::matrix::Csc*) KLU->getUFactor(); |
| 186 | + |
| 187 | + index_type* P = KLU->getPOrdering(); |
| 188 | + index_type* Q = KLU->getQOrdering(); |
| 189 | + |
| 190 | + Rf->setup(A, L, U, P, Q, vec_rhs); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + } // for (int i = 0; i < numSystems; ++i) |
| 196 | + |
| 197 | + //now DELETE |
| 198 | + delete A; |
| 199 | + delete A_coo; |
| 200 | + delete KLU; |
| 201 | + delete Rf; |
| 202 | + delete [] x; |
| 203 | + delete [] rhs; |
| 204 | + delete vec_r; |
| 205 | + delete vec_x; |
| 206 | + delete workspace_HIP; |
| 207 | + delete matrix_handler; |
| 208 | + delete vector_handler; |
| 209 | + return 0; |
| 210 | +} |
0 commit comments