From 430a59e0cec0d8a27dfda25cdaf9dfbbd69988bc Mon Sep 17 00:00:00 2001 From: David Wells Date: Sun, 11 Aug 2024 15:11:44 -0600 Subject: [PATCH] PETScWrappers::MPI::Vector: add index conversion checks. --- source/lac/petsc_parallel_vector.cc | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/source/lac/petsc_parallel_vector.cc b/source/lac/petsc_parallel_vector.cc index 06f20d348e..ff25f6cf9c 100644 --- a/source/lac/petsc_parallel_vector.cc +++ b/source/lac/petsc_parallel_vector.cc @@ -293,21 +293,25 @@ namespace PETScWrappers { (void)n; AssertIndexRange(locally_owned_size, n + 1); + // If the size of the index set can be converted to a PetscInt then every + // index can also be converted + AssertThrowIntegerConversion(static_cast(n), n); ghosted = true; ghost_indices = ghostnodes; - const std::vector ghostindices = ghostnodes.get_index_vector(); - - const PetscInt *ptr = - (ghostindices.size() > 0 ? - reinterpret_cast(ghostindices.data()) : - nullptr); + std::size_t i = 0; + std::vector petsc_ghost_indices(ghostnodes.n_elements()); + for (const auto &index : ghostnodes) + { + petsc_ghost_indices[i] = static_cast(index); + ++i; + } PetscErrorCode ierr = VecCreateGhost(communicator, locally_owned_size, PETSC_DETERMINE, - ghostindices.size(), - ptr, + petsc_ghost_indices.size(), + petsc_ghost_indices.data(), &vector); AssertThrow(ierr == 0, ExcPETScError(ierr)); -- 2.39.5