Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out ParticleList #3252

Merged
merged 5 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/core/Cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#ifndef CORE_CELL_HPP
#define CORE_CELL_HPP

#include "particle_data.hpp"
#include "Particle.hpp"
#include "ParticleList.hpp"

#include <utils/Span.hpp>

Expand Down Expand Up @@ -106,10 +107,6 @@ class Cell : public ParticleList {
* @brief All neighbors of the cell.
*/
neighbors_type &neighbors() { return m_neighbors; }

void resize(size_t size) {
realloc_particlelist(static_cast<ParticleList *>(this), this->n = size);
}
};

#endif
2 changes: 2 additions & 0 deletions src/core/PartCfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "ParticleCache.hpp"
#include "cells.hpp"
#include "grid.hpp"
#include "particle_data.hpp"

#include "serialization/Particle.hpp"
#include <utils/SkipIterator.hpp>

Expand Down
89 changes: 89 additions & 0 deletions src/core/ParticleList.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2010-2019 The ESPResSo project
* Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
* Max-Planck-Institute for Polymer Research, Theory Group
*
* This file is part of ESPResSo.
*
* ESPResSo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ESPResSo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ESPRESSO_CORE_PARTICLE_LIST_HPP
fweik marked this conversation as resolved.
Show resolved Hide resolved
#define ESPRESSO_CORE_PARTICLE_LIST_HPP

#include "Particle.hpp"

#include <utils/Span.hpp>
#include <utils/memory.hpp>

/** List of particles. The particle array is resized using a sophisticated
* (we hope) algorithm to avoid unnecessary resizes.
*/
struct ParticleList {
ParticleList() : part{nullptr}, n{0}, max{0} {}
/** The particles payload */
Particle *part;
/** Number of particles contained */
int n;

private:
/** Number of particles that fit in until a resize is needed */
int max;

int realloc(int size) {
assert(size >= 0);
int old_max = max;
Particle *old_start = part;

if (size < max) {
if (size == 0)
/* to be able to free an array again */
max = 0;
else
/* shrink not as fast, just lose half, rounded up */
max = INCREMENT * (((max + size + 1) / 2 + INCREMENT - 1) / INCREMENT);
} else
/* round up */
max = INCREMENT * ((size + INCREMENT - 1) / INCREMENT);
if (max != old_max)
part = Utils::realloc(part, sizeof(Particle) * max);
return part != old_start;
}

public:
/** Current allocation size. */
auto capacity() const { return max; }

Utils::Span<Particle> particles() { return {part, static_cast<size_t>(n)}; }

/** granularity of the particle buffers in particles */
static constexpr int INCREMENT = 8;

/**
* @brief Resize storage for local particles and ghosts.
*
* This version does \em not care for the bond information to be freed if
* necessary.
* @param size the size to provide at least. It is rounded
* up to multiples of @ref ParticleList::INCREMENT.
* @return true iff particle addresses have changed
*/
int resize(int size) { return realloc(this->n = size); }

/**
* @brief Resize the List to zero.
*/
void clear() { resize(0); }
};

#endif
3 changes: 2 additions & 1 deletion src/core/cells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "layered.hpp"
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "nsquare.hpp"
#include "particle_data.hpp"

#include <utils/NoOp.hpp>
#include <utils/mpi/gather_buffer.hpp>
Expand Down Expand Up @@ -409,7 +410,7 @@ void cells_resort_particles(int global_flag) {
resort_particles = Cells::RESORT_NONE;
rebuild_verletlist = true;

realloc_particlelist(&displaced_parts, 0);
displaced_parts.clear();

on_resort_particles(local_cells.particles());
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
*/

#include "collision.hpp"

#ifdef COLLISION_DETECTION
#include "Particle.hpp"
#include "cells.hpp"
#include "communication.hpp"
#include "errorhandling.hpp"
#include "event.hpp"
#include "grid.hpp"
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "particle_data.hpp"
#include "rotation.hpp"
#include "virtual_sites/VirtualSitesRelative.hpp"

Expand All @@ -37,8 +40,6 @@

#include <vector>

#ifdef COLLISION_DETECTION

/// Data type holding the info about a single collision
typedef struct {
int pp1; // 1st particle id
Expand Down
1 change: 1 addition & 0 deletions src/core/cuda_common_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "cuda_utils.hpp"
#include "errorhandling.hpp"
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "particle_data.hpp"

#include <utils/constants.hpp>

Expand Down
1 change: 1 addition & 0 deletions src/core/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "cells.hpp"
#include "errorhandling.hpp"
#include "grid.hpp"
#include "particle_data.hpp"

void check_particle_consistency() {
int n, c;
Expand Down
9 changes: 5 additions & 4 deletions src/core/domain_decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "communication.hpp"
#include "errorhandling.hpp"
#include "grid.hpp"
#include "particle_data.hpp"

#include "serialization/ParticleList.hpp"
#include <utils/index.hpp>
Expand Down Expand Up @@ -699,7 +700,7 @@ void move_if_local(ParticleList &src, ParticleList &rest) {
}
}

realloc_particlelist(&src, src.n = 0);
src.resize(0);
}

/**
Expand Down Expand Up @@ -760,7 +761,7 @@ void exchange_neighbors(ParticleList *pl, const Utils::Vector3i &grid) {
Utils::Mpi::sendrecv(comm_cart, node_neighbors[2 * dir], 0, send_buf,
node_neighbors[2 * dir], 0, recv_buf);

realloc_particlelist(&send_buf, 0);
send_buf.clear();

move_if_local(recv_buf, *pl);
} else {
Expand All @@ -783,8 +784,8 @@ void exchange_neighbors(ParticleList *pl, const Utils::Vector3i &grid) {
move_if_local(recv_buf_l, *pl);
move_if_local(recv_buf_r, *pl);

realloc_particlelist(&send_buf_l, 0);
realloc_particlelist(&send_buf_r, 0);
send_buf_l.clear();
send_buf_r.clear();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "errorhandling.hpp"
#include "grid.hpp"
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "particle_data.hpp"

#include <utils/constants.hpp>

Expand Down
1 change: 1 addition & 0 deletions src/core/electrostatics_magnetostatics/mdlc_correction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "errorhandling.hpp"
#include "global.hpp"
#include "grid.hpp"
#include "particle_data.hpp"

DLC_struct dlc_params = {1e100, 0, 0, 0, 0};

Expand Down
1 change: 1 addition & 0 deletions src/core/electrostatics_magnetostatics/p3m_gpu_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "EspressoSystemInterface.hpp"
#include "electrostatics_magnetostatics/coulomb.hpp"
#include "global.hpp"
#include "particle_data.hpp"

#include <utils/math/int_pow.hpp>
using Utils::int_pow;
Expand Down
1 change: 1 addition & 0 deletions src/core/electrostatics_magnetostatics/scafacos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "global.hpp"
#include "grid.hpp"
#include "integrate.hpp"
#include "particle_data.hpp"
#include "tuning.hpp"

#include "electrostatics_magnetostatics/coulomb.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/core/forces_inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "object-in-fluid/oif_global_forces.hpp"
#include "object-in-fluid/oif_local_forces.hpp"
#include "object-in-fluid/out_direction.hpp"
#include "particle_data.hpp"
#include "thermostat.hpp"

#ifdef DIPOLES
Expand Down
13 changes: 8 additions & 5 deletions src/core/ghosts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "Particle.hpp"
#include "communication.hpp"
#include "errorhandling.hpp"
#include "particle_data.hpp"

#include <algorithm>
#include <cstdio>
Expand Down Expand Up @@ -192,11 +193,12 @@ void prepare_send_buffer(GhostCommunication *gc, int data_parts) {

static void prepare_ghost_cell(Cell *cell, int size) {
using Utils::make_span;
auto const old_cap = cell->max;
auto const old_cap = cell->capacity();

/* reset excess particles */
if (size < cell->max) {
for (auto &p : make_span<Particle>(cell->part + size, cell->max - size)) {
if (size < cell->capacity()) {
for (auto &p :
make_span<Particle>(cell->part + size, cell->capacity() - size)) {
p = Particle{};
p.l.ghost = true;
}
Expand All @@ -206,8 +208,9 @@ static void prepare_ghost_cell(Cell *cell, int size) {
cell->resize(size);

/* initialize new particles */
if (old_cap < cell->max) {
auto new_parts = make_span(cell->part + old_cap, cell->max - old_cap);
if (old_cap < cell->capacity()) {
auto new_parts =
make_span(cell->part + old_cap, cell->capacity() - old_cap);
std::uninitialized_fill(new_parts.begin(), new_parts.end(), Particle{});
for (auto &p : new_parts) {
p.l.ghost = true;
Expand Down
2 changes: 2 additions & 0 deletions src/core/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "npt.hpp"
#include "object-in-fluid/oif_global_forces.hpp"
#include "particle_data.hpp"
#include "rattle.hpp"
#include "thermostat.hpp"
#include "tuning.hpp"

#include <utils/mpi/all_compare.hpp>

#include <boost/functional/hash.hpp>
Expand Down
1 change: 1 addition & 0 deletions src/core/grid_based_algorithms/lb_particle_coupling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "lb_interface.hpp"
#include "lb_interpolation.hpp"
#include "lbgpu.hpp"
#include "particle_data.hpp"
#include "random.hpp"

#include <profiler/profiler.hpp>
Expand Down
1 change: 1 addition & 0 deletions src/core/immersed_boundary/ImmersedBoundaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "communication.hpp"
#include "errorhandling.hpp"
#include "grid.hpp"
#include "particle_data.hpp"

#include <utils/constants.hpp>

Expand Down
1 change: 1 addition & 0 deletions src/core/integrators/steepest_descent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "communication.hpp"
#include "event.hpp"
#include "integrate.hpp"
#include "particle_data.hpp"
#include "rotation.hpp"

#include <utils/math/sqr.hpp>
Expand Down
5 changes: 3 additions & 2 deletions src/core/integrators/velocity_verlet_npt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
#include "config.hpp"

#ifdef NPT
#include "Particle.hpp"
#include "ParticleRange.hpp"
#include "cells.hpp"
#include "communication.hpp"
#include "grid.hpp"
#include "integrate.hpp"
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
#include "npt.hpp"
#include "particle_data.hpp"
#include "thermostat.hpp"
#include "utils/math/sqr.hpp"

#include <utils/math/sqr.hpp>

void velocity_verlet_npt_propagate_vel_final(const ParticleRange &particles) {
nptiso.p_vel[0] = nptiso.p_vel[1] = nptiso.p_vel[2] = 0.0;
Expand Down
1 change: 1 addition & 0 deletions src/core/io/mpiio/mpiio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "event.hpp"
#include "integrate.hpp"
#include "mpiio.hpp"
#include "particle_data.hpp"

#include <mpi.h>

Expand Down
4 changes: 2 additions & 2 deletions src/core/layered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,6 @@ void layered_exchange_and_sort_particles(int global_flag,
}
}

realloc_particlelist(&recv_buf_up, 0);
realloc_particlelist(&recv_buf_dn, 0);
recv_buf_up.clear();
recv_buf_dn.clear();
}
2 changes: 1 addition & 1 deletion src/core/nsquare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void nsq_exchange_particles(int global_flag, ParticleList *displaced_parts) {
auto const target_node = (p.identity() % n_nodes);
send_buf.at(target_node).emplace_back(std::move(p));
}
realloc_particlelist(displaced_parts, displaced_parts->n = 0);
displaced_parts->resize(0);

/* Exchange particles */
std::vector<std::vector<Particle>> recv_buf(n_nodes);
Expand Down
1 change: 1 addition & 0 deletions src/core/object-in-fluid/oif_global_forces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "errorhandling.hpp"
#include "grid.hpp"
#include "grid_based_algorithms/lb_interface.hpp"
#include "particle_data.hpp"

#include <utils/math/triangle_functions.hpp>
using Utils::angle_btw_triangles;
Expand Down
Loading