Actual source code: ex302k.kokkos.cxx
1: static char help[] = "Testing MatCreateSeqAIJKokkosWithKokkosViews(), various read/write Kokkos view APIs on the device, and MatConvert() from MATMPIAIJ to MATAIJKOKKOS.\n\n";
3: #include <petsc_kokkos.hpp>
4: #include <petscvec_kokkos.hpp>
5: #include <petscmat.h>
6: #include <petscmat_kokkos.hpp>
7: #include <Kokkos_Core.hpp>
8: #include <Kokkos_DualView.hpp>
10: using HostMirrorMemorySpace = Kokkos::DualView<PetscScalar *>::host_mirror_space::memory_space;
12: static PetscErrorCode CheckBlockTypes(Mat A, MatType type)
13: {
14: Mat Ad, Ao;
15: PetscBool same;
17: PetscFunctionBeginUser;
18: PetscCall(MatMPIAIJGetSeqAIJ(A, &Ad, &Ao, NULL));
19: PetscCall(PetscObjectTypeCompare((PetscObject)Ad, type, &same));
20: PetscCheck(same, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Diagonal block is not %s", type);
21: PetscCall(PetscObjectTypeCompare((PetscObject)Ao, type, &same));
22: PetscCheck(same, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Off-diagonal block is not %s", type);
23: PetscFunctionReturn(PETSC_SUCCESS);
24: }
26: // MatConvert() from MATMPIAIJ to MATAIJKOKKOS must retype the blocks of the new matrix and leave those of the source alone
27: static PetscErrorCode TestConvertFromHost(Mat A)
28: {
29: Mat host, kok;
30: PetscBool equal;
32: PetscFunctionBeginUser;
33: PetscCall(MatConvert(A, MATMPIAIJ, MAT_INITIAL_MATRIX, &host));
34: PetscCall(CheckBlockTypes(host, MATSEQAIJ));
36: PetscCall(MatConvert(host, MATAIJKOKKOS, MAT_INITIAL_MATRIX, &kok));
37: PetscCall(CheckBlockTypes(kok, MATSEQAIJKOKKOS));
38: PetscCall(CheckBlockTypes(host, MATSEQAIJ));
39: PetscCall(MatMultEqual(host, kok, 3, &equal));
40: PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "MatConvert() with MAT_INITIAL_MATRIX gave wrong values");
42: PetscCall(MatScale(host, 2.0));
43: PetscCall(MatConvert(host, MATAIJKOKKOS, MAT_REUSE_MATRIX, &kok));
44: PetscCall(CheckBlockTypes(kok, MATSEQAIJKOKKOS));
45: PetscCall(CheckBlockTypes(host, MATSEQAIJ));
46: PetscCall(MatMultEqual(host, kok, 3, &equal));
47: PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "MatConvert() with MAT_REUSE_MATRIX gave wrong values");
49: PetscCall(MatDestroy(&kok));
50: PetscCall(MatDestroy(&host));
51: PetscFunctionReturn(PETSC_SUCCESS);
52: }
54: int main(int argc, char **argv)
55: {
56: Mat A, B, BT;
57: PetscInt i, j, column, M, N, m, n, m_ab, n_ab;
58: PetscInt *di, *dj, *oi, *oj, nd;
59: const PetscInt *garray;
60: PetscInt *garray_h;
61: PetscScalar *oa, *da;
62: PetscScalar value;
63: PetscRandom rctx;
64: PetscBool equal, done;
65: Mat AA, AB;
66: PetscMPIInt size, rank;
68: // ~~~~~~~~~~~~~~~~~~~~~
69: // This test shows the routines needed to build a kokkos matrix without preallocation
70: // on the host
71: // ~~~~~~~~~~~~~~~~~~~~~
73: PetscFunctionBeginUser;
74: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
75: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
76: PetscCheck(size > 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must run with 2 or more processes");
77: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
79: /* Create a mpiaij matrix for checking */
80: PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, 5, 5, PETSC_DECIDE, PETSC_DECIDE, 0, NULL, 0, NULL, &A));
81: PetscCall(MatSetFromOptions(A));
82: PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_FALSE));
83: PetscCall(MatSetUp(A));
84: PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rctx));
85: PetscCall(PetscRandomSetFromOptions(rctx));
87: for (i = 5 * rank; i < 5 * rank + 5; i++) {
88: for (j = 0; j < 5 * size; j++) {
89: PetscCall(PetscRandomGetValue(rctx, &value));
90: column = (PetscInt)(5 * size * PetscRealPart(value));
92: // rank 0 has no off-process entries
93: if (rank == 0 && (column < i || column >= i)) column = i;
95: PetscCall(PetscRandomGetValue(rctx, &value));
96: PetscCall(MatSetValues(A, 1, &i, 1, &column, &value, INSERT_VALUES));
97: }
98: }
99: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
100: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
102: PetscCall(MatGetSize(A, &M, &N));
103: PetscCall(MatGetLocalSize(A, &m, &n));
104: PetscCall(MatMPIAIJGetSeqAIJ(A, &AA, &AB, &garray));
105: PetscCall(MatGetRowIJ(AA, 0, PETSC_FALSE, PETSC_FALSE, &nd, (const PetscInt **)&di, (const PetscInt **)&dj, &done));
106: PetscCall(MatSeqAIJGetArray(AA, &da));
107: PetscCall(MatGetRowIJ(AB, 0, PETSC_FALSE, PETSC_FALSE, &nd, (const PetscInt **)&oi, (const PetscInt **)&oj, &done));
108: PetscCall(MatSeqAIJGetArray(AB, &oa));
109: PetscCall(MatGetSize(AB, &m_ab, &n_ab));
111: Mat output_mat_local, output_mat_nonlocal;
112: // Be careful about scope given the kokkos memory reference counts
113: {
114: // Local
115: Kokkos::View<PetscScalar *> a_local_d;
116: Kokkos::View<PetscInt *> i_local_d;
117: Kokkos::View<PetscInt *> j_local_d;
119: // Nonlocal
120: Kokkos::View<PetscScalar *> a_nonlocal_d;
121: Kokkos::View<PetscInt *> i_nonlocal_d;
122: Kokkos::View<PetscInt *> j_nonlocal_d;
124: // Create device memory
125: PetscCallCXX(a_local_d = Kokkos::View<PetscScalar *>("a_local_d", di[5]));
126: PetscCallCXX(i_local_d = Kokkos::View<PetscInt *>("i_local_d", m + 1));
127: PetscCallCXX(j_local_d = Kokkos::View<PetscInt *>("j_local_d", di[5]));
129: // Create non-local device memory
130: PetscCallCXX(a_nonlocal_d = Kokkos::View<PetscScalar *>("a_nonlocal_d", oi[5]));
131: PetscCallCXX(i_nonlocal_d = Kokkos::View<PetscInt *>("i_nonlocal_d", m + 1));
132: PetscCallCXX(j_nonlocal_d = Kokkos::View<PetscInt *>("j_nonlocal_d", oi[5]));
134: // ~~~~~~~~~~~~~~~~~~~~~
135: // Could fill the aij on the device - we're just going to test
136: // by copying in the existing host values
137: // ~~~~~~~~~~~~~~~~~~~~~
138: Kokkos::View<PetscScalar *, HostMirrorMemorySpace> a_local_h;
139: Kokkos::View<PetscInt *, HostMirrorMemorySpace> i_local_h;
140: Kokkos::View<PetscInt *, HostMirrorMemorySpace> j_local_h;
141: Kokkos::View<PetscScalar *, HostMirrorMemorySpace> a_nonlocal_h;
142: Kokkos::View<PetscInt *, HostMirrorMemorySpace> i_nonlocal_h;
143: Kokkos::View<PetscInt *, HostMirrorMemorySpace> j_nonlocal_h;
145: PetscCallCXX(a_local_h = Kokkos::View<PetscScalar *, HostMirrorMemorySpace>(da, di[5]));
146: PetscCallCXX(i_local_h = Kokkos::View<PetscInt *, HostMirrorMemorySpace>(di, m + 1));
147: PetscCallCXX(j_local_h = Kokkos::View<PetscInt *, HostMirrorMemorySpace>(dj, di[5]));
148: PetscCallCXX(a_nonlocal_h = Kokkos::View<PetscScalar *, HostMirrorMemorySpace>(oa, oi[5]));
149: PetscCallCXX(i_nonlocal_h = Kokkos::View<PetscInt *, HostMirrorMemorySpace>(oi, m + 1));
150: PetscCallCXX(j_nonlocal_h = Kokkos::View<PetscInt *, HostMirrorMemorySpace>(oj, oi[5]));
152: // Haven't specified an exec space so these will all be synchronous
153: // and finish without a need to call fence after
154: PetscCallCXX(Kokkos::deep_copy(a_local_d, a_local_h));
155: PetscCallCXX(Kokkos::deep_copy(i_local_d, i_local_h));
156: PetscCallCXX(Kokkos::deep_copy(j_local_d, j_local_h));
157: PetscCallCXX(Kokkos::deep_copy(a_nonlocal_d, a_nonlocal_h));
158: PetscCallCXX(Kokkos::deep_copy(i_nonlocal_d, i_nonlocal_h));
159: PetscCallCXX(Kokkos::deep_copy(j_nonlocal_d, j_nonlocal_h));
161: // The garray passed in has to be on the host, but it can be created
162: // on device and copied to the host
163: // We're just going to copy the existing host values here
164: PetscCall(PetscMalloc1(n_ab, &garray_h));
165: for (int i = 0; i < n_ab; i++) garray_h[i] = garray[i];
167: // ~~~~~~~~~~~~~~~~~~~~~
169: // ~~~~~~~~~~~~~~~~~
170: // Test MatCreateSeqAIJKokkosWithKokkosViews
171: // ~~~~~~~~~~~~~~~~~
173: // We can create our local diagonal block matrix directly on the device
174: PetscCall(MatCreateSeqAIJKokkosWithKokkosViews(PETSC_COMM_SELF, m, n, i_local_d, j_local_d, a_local_d, &output_mat_local));
176: // We can create our nonlocal diagonal block matrix directly on the device
177: PetscCall(MatCreateSeqAIJKokkosWithKokkosViews(PETSC_COMM_SELF, m, n_ab, i_nonlocal_d, j_nonlocal_d, a_nonlocal_d, &output_mat_nonlocal));
179: // Build our MPI matrix
180: // If we provide garray and output_mat_nonlocal with local indices and the compactified size
181: // almost nothing happens on the host
182: PetscCall(MatCreateMPIAIJWithSeqAIJ(PETSC_COMM_WORLD, M, N, output_mat_local, output_mat_nonlocal, garray_h, &B));
184: PetscCall(MatEqual(A, B, &equal));
185: PetscCall(MatRestoreRowIJ(AA, 0, PETSC_FALSE, PETSC_FALSE, &nd, (const PetscInt **)&di, (const PetscInt **)&dj, &done));
186: PetscCall(MatSeqAIJRestoreArray(AA, &da));
187: PetscCall(MatRestoreRowIJ(AB, 0, PETSC_FALSE, PETSC_FALSE, &nd, (const PetscInt **)&oi, (const PetscInt **)&oj, &done));
188: PetscCall(MatSeqAIJRestoreArray(AB, &oa));
189: PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Likely a bug in MatCreateSeqAIJKokkosWithKokkosViews()");
191: // ~~~~~~~~~~~~~~~~~
192: // Test MatSeqAIJ{Get,Restore}KokkosView{,Write}
193: // ~~~~~~~~~~~~~~~~~
195: PetscReal norm_before, norm_after;
196: PetscInt nnz_AA = 0;
197: PetscScalar host_sum = 0.0;
199: PetscCall(MatNorm(AA, NORM_FROBENIUS, &norm_before));
201: /* Const overload: read-only access. Sum the values and check it matches a host-side reference. */
202: {
203: Kokkos::View<const PetscScalar *> ro_view;
204: PetscCall(MatSeqAIJGetKokkosView(AA, &ro_view));
205: nnz_AA = (PetscInt)ro_view.extent(0);
207: PetscScalar device_sum = 0.0;
208: Kokkos::parallel_reduce("ex302k_sum", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, nnz_AA), KOKKOS_LAMBDA(const PetscInt k, PetscScalar &lsum) { lsum += ro_view(k); }, device_sum);
210: const PetscScalar *aa_host;
211: PetscCall(MatSeqAIJGetArrayRead(AA, &aa_host));
212: for (PetscInt k = 0; k < nnz_AA; k++) host_sum += aa_host[k];
213: PetscCall(MatSeqAIJRestoreArrayRead(AA, &aa_host));
215: PetscCheck(PetscAbsScalar(device_sum - host_sum) <= PETSC_SMALL * PetscAbsScalar(host_sum), PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSeqAIJGetKokkosView (const) device/host mismatch");
216: PetscCall(MatSeqAIJRestoreKokkosView(AA, &ro_view));
217: }
219: /* Snapshot the original values into a host Kokkos view so we can restore them later via the write-only overload. */
220: Kokkos::View<PetscScalar *, HostMirrorMemorySpace> saved_h("saved_h", nnz_AA);
221: {
222: const PetscScalar *aa_host;
223: PetscCall(MatSeqAIJGetArrayRead(AA, &aa_host));
224: for (PetscInt k = 0; k < nnz_AA; k++) saved_h(k) = aa_host[k];
225: PetscCall(MatSeqAIJRestoreArrayRead(AA, &aa_host));
226: }
228: /* Write-only overload: overwrite all entries with zero (no read of the prior contents). */
229: {
230: Kokkos::View<PetscScalar *> rw_view;
231: PetscCall(MatSeqAIJGetKokkosViewWrite(AA, &rw_view));
232: PetscCallCXX(Kokkos::deep_copy(rw_view, (PetscScalar)0.0));
233: PetscCall(MatSeqAIJRestoreKokkosViewWrite(AA, &rw_view));
234: }
235: {
236: PetscReal norm_zero;
237: PetscCall(MatNorm(AA, NORM_FROBENIUS, &norm_zero));
238: PetscCheck(norm_zero == 0.0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSeqAIJ{Get,Restore}KokkosViewWrite did not propagate device write");
239: }
241: /* Write-only overload again: restore the original values from the host snapshot. */
242: {
243: Kokkos::View<PetscScalar *> rw_view;
244: PetscCall(MatSeqAIJGetKokkosViewWrite(AA, &rw_view));
245: PetscCallCXX(Kokkos::deep_copy(rw_view, saved_h));
246: PetscCall(MatSeqAIJRestoreKokkosViewWrite(AA, &rw_view));
247: }
249: /* Read-write overload: scale by 2, then by 1/2 to undo. */
250: {
251: Kokkos::View<PetscScalar *> rw_view;
252: PetscCall(MatSeqAIJGetKokkosView(AA, &rw_view));
253: // It seems with SYCL, the user code has to use the same execution space as PETSc's. MatNorm() will sync the execution space before accessing data on host
254: Kokkos::parallel_for("ex302k_scale_up", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, nnz_AA), KOKKOS_LAMBDA(const PetscInt k) { rw_view(k) *= (PetscScalar)2.0; });
255: PetscCall(MatSeqAIJRestoreKokkosView(AA, &rw_view));
256: }
257: {
258: Kokkos::View<PetscScalar *> rw_view;
259: PetscCall(MatSeqAIJGetKokkosView(AA, &rw_view));
260: Kokkos::parallel_for("ex302k_scale_down", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, nnz_AA), KOKKOS_LAMBDA(const PetscInt k) { rw_view(k) *= (PetscScalar)0.5; });
261: PetscCall(MatSeqAIJRestoreKokkosView(AA, &rw_view));
262: }
264: PetscCall(MatNorm(AA, NORM_FROBENIUS, &norm_after));
265: PetscCheck(PetscAbsReal(norm_after - norm_before) <= PETSC_SMALL * norm_before, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSeqAIJ{Get,Restore}KokkosView round trip changed AA");
266: }
268: PetscCall(MatTranspose(B, MAT_INITIAL_MATRIX, &BT));
270: PetscCall(TestConvertFromHost(A));
272: /* Free spaces */
273: PetscCall(PetscRandomDestroy(&rctx));
274: PetscCall(MatDestroy(&A));
275: PetscCall(MatDestroy(&B));
276: PetscCall(MatDestroy(&BT));
277: PetscCall(PetscFinalize());
278: return 0;
279: }
281: /*TEST
282: build:
283: requires: kokkos_kernels
285: test:
286: nsize: 2
287: args: -mat_type aijkokkos
288: requires: kokkos_kernels
289: output_file: output/empty.out
291: TEST*/