From f3b88da330fcce035159000ef594e497d0e716e3 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 4 Mar 2022 22:02:21 -0700 Subject: [PATCH] Add a test. --- tests/mpi/broadcast_03.cc | 98 ++++++++++++++++++++++++++ tests/mpi/broadcast_03.mpirun=1.output | 4 ++ tests/mpi/broadcast_03.mpirun=5.output | 24 +++++++ 3 files changed, 126 insertions(+) create mode 100644 tests/mpi/broadcast_03.cc create mode 100644 tests/mpi/broadcast_03.mpirun=1.output create mode 100644 tests/mpi/broadcast_03.mpirun=5.output diff --git a/tests/mpi/broadcast_03.cc b/tests/mpi/broadcast_03.cc new file mode 100644 index 0000000000..7ee6bf7468 --- /dev/null +++ b/tests/mpi/broadcast_03.cc @@ -0,0 +1,98 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2021 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// Test Utilities::MPI::broadcast() for built-in types. + +#include + +#include + +#include "../tests.h" + +void +check(const int int_value) +{ + const auto my_proc = Utilities::MPI::this_mpi_process(MPI_COMM_WORLD); + + int buffer; + if (my_proc == 0) + { + buffer = int_value; + } + + const auto result = Utilities::MPI::broadcast(MPI_COMM_WORLD, buffer); + + deallog << result << " "; + deallog << std::endl; +} + + +void +check(const double double_value) +{ + const auto my_proc = Utilities::MPI::this_mpi_process(MPI_COMM_WORLD); + + double buffer; + if (my_proc == 0) + { + buffer = double_value; + } + + const auto result = Utilities::MPI::broadcast(MPI_COMM_WORLD, buffer); + + deallog << std::setprecision(4) << result << " "; + deallog << std::endl; +} + + +void +check(const std::complex &complex_value) +{ + const auto my_proc = Utilities::MPI::this_mpi_process(MPI_COMM_WORLD); + + std::complex buffer; + if (my_proc == 0) + { + buffer = complex_value; + } + + const auto result = Utilities::MPI::broadcast(MPI_COMM_WORLD, buffer); + + deallog << std::setprecision(2) << result << " "; + deallog << std::endl; +} + + +int +main(int argc, char *argv[]) +{ + Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1); + MPILogInitAll log; + + const unsigned int root = 0; + const bool on_root = + (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == root); + + // Test broadcast with int, double, and std::complex data + // types. Only provide the right value on the root, and some other + // value on non-root processes. We should only see the root value in + // the output. + check(on_root ? 23 : 12); + check(on_root ? 14.32 : std::numeric_limits::max()); + check(on_root ? std::complex(1., 2.) : + std::complex(1., 222222.)); +} diff --git a/tests/mpi/broadcast_03.mpirun=1.output b/tests/mpi/broadcast_03.mpirun=1.output new file mode 100644 index 0000000000..662a8eea4b --- /dev/null +++ b/tests/mpi/broadcast_03.mpirun=1.output @@ -0,0 +1,4 @@ + +DEAL:0::23 +DEAL:0::14.32 +DEAL:0::(1.0,2.0) diff --git a/tests/mpi/broadcast_03.mpirun=5.output b/tests/mpi/broadcast_03.mpirun=5.output new file mode 100644 index 0000000000..4ce8011d9e --- /dev/null +++ b/tests/mpi/broadcast_03.mpirun=5.output @@ -0,0 +1,24 @@ + +DEAL:0::23 +DEAL:0::14.32 +DEAL:0::(1.0,2.0) + +DEAL:1::23 +DEAL:1::14.32 +DEAL:1::(1.0,2.0) + + +DEAL:2::23 +DEAL:2::14.32 +DEAL:2::(1.0,2.0) + + +DEAL:3::23 +DEAL:3::14.32 +DEAL:3::(1.0,2.0) + + +DEAL:4::23 +DEAL:4::14.32 +DEAL:4::(1.0,2.0) + -- 2.39.5