|
| 1 | +/** |
| 2 | + * @file SpGEMMCpu.cpp |
| 3 | + * @author Adham Ibrahim (ibrahimas@ornl.gov) |
| 4 | + * @brief Implementation of SpGEMM using CHOLMOD for CPU |
| 5 | + */ |
| 6 | + |
| 7 | +#include "SpGEMMCpu.hpp" |
| 8 | + |
| 9 | +namespace ReSolve |
| 10 | +{ |
| 11 | + using real_type = ReSolve::real_type; |
| 12 | + |
| 13 | + namespace hykkt |
| 14 | + { |
| 15 | + /** |
| 16 | + * Constructor for SpGEMMCpu |
| 17 | + * @param alpha[in] - Scalar multiplier for the product. |
| 18 | + * @param beta[in] - Scalar multiplier for the sum. |
| 19 | + */ |
| 20 | + SpGEMMCpu::SpGEMMCpu(real_type alpha, real_type beta) |
| 21 | + : alpha_(alpha), beta_(beta) |
| 22 | + { |
| 23 | + cholmod_start(&Common_); |
| 24 | + |
| 25 | + A_ = nullptr; |
| 26 | + B_ = nullptr; |
| 27 | + D_ = nullptr; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Destructor for SpGEMMCpu |
| 32 | + */ |
| 33 | + SpGEMMCpu::~SpGEMMCpu() |
| 34 | + { |
| 35 | + if (A_) |
| 36 | + { |
| 37 | + cholmod_free_sparse(&A_, &Common_); |
| 38 | + } |
| 39 | + if (B_) |
| 40 | + { |
| 41 | + cholmod_free_sparse(&B_, &Common_); |
| 42 | + } |
| 43 | + if (D_) |
| 44 | + { |
| 45 | + cholmod_free_sparse(&D_, &Common_); |
| 46 | + } |
| 47 | + cholmod_finish(&Common_); |
| 48 | + } |
| 49 | + |
| 50 | + void SpGEMMCpu::loadProductMatrices(matrix::Csr* A, matrix::Csr* B) |
| 51 | + { |
| 52 | + if (!A_) |
| 53 | + { |
| 54 | + A_ = allocateCholmodType(A); |
| 55 | + } |
| 56 | + if (!B_) |
| 57 | + { |
| 58 | + B_ = allocateCholmodType(B); |
| 59 | + } |
| 60 | + copyDataToCholmodType(A, A_); |
| 61 | + copyDataToCholmodType(B, B_); |
| 62 | + } |
| 63 | + |
| 64 | + void SpGEMMCpu::loadSumMatrix(matrix::Csr* D) |
| 65 | + { |
| 66 | + if (!D_) |
| 67 | + { |
| 68 | + D_ = allocateCholmodType(D); |
| 69 | + } |
| 70 | + copyDataToCholmodType(D, D_); |
| 71 | + } |
| 72 | + |
| 73 | + void SpGEMMCpu::loadResultMatrix(matrix::Csr** E_ptr) |
| 74 | + { |
| 75 | + E_ptr_ = E_ptr; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @brief Computes the result of the SpGEMM operation |
| 80 | + * |
| 81 | + * Does not store partial results for reuse, as is this is not supported |
| 82 | + * by the CHOLMOD package. |
| 83 | + */ |
| 84 | + void SpGEMMCpu::compute() |
| 85 | + { |
| 86 | + cholmod_sparse* C_chol = cholmod_ssmult(B_, A_, 0, 1, 0, &Common_); |
| 87 | + cholmod_sparse* E_chol = cholmod_add(C_chol, D_, &alpha_, &beta_, 1, 0, &Common_); |
| 88 | + |
| 89 | + if (!(*E_ptr_)) |
| 90 | + { |
| 91 | + *E_ptr_ = new matrix::Csr((index_type) E_chol->nrow, (index_type) E_chol->ncol, (index_type) E_chol->nzmax); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + (*E_ptr_)->destroyMatrixData(memory::HOST); |
| 96 | + } |
| 97 | + |
| 98 | + // Previous data must be de-allocated and new data copied |
| 99 | + // Cholmod does not allow for reuse of arrays |
| 100 | + (*E_ptr_)->copyDataFrom(static_cast<index_type*>(E_chol->p), |
| 101 | + static_cast<index_type*>(E_chol->i), |
| 102 | + static_cast<real_type*>(E_chol->x), |
| 103 | + memory::HOST, |
| 104 | + memory::HOST); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @brief Allocates a CHOLMOD sparse matrix of the same size as the input CSR matrix |
| 109 | + * @param A[in] - Pointer to CSR matrix |
| 110 | + * @return Pointer to the allocated CHOLMOD sparse matrix |
| 111 | + */ |
| 112 | + cholmod_sparse* SpGEMMCpu::allocateCholmodType(matrix::Csr* A) |
| 113 | + { |
| 114 | + return cholmod_allocate_sparse((size_t) A->getNumRows(), |
| 115 | + (size_t) A->getNumColumns(), |
| 116 | + (size_t) A->getNnz(), |
| 117 | + 1, |
| 118 | + 1, |
| 119 | + 0, |
| 120 | + CHOLMOD_REAL, |
| 121 | + &Common_); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Copies the data from a CSR matrix to a CHOLMOD sparse matrix |
| 126 | + * @param A[in] - Pointer to CSR matrix |
| 127 | + * @param A_chol[in] - Pointer to CHOLMOD sparse matrix |
| 128 | + */ |
| 129 | + void SpGEMMCpu::copyDataToCholmodType(matrix::Csr* A, cholmod_sparse* A_chol) |
| 130 | + { |
| 131 | + mem_.copyArrayHostToHost( |
| 132 | + static_cast<int*>(A_chol->p), A->getRowData(memory::HOST), A->getNumRows() + 1); |
| 133 | + mem_.copyArrayHostToHost( |
| 134 | + static_cast<int*>(A_chol->i), A->getColData(memory::HOST), A->getNnz()); |
| 135 | + mem_.copyArrayHostToHost( |
| 136 | + static_cast<double*>(A_chol->x), A->getValues(memory::HOST), A->getNnz()); |
| 137 | + } |
| 138 | + } // namespace hykkt |
| 139 | +} // namespace ReSolve |
0 commit comments