Skip to content

Commit

Permalink
Use C++ ranges algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Feb 12, 2025
1 parent 91fa77a commit f5f1191
Show file tree
Hide file tree
Showing 51 changed files with 228 additions and 336 deletions.
30 changes: 14 additions & 16 deletions src/core/accumulators/Correlator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ std::vector<double> compress_linear(std::vector<double> const &A1,
assert(A1.size() == A2.size());
std::vector<double> A_compressed(A1.size());

std::transform(A1.begin(), A1.end(), A2.begin(), A_compressed.begin(),
[](double a, double b) -> double { return 0.5 * (a + b); });
std::ranges::transform(A1, A2, A_compressed.begin(),
[](double a, double b) { return 0.5 * (a + b); });

return A_compressed;
}
Expand Down Expand Up @@ -95,7 +95,7 @@ std::vector<double> componentwise_product(std::vector<double> const &A,
"Error in componentwise product: The vector sizes do not match");
}

std::transform(A.begin(), A.end(), B.begin(), C.begin(), std::multiplies<>());
std::ranges::transform(A, B, C.begin(), std::multiplies<>());

return C;
}
Expand Down Expand Up @@ -126,9 +126,9 @@ std::vector<double> square_distance_componentwise(std::vector<double> const &A,

std::vector<double> C(A.size());

std::transform(
A.begin(), A.end(), B.begin(), C.begin(),
[](double a, double b) -> double { return Utils::sqr(a - b); });
std::ranges::transform(A, B, C.begin(), [](double a, double b) -> double {
return Utils::sqr(a - b);
});

return C;
}
Expand All @@ -143,23 +143,21 @@ std::vector<double> fcs_acf(std::vector<double> const &A,
"Error in fcs_acf: The vector sizes do not match.");
}

auto const C_size = A.size() / 3;
assert(3 * C_size == A.size());
auto const C_size = A.size() / 3u;
assert(3u * C_size == A.size());

std::vector<double> C(C_size, 0);
std::vector<double> C(C_size, 0.);

for (std::size_t i = 0; i < C_size; i++) {
auto acc = 0.;
for (int j = 0; j < 3; j++) {
auto const &a = A[3 * i + j];
auto const &b = B[3 * i + j];

C[i] -= Utils::sqr(a - b) / wsquare[j];
auto const a = A[3 * i + j];
auto const b = B[3 * i + j];
acc += Utils::sqr(a - b) / wsquare[j];
}
C[i] = std::exp(-acc);
}

std::transform(C.begin(), C.end(), C.begin(),
[](double c) -> double { return std::exp(c); });

return C;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/bonded_interactions/bonded_interaction_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ double BondedInteractionsMap::maximal_cutoff() const {
});

/* Check if there are dihedrals */
auto const any_dihedrals = std::any_of(begin(), end(), [](auto const &kv) {
auto const any_dihedrals = std::ranges::any_of(*this, [](auto const &kv) {
return (boost::get<DihedralBond>(&(*kv.second)) ||
boost::get<TabulatedDihedralBond>(&(*kv.second)));
});
Expand Down
7 changes: 3 additions & 4 deletions src/core/cell_system/Cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ template <class CellRef> class Neighbors {
Neighbors(std::span<const CellRef> red_neighbors,
std::span<const CellRef> black_neighbors) {
m_neighbors.resize(red_neighbors.size() + black_neighbors.size());
m_red_black_divider = std::copy(red_neighbors.begin(), red_neighbors.end(),
m_neighbors.begin());
std::copy(black_neighbors.begin(), black_neighbors.end(),
m_red_black_divider);
auto const res = std::ranges::copy(red_neighbors, m_neighbors.begin());
m_red_black_divider = res.out;
std::ranges::copy(black_neighbors, m_red_black_divider);
}

/**
Expand Down
23 changes: 9 additions & 14 deletions src/core/cell_system/HybridDecomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,36 @@ HybridDecomposition::HybridDecomposition(boost::mpi::communicator comm,
/* Vector containing cells of both child decompositions */
m_local_cells = m_regular_decomposition.get_local_cells();
auto local_cells_n_square = m_n_square.get_local_cells();
std::copy(local_cells_n_square.begin(), local_cells_n_square.end(),
std::back_inserter(m_local_cells));
std::ranges::copy(local_cells_n_square, std::back_inserter(m_local_cells));

/* Vector containing ghost cells of both child decompositions */
m_ghost_cells = m_regular_decomposition.get_ghost_cells();
auto ghost_cells_n_square = m_n_square.get_ghost_cells();
std::copy(ghost_cells_n_square.begin(), ghost_cells_n_square.end(),
std::back_inserter(m_ghost_cells));
std::ranges::copy(ghost_cells_n_square, std::back_inserter(m_ghost_cells));

/* Communicators that contain communications of both child decompositions */
m_exchange_ghosts_comm = m_regular_decomposition.exchange_ghosts_comm();
auto exchange_ghosts_comm_n_square = m_n_square.exchange_ghosts_comm();
std::copy(exchange_ghosts_comm_n_square.communications.begin(),
exchange_ghosts_comm_n_square.communications.end(),
std::back_inserter(m_exchange_ghosts_comm.communications));
std::ranges::copy(exchange_ghosts_comm_n_square.communications,
std::back_inserter(m_exchange_ghosts_comm.communications));

m_collect_ghost_force_comm =
m_regular_decomposition.collect_ghost_force_comm();
auto collect_ghost_force_comm_n_square =
m_n_square.collect_ghost_force_comm();
std::copy(collect_ghost_force_comm_n_square.communications.begin(),
collect_ghost_force_comm_n_square.communications.end(),
std::back_inserter(m_collect_ghost_force_comm.communications));
std::ranges::copy(
collect_ghost_force_comm_n_square.communications,
std::back_inserter(m_collect_ghost_force_comm.communications));

/* coupling between the child decompositions via neighborship relation */
std::vector<Cell *> additional_reds = m_n_square.get_local_cells();
std::copy(ghost_cells_n_square.begin(), ghost_cells_n_square.end(),
std::back_inserter(additional_reds));
std::ranges::copy(ghost_cells_n_square, std::back_inserter(additional_reds));
for (auto &local_cell : m_regular_decomposition.local_cells()) {
std::vector<Cell *> red_neighbors(local_cell->m_neighbors.red().begin(),
local_cell->m_neighbors.red().end());
std::vector<Cell *> black_neighbors(local_cell->m_neighbors.black().begin(),
local_cell->m_neighbors.black().end());
std::copy(additional_reds.begin(), additional_reds.end(),
std::back_inserter(red_neighbors));
std::ranges::copy(additional_reds, std::back_inserter(red_neighbors));
local_cell->m_neighbors = Neighbors<Cell *>(red_neighbors, black_neighbors);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/core/cells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ get_pairs_of_types(System::System const &system, double const distance,
std::vector<int> const &types) {
detail::search_neighbors_sanity_checks(system, distance);
return get_pairs_filtered(system, distance, [types](Particle const &p) {
return std::any_of(types.begin(), types.end(),
// NOLINTNEXTLINE(bugprone-exception-escape)
[p](int const type) { return p.type() == type; });
return std::ranges::any_of(
types, [target = p.type()](int const type) { return type == target; });
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/cluster_analysis/Cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ std::vector<std::size_t> sort_indices(const std::vector<T> &v) {

// Unsorted for unsorted vector (0..n-1)
std::vector<std::size_t> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);
std::iota(idx.begin(), idx.end(), std::size_t{0u});

// sort indices based on comparing values in v
std::sort(idx.begin(), idx.end(),
[&v](std::size_t i1, std::size_t i2) { return v[i1] < v[i2]; });
std::ranges::sort(
idx, [&v](std::size_t i1, std::size_t i2) { return v[i1] < v[i2]; });
return idx;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/cluster_analysis/ClusterStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void ClusterStructure::merge_clusters() {

// Sort particles ids in the clusters
for (const auto &c : clusters) {
std::sort(c.second->particles.begin(), c.second->particles.end());
std::ranges::sort(c.second->particles);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/constraints/Constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Constraints : public System::Leaf<Constraints> {

public:
bool contains(std::shared_ptr<Constraint> const &constraint) const noexcept {
return std::find(begin(), end(), constraint) != end();
return std::ranges::find(*this, constraint) != end();
}
void add(std::shared_ptr<Constraint> const &constraint);
void remove(std::shared_ptr<Constraint> const &constraint);
Expand Down
8 changes: 4 additions & 4 deletions src/core/cuda/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <mpi.h>

#include <algorithm>
#include <cstring>
#include <iterator>
#include <set>
Expand Down Expand Up @@ -83,8 +84,8 @@ std::vector<EspressoGpuDevice> cuda_gather_gpus() {
MPI_Gather(&n_gpus, 1, MPI_INT, n_gpu_array, 1, MPI_INT, 0, MPI_COMM_WORLD);

/* insert local devices */
std::copy(devices_local.begin(), devices_local.end(),
std::inserter(device_set, device_set.begin()));
std::ranges::copy(devices_local,
std::inserter(device_set, device_set.begin()));

EspressoGpuDevice device;
MPI_Status s;
Expand All @@ -97,8 +98,7 @@ std::vector<EspressoGpuDevice> cuda_gather_gpus() {
}
}
/* Copy unique devices to result, if any */
std::copy(device_set.begin(), device_set.end(),
std::inserter(devices_global, devices_global.begin()));
std::ranges::copy(device_set, std::back_inserter(devices_global));
delete[] n_gpu_array;
} else {
/* Send number of devices to head node */
Expand Down
2 changes: 1 addition & 1 deletion src/core/ek/EKReactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ template <class EKReaction> class EKReactions {

public:
bool contains(std::shared_ptr<EKReaction> const &ek_reaction) const noexcept {
return std::find(begin(), end(), ek_reaction) != end();
return std::ranges::find(*this, ek_reaction) != end();
}
void add(std::shared_ptr<EKReaction> const &ek_reaction) {
assert(not contains(ek_reaction));
Expand Down
4 changes: 2 additions & 2 deletions src/core/electrostatics/p3m_gpu_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ void p3m_gpu_init(std::shared_ptr<P3MGpuParams> &data, int cao,
}

if (not data->is_initialized or mesh != Utils::Vector3i(p3m_gpu_data.mesh)) {
std::copy(mesh.begin(), mesh.end(), p3m_gpu_data.mesh);
std::ranges::copy(mesh, p3m_gpu_data.mesh);
mesh_changed = true;
do_reinit = true;
}
Expand All @@ -582,7 +582,7 @@ void p3m_gpu_init(std::shared_ptr<P3MGpuParams> &data, int cao,
static_cast<double>(std::numeric_limits<float>::epsilon());
not data->is_initialized or
(box_l - Utils::Vector3d(p3m_gpu_data.box)).norm() >= eps) {
std::copy(box_l.begin(), box_l.end(), p3m_gpu_data.box);
std::ranges::copy(box_l, p3m_gpu_data.box);
do_reinit = true;
}

Expand Down
8 changes: 3 additions & 5 deletions src/core/energy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,9 @@ std::optional<double> System::particle_bond_energy(int pid, int bond_id,
if (cell_structure->get_resort_particles()) {
cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
}
const Particle *p = cell_structure->get_local_particle(pid);
if (not p)
return {}; // not available on this MPI rank
if (p->is_ghost())
return {};
Particle const *p = cell_structure->get_local_particle(pid);
if (not p or p->is_ghost())
return {}; // not available on this MPI rank or ghost
auto const &iaparams = *bonded_ias->at(bond_id);
try {
auto resolved_partners = cell_structure->resolve_bond_partners(partners);
Expand Down
5 changes: 2 additions & 3 deletions src/core/error_handling/RuntimeErrorCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ int RuntimeErrorCollector::count() const {
}

int RuntimeErrorCollector::count(RuntimeError::ErrorLevel level) {
return static_cast<int>(std::count_if(
m_errors.begin(), m_errors.end(),
[level](const RuntimeError &e) { return e.level() >= level; }));
return static_cast<int>(std::ranges::count_if(
m_errors, [level](auto const &e) { return e.level() >= level; }));
}

void RuntimeErrorCollector::clear() { m_errors.clear(); }
Expand Down
4 changes: 2 additions & 2 deletions src/core/exclusions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
inline bool do_nonbonded(Particle const &p1, Particle const &p2) {
/* check for particle 2 in particle 1's exclusion list. The exclusion list is
* symmetric, so this is sufficient. */
return std::none_of(p1.exclusions().begin(), p1.exclusions().end(),
[&p2](int id) { return p2.id() == id; });
return std::ranges::none_of(
p1.exclusions(), [p2_id = p2.id()](int id) { return id == p2_id; });
}

/** Remove exclusion from particle if possible */
Expand Down
10 changes: 8 additions & 2 deletions src/core/galilei/ComFixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
#include "communication.hpp"

#include <utils/Vector.hpp>
#include <utils/keys.hpp>

#include <boost/mpi/collectives/all_reduce.hpp>
#include <boost/mpi/communicator.hpp>

#include <algorithm>
#include <functional>
#include <ranges>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -77,7 +78,12 @@ class ComFixed {
}
}

std::vector<int> get_fixed_types() const { return Utils::keys(m_type_index); }
std::vector<int> get_fixed_types() const {
std::vector<int> types{};
std::ranges::copy(std::views::keys(m_type_index),
std::back_inserter(types));
return types;
}

void apply(ParticleRange const &particles) const {
/* Bail out early if there is nothing to do. */
Expand Down
10 changes: 3 additions & 7 deletions src/core/observables/PidObservable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,12 @@ inline auto get_argsort(boost::mpi::communicator const &comm,
}
}
// get vector of indices that sorts the data vectors
std::vector<unsigned int> iota(n_part);
std::iota(iota.begin(), iota.end(), 0u);
argsort.reserve(n_part);
auto const pid_begin = std::begin(unsorted_pids);
auto const pid_end = std::end(unsorted_pids);
for (auto const pid : sorted_pids) {
auto const pid_pos = std::find(pid_begin, pid_end, pid);
auto const i =
static_cast<std::size_t>(std::distance(pid_begin, pid_pos));
argsort.emplace_back(iota[i]);
auto const pid_pos = std::ranges::find(unsorted_pids, pid);
auto const hops = std::distance(pid_begin, pid_pos);
argsort.emplace_back(static_cast<unsigned>(hops));
}
}
return argsort;
Expand Down
2 changes: 1 addition & 1 deletion src/core/observables/fetch_particles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ inline auto fetch_particles(std::vector<int> const &ids) {
Observables::ParticleReferenceRange local_particle_refs;
std::copy_if(local_particles.begin(), local_particles.end(),
std::back_inserter(local_particle_refs),
[&ids_set](Particle &p) { return ids_set.count(p.id()) != 0; });
[&ids_set](auto const &p) { return ids_set.contains(p.id()); });
return local_particle_refs;
}
#endif
14 changes: 8 additions & 6 deletions src/core/particle_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include <utils/Cache.hpp>
#include <utils/Vector.hpp>
#include <utils/keys.hpp>
#include <utils/mpi/gatherv.hpp>

#include <boost/mpi/collectives/all_gather.hpp>
Expand All @@ -44,6 +43,7 @@
#include <cmath>
#include <functional>
#include <iterator>
#include <ranges>
#include <span>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -264,7 +264,7 @@ void prefetch_particle_data(std::span<const int> in_ids) {
auto out_ids = std::back_inserter(ids);

/* Don't prefetch particles already on the head node or already cached. */
std::copy_if(in_ids.begin(), in_ids.end(), out_ids, [](int id) {
std::ranges::copy_if(in_ids, out_ids, [](int id) {
return (get_particle_node(id) != this_node) && particle_fetch_cache.has(id);
});

Expand Down Expand Up @@ -574,17 +574,19 @@ std::vector<int> get_particle_ids() {
if (particle_node.empty())
build_particle_node();

auto ids = Utils::keys(particle_node);
std::ranges::sort(ids);
std::vector<int> pids{};
std::ranges::copy(std::views::keys(particle_node), std::back_inserter(pids));
std::ranges::sort(pids);

return ids;
return pids;
}

std::vector<int> get_particle_ids_parallel() {
if (rebuild_needed()) {
build_particle_node_parallel();
}
auto pids = Utils::keys(particle_node);
std::vector<int> pids{};
std::ranges::copy(std::views::keys(particle_node), std::back_inserter(pids));
boost::mpi::broadcast(::comm_cart, pids, 0);
return pids;
}
Expand Down
Loading

0 comments on commit f5f1191

Please sign in to comment.