xref: /petsc/src/mat/impls/aij/seq/kokkos/aijkok.hpp (revision f78ce678aed4d3e450e422d3a2725e4faeb03dcb)
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 
2042550becSJunchao Zhang template<class MemorySpace> using KokkosCsrMatrixType   = typename KokkosSparse::CrsMatrix<MatScalarType,MatColIdxType,MemorySpace,void/* MemoryTraits */,MatRowMapType>;
2142550becSJunchao Zhang template<class MemorySpace> using KokkosCsrGraphType    = typename KokkosCsrMatrixType<MemorySpace>::staticcrsgraph_type;
2242550becSJunchao Zhang 
2342550becSJunchao Zhang using KokkosCsrGraph                 = KokkosCsrGraphType<DefaultMemorySpace>;
2442550becSJunchao Zhang using KokkosCsrGraphHost             = KokkosCsrGraphType<Kokkos::HostSpace>;
2542550becSJunchao Zhang 
2642550becSJunchao Zhang using KokkosCsrMatrix                = KokkosCsrMatrixType<DefaultMemorySpace>;
2742550becSJunchao Zhang using KokkosCsrMatrixHost            = KokkosCsrMatrixType<Kokkos::HostSpace>;
2842550becSJunchao Zhang 
2942550becSJunchao Zhang using MatRowMapKokkosView            = KokkosCsrGraph::row_map_type::non_const_type;
3042550becSJunchao Zhang using MatColIdxKokkosView            = KokkosCsrGraph::entries_type::non_const_type;
3142550becSJunchao Zhang using MatScalarKokkosView            = KokkosCsrMatrix::values_type::non_const_type;
3242550becSJunchao Zhang 
3342550becSJunchao Zhang using MatRowMapKokkosViewHost        = KokkosCsrGraphHost::row_map_type::non_const_type;
3442550becSJunchao Zhang using MatColIdxKokkosViewHost        = KokkosCsrGraphHost::entries_type::non_const_type;
3542550becSJunchao Zhang using MatScalarKokkosViewHost        = KokkosCsrMatrixHost::values_type::non_const_type;
3642550becSJunchao Zhang 
3742550becSJunchao Zhang using ConstMatRowMapKokkosView       = KokkosCsrGraph::row_map_type::const_type;
3842550becSJunchao Zhang using ConstMatColIdxKokkosView       = KokkosCsrGraph::entries_type::const_type;
3942550becSJunchao Zhang using ConstMatScalarKokkosView       = KokkosCsrMatrix::values_type::const_type;
4042550becSJunchao Zhang 
4142550becSJunchao Zhang using ConstMatRowMapKokkosViewHost   = KokkosCsrGraphHost::row_map_type::const_type;
4242550becSJunchao Zhang using ConstMatColIdxKokkosViewHost   = KokkosCsrGraphHost::entries_type::const_type;
4342550becSJunchao Zhang using ConstMatScalarKokkosViewHost   = KokkosCsrMatrixHost::values_type::const_type;
4442550becSJunchao Zhang 
4542550becSJunchao Zhang using MatRowMapKokkosDualView        = Kokkos::DualView<MatRowMapType*>;
4642550becSJunchao Zhang using MatColIdxKokkosDualView        = Kokkos::DualView<MatColIdxType*>;
4742550becSJunchao Zhang using MatScalarKokkosDualView        = Kokkos::DualView<MatScalarType*>;
4842550becSJunchao Zhang 
4942550becSJunchao Zhang using KernelHandle                   = KokkosKernels::Experimental::KokkosKernelsHandle<MatRowMapType,MatColIdxType,MatScalarType,DefaultExecutionSpace,DefaultMemorySpace,DefaultMemorySpace>;
5042550becSJunchao Zhang 
5142550becSJunchao Zhang using KokkosTeamMemberType           = Kokkos::TeamPolicy<DefaultExecutionSpace>::member_type;
5242550becSJunchao Zhang 
5342550becSJunchao Zhang /* For mat->spptr of a factorized matrix */
5442550becSJunchao Zhang struct Mat_SeqAIJKokkosTriFactors {
5542550becSJunchao Zhang   MatRowMapKokkosView       iL_d,iU_d,iLt_d,iUt_d; /* rowmap for L, U, L^t, U^t of A=LU */
5642550becSJunchao Zhang   MatColIdxKokkosView       jL_d,jU_d,jLt_d,jUt_d; /* column ids */
5742550becSJunchao Zhang   MatScalarKokkosView       aL_d,aU_d,aLt_d,aUt_d; /* matrix values */
5842550becSJunchao Zhang   KernelHandle              kh,khL,khU,khLt,khUt;  /* Kernel handles for A, L, U, L^t, U^t */
5942550becSJunchao Zhang   PetscBool                 transpose_updated;     /* Are L^T, U^T updated wrt L, U*/
6042550becSJunchao Zhang   PetscBool                 sptrsv_symbolic_completed; /* Have we completed the symbolic solve for L and U */
6142550becSJunchao Zhang   PetscScalarKokkosView     workVector;
6242550becSJunchao Zhang 
6342550becSJunchao Zhang   Mat_SeqAIJKokkosTriFactors(PetscInt n)
6442550becSJunchao Zhang     : transpose_updated(PETSC_FALSE),sptrsv_symbolic_completed(PETSC_FALSE),workVector("workVector",n) {}
6542550becSJunchao Zhang 
6642550becSJunchao Zhang   ~Mat_SeqAIJKokkosTriFactors() {Destroy();}
6742550becSJunchao Zhang 
6842550becSJunchao Zhang   void Destroy() {
6942550becSJunchao Zhang     kh.destroy_spiluk_handle();
7042550becSJunchao Zhang     khL.destroy_sptrsv_handle();
7142550becSJunchao Zhang     khU.destroy_sptrsv_handle();
7242550becSJunchao Zhang     khLt.destroy_sptrsv_handle();
7342550becSJunchao Zhang     khUt.destroy_sptrsv_handle();
7442550becSJunchao Zhang     transpose_updated = sptrsv_symbolic_completed = PETSC_FALSE;
7542550becSJunchao Zhang   }
7642550becSJunchao Zhang };
7742550becSJunchao Zhang 
7842550becSJunchao Zhang /* For mat->spptr of a regular matrix */
7942550becSJunchao Zhang struct Mat_SeqAIJKokkos {
8042550becSJunchao Zhang   MatRowMapKokkosDualView    i_dual;
8142550becSJunchao Zhang   MatColIdxKokkosDualView    j_dual;
8242550becSJunchao Zhang   MatScalarKokkosDualView    a_dual;
8342550becSJunchao Zhang 
84*f78ce678SMark Adams   MatRowMapKokkosDualView    diag_dual; /* Diagonal pointer, built on demand */
85*f78ce678SMark Adams 
8642550becSJunchao Zhang   KokkosCsrMatrix            csrmat; /* The CSR matrix, used to call KK functions */
8742550becSJunchao Zhang   PetscObjectState           nonzerostate; /* State of the nonzero pattern (graph) on device */
8842550becSJunchao Zhang 
8942550becSJunchao Zhang   KokkosCsrMatrix            csrmatT,csrmatH; /* Transpose and Hermitian of the matrix (built on demand) */
9042550becSJunchao Zhang   PetscBool                  transpose_updated,hermitian_updated; /* Are At, Ah updated wrt the matrix? */
9142550becSJunchao Zhang 
92394ed5ebSJunchao Zhang   /* COO stuff */
93394ed5ebSJunchao 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 */
94394ed5ebSJunchao Zhang   PetscCountKokkosView       perm_d; /* The permutation array in sorting (i,j) by row and then by col */
9542550becSJunchao Zhang 
96300d22a6SJunchao Zhang   Kokkos::View<PetscInt*>         i_uncompressed_d;
97300d22a6SJunchao Zhang   Kokkos::View<PetscInt*>         colmap_d; // ugh, this is a parallel construct
9842550becSJunchao Zhang   Kokkos::View<SplitCSRMat,DefaultMemorySpace> device_mat_d;
99300d22a6SJunchao Zhang   Kokkos::View<PetscInt*>         diag_d; // factorizations
10042550becSJunchao Zhang 
10142550becSJunchao Zhang   /* Construct a nrows by ncols matrix with nnz nonzeros from the given (i,j,a) on host. Caller also specifies a nonzero state */
10242550becSJunchao Zhang   Mat_SeqAIJKokkos(PetscInt nrows,PetscInt ncols,PetscInt nnz,const MatRowMapType *i,MatColIdxType *j,MatScalarType *a,PetscObjectState nzstate,PetscBool copyValues=PETSC_TRUE)
10342550becSJunchao Zhang   {
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. */
125ff751488SJunchao Zhang   Mat_SeqAIJKokkos(const KokkosCsrMatrix& csr) : csrmat(csr) /* Shallow-copy csr's views to csrmat */
12642550becSJunchao Zhang   {
12742550becSJunchao Zhang     auto a_d = csr.values;
12842550becSJunchao Zhang     /* Get a non-const version since I don't want to deal with DualView<const T*>, which is not well defined */
12942550becSJunchao Zhang     MatRowMapKokkosView i_d(const_cast<MatRowMapType*>(csr.graph.row_map.data()),csr.graph.row_map.extent(0));
13042550becSJunchao Zhang     auto j_d = csr.graph.entries;
13142550becSJunchao Zhang     auto a_h = Kokkos::create_mirror_view(Kokkos::HostSpace(),a_d);
13242550becSJunchao Zhang     auto i_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),i_d);
13342550becSJunchao Zhang     auto j_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),j_d);
13442550becSJunchao Zhang 
13542550becSJunchao Zhang     a_dual = MatScalarKokkosDualView(a_d,a_h);
13642550becSJunchao Zhang     a_dual.modify_device(); /* since we did not copy a_d to a_h, we mark device has the latest data */
13742550becSJunchao Zhang     i_dual = MatRowMapKokkosDualView(i_d,i_h);
13842550becSJunchao Zhang     j_dual = MatColIdxKokkosDualView(j_d,j_h);
13942550becSJunchao Zhang     Init();
14042550becSJunchao Zhang   }
14142550becSJunchao Zhang 
14242550becSJunchao Zhang   Mat_SeqAIJKokkos(PetscInt nrows,PetscInt ncols,PetscInt nnz,
14342550becSJunchao Zhang                    MatRowMapKokkosDualView& i,MatColIdxKokkosDualView& j,MatScalarKokkosDualView a)
14442550becSJunchao Zhang     :i_dual(i),j_dual(j),a_dual(a)
14542550becSJunchao Zhang   {
14642550becSJunchao Zhang     csrmat = KokkosCsrMatrix("csrmat",nrows,ncols,nnz,a.view_device(),i.view_device(),j.view_device());
14742550becSJunchao Zhang     Init();
14842550becSJunchao Zhang   }
14942550becSJunchao Zhang 
15042550becSJunchao Zhang   MatScalarType* a_host_data() {return a_dual.view_host().data();}
15142550becSJunchao Zhang   MatRowMapType* i_host_data() {return i_dual.view_host().data();}
15242550becSJunchao Zhang   MatColIdxType* j_host_data() {return j_dual.view_host().data();}
15342550becSJunchao Zhang 
15442550becSJunchao Zhang   MatScalarType* a_device_data() {return a_dual.view_device().data();}
15542550becSJunchao Zhang   MatRowMapType* i_device_data() {return i_dual.view_device().data();}
15642550becSJunchao Zhang   MatColIdxType* j_device_data() {return j_dual.view_device().data();}
15742550becSJunchao Zhang 
15842550becSJunchao Zhang   MatColIdxType  nrows() {return csrmat.numRows();}
15942550becSJunchao Zhang   MatColIdxType  ncols() {return csrmat.numCols();}
16042550becSJunchao Zhang   MatRowMapType  nnz()   {return csrmat.nnz();}
16142550becSJunchao Zhang 
16242550becSJunchao Zhang   /* Change the csrmat size to n */
16342550becSJunchao Zhang   void SetColSize(MatColIdxType n) {csrmat = KokkosCsrMatrix("csrmat",n,a_dual.view_device(),csrmat.graph);}
16442550becSJunchao Zhang 
165394ed5ebSJunchao Zhang   void SetUpCOO(const Mat_SeqAIJ *aij) {
166394ed5ebSJunchao Zhang     jmap_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(),PetscCountKokkosViewHost(aij->jmap,aij->nz+1));
167394ed5ebSJunchao Zhang     perm_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(),PetscCountKokkosViewHost(aij->perm,aij->Atot));
16842550becSJunchao Zhang   }
16942550becSJunchao Zhang 
170*f78ce678SMark Adams   void SetDiagonal(const MatRowMapType *diag)
171*f78ce678SMark Adams   {
172*f78ce678SMark Adams     MatRowMapKokkosViewHost diag_h(const_cast<MatRowMapType*>(diag),nrows());
173*f78ce678SMark Adams     auto diag_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(),diag_h);
174*f78ce678SMark Adams     diag_dual = MatRowMapKokkosDualView(diag_d,diag_h);
175*f78ce678SMark Adams   }
176*f78ce678SMark Adams 
17742550becSJunchao Zhang   /* Shared init stuff */
17842550becSJunchao Zhang   void Init(void)
17942550becSJunchao Zhang   {
18042550becSJunchao Zhang     transpose_updated = hermitian_updated = PETSC_FALSE;
18142550becSJunchao Zhang     nonzerostate      = 0;
18242550becSJunchao Zhang   }
18342550becSJunchao Zhang 
18442550becSJunchao Zhang   PetscErrorCode DestroyMatTranspose(void)
18542550becSJunchao Zhang   {
18642550becSJunchao Zhang     PetscFunctionBegin;
18742550becSJunchao Zhang     csrmatT = KokkosCsrMatrix(); /* Overwrite with empty matrices */
18842550becSJunchao Zhang     csrmatH = KokkosCsrMatrix();
18942550becSJunchao Zhang     PetscFunctionReturn(0);
19042550becSJunchao Zhang   }
19142550becSJunchao Zhang };
19242550becSJunchao Zhang 
19342550becSJunchao Zhang struct MatProductData_SeqAIJKokkos {
19442550becSJunchao Zhang   KernelHandle kh;
19542550becSJunchao Zhang   PetscBool    reusesym;
19642550becSJunchao Zhang   MatProductData_SeqAIJKokkos() : reusesym(PETSC_FALSE){}
19742550becSJunchao Zhang };
19842550becSJunchao Zhang 
19942550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatSetSeqAIJKokkosWithCSRMatrix(Mat,Mat_SeqAIJKokkos*);
20042550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatCreateSeqAIJKokkosWithCSRMatrix(MPI_Comm,Mat_SeqAIJKokkos*,Mat*);
20142550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJKokkosMergeMats(Mat,Mat,MatReuse,Mat*);
20242550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJKokkosSyncDevice(Mat);
20342550becSJunchao Zhang PETSC_INTERN PetscErrorCode PrintCsrMatrix(const KokkosCsrMatrix& csrmat);
20442550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat,MatType,MatReuse,Mat*);
20542550becSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJKokkosModifyDevice(Mat);
206394ed5ebSJunchao Zhang 
207394ed5ebSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJGetKokkosView(Mat,MatScalarKokkosView*);
208394ed5ebSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJRestoreKokkosView(Mat,MatScalarKokkosView*);
209394ed5ebSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJGetKokkosViewWrite(Mat,MatScalarKokkosView*);
210394ed5ebSJunchao Zhang PETSC_INTERN PetscErrorCode MatSeqAIJRestoreKokkosViewWrite(Mat,MatScalarKokkosView*);
21142550becSJunchao Zhang #endif
212