Actual source code: ex302k.kokkos.cxx
1: static char help[] = "Testing MatCreateSeqAIJKokkosWithKokkosViews() and various read/write Kokkos view APIs on the device.\n\n";
3: #include <petsc_kokkos.hpp>
4: #include <petscvec_kokkos.hpp>
5: #include <petscdevice.h>
6: #include <petscmat.h>
7: #include <petscmat_kokkos.hpp>
8: #include <Kokkos_Core.hpp>
9: #include <Kokkos_DualView.hpp>
11: using HostMirrorMemorySpace = Kokkos::DualView<PetscScalar *>::host_mirror_space::memory_space;
13: int main(int argc, char **argv)
14: {
15: Mat A, B, BT;
16: PetscInt i, j, column, M, N, m, n, m_ab, n_ab;
17: PetscInt *di, *dj, *oi, *oj, nd;
18: const PetscInt *garray;
19: PetscInt *garray_h;
20: PetscScalar *oa, *da;
21: PetscScalar value;
22: PetscRandom rctx;
23: PetscBool equal, done;
24: Mat AA, AB;
25: PetscMPIInt size, rank;
27: // ~~~~~~~~~~~~~~~~~~~~~
28: // This test shows the routines needed to build a kokkos matrix without preallocation
29: // on the host
30: // ~~~~~~~~~~~~~~~~~~~~~
32: PetscFunctionBeginUser;
33: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
34: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
35: PetscCheck(size > 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must run with 2 or more processes");
36: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
38: /* Create a mpiaij matrix for checking */
39: PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, 5, 5, PETSC_DECIDE, PETSC_DECIDE, 0, NULL, 0, NULL, &A));
40: PetscCall(MatSetFromOptions(A));
41: PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_FALSE));
42: PetscCall(MatSetUp(A));
43: PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rctx));
44: PetscCall(PetscRandomSetFromOptions(rctx));
46: for (i = 5 * rank; i < 5 * rank + 5; i++) {
47: for (j = 0; j < 5 * size; j++) {
48: PetscCall(PetscRandomGetValue(rctx, &value));
49: column = (PetscInt)(5 * size * PetscRealPart(value));
51: // rank 0 has no off-process entries
52: if (rank == 0 && (column < i || column >= i)) column = i;
54: PetscCall(PetscRandomGetValue(rctx, &value));
55: PetscCall(MatSetValues(A, 1, &i, 1, &column, &value, INSERT_VALUES));
56: }
57: }
58: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
59: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
61: PetscCall(MatGetSize(A, &M, &N));
62: PetscCall(MatGetLocalSize(A, &m, &n));
63: PetscCall(MatMPIAIJGetSeqAIJ(A, &AA, &AB, &garray));
64: PetscCall(MatGetRowIJ(AA, 0, PETSC_FALSE, PETSC_FALSE, &nd, (const PetscInt **)&di, (const PetscInt **)&dj, &done));
65: PetscCall(MatSeqAIJGetArray(AA, &da));
66: PetscCall(MatGetRowIJ(AB, 0, PETSC_FALSE, PETSC_FALSE, &nd, (const PetscInt **)&oi, (const PetscInt **)&oj, &done));
67: PetscCall(MatSeqAIJGetArray(AB, &oa));
68: PetscCall(MatGetSize(AB, &m_ab, &n_ab));
70: Mat output_mat_local, output_mat_nonlocal;
71: // Be careful about scope given the kokkos memory reference counts
72: {
73: // Local
74: Kokkos::View<PetscScalar *> a_local_d;
75: Kokkos::View<PetscInt *> i_local_d;
76: Kokkos::View<PetscInt *> j_local_d;
78: // Nonlocal
79: Kokkos::View<PetscScalar *> a_nonlocal_d;
80: Kokkos::View<PetscInt *> i_nonlocal_d;
81: Kokkos::View<PetscInt *> j_nonlocal_d;
83: // Create device memory
84: PetscCallCXX(a_local_d = Kokkos::View<PetscScalar *>("a_local_d", di[5]));
85: PetscCallCXX(i_local_d = Kokkos::View<PetscInt *>("i_local_d", m + 1));
86: PetscCallCXX(j_local_d = Kokkos::View<PetscInt *>("j_local_d", di[5]));
88: // Create non-local device memory
89: PetscCallCXX(a_nonlocal_d = Kokkos::View<PetscScalar *>("a_nonlocal_d", oi[5]));
90: PetscCallCXX(i_nonlocal_d = Kokkos::View<PetscInt *>("i_nonlocal_d", m + 1));
91: PetscCallCXX(j_nonlocal_d = Kokkos::View<PetscInt *>("j_nonlocal_d", oi[5]));
93: // ~~~~~~~~~~~~~~~~~~~~~
94: // Could fill the aij on the device - we're just going to test
95: // by copying in the existing host values
96: // ~~~~~~~~~~~~~~~~~~~~~
97: Kokkos::View<PetscScalar *, HostMirrorMemorySpace> a_local_h;
98: Kokkos::View<PetscInt *, HostMirrorMemorySpace> i_local_h;
99: Kokkos::View<PetscInt *, HostMirrorMemorySpace> j_local_h;
100: Kokkos::View<PetscScalar *, HostMirrorMemorySpace> a_nonlocal_h;
101: Kokkos::View<PetscInt *, HostMirrorMemorySpace> i_nonlocal_h;
102: Kokkos::View<PetscInt *, HostMirrorMemorySpace> j_nonlocal_h;
104: PetscCallCXX(a_local_h = Kokkos::View<PetscScalar *, HostMirrorMemorySpace>(da, di[5]));
105: PetscCallCXX(i_local_h = Kokkos::View<PetscInt *, HostMirrorMemorySpace>(di, m + 1));
106: PetscCallCXX(j_local_h = Kokkos::View<PetscInt *, HostMirrorMemorySpace>(dj, di[5]));
107: PetscCallCXX(a_nonlocal_h = Kokkos::View<PetscScalar *, HostMirrorMemorySpace>(oa, oi[5]));
108: PetscCallCXX(i_nonlocal_h = Kokkos::View<PetscInt *, HostMirrorMemorySpace>(oi, m + 1));
109: PetscCallCXX(j_nonlocal_h = Kokkos::View<PetscInt *, HostMirrorMemorySpace>(oj, oi[5]));
111: // Haven't specified an exec space so these will all be synchronous
112: // and finish without a need to call fence after
113: PetscCallCXX(Kokkos::deep_copy(a_local_d, a_local_h));
114: PetscCallCXX(Kokkos::deep_copy(i_local_d, i_local_h));
115: PetscCallCXX(Kokkos::deep_copy(j_local_d, j_local_h));
116: PetscCallCXX(Kokkos::deep_copy(a_nonlocal_d, a_nonlocal_h));
117: PetscCallCXX(Kokkos::deep_copy(i_nonlocal_d, i_nonlocal_h));
118: PetscCallCXX(Kokkos::deep_copy(j_nonlocal_d, j_nonlocal_h));
120: // The garray passed in has to be on the host, but it can be created
121: // on device and copied to the host
122: // We're just going to copy the existing host values here
123: PetscCall(PetscMalloc1(n_ab, &garray_h));
124: for (int i = 0; i < n_ab; i++) garray_h[i] = garray[i];
126: // ~~~~~~~~~~~~~~~~~~~~~
128: // ~~~~~~~~~~~~~~~~~
129: // Test MatCreateSeqAIJKokkosWithKokkosViews
130: // ~~~~~~~~~~~~~~~~~
132: // We can create our local diagonal block matrix directly on the device
133: PetscCall(MatCreateSeqAIJKokkosWithKokkosViews(PETSC_COMM_SELF, m, n, i_local_d, j_local_d, a_local_d, &output_mat_local));
135: // We can create our nonlocal diagonal block matrix directly on the device
136: PetscCall(MatCreateSeqAIJKokkosWithKokkosViews(PETSC_COMM_SELF, m, n_ab, i_nonlocal_d, j_nonlocal_d, a_nonlocal_d, &output_mat_nonlocal));
138: // Build our MPI matrix
139: // If we provide garray and output_mat_nonlocal with local indices and the compactified size
140: // almost nothing happens on the host
141: PetscCall(MatCreateMPIAIJWithSeqAIJ(PETSC_COMM_WORLD, M, N, output_mat_local, output_mat_nonlocal, garray_h, &B));
143: PetscCall(MatEqual(A, B, &equal));
144: PetscCall(MatRestoreRowIJ(AA, 0, PETSC_FALSE, PETSC_FALSE, &nd, (const PetscInt **)&di, (const PetscInt **)&dj, &done));
145: PetscCall(MatSeqAIJRestoreArray(AA, &da));
146: PetscCall(MatRestoreRowIJ(AB, 0, PETSC_FALSE, PETSC_FALSE, &nd, (const PetscInt **)&oi, (const PetscInt **)&oj, &done));
147: PetscCall(MatSeqAIJRestoreArray(AB, &oa));
148: PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Likely a bug in MatCreateSeqAIJKokkosWithKokkosViews()");
150: // ~~~~~~~~~~~~~~~~~
151: // Test MatSeqAIJ{Get,Restore}KokkosView{,Write}
152: // ~~~~~~~~~~~~~~~~~
154: PetscReal norm_before, norm_after;
155: PetscInt nnz_AA = 0;
156: PetscScalar host_sum = 0.0;
158: PetscCall(MatNorm(AA, NORM_FROBENIUS, &norm_before));
160: /* Const overload: read-only access. Sum the values and check it matches a host-side reference. */
161: {
162: Kokkos::View<const PetscScalar *> ro_view;
163: PetscCall(MatSeqAIJGetKokkosView(AA, &ro_view));
164: nnz_AA = (PetscInt)ro_view.extent(0);
166: PetscScalar device_sum = 0.0;
167: Kokkos::parallel_reduce("ex302k_sum", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, nnz_AA), KOKKOS_LAMBDA(const PetscInt k, PetscScalar &lsum) { lsum += ro_view(k); }, device_sum);
169: const PetscScalar *aa_host;
170: PetscCall(MatSeqAIJGetArrayRead(AA, &aa_host));
171: for (PetscInt k = 0; k < nnz_AA; k++) host_sum += aa_host[k];
172: PetscCall(MatSeqAIJRestoreArrayRead(AA, &aa_host));
174: PetscCheck(PetscAbsScalar(device_sum - host_sum) <= PETSC_SMALL * PetscAbsScalar(host_sum), PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSeqAIJGetKokkosView (const) device/host mismatch");
175: PetscCall(MatSeqAIJRestoreKokkosView(AA, &ro_view));
176: }
178: /* Snapshot the original values into a host Kokkos view so we can restore them later via the write-only overload. */
179: Kokkos::View<PetscScalar *, HostMirrorMemorySpace> saved_h("saved_h", nnz_AA);
180: {
181: const PetscScalar *aa_host;
182: PetscCall(MatSeqAIJGetArrayRead(AA, &aa_host));
183: for (PetscInt k = 0; k < nnz_AA; k++) saved_h(k) = aa_host[k];
184: PetscCall(MatSeqAIJRestoreArrayRead(AA, &aa_host));
185: }
187: /* Write-only overload: overwrite all entries with zero (no read of the prior contents). */
188: {
189: Kokkos::View<PetscScalar *> rw_view;
190: PetscCall(MatSeqAIJGetKokkosViewWrite(AA, &rw_view));
191: PetscCallCXX(Kokkos::deep_copy(rw_view, (PetscScalar)0.0));
192: PetscCall(MatSeqAIJRestoreKokkosViewWrite(AA, &rw_view));
193: }
194: {
195: PetscReal norm_zero;
196: PetscCall(MatNorm(AA, NORM_FROBENIUS, &norm_zero));
197: PetscCheck(norm_zero == 0.0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSeqAIJ{Get,Restore}KokkosViewWrite did not propagate device write");
198: }
200: /* Write-only overload again: restore the original values from the host snapshot. */
201: {
202: Kokkos::View<PetscScalar *> rw_view;
203: PetscCall(MatSeqAIJGetKokkosViewWrite(AA, &rw_view));
204: PetscCallCXX(Kokkos::deep_copy(rw_view, saved_h));
205: PetscCall(MatSeqAIJRestoreKokkosViewWrite(AA, &rw_view));
206: }
208: /* Read-write overload: scale by 2, then by 1/2 to undo. */
209: {
210: Kokkos::View<PetscScalar *> rw_view;
211: PetscCall(MatSeqAIJGetKokkosView(AA, &rw_view));
212: // 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
213: Kokkos::parallel_for("ex302k_scale_up", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, nnz_AA), KOKKOS_LAMBDA(const PetscInt k) { rw_view(k) *= (PetscScalar)2.0; });
214: PetscCall(MatSeqAIJRestoreKokkosView(AA, &rw_view));
215: }
216: {
217: Kokkos::View<PetscScalar *> rw_view;
218: PetscCall(MatSeqAIJGetKokkosView(AA, &rw_view));
219: Kokkos::parallel_for("ex302k_scale_down", Kokkos::RangePolicy<>(PetscGetKokkosExecutionSpace(), 0, nnz_AA), KOKKOS_LAMBDA(const PetscInt k) { rw_view(k) *= (PetscScalar)0.5; });
220: PetscCall(MatSeqAIJRestoreKokkosView(AA, &rw_view));
221: }
223: PetscCall(MatNorm(AA, NORM_FROBENIUS, &norm_after));
224: PetscCheck(PetscAbsReal(norm_after - norm_before) <= PETSC_SMALL * norm_before, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatSeqAIJ{Get,Restore}KokkosView round trip changed AA");
225: }
227: PetscCall(MatTranspose(B, MAT_INITIAL_MATRIX, &BT));
229: /* Free spaces */
230: PetscCall(PetscRandomDestroy(&rctx));
231: PetscCall(MatDestroy(&A));
232: PetscCall(MatDestroy(&B));
233: PetscCall(MatDestroy(&BT));
234: PetscCall(PetscFinalize());
235: return 0;
236: }
238: /*TEST
239: build:
240: requires: kokkos_kernels
242: test:
243: nsize: 2
244: args: -mat_type aijkokkos
245: requires: kokkos_kernels
246: output_file: output/empty.out
248: TEST*/