Skip to content

Kokkos audit: add missing fences after async H2D copies, fix view/index bugs - #263

Merged
stevendargaville merged 1 commit into
mainfrom
kokkos_missing_fences
Aug 1, 2026
Merged

Kokkos audit: add missing fences after async H2D copies, fix view/index bugs#263
stevendargaville merged 1 commit into
mainfrom
kokkos_missing_fences

Conversation

@stevendargaville

Copy link
Copy Markdown
Collaborator

Summary

Full review of all Kokkos code (src/*.kokkos.cxx + include/kokkos_helper.hpp) for bugs, with emphasis on missing fences.

Missing fences (use-after-free race class)

Async Kokkos::deep_copy(exec, device, host) copies were followed by release of the host source buffer — ISRestoreIndices (which frees a temp array for stride/block IS types) or scope exit of a managed create_mirror_view host mirror — with no fence in between. These pass on CUDA today only because async copies from pageable host memory block the host; they are genuine races on HIP/SYCL or with pinned mirrors. Each new fence carries a single-line comment stating exactly what it fences after:

  • VecISCopyLocalk.kokkos.cxx — fine and coarse IS index copies vs ISRestoreIndices
  • Gmres_Polyk.kokkos.cxx / Gmres_Poly_Newtonk.kokkos.cxx — input→submat colmap mapping vs host mirror freed at scope exit
  • PETSc_Helperk.kokkos.cxxMatCreateSubMatrix_kokkos is_row/is_col copies and the MatCreateSubMatrix_kokkos_view reuse-path iscol_o copy vs ISRestoreIndices

Other fixes

  • SAI_Zk.kokkos.cxx: the "nothing to do" early return skipped every MatSeqAIJRestoreKokkosView/RestoreKokkosViewWrite; control now falls through to the shared restore/cleanup epilogue (the existing j_max > 0 / count_iter > 0 guards already skip the kernels)
  • PETSc_Helperk.kokkos.cxx (remove_small_from_sparse_kokkos): a lumped diagonal inserted into the local block used local row index i as the column; now uses row_index_global - global_col_start, correct when row/col ownership starts differ
  • MatDiagDomk.kokkos.cxx: fence moved before the VecDestroy calls whose device buffers the conversion kernel reads
  • Grid_Transferk.kokkos.cxx (compute_R_from_Z_kokkos reuse path): identity-skip offset now guarded by identity_int
  • PMISR_Modulek.kokkos.cxx: documented that the ADD-reverse-scatter marker merge relies on the strength matrix being structurally symmetric (S+S^T)
  • MatSetAllValues_kokkos: trailing fence added to match the file convention
  • Cleanups: PetscCallVoid(PetscFree(...)) instead of (void)PetscFree(...), PetscCallMPIAbort around an unchecked MPI_Scan, removed the deprecated volatile join() reducer overloads in kokkos_helper.hpp, stray semicolon

Testing

With make -j3 build_tests warning-clean, make check + make tests_short pass:

  1. plain
  2. PETSC_OPTIONS="-mat_type aijkokkos -vec_type kokkos -dm_mat_type aijkokkos -dm_vec_type kokkos -on_error_abort"
  3. same + PFLARE_KOKKOS_DEBUG=1 (CPU/Kokkos debug-compare)

The fence fixes remove latent races that don't reproduce on a CUDA host (pageable-memory async copies block), so the tests validate no regression rather than reproducing the race.

🤖 Generated with Claude Code

Fences (each commented with what it fences after): async deep_copys of IS
indices/host mirrors were racing ISRestoreIndices or scope exit of the host
buffer in VecISCopyLocalk, Gmres_Polyk, Gmres_Poly_Newtonk and PETSc_Helperk
(MatCreateSubMatrix paths). Also: SAI_Zk no longer returns early past its
MatSeqAIJRestoreKokkosView calls, remove_small_from_sparse_kokkos inserts the
lumped diagonal at the correct local column when row/col ownership starts
differ, MatDiagDomk fences before destroying the scatter Vecs, the
compute_R_from_Z reuse path only applies the identity-skip offset when the
identity was included, MatSetAllValues_kokkos fences before exit, plus small
cleanups (PetscCallVoid on PetscFree, PetscCallMPIAbort on MPI_Scan, removed
deprecated volatile reducer joins, stray semicolon).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@stevendargaville
stevendargaville merged commit 20e5f8c into main Aug 1, 2026
19 checks passed
@stevendargaville
stevendargaville deleted the kokkos_missing_fences branch August 1, 2026 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant