1 #ifndef __SEQAIJKOKKOSIMPL_HPP 2 #define __SEQAIJKOKKOSIMPL_HPP 3 4 #include <petsc/private/vecimpl_kokkos.hpp> 5 #include <../src/mat/impls/aij/seq/aij.h> 6 #include <KokkosSparse_CrsMatrix.hpp> 7 #include <KokkosSparse_spiluk.hpp> 8 #include <string> 9 10 namespace 11 { 12 PETSC_NODISCARD inline decltype(auto) NoInit(std::string label) 13 { 14 return Kokkos::view_alloc(Kokkos::WithoutInitializing, std::move(label)); 15 } 16 } // namespace 17 18 using MatRowMapType = PetscInt; 19 using MatColIdxType = PetscInt; 20 using MatScalarType = PetscScalar; 21 22 template <class MemorySpace> 23 using KokkosCsrMatrixType = typename KokkosSparse::CrsMatrix<MatScalarType, MatColIdxType, MemorySpace, void /* MemoryTraits */, MatRowMapType>; 24 template <class MemorySpace> 25 using KokkosCsrGraphType = typename KokkosCsrMatrixType<MemorySpace>::staticcrsgraph_type; 26 27 using KokkosCsrGraph = KokkosCsrGraphType<DefaultMemorySpace>; 28 using KokkosCsrGraphHost = KokkosCsrGraphType<Kokkos::HostSpace>; 29 30 using KokkosCsrMatrix = KokkosCsrMatrixType<DefaultMemorySpace>; 31 using KokkosCsrMatrixHost = KokkosCsrMatrixType<Kokkos::HostSpace>; 32 33 using MatRowMapKokkosView = KokkosCsrGraph::row_map_type::non_const_type; 34 using MatColIdxKokkosView = KokkosCsrGraph::entries_type::non_const_type; 35 using MatScalarKokkosView = KokkosCsrMatrix::values_type::non_const_type; 36 37 using MatRowMapKokkosViewHost = KokkosCsrGraphHost::row_map_type::non_const_type; 38 using MatColIdxKokkosViewHost = KokkosCsrGraphHost::entries_type::non_const_type; 39 using MatScalarKokkosViewHost = KokkosCsrMatrixHost::values_type::non_const_type; 40 41 using ConstMatRowMapKokkosView = KokkosCsrGraph::row_map_type::const_type; 42 using ConstMatColIdxKokkosView = KokkosCsrGraph::entries_type::const_type; 43 using ConstMatScalarKokkosView = KokkosCsrMatrix::values_type::const_type; 44 45 using ConstMatRowMapKokkosViewHost = KokkosCsrGraphHost::row_map_type::const_type; 46 using ConstMatColIdxKokkosViewHost = KokkosCsrGraphHost::entries_type::const_type; 47 using ConstMatScalarKokkosViewHost = KokkosCsrMatrixHost::values_type::const_type; 48 49 using MatRowMapKokkosDualView = Kokkos::DualView<MatRowMapType *>; 50 using MatColIdxKokkosDualView = Kokkos::DualView<MatColIdxType *>; 51 using MatScalarKokkosDualView = Kokkos::DualView<MatScalarType *>; 52 53 using KernelHandle = KokkosKernels::Experimental::KokkosKernelsHandle<MatRowMapType, MatColIdxType, MatScalarType, DefaultExecutionSpace, DefaultMemorySpace, DefaultMemorySpace>; 54 55 using KokkosTeamMemberType = Kokkos::TeamPolicy<DefaultExecutionSpace>::member_type; 56 57 /* For mat->spptr of a factorized matrix */ 58 struct Mat_SeqAIJKokkosTriFactors { 59 MatRowMapKokkosView iL_d, iU_d, iLt_d, iUt_d; /* rowmap for L, U, L^t, U^t of A=LU */ 60 MatColIdxKokkosView jL_d, jU_d, jLt_d, jUt_d; /* column ids */ 61 MatScalarKokkosView aL_d, aU_d, aLt_d, aUt_d; /* matrix values */ 62 KernelHandle kh, khL, khU, khLt, khUt; /* Kernel handles for A, L, U, L^t, U^t */ 63 PetscBool transpose_updated; /* Are L^T, U^T updated wrt L, U*/ 64 PetscBool sptrsv_symbolic_completed; /* Have we completed the symbolic solve for L and U */ 65 PetscScalarKokkosView workVector; 66 67 Mat_SeqAIJKokkosTriFactors(PetscInt n) : transpose_updated(PETSC_FALSE), sptrsv_symbolic_completed(PETSC_FALSE), workVector("workVector", n) { } 68 69 ~Mat_SeqAIJKokkosTriFactors() { Destroy(); } 70 71 void Destroy() 72 { 73 kh.destroy_spiluk_handle(); 74 khL.destroy_sptrsv_handle(); 75 khU.destroy_sptrsv_handle(); 76 khLt.destroy_sptrsv_handle(); 77 khUt.destroy_sptrsv_handle(); 78 transpose_updated = sptrsv_symbolic_completed = PETSC_FALSE; 79 } 80 }; 81 82 /* For mat->spptr of a regular matrix */ 83 struct Mat_SeqAIJKokkos { 84 MatRowMapKokkosDualView i_dual; 85 MatColIdxKokkosDualView j_dual; 86 MatScalarKokkosDualView a_dual; 87 88 MatRowMapKokkosDualView diag_dual; /* Diagonal pointer, built on demand */ 89 90 KokkosCsrMatrix csrmat; /* The CSR matrix, used to call KK functions */ 91 PetscObjectState nonzerostate; /* State of the nonzero pattern (graph) on device */ 92 93 KokkosCsrMatrix csrmatT, csrmatH; /* Transpose and Hermitian of the matrix (built on demand) */ 94 PetscBool transpose_updated, hermitian_updated; /* Are At, Ah updated wrt the matrix? */ 95 MatRowMapKokkosView transpose_perm; // A permutation array making Ta(i) = Aa(perm(i)), where T = A^t 96 97 /* Construct a nrows by ncols matrix with nnz nonzeros from the given (i,j,a) on host. Caller also specifies a nonzero state */ 98 Mat_SeqAIJKokkos(PetscInt nrows, PetscInt ncols, PetscInt nnz, const MatRowMapType *i, MatColIdxType *j, MatScalarType *a, PetscObjectState nzstate, PetscBool copyValues = PETSC_TRUE) 99 { 100 MatScalarKokkosViewHost a_h(a, nnz); 101 MatRowMapKokkosViewHost i_h(const_cast<MatRowMapType *>(i), nrows + 1); 102 MatColIdxKokkosViewHost j_h(j, nnz); 103 104 auto a_d = Kokkos::create_mirror_view(DefaultMemorySpace(), a_h); 105 auto i_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), i_h); 106 auto j_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), j_h); 107 108 a_dual = MatScalarKokkosDualView(a_d, a_h); 109 i_dual = MatRowMapKokkosDualView(i_d, i_h); 110 j_dual = MatColIdxKokkosDualView(j_d, j_h); 111 112 a_dual.modify_host(); /* Since caller provided values on host */ 113 if (copyValues) a_dual.sync_device(); 114 115 csrmat = KokkosCsrMatrix("csrmat", ncols, a_d, KokkosCsrGraph(j_d, i_d)); 116 nonzerostate = nzstate; 117 transpose_updated = hermitian_updated = PETSC_FALSE; 118 } 119 120 /* Construct with a KokkosCsrMatrix. For performance, only i, j are copied to host, but not the matrix values. */ 121 Mat_SeqAIJKokkos(const KokkosCsrMatrix &csr) : csrmat(csr) /* Shallow-copy csr's views to csrmat */ 122 { 123 auto a_d = csr.values; 124 /* Get a non-const version since I don't want to deal with DualView<const T*>, which is not well defined */ 125 MatRowMapKokkosView i_d(const_cast<MatRowMapType *>(csr.graph.row_map.data()), csr.graph.row_map.extent(0)); 126 auto j_d = csr.graph.entries; 127 auto a_h = Kokkos::create_mirror_view(Kokkos::HostSpace(), a_d); 128 auto i_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), i_d); 129 auto j_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), j_d); 130 131 a_dual = MatScalarKokkosDualView(a_d, a_h); 132 a_dual.modify_device(); /* since we did not copy a_d to a_h, we mark device has the latest data */ 133 i_dual = MatRowMapKokkosDualView(i_d, i_h); 134 j_dual = MatColIdxKokkosDualView(j_d, j_h); 135 Init(); 136 } 137 138 Mat_SeqAIJKokkos(PetscInt nrows, PetscInt ncols, PetscInt nnz, MatRowMapKokkosDualView &i, MatColIdxKokkosDualView &j, MatScalarKokkosDualView a) : i_dual(i), j_dual(j), a_dual(a) 139 { 140 csrmat = KokkosCsrMatrix("csrmat", nrows, ncols, nnz, a.view_device(), i.view_device(), j.view_device()); 141 Init(); 142 } 143 144 MatScalarType *a_host_data() { return a_dual.view_host().data(); } 145 MatRowMapType *i_host_data() { return i_dual.view_host().data(); } 146 MatColIdxType *j_host_data() { return j_dual.view_host().data(); } 147 148 MatScalarType *a_device_data() { return a_dual.view_device().data(); } 149 MatRowMapType *i_device_data() { return i_dual.view_device().data(); } 150 MatColIdxType *j_device_data() { return j_dual.view_device().data(); } 151 152 MatColIdxType nrows() { return csrmat.numRows(); } 153 MatColIdxType ncols() { return csrmat.numCols(); } 154 MatRowMapType nnz() { return csrmat.nnz(); } 155 156 /* Change the csrmat size to n */ 157 void SetColSize(MatColIdxType n) { csrmat = KokkosCsrMatrix("csrmat", n, a_dual.view_device(), csrmat.graph); } 158 159 void SetDiagonal(const MatRowMapType *diag) 160 { 161 MatRowMapKokkosViewHost diag_h(const_cast<MatRowMapType *>(diag), nrows()); 162 auto diag_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), diag_h); 163 diag_dual = MatRowMapKokkosDualView(diag_d, diag_h); 164 } 165 166 /* Shared init stuff */ 167 void Init(void) 168 { 169 transpose_updated = hermitian_updated = PETSC_FALSE; 170 nonzerostate = 0; 171 } 172 173 PetscErrorCode DestroyMatTranspose(void) 174 { 175 PetscFunctionBegin; 176 csrmatT = KokkosCsrMatrix(); /* Overwrite with empty matrices */ 177 csrmatH = KokkosCsrMatrix(); 178 PetscFunctionReturn(PETSC_SUCCESS); 179 } 180 }; 181 182 struct MatProductData_SeqAIJKokkos { 183 KernelHandle kh; 184 PetscBool reusesym; 185 MatProductData_SeqAIJKokkos() : reusesym(PETSC_FALSE) { } 186 }; 187 188 PETSC_INTERN PetscErrorCode MatSetSeqAIJKokkosWithCSRMatrix(Mat, Mat_SeqAIJKokkos *); 189 PETSC_INTERN PetscErrorCode MatCreateSeqAIJKokkosWithCSRMatrix(MPI_Comm, Mat_SeqAIJKokkos *, Mat *); 190 PETSC_INTERN PetscErrorCode MatSeqAIJKokkosMergeMats(Mat, Mat, MatReuse, Mat *); 191 PETSC_INTERN PetscErrorCode MatSeqAIJKokkosSyncDevice(Mat); 192 PETSC_INTERN PetscErrorCode MatSeqAIJKokkosGetKokkosCsrMatrix(Mat, KokkosCsrMatrix *); 193 PETSC_INTERN PetscErrorCode MatCreateSeqAIJKokkosWithKokkosCsrMatrix(MPI_Comm, KokkosCsrMatrix, Mat *); 194 PETSC_INTERN PetscErrorCode PrintCsrMatrix(const KokkosCsrMatrix &csrmat); 195 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *); 196 PETSC_INTERN PetscErrorCode MatSeqAIJKokkosModifyDevice(Mat); 197 PETSC_INTERN PetscErrorCode MatSeqAIJKokkosGenerateTranspose_Private(Mat, KokkosCsrMatrix *); 198 199 PETSC_INTERN PetscErrorCode MatSeqAIJGetKokkosView(Mat, MatScalarKokkosView *); 200 PETSC_INTERN PetscErrorCode MatSeqAIJRestoreKokkosView(Mat, MatScalarKokkosView *); 201 PETSC_INTERN PetscErrorCode MatSeqAIJGetKokkosViewWrite(Mat, MatScalarKokkosView *); 202 PETSC_INTERN PetscErrorCode MatSeqAIJRestoreKokkosViewWrite(Mat, MatScalarKokkosView *); 203 #endif 204