From 6cea389e1f920e0f2d0b7c1cca1621628ff6bfb4 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Fri, 1 Nov 2024 15:46:50 +0100
Subject: [PATCH] [MaL/PETSc] allgatherv replaces gatherLocalVectors

---
 MathLib/LinAlg/PETSc/PETScVector.cpp | 18 ++----------------
 MathLib/LinAlg/PETSc/PETScVector.h   |  8 --------
 2 files changed, 2 insertions(+), 24 deletions(-)

diff --git a/MathLib/LinAlg/PETSc/PETScVector.cpp b/MathLib/LinAlg/PETSc/PETScVector.cpp
index 13ca4b1ee4c..a6201f116d8 100644
--- a/MathLib/LinAlg/PETSc/PETScVector.cpp
+++ b/MathLib/LinAlg/PETSc/PETScVector.cpp
@@ -115,21 +115,6 @@ void PETScVector::finalizeAssembly()
     VecAssemblyEnd(v_);
 }
 
-void PETScVector::gatherLocalVectors(PetscScalar local_array[],
-                                     PetscScalar global_array[]) const
-{
-    BaseLib::MPI::Mpi mpi{PETSC_COMM_WORLD};
-
-    // number of elements to be sent for each rank
-    std::vector<PetscInt> const i_cnt = BaseLib::MPI::allgather(size_loc_, mpi);
-
-    // offset in the receive vector of the data from each rank
-    std::vector<PetscInt> const i_disp = BaseLib::sizesToOffsets(i_cnt);
-
-    MPI_Allgatherv(local_array, size_loc_, MPI_DOUBLE, global_array, &i_cnt[0],
-                   &i_disp[0], MPI_DOUBLE, PETSC_COMM_WORLD);
-}
-
 void PETScVector::getGlobalVector(std::vector<PetscScalar>& u) const
 {
 #ifdef TEST_MEM_PETSC
@@ -149,7 +134,8 @@ void PETScVector::getGlobalVector(std::vector<PetscScalar>& u) const
     PetscScalar* xp = nullptr;
     VecGetArray(v_, &xp);
 
-    gatherLocalVectors(xp, u.data());
+    BaseLib::MPI::Mpi mpi{PETSC_COMM_WORLD};
+    BaseLib::MPI::allgatherv(std::span(xp, size_loc_), u, mpi);
 
     // This following line may be needed late on
     //  for a communication load balance:
diff --git a/MathLib/LinAlg/PETSc/PETScVector.h b/MathLib/LinAlg/PETSc/PETScVector.h
index ed2734878fb..20983dacf2d 100644
--- a/MathLib/LinAlg/PETSc/PETScVector.h
+++ b/MathLib/LinAlg/PETSc/PETScVector.h
@@ -267,14 +267,6 @@ private:
     /// Map global indices of ghost entries to local indices
     mutable std::map<PetscInt, PetscInt> global_ids2local_ids_ghost_;
 
-    /*!
-          \brief  Collect local vectors
-          \param  local_array Local array
-          \param  global_array Global array
-    */
-    void gatherLocalVectors(PetscScalar local_array[],
-                            PetscScalar global_array[]) const;
-
     /*!
        Get local vector, i.e. entries in the same rank
     */
-- 
GitLab