From fa2a1e6d1d609f5aa3d057a3e10d9e18a29bbfcc Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 15 Dec 2023 01:03:37 -0700 Subject: [PATCH] Choose a more efficient algorithm. --- include/deal.II/lac/sparse_matrix_tools.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/deal.II/lac/sparse_matrix_tools.h b/include/deal.II/lac/sparse_matrix_tools.h index 455a633e26..4f49a41a18 100644 --- a/include/deal.II/lac/sparse_matrix_tools.h +++ b/include/deal.II/lac/sparse_matrix_tools.h @@ -157,6 +157,7 @@ namespace SparseMatrixTools { T prefix = {}; + // First obtain every process's prefix sum: int ierr = MPI_Exscan(&value, &prefix, @@ -166,7 +167,12 @@ namespace SparseMatrixTools comm); AssertThrowMPI(ierr); - T sum = Utilities::MPI::sum(value, comm); + // Then we also need the total sum. We could obtain it by + // calling Utilities::MPI::sum(), but it is cheaper if we + // broadcast it from the last process, which can compute it + // from its own prefix sum plus its own value. + T sum = Utilities::MPI::broadcast( + comm, prefix + value, Utilities::MPI::n_mpi_processes(comm) - 1); return {prefix, sum}; } -- 2.39.5