Actual source code: aijkok.hpp
1: #pragma once
2: #include <petsc_kokkos.hpp>
3: #include <petscmat_kokkos.hpp>
4: #include <petsc/private/kokkosimpl.hpp>
5: #include <../src/mat/impls/aij/seq/aij.h>
6: #include <KokkosSparse_CrsMatrix.hpp>
7: #include <KokkosSparse_spiluk.hpp>
8: #include <string>
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
18: using MatRowMapType = PetscInt;
19: using MatColIdxType = PetscInt;
20: using MatScalarType = PetscScalar;
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;
27: using KokkosCsrGraph = KokkosCsrGraphType<DefaultMemorySpace>;
28: using KokkosCsrGraphHost = KokkosCsrGraphType<HostMirrorMemorySpace>;
30: using KokkosCsrMatrix = KokkosCsrMatrixType<DefaultMemorySpace>;
31: using KokkosCsrMatrixHost = KokkosCsrMatrixType<HostMirrorMemorySpace>;
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;
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;
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;
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;
49: using MatRowMapKokkosDualView = Kokkos::DualView<MatRowMapType *>;
50: using MatColIdxKokkosDualView = Kokkos::DualView<MatColIdxType *>;
51: using MatScalarKokkosDualView = Kokkos::DualView<MatScalarType *>;
53: using KernelHandle = KokkosKernels::Experimental::KokkosKernelsHandle<MatRowMapType, MatColIdxType, MatScalarType, DefaultExecutionSpace, DefaultMemorySpace, DefaultMemorySpace>;
55: using KokkosTeamMemberType = Kokkos::TeamPolicy<DefaultExecutionSpace>::member_type;
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 ILU factorization of A, and TRSV of L, U, L^t, U^t */
63: PetscScalarKokkosView workVector;
64: PetscBool transpose_updated; /* Are L^T, U^T updated wrt L, U*/
65: PetscBool sptrsv_symbolic_completed; /* Have we completed the symbolic solve for L and U */
67: MatRowMapKokkosViewHost iL_h, iU_h, iLt_h, iUt_h; // temp. buffers when we do factorization with PETSc on host. We copy L, U to these buffers and then copy to device
68: MatColIdxKokkosViewHost jL_h, jU_h, jLt_h, jUt_h;
69: MatScalarKokkosViewHost aL_h, aU_h, aLt_h, aUt_h, D_h; // D is for LDLT factorization
70: MatScalarKokkosView D_d;
71: Mat L, U, Lt, Ut; // MATSEQAIJ on host if needed. Their arrays are alias to (iL_h, jL_h, aL_h), (iU_h, jU_h, aU_h) and their transpose.
72: // MatTranspose() on host might be faster than KK's csr transpose on device.
74: PetscIntKokkosView rowperm, colperm; // row permutation and column permutation
76: Mat_SeqAIJKokkosTriFactors(PetscInt n) : workVector("workVector", n)
77: {
78: L = U = Lt = Ut = nullptr;
79: transpose_updated = sptrsv_symbolic_completed = PETSC_FALSE;
80: }
82: ~Mat_SeqAIJKokkosTriFactors() { Destroy(); }
84: void Destroy()
85: {
86: PetscFunctionBeginUser;
87: kh.destroy_spiluk_handle();
88: khL.destroy_sptrsv_handle();
89: khU.destroy_sptrsv_handle();
90: khLt.destroy_sptrsv_handle();
91: khUt.destroy_sptrsv_handle();
92: PetscCallVoid(MatDestroy(&L));
93: PetscCallVoid(MatDestroy(&U));
94: PetscCallVoid(MatDestroy(&Lt));
95: PetscCallVoid(MatDestroy(&Ut));
96: transpose_updated = sptrsv_symbolic_completed = PETSC_FALSE;
97: PetscFunctionReturnVoid();
98: }
99: };
101: /* For mat->spptr of a regular matrix */
102: struct Mat_SeqAIJKokkos {
103: MatRowMapKokkosDualView i_dual;
104: MatColIdxKokkosDualView j_dual;
105: MatScalarKokkosDualView a_dual;
106: PetscBool host_aij_allocated_by_kokkos = PETSC_FALSE; /* Are host views of a, i, j in the duals allocated by Kokkos? */
108: MatRowMapKokkosDualView diag_dual; /* Diagonal pointer, built on demand */
110: KokkosCsrMatrix csrmat; /* The CSR matrix, used to call KK functions */
111: PetscObjectState nonzerostate; /* State of the nonzero pattern (graph) on device */
113: KokkosCsrMatrix csrmatT, csrmatH; /* Transpose and Hermitian of the matrix (built on demand) */
114: PetscBool transpose_updated, hermitian_updated; /* Are At, Ah updated wrt the matrix? */
115: MatRowMapKokkosView transpose_perm; // A permutation array making Ta(i) = Aa(perm(i)), where T = A^t
117: /* Construct a nrows by ncols matrix with given aseq on host. Caller also specifies a nonzero state */
118: Mat_SeqAIJKokkos(Mat A, PetscInt nrows, PetscInt ncols, Mat_SeqAIJ *aseq, PetscObjectState nzstate, PetscBool copyValues = PETSC_TRUE)
119: {
120: auto exec = PetscGetKokkosExecutionSpace();
122: MatScalarKokkosViewHost a_h(aseq->a, aseq->nz);
123: MatRowMapKokkosViewHost i_h(const_cast<MatRowMapType *>(aseq->i), nrows + 1);
124: MatColIdxKokkosViewHost j_h(aseq->j, aseq->nz);
126: auto a_d = Kokkos::create_mirror_view(Kokkos::WithoutInitializing, exec, a_h);
127: auto i_d = Kokkos::create_mirror_view_and_copy(exec, i_h);
128: auto j_d = Kokkos::create_mirror_view_and_copy(exec, j_h);
129: a_dual = MatScalarKokkosDualView(a_d, a_h);
130: i_dual = MatRowMapKokkosDualView(i_d, i_h);
131: j_dual = MatColIdxKokkosDualView(j_d, j_h);
132: PetscCallVoid(SetDiagonalMarkers(A));
134: a_dual.modify_host(); /* Since caller provided values on host */
135: if (copyValues) (void)KokkosDualViewSyncDevice(a_dual, exec);
137: csrmat = KokkosCsrMatrix("csrmat", ncols, a_d, KokkosCsrGraph(j_d, i_d));
138: Init(nzstate);
139: }
141: /* Construct with a KokkosCsrMatrix. For performance, only i, j are copied to host, but not the matrix values. */
142: Mat_SeqAIJKokkos(const KokkosCsrMatrix &csr) : csrmat(csr) /* Shallow-copy csr's views to csrmat */
143: {
144: auto a_d = csr.values;
145: /* Get a non-const version since I don't want to deal with DualView<const T*>, which is not well defined */
146: MatRowMapKokkosView i_d(const_cast<MatRowMapType *>(csr.graph.row_map.data()), csr.graph.row_map.extent(0));
147: auto j_d = csr.graph.entries;
148: auto a_h = Kokkos::create_mirror_view(Kokkos::WithoutInitializing, HostMirrorMemorySpace(), a_d);
149: auto i_h = Kokkos::create_mirror_view_and_copy(HostMirrorMemorySpace(), i_d);
150: auto j_h = Kokkos::create_mirror_view_and_copy(HostMirrorMemorySpace(), j_d);
152: // MatAssemblyEnd_SeqAIJKokkos() sets diag_dual
153: a_dual = MatScalarKokkosDualView(a_d, a_h);
154: a_dual.modify_device(); /* since we did not copy a_d to a_h, we mark device has the latest data */
155: i_dual = MatRowMapKokkosDualView(i_d, i_h);
156: j_dual = MatColIdxKokkosDualView(j_d, j_h);
157: host_aij_allocated_by_kokkos = PETSC_TRUE; /* That means after deleting aijkok, one shouldn't access aijseq->{a,i,j} anymore! */
158: Init();
159: }
161: // Don't use DualView argument types as we want to be sure that a,i,j on host are allocated by Mat_SeqAIJKokkos itself (vs. by users)
162: Mat_SeqAIJKokkos(PetscInt nrows, PetscInt ncols, PetscInt nnz, const MatRowMapKokkosView &i_d, const MatColIdxKokkosView &j_d, const MatScalarKokkosView &a_d) : Mat_SeqAIJKokkos(KokkosCsrMatrix("csrmat", nrows, ncols, nnz, a_d, i_d, j_d)) { }
164: MatScalarType *a_host_data() { return a_dual.view_host().data(); }
165: MatRowMapType *i_host_data() { return i_dual.view_host().data(); }
166: MatColIdxType *j_host_data() { return j_dual.view_host().data(); }
168: MatScalarType *a_device_data() { return a_dual.view_device().data(); }
169: MatRowMapType *i_device_data() { return i_dual.view_device().data(); }
170: MatColIdxType *j_device_data() { return j_dual.view_device().data(); }
172: MatColIdxType nrows() { return csrmat.numRows(); }
173: MatColIdxType ncols() { return csrmat.numCols(); }
174: MatRowMapType nnz() { return csrmat.nnz(); }
176: /* Change the csrmat size to n */
177: void SetColSize(MatColIdxType n) { csrmat = KokkosCsrMatrix("csrmat", n, a_dual.view_device(), csrmat.graph); }
179: void SetDiagonal(const MatRowMapType *diag)
180: {
181: MatRowMapKokkosViewHost diag_h(const_cast<MatRowMapType *>(diag), nrows());
182: auto diag_d = Kokkos::create_mirror_view_and_copy(DefaultMemorySpace(), diag_h);
183: diag_dual = MatRowMapKokkosDualView(diag_d, diag_h);
184: }
186: /*
187: Find the diagonal markers of A on the device. The caller must set i_dual and j_dual first.
188: A row i without a diagonal entry gets the marker Ai[i + 1].
189: This routine fills only the device side of diag_dual and sets diagDense.
190: MatGetDiagonalMarkers_SeqAIJ() builds the host markers when host code needs them.
191: */
192: PetscErrorCode SetDiagonalMarkers(Mat A)
193: {
194: Mat_SeqAIJ *aseq = static_cast<Mat_SeqAIJ *>(A->data);
195: const PetscInt m = A->rmap->n;
196: PetscInt nmissing;
197: const auto &Ai = i_dual.view_device();
198: const auto &Aj = j_dual.view_device();
199: MatRowMapKokkosView diag_d(NoInit("diag"), m);
201: PetscFunctionBegin;
202: PetscCallCXX(Kokkos::parallel_reduce(
203: Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, m),
204: KOKKOS_LAMBDA(const PetscInt i, PetscInt &lmissing) {
205: MatRowMapType d = Ai(i + 1);
207: for (MatRowMapType k = Ai(i); k < Ai(i + 1); k++) {
208: if (Aj(k) == i) {
209: d = k;
210: break;
211: }
212: }
213: diag_d(i) = d;
214: if (d == Ai(i + 1)) lmissing++;
215: },
216: nmissing));
217: auto diag_h = Kokkos::create_mirror_view(Kokkos::WithoutInitializing, HostMirrorMemorySpace(), diag_d);
218: diag_dual = MatRowMapKokkosDualView(diag_d, diag_h);
219: diag_dual.modify_device();
221: aseq->diagDense = nmissing == 0 ? PETSC_TRUE : PETSC_FALSE;
222: // MatGetDiagonalMarkers_SeqAIJ() must still rebuild an aseq->diag from an earlier nonzero pattern
223: if (aseq->diag == NULL) aseq->diagNonzeroState = A->nonzerostate;
224: PetscFunctionReturn(PETSC_SUCCESS);
225: }
227: /* Shared init stuff */
228: void Init(PetscObjectState nzstate = 0)
229: {
230: nonzerostate = nzstate;
231: transpose_updated = PETSC_FALSE;
232: hermitian_updated = PETSC_FALSE;
233: }
235: PetscErrorCode DestroyMatTranspose(void)
236: {
237: PetscFunctionBegin;
238: csrmatT = KokkosCsrMatrix(); /* Overwrite with empty matrices */
239: csrmatH = KokkosCsrMatrix();
240: PetscFunctionReturn(PETSC_SUCCESS);
241: }
242: };
244: struct MatProductCtx_SeqAIJKokkos {
245: KernelHandle kh;
246: PetscBool reusesym;
247: MatProductCtx_SeqAIJKokkos() : reusesym(PETSC_FALSE) { }
248: };
250: PETSC_INTERN PetscErrorCode MatSetSeqAIJKokkosWithCSRMatrix(Mat, Mat_SeqAIJKokkos *);
251: PETSC_INTERN PetscErrorCode MatCreateSeqAIJKokkosWithCSRMatrix(MPI_Comm, Mat_SeqAIJKokkos *, Mat *);
252: PETSC_INTERN PetscErrorCode MatSeqAIJKokkosMergeMats(Mat, Mat, MatReuse, Mat *);
253: PETSC_INTERN PetscErrorCode MatSeqAIJKokkosGetKokkosCsrMatrix(Mat, KokkosCsrMatrix *);
254: PETSC_INTERN PetscErrorCode MatCreateSeqAIJKokkosWithKokkosCsrMatrix(MPI_Comm, KokkosCsrMatrix, Mat *);
255: PETSC_INTERN PetscErrorCode PrintCsrMatrix(const KokkosCsrMatrix &csrmat);
256: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *);
257: PETSC_INTERN PetscErrorCode MatSeqAIJKokkosModifyDevice(Mat);
258: PETSC_INTERN PetscErrorCode MatSeqAIJKokkosGenerateTranspose_Private(Mat, KokkosCsrMatrix *);
259: PETSC_INTERN PetscErrorCode MatSeqAIJKokkosGetFrobeniusSquared_Private(Mat, PetscReal *);
260: PETSC_INTERN PetscErrorCode MatSeqAIJKokkosGetRowAbsSums_Private(Mat, Mat, PetscScalarKokkosView &);
261: PETSC_INTERN PetscErrorCode MatGetRowSumAbs_AIJKokkos(Mat, Vec);
262: PETSC_INTERN PetscErrorCode MatSeqAIJKokkosGetMaxRowAbsSum_Private(Mat, Mat, PetscReal *);
263: PETSC_INTERN PetscErrorCode MatSeqAIJKokkosGetColumnAbsSums_Private(Mat, PetscScalarKokkosView &);
264: PETSC_INTERN PetscErrorCode MatBindToCPU_SeqAIJKokkos(Mat, PetscBool);
266: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode MatSeqAIJKokkosSyncDevice(Mat);
267: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJKokkos(Mat, const PetscIntKokkosView &, const PetscIntKokkosView &, const PetscIntKokkosView &, PetscScalarKokkosView &, PetscScalarKokkosView &);