142550becSJunchao Zhang #ifndef __SEQAIJKOKKOSIMPL_HPP 242550becSJunchao Zhang #define __SEQAIJKOKKOSIMPL_HPP 342550becSJunchao Zhang 442550becSJunchao Zhang #include <petscaijdevice.h> 542550becSJunchao Zhang #include <petsc/private/vecimpl_kokkos.hpp> 6394ed5ebSJunchao Zhang #include <../src/mat/impls/aij/seq/aij.h> 742550becSJunchao Zhang #include <KokkosSparse_CrsMatrix.hpp> 842550becSJunchao Zhang #include <KokkosSparse_spiluk.hpp> 942550becSJunchao Zhang 1042550becSJunchao Zhang /* 1142550becSJunchao Zhang Kokkos::View<struct _n_SplitCSRMat,DefaultMemorySpace> is not handled correctly so we define SplitCSRMat 1242550becSJunchao Zhang for the singular purpose of working around this. 1342550becSJunchao Zhang */ 1442550becSJunchao Zhang typedef struct _n_SplitCSRMat SplitCSRMat; 1542550becSJunchao Zhang 1642550becSJunchao Zhang using MatRowMapType = PetscInt; 1742550becSJunchao Zhang using MatColIdxType = PetscInt; 1842550becSJunchao Zhang using MatScalarType = PetscScalar; 1942550becSJunchao Zhang 20*9371c9d4SSatish Balay template <class MemorySpace> 21*9371c9d4SSatish Balay using KokkosCsrMatrixType = typename KokkosSparse::CrsMatrix<MatScalarType, MatColIdxType, MemorySpace, void /* MemoryTraits */, MatRowMapType>; 22*9371c9d4SSatish Balay template <class MemorySpace> 23*9371c9d4SSatish Balay using KokkosCsrGraphType = typename KokkosCsrMatrixType<MemorySpace>::staticcrsgraph_type; 2442550becSJunchao Zhang 2542550becSJunchao Zhang using KokkosCsrGraph = KokkosCsrGraphType<DefaultMemorySpace>; 2642550becSJunchao Zhang using KokkosCsrGraphHost = KokkosCsrGraphType<Kokkos::HostSpace>; 2742550becSJunchao Zhang 2842550becSJunchao Zhang using KokkosCsrMatrix = KokkosCsrMatrixType<DefaultMemorySpace>; 2942550becSJunchao Zhang using KokkosCsrMatrixHost = KokkosCsrMatrixType<Kokkos::HostSpace>; 3042550becSJunchao Zhang 3142550becSJunchao Zhang using MatRowMapKokkosView = KokkosCsrGraph::row_map_type::non_const_type; 3242550becSJunchao Zhang using MatColIdxKokkosView = KokkosCsrGraph::entries_type::non_const_type; 3342550becSJunchao Zhang using MatScalarKokkosView = KokkosCsrMatrix::values_type::non_const_type; 3442550becSJunchao Zhang 3542550becSJunchao Zhang using MatRowMapKokkosViewHost = KokkosCsrGraphHost::row_map_type::non_const_type; 3642550becSJunchao Zhang using MatColIdxKokkosViewHost = KokkosCsrGraphHost::entries_type::non_const_type; 3742550becSJunchao Zhang using MatScalarKokkosViewHost = KokkosCsrMatrixHost::values_type::non_const_type; 3842550becSJunchao Zhang 3942550becSJunchao Zhang using ConstMatRowMapKokkosView = KokkosCsrGraph::row_map_type::const_type; 4042550becSJunchao Zhang using ConstMatColIdxKokkosView = KokkosCsrGraph::entries_type::const_type; 4142550becSJunchao Zhang using ConstMatScalarKokkosView = KokkosCsrMatrix::values_type::const_type; 4242550becSJunchao Zhang 4342550becSJunchao Zhang using ConstMatRowMapKokkosViewHost = KokkosCsrGraphHost::row_map_type::const_type; 4442550becSJunchao Zhang using ConstMatColIdxKokkosViewHost = KokkosCsrGraphHost::entries_type::const_type; 4542550becSJunchao Zhang using ConstMatScalarKokkosViewHost = KokkosCsrMatrixHost::values_type::const_type; 4642550becSJunchao Zhang 4742550becSJunchao Zhang using MatRowMapKokkosDualView = Kokkos::DualView<MatRowMapType *>; 4842550becSJunchao Zhang using MatColIdxKokkosDualView = Kokkos::DualView<MatColIdxType *>; 4942550becSJunchao Zhang using MatScalarKokkosDualView = Kokkos::DualView<MatScalarType *>; 5042550becSJunchao Zhang 5142550becSJunchao Zhang using KernelHandle = KokkosKernels::Experimental::KokkosKernelsHandle<MatRowMapType, MatColIdxType, MatScalarType, DefaultExecutionSpace, DefaultMemorySpace, DefaultMemorySpace>; 5242550becSJunchao Zhang 5342550becSJunchao Zhang using KokkosTeamMemberType = Kokkos::TeamPolicy<DefaultExecutionSpace>::member_type; 5442550becSJunchao Zhang 5542550becSJunchao Zhang /* For mat->spptr of a factorized matrix */ 5642550becSJunchao Zhang struct Mat_SeqAIJKokkosTriFactors { 5742550becSJunchao Zhang MatRowMapKokkosView iL_d, iU_d, iLt_d, iUt_d; /* rowmap for L, U, L^t, U^t of A=LU */ 5842550becSJunchao Zhang MatColIdxKokkosView jL_d, jU_d, jLt_d, jUt_d; /* column ids */ 5942550becSJunchao Zhang MatScalarKokkosView aL_d, aU_d, aLt_d, aUt_d; /* matrix values */ 6042550becSJunchao Zhang KernelHandle kh, khL, khU, khLt, khUt; /* Kernel handles for A, L, U, L^t, U^t */ 6142550becSJunchao Zhang PetscBool transpose_updated; /* Are L^T, U^T updated wrt L, U*/ 6242550becSJunchao Zhang PetscBool sptrsv_symbolic_completed; /* Have we completed the symbolic solve for L and U */ 6342550becSJunchao Zhang PetscScalarKokkosView workVector; 6442550becSJunchao Zhang 65*9371c9d4SSatish Balay Mat_SeqAIJKokkosTriFactors(PetscInt n) : transpose_updated(PETSC_FALSE), sptrsv_symbolic_completed(PETSC_FALSE), workVector("workVector", n) { } 6642550becSJunchao Zhang 6742550becSJunchao Zhang ~Mat_SeqAIJKokkosTriFactors() { Destroy(); } 6842550becSJunchao Zhang 6942550becSJunchao Zhang void Destroy() { 7042550becSJunchao Zhang kh.destroy_spiluk_handle(); 7142550becSJunchao Zhang khL.destroy_sptrsv_handle(); 7242550becSJunchao Zhang khU.destroy_sptrsv_handle(); 7342550becSJunchao Zhang khLt.destroy_sptrsv_handle(); 7442550becSJunchao Zhang khUt.destroy_sptrsv_handle(); 7542550becSJunchao Zhang transpose_updated = sptrsv_symbolic_completed = PETSC_FALSE; 7642550becSJunchao Zhang } 7742550becSJunchao Zhang }; 7842550becSJunchao Zhang 7942550becSJunchao Zhang /* For mat->spptr of a regular matrix */ 8042550becSJunchao Zhang struct Mat_SeqAIJKokkos { 8142550becSJunchao Zhang MatRowMapKokkosDualView i_dual; 8242550becSJunchao Zhang MatColIdxKokkosDualView j_dual; 8342550becSJunchao Zhang MatScalarKokkosDualView a_dual; 8442550becSJunchao Zhang 85f78ce678SMark Adams MatRowMapKokkosDualView diag_dual; /* Diagonal pointer, built on demand */ 86f78ce678SMark Adams 8742550becSJunchao Zhang KokkosCsrMatrix csrmat; /* The CSR matrix, used to call KK functions */ 8842550becSJunchao Zhang PetscObjectState nonzerostate; /* State of the nonzero pattern (graph) on device */ 8942550becSJunchao Zhang 9042550becSJunchao Zhang KokkosCsrMatrix csrmatT, csrmatH; /* Transpose and Hermitian of the matrix (built on demand) */ 9142550becSJunchao Zhang PetscBool transpose_updated, hermitian_updated; /* Are At, Ah updated wrt the matrix? */ 9242550becSJunchao Zhang 93394ed5ebSJunchao Zhang /* COO stuff */ 94394ed5ebSJunchao Zhang PetscCountKokkosView jmap_d; /* perm[disp+jmap[i]..disp+jmap[i+1]) gives indices of entries in v[] associated with i-th nonzero of the matrix */ 95394ed5ebSJunchao Zhang PetscCountKokkosView perm_d; /* The permutation array in sorting (i,j) by row and then by col */ 9642550becSJunchao Zhang 97300d22a6SJunchao Zhang Kokkos::View<PetscInt *> i_uncompressed_d; 98300d22a6SJunchao Zhang Kokkos::View<PetscInt *> colmap_d; // ugh, this is a parallel construct 9942550becSJunchao Zhang Kokkos::View<SplitCSRMat, DefaultMemorySpace> device_mat_d; 100300d22a6SJunchao Zhang Kokkos::View<PetscInt *> diag_d; // factorizations 10142550becSJunchao Zhang 10242550becSJunchao Zhang /* Construct a nrows by ncols matrix with nnz nonzeros from the given (i,j,a) on host. Caller also specifies a nonzero state */ 103*9371c9d4SSatish Balay Mat_SeqAIJKokkos(PetscInt nrows, PetscInt ncols, PetscInt nnz, const MatRowMapType *i, MatColIdxType *j, MatScalarType *a, PetscObjectState nzstate, PetscBool copyValues = PETSC_TRUE) { 10442550becSJunchao Zhang MatScalarKokkosViewHost a_h(a, nnz); 10542550becSJunchao Zhang MatRowMapKokkosViewHost i_h(const_cast<MatRowMapType *>(i), nrows + 1); 10642550becSJunchao Zhang MatColIdxKokkosViewHost j_h(j, nnz); 10742550becSJunchao Zhang 10842550becSJunchao Zhang auto a_d = Kokkos::create_mirror_view(DefaultMemorySpace(), a_h); 10942550becSJunchao Zhang auto i_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), i_h); 11042550becSJunchao Zhang auto j_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), j_h); 11142550becSJunchao Zhang 11242550becSJunchao Zhang a_dual = MatScalarKokkosDualView(a_d, a_h); 11342550becSJunchao Zhang i_dual = MatRowMapKokkosDualView(i_d, i_h); 11442550becSJunchao Zhang j_dual = MatColIdxKokkosDualView(j_d, j_h); 11542550becSJunchao Zhang 11642550becSJunchao Zhang a_dual.modify_host(); /* Since caller provided values on host */ 11742550becSJunchao Zhang if (copyValues) a_dual.sync_device(); 11842550becSJunchao Zhang 11942550becSJunchao Zhang csrmat = KokkosCsrMatrix("csrmat", ncols, a_d, KokkosCsrGraph(j_d, i_d)); 12042550becSJunchao Zhang nonzerostate = nzstate; 12142550becSJunchao Zhang transpose_updated = hermitian_updated = PETSC_FALSE; 12242550becSJunchao Zhang } 12342550becSJunchao Zhang 12442550becSJunchao Zhang /* Construct with a KokkosCsrMatrix. For performance, only i, j are copied to host, but not the matrix values. */ 125*9371c9d4SSatish Balay Mat_SeqAIJKokkos(const KokkosCsrMatrix &csr) : 126*9371c9d4SSatish Balay csrmat(csr) /* Shallow-copy csr's views to csrmat */ 12742550becSJunchao Zhang { 12842550becSJunchao Zhang auto a_d = csr.values; 12942550becSJunchao Zhang /* Get a non-const version since I don't want to deal with DualView<const T*>, which is not well defined */ 13042550becSJunchao Zhang MatRowMapKokkosView i_d(const_cast<MatRowMapType *>(csr.graph.row_map.data()), csr.graph.row_map.extent(0)); 13142550becSJunchao Zhang auto j_d = csr.graph.entries; 13242550becSJunchao Zhang auto a_h = Kokkos::create_mirror_view(Kokkos::HostSpace(), a_d); 13342550becSJunchao Zhang auto i_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), i_d); 13442550becSJunchao Zhang auto j_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), j_d); 13542550becSJunchao Zhang 13642550becSJunchao Zhang a_dual = MatScalarKokkosDualView(a_d, a_h); 13742550becSJunchao Zhang a_dual.modify_device(); /* since we did not copy a_d to a_h, we mark device has the latest data */ 13842550becSJunchao Zhang i_dual = MatRowMapKokkosDualView(i_d, i_h); 13942550becSJunchao Zhang j_dual = MatColIdxKokkosDualView(j_d, j_h); 14042550becSJunchao Zhang Init(); 14142550becSJunchao Zhang } 14242550becSJunchao Zhang 143*9371c9d4SSatish Balay Mat_SeqAIJKokkos(PetscInt nrows, PetscInt ncols, PetscInt nnz, MatRowMapKokkosDualView &i, MatColIdxKokkosDualView &j, MatScalarKokkosDualView a) : i_dual(i), j_dual(j), a_dual(a) { 14442550becSJunchao Zhang csrmat = KokkosCsrMatrix("csrmat", nrows, ncols, nnz, a.view_device(), i.view_device(), j.view_device()); 14542550becSJunchao Zhang Init(); 14642550becSJunchao Zhang } 14742550becSJunchao Zhang 14842550becSJunchao Zhang MatScalarType *a_host_data() { return a_dual.view_host().data(); } 14942550becSJunchao Zhang MatRowMapType *i_host_data() { return i_dual.view_host().data(); } 15042550becSJunchao Zhang MatColIdxType *j_host_data() { return j_dual.view_host().data(); } 15142550becSJunchao Zhang 15242550becSJunchao Zhang MatScalarType *a_device_data() { return a_dual.view_device().data(); } 15342550becSJunchao Zhang MatRowMapType *i_device_data() { return i_dual.view_device().data(); } 15442550becSJunchao Zhang MatColIdxType *j_device_data() { return j_dual.view_device().data(); } 15542550becSJunchao Zhang 15642550becSJunchao Zhang MatColIdxType nrows() { return csrmat.numRows(); } 15742550becSJunchao Zhang MatColIdxType ncols() { return csrmat.numCols(); } 15842550becSJunchao Zhang MatRowMapType nnz() { return csrmat.nnz(); } 15942550becSJunchao Zhang 16042550becSJunchao Zhang /* Change the csrmat size to n */ 16142550becSJunchao Zhang void SetColSize(MatColIdxType n) { csrmat = KokkosCsrMatrix("csrmat", n, a_dual.view_device(), csrmat.graph); } 16242550becSJunchao Zhang 163394ed5ebSJunchao Zhang void SetUpCOO(const Mat_SeqAIJ *aij) { 164394ed5ebSJunchao Zhang jmap_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), PetscCountKokkosViewHost(aij->jmap, aij->nz + 1)); 165394ed5ebSJunchao Zhang perm_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), PetscCountKokkosViewHost(aij->perm, aij->Atot)); 16642550becSJunchao Zhang } 16742550becSJunchao Zhang 168*9371c9d4SSatish Balay void SetDiagonal(const MatRowMapType *diag) { 169f78ce678SMark Adams MatRowMapKokkosViewHost diag_h(const_cast<MatRowMapType *>(diag), nrows()); 170f78ce678SMark Adams auto diag_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), diag_h); 171f78ce678SMark Adams diag_dual = MatRowMapKokkosDualView(diag_d, diag_h); 172f78ce678SMark Adams } 173f78ce678SMark Adams 17442550becSJunchao Zhang /* Shared init stuff */ 175*9371c9d4SSatish Balay void Init(void) { 17642550becSJunchao Zhang transpose_updated = hermitian_updated = PETSC_FALSE; 17742550becSJunchao Zhang nonzerostate = 0; 17842550becSJunchao Zhang } 17942550becSJunchao Zhang 180*9371c9d4SSatish Balay PetscErrorCode DestroyMatTranspose(void) { 18142550becSJunchao Zhang PetscFunctionBegin; 18242550becSJunchao Zhang csrmatT = KokkosCsrMatrix(); /* Overwrite with empty matrices */ 18342550becSJunchao Zhang csrmatH = KokkosCsrMatrix(); 18442550becSJunchao Zhang PetscFunctionReturn(0); 18542550becSJunchao Zhang } 18642550becSJunchao Zhang }; 18742550becSJunchao Zhang 18842550becSJunchao Zhang struct MatProductData_SeqAIJKokkos { 18942550becSJunchao Zhang KernelHandle kh; 19042550becSJunchao Zhang PetscBool reusesym; 19142550becSJunchao Zhang MatProductData_SeqAIJKokkos() : reusesym(PETSC_FALSE) { } 19242550becSJunchao Zhang }; 19342550becSJunchao Zhang 19442550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatSetSeqAIJKokkosWithCSRMatrix(Mat, Mat_SeqAIJKokkos *); 19542550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatCreateSeqAIJKokkosWithCSRMatrix(MPI_Comm, Mat_SeqAIJKokkos *, Mat *); 19642550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJKokkosMergeMats(Mat, Mat, MatReuse, Mat *); 19742550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJKokkosSyncDevice(Mat); 19842550becSJunchao Zhang PETSC_INTERN PetscErrorCode PrintCsrMatrix(const KokkosCsrMatrix &csrmat); 19942550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *); 20042550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJKokkosModifyDevice(Mat); 201394ed5ebSJunchao Zhang 202394ed5ebSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJGetKokkosView(Mat, MatScalarKokkosView *); 203394ed5ebSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJRestoreKokkosView(Mat, MatScalarKokkosView *); 204394ed5ebSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJGetKokkosViewWrite(Mat, MatScalarKokkosView *); 205394ed5ebSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJRestoreKokkosViewWrite(Mat, MatScalarKokkosView *); 20642550becSJunchao Zhang #endif 207