Skip to content

Commit

Permalink
Reduce MpiCallbacks complexity (#4767)
Browse files Browse the repository at this point in the history
Follow-up to #4614
Follow-up to #3180 (comment)
  • Loading branch information
kodiakhq[bot] authored Aug 7, 2023
2 parents f1f64c8 + 50af4b1 commit c0a3b74
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 573 deletions.
371 changes: 10 additions & 361 deletions src/core/MpiCallbacks.hpp

Large diffs are not rendered by default.

33 changes: 1 addition & 32 deletions src/core/communication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "MpiCallbacks.hpp"

#include <boost/mpi/communicator.hpp>
#include <boost/mpi/environment.hpp>

#include <memory>
#include <utility>
Expand Down Expand Up @@ -97,38 +98,6 @@ void mpi_call_all(void (*fp)(Args...), ArgRef &&...args) {
Communication::mpiCallbacks().call_all(fp, std::forward<ArgRef>(args)...);
}

/** @brief Call a local function.
* @tparam Tag Any tag type defined in @ref Communication::Result
* @tparam R Return type of the local function
* @tparam Args Local function argument types
* @tparam ArgRef Local function argument types
* @param tag Reduction strategy
* @param fp Local function
* @param args Local function arguments
*/
template <class Tag, class R, class... Args, class... ArgRef>
auto mpi_call(Tag tag, R (*fp)(Args...), ArgRef &&...args) {
return Communication::mpiCallbacks().call(tag, fp,
std::forward<ArgRef>(args)...);
}

/** @brief Call a local function.
* @tparam Tag Any tag type defined in @ref Communication::Result
* @tparam TagArg Types of arguments to @p Tag
* @tparam R Return type of the local function
* @tparam Args Local function argument types
* @tparam ArgRef Local function argument types
* @param tag Reduction strategy
* @param tag_arg Arguments to the reduction strategy
* @param fp Local function
* @param args Local function arguments
*/
template <class Tag, class TagArg, class R, class... Args, class... ArgRef>
auto mpi_call(Tag tag, TagArg &&tag_arg, R (*fp)(Args...), ArgRef &&...args) {
return Communication::mpiCallbacks().call(tag, std::forward<TagArg>(tag_arg),
fp, std::forward<ArgRef>(args)...);
}

/** Process requests from head node. Worker nodes main loop. */
void mpi_loop();

Expand Down
6 changes: 3 additions & 3 deletions src/core/observables/CylindricalDensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "BoxGeometry.hpp"
#include "grid.hpp"
#include "utils_histogram.hpp"

#include <utils/Histogram.hpp>
#include <utils/math/coordinate_transformation.hpp>
Expand Down Expand Up @@ -53,9 +54,8 @@ class CylindricalDensityProfile : public CylindricalPidProfileObservable {
transform_params->orientation()));
}

std::vector<decltype(local_folded_positions)> global_folded_positions{};
boost::mpi::gather(comm, local_folded_positions, global_folded_positions,
0);
auto const global_folded_positions =
detail::gather(comm, local_folded_positions);

if (comm.rank() != 0) {
return {};
Expand Down
12 changes: 3 additions & 9 deletions src/core/observables/CylindricalFluxDensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,15 @@ class CylindricalFluxDensityProfile : public CylindricalPidProfileObservable {
traits.velocity(p), transform_params->axis(), pos));
}

auto const world_size = comm.size();
std::vector<decltype(local_folded_positions)> global_folded_positions{};
std::vector<decltype(local_velocities)> global_velocities{};
global_folded_positions.reserve(world_size);
global_velocities.reserve(world_size);
boost::mpi::gather(comm, local_folded_positions, global_folded_positions,
0);
boost::mpi::gather(comm, local_velocities, global_velocities, 0);
auto const [global_folded_positions, global_velocities] =
detail::gather(comm, local_folded_positions, local_velocities);

if (comm.rank() != 0) {
return {};
}

Utils::CylindricalHistogram<double, 3> histogram(n_bins(), limits());
accumulate(histogram, global_folded_positions, global_velocities);
detail::accumulate(histogram, global_folded_positions, global_velocities);
histogram.normalize();
return histogram.get_histogram();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ CylindricalLBFluxDensityProfileAtParticlePositions::evaluate(
local_flux_densities.emplace_back(flux_cyl);
}

std::vector<decltype(local_folded_positions)> global_folded_positions{};
std::vector<decltype(local_flux_densities)> global_flux_densities{};
boost::mpi::gather(comm, local_folded_positions, global_folded_positions, 0);
boost::mpi::gather(comm, local_flux_densities, global_flux_densities, 0);
auto const [global_folded_positions, global_flux_densities] =
detail::gather(comm, local_folded_positions, local_flux_densities);

if (comm.rank() != 0) {
return {};
}

Utils::CylindricalHistogram<double, 3> histogram(n_bins(), limits());
accumulate(histogram, global_folded_positions, global_flux_densities);
return normalize_by_bin_size(histogram);
detail::accumulate(histogram, global_folded_positions, global_flux_densities);
return detail::normalize_by_bin_size(histogram);
}
} // namespace Observables
16 changes: 4 additions & 12 deletions src/core/observables/CylindricalLBVelocityProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
#include <utils/Histogram.hpp>
#include <utils/math/coordinate_transformation.hpp>

#include <boost/mpi/collectives/gather.hpp>
#include <boost/serialization/vector.hpp>

#include <vector>

namespace Observables {
Expand Down Expand Up @@ -55,21 +52,16 @@ std::vector<double> CylindricalLBVelocityProfile::operator()(
}
}

auto const world_size = comm.size();
std::vector<decltype(sampling_positions)> global_positions{};
std::vector<decltype(local_velocities)> global_velocities{};
global_positions.reserve(world_size);
global_velocities.reserve(world_size);
boost::mpi::gather(comm, local_positions, global_positions, 0);
boost::mpi::gather(comm, local_velocities, global_velocities, 0);
auto const [global_positions, global_velocities] =
detail::gather(comm, local_positions, local_velocities);

if (comm.rank() != 0) {
return {};
}

Utils::CylindricalHistogram<double, 3> histogram(n_bins(), limits());
accumulate(histogram, global_positions, global_velocities);
return normalize_by_bin_size(histogram);
detail::accumulate(histogram, global_positions, global_velocities);
return detail::normalize_by_bin_size(histogram);
}

} // namespace Observables
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
#include <utils/Histogram.hpp>
#include <utils/math/coordinate_transformation.hpp>

#include <boost/mpi/collectives/gather.hpp>
#include <boost/serialization/vector.hpp>

#include <utility>
#include <vector>

Expand Down Expand Up @@ -59,21 +56,16 @@ std::vector<double> CylindricalLBVelocityProfileAtParticlePositions::evaluate(
local_velocities.emplace_back(vel_cyl);
}

auto const world_size = comm.size();
std::vector<decltype(local_folded_positions)> global_folded_positions{};
std::vector<decltype(local_velocities)> global_velocities{};
global_folded_positions.reserve(world_size);
global_velocities.reserve(world_size);
boost::mpi::gather(comm, local_folded_positions, global_folded_positions, 0);
boost::mpi::gather(comm, local_velocities, global_velocities, 0);
auto const [global_folded_positions, global_velocities] =
detail::gather(comm, local_folded_positions, local_velocities);

if (comm.rank() != 0) {
return {};
}

Utils::CylindricalHistogram<double, 3> histogram(n_bins(), limits());
accumulate(histogram, global_folded_positions, global_velocities);
return normalize_by_bin_size(histogram);
detail::accumulate(histogram, global_folded_positions, global_velocities);
return detail::normalize_by_bin_size(histogram);
}

} // namespace Observables
15 changes: 4 additions & 11 deletions src/core/observables/CylindricalVelocityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <utils/math/coordinate_transformation.hpp>

#include <cstddef>
#include <utility>
#include <vector>

namespace Observables {
Expand Down Expand Up @@ -59,22 +58,16 @@ class CylindricalVelocityProfile : public CylindricalPidProfileObservable {
traits.velocity(p), transform_params->axis(), pos));
}

auto const world_size = comm.size();
std::vector<decltype(local_folded_positions)> global_folded_positions{};
std::vector<decltype(local_velocities)> global_velocities{};
global_folded_positions.reserve(world_size);
global_velocities.reserve(world_size);
boost::mpi::gather(comm, local_folded_positions, global_folded_positions,
0);
boost::mpi::gather(comm, local_velocities, global_velocities, 0);
auto const [global_folded_positions, global_velocities] =
detail::gather(comm, local_folded_positions, local_velocities);

if (comm.rank() != 0) {
return {};
}

Utils::CylindricalHistogram<double, 3> histogram(n_bins(), limits());
accumulate(histogram, global_folded_positions, global_velocities);
return normalize_by_bin_size(histogram);
detail::accumulate(histogram, global_folded_positions, global_velocities);
return detail::normalize_by_bin_size(histogram);
}

std::vector<std::size_t> shape() const override {
Expand Down
8 changes: 3 additions & 5 deletions src/core/observables/DensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Particle.hpp"
#include "PidProfileObservable.hpp"
#include "grid.hpp"
#include "utils_histogram.hpp"

#include <utils/Histogram.hpp>

Expand All @@ -49,11 +50,8 @@ class DensityProfile : public PidProfileObservable {
folded_position(traits.position(p), box_geo));
}

auto const world_size = comm.size();
std::vector<decltype(local_folded_positions)> global_folded_positions{};
global_folded_positions.reserve(world_size);
boost::mpi::gather(comm, local_folded_positions, global_folded_positions,
0);
auto const global_folded_positions =
detail::gather(comm, local_folded_positions);

if (comm.rank() != 0) {
return {};
Expand Down
14 changes: 3 additions & 11 deletions src/core/observables/FluxDensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#include <utils/Histogram.hpp>

#include <boost/range/combine.hpp>

#include <cstddef>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -60,21 +58,15 @@ class FluxDensityProfile : public PidProfileObservable {
local_velocities.emplace_back(traits.velocity(p));
}

auto const world_size = comm.size();
std::vector<decltype(local_folded_positions)> global_folded_positions{};
std::vector<decltype(local_velocities)> global_velocities{};
global_folded_positions.reserve(world_size);
global_velocities.reserve(world_size);
boost::mpi::gather(comm, local_folded_positions, global_folded_positions,
0);
boost::mpi::gather(comm, local_velocities, global_velocities, 0);
auto const [global_folded_positions, global_velocities] =
detail::gather(comm, local_folded_positions, local_velocities);

if (comm.rank() != 0) {
return {};
}

Utils::Histogram<double, 3> histogram(n_bins(), limits());
accumulate(histogram, global_folded_positions, global_velocities);
detail::accumulate(histogram, global_folded_positions, global_velocities);
histogram.normalize();
return histogram.get_histogram();
}
Expand Down
12 changes: 3 additions & 9 deletions src/core/observables/ForceDensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,15 @@ class ForceDensityProfile : public PidProfileObservable {
local_forces.emplace_back(traits.force(p));
}

auto const world_size = comm.size();
std::vector<decltype(local_folded_positions)> global_folded_positions{};
std::vector<decltype(local_forces)> global_forces{};
global_folded_positions.reserve(world_size);
global_forces.reserve(world_size);
boost::mpi::gather(comm, local_folded_positions, global_folded_positions,
0);
boost::mpi::gather(comm, local_forces, global_forces, 0);
auto const [global_folded_positions, global_forces] =
detail::gather(comm, local_folded_positions, local_forces);

if (comm.rank() != 0) {
return {};
}

Utils::Histogram<double, 3> histogram(n_bins(), limits());
accumulate(histogram, global_folded_positions, global_forces);
detail::accumulate(histogram, global_folded_positions, global_forces);
histogram.normalize();
return histogram.get_histogram();
}
Expand Down
18 changes: 5 additions & 13 deletions src/core/observables/LBVelocityProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
#include <utils/Histogram.hpp>
#include <utils/Vector.hpp>

#include <boost/mpi/collectives/gather.hpp>
#include <boost/serialization/vector.hpp>

#include <stdexcept>
#include <vector>

Expand All @@ -48,23 +45,18 @@ LBVelocityProfile::operator()(boost::mpi::communicator const &comm) const {
}
}

auto const world_size = comm.size();
std::vector<decltype(sampling_positions)> global_positions{};
std::vector<decltype(local_velocities)> global_velocities{};
global_positions.reserve(world_size);
global_velocities.reserve(world_size);
boost::mpi::gather(comm, local_positions, global_positions, 0);
boost::mpi::gather(comm, local_velocities, global_velocities, 0);
auto const [global_positions, global_velocities] =
detail::gather(comm, local_positions, local_velocities);

if (comm.rank() != 0) {
return {};
}

Utils::Histogram<double, 3> histogram(n_bins(), limits());
accumulate(histogram, global_positions, global_velocities);
detail::accumulate(histogram, global_positions, global_velocities);
try {
return normalize_by_bin_size(histogram, allow_empty_bins);
} catch (empty_bin_exception const &) {
return detail::normalize_by_bin_size(histogram, allow_empty_bins);
} catch (detail::empty_bin_exception const &) {
throw std::runtime_error(
"Decrease sampling delta(s), some bins have no hit");
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/observables/PidObservable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

#include <boost/mpi/collectives/gather.hpp>
#include <boost/mpi/collectives/reduce.hpp>

#include <boost/range/algorithm/copy.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/vector.hpp>

#include <cassert>
#include <cstddef>
Expand Down
34 changes: 32 additions & 2 deletions src/core/observables/utils_histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,40 @@

#include <utils/Histogram.hpp>

#include <boost/mpi/collectives/gather.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/serialization/vector.hpp>

#include <cstddef>
#include <utility>
#include <vector>

namespace Observables {
namespace Observables::detail {

/** @brief Gather data from all MPI ranks. */
template <class Pos>
auto gather(boost::mpi::communicator const &comm,
std::vector<Pos> const &local_pos) {
std::vector<std::vector<Pos>> global_pos{};
global_pos.reserve(comm.size());
boost::mpi::gather(comm, local_pos, global_pos, 0);
return global_pos;
}

/** @brief Gather data from all MPI ranks. */
template <class Pos, class Val>
auto gather(boost::mpi::communicator const &comm,
std::vector<Pos> const &local_pos,
std::vector<Val> const &local_val) {
auto const world_size = comm.size();
std::vector<std::vector<Pos>> global_pos{};
global_pos.reserve(world_size);
boost::mpi::gather(comm, local_pos, global_pos, 0);
std::vector<std::vector<Val>> global_val{};
global_val.reserve(world_size);
boost::mpi::gather(comm, local_val, global_val, 0);
return std::make_pair(global_pos, global_val);
}

/** @brief Accumulate histogram data gathered from multiple MPI ranks. */
template <class T, std::size_t N, std::size_t M, class U, class Pos, class Val>
Expand Down Expand Up @@ -58,4 +88,4 @@ auto normalize_by_bin_size(Utils::Histogram<T, N, M, U> &histogram,
return hist_data;
}

} // Namespace Observables
} // Namespace Observables::detail
Loading

0 comments on commit c0a3b74

Please sign in to comment.