From: Wolfgang Bangerth Date: Sat, 5 Mar 2022 05:02:21 +0000 (-0700) Subject: Add a test. X-Git-Tag: v9.4.0-rc1~389^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f3b88da330fcce035159000ef594e497d0e716e3;p=dealii.git Add a test. --- 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) +