28#include "communication.hpp"
31#include "system/System.hpp"
38#include <boost/container/flat_set.hpp>
39#include <boost/mpi/collectives/all_reduce.hpp>
40#include <boost/mpi/communicator.hpp>
41#include <boost/mpi/request.hpp>
42#include <boost/range/numeric.hpp>
44#include <Kokkos_Core.hpp>
53#include <initializer_list>
64int RegularDecomposition::position_to_cell_index(
68 for (
auto i = 0
u; i < 3u; i++) {
95void RegularDecomposition::move_if_local(
98 for (
auto &part : src) {
105 rest.
insert(std::move(part));
112void RegularDecomposition::move_left_or_right(
ParticleList &src,
123 auto const pos =
it->pos()[dir];
128 left.insert(std::move(*
it));
136void RegularDecomposition::exchange_neighbors(
141 for (
int dir = 0; dir < 3; dir++) {
143 if (Utils::Mpi::cart_get<3>(
m_comm).dims[dir] == 1) {
148 if (Utils::Mpi::cart_get<3>(
m_comm).dims[dir] == 2) {
156 using boost::mpi::request;
167 boost::mpi::wait_all(
reqs.begin(),
reqs.end());
188 std::vector<ParticleChange> &
diff) {
212 if (Kokkos::DefaultHostExecutionSpace().concurrency() == 1) {
216 for (
auto it = c->particles().begin();
it != c->particles().end();) {
227 auto p = std::move(*
it);
228 it = c->particles().erase(
it);
233 using exec_space = Kokkos::DefaultHostExecutionSpace;
247 Kokkos::RangePolicy<exec_space> policy(std::size_t{0},
n_cells);
248 Kokkos::parallel_for(
249 "RegularDecomposition::resort::classify", policy,
250 [&](std::size_t
const ci) {
253 for (
auto &p : cell->particles()) {
256 }
catch (std::exception
const &
err) {
279 std::vector<int>
slot;
286 auto &
parts = c->particles();
287 auto const n =
static_cast<int>(
parts.size());
293 std::iota(
slot.begin(),
slot.end(), 0);
305 auto p = std::move(*(
parts.begin() + i));
314 auto const grid = Utils::Mpi::cart_get<3>(
m_comm).dims;
323 std::plus<std::size_t>());
338 <<
"than one local box length in one timestep";
339 sort_cell->particles().insert(std::move(part));
346void RegularDecomposition::mark_cells() {
371int RegularDecomposition::calc_processor_min_num_cells()
const {
377 return boost::accumulate(Utils::Mpi::cart_get<3>(
m_comm).dims, 1,
383void RegularDecomposition::create_cell_grid(
double range) {
401 auto const scale = std::cbrt(RegularDecomposition::max_num_cells / volume);
403 for (
auto i = 0
u; i < 3u; i++) {
408 if (cell_range[i] <
range) {
413 <<
"interaction range " <<
range <<
" in direction " << i
414 <<
" is larger than the local box size " <<
local_box_l[i];
436 for (
auto i = 1u; i < 3u; ++i) {
451 <<
": either interaction range is too large for "
452 <<
"the current skin (range=" <<
range <<
", "
453 <<
"half_local_box_l=[" <<
local_box_l / 2. <<
"]) "
454 <<
"or min_num_cells too large";
466 for (
auto i = 0
u; i < 3u; i++) {
482 return boost::container::flat_set<K, std::remove_reference_t<Comparator>>(
483 std::forward<Comparator>(
comp));
486void RegularDecomposition::init_cell_interactions() {
517 return std::abs((a - b)[
fc_dir]) > 1;
547 throw std::domain_error(
"fully_connected_boundary normal and connection "
548 "coordinates need to differ.");
550 if (node_grid[
fc_dir] != 1) {
551 throw std::runtime_error(
552 "The MPI nodegrid must be 1 in the fully connected direction.");
555 throw std::runtime_error(
556 "The fully connected boundary requires periodicity in the "
557 "boundary normal direction.");
569 for (
int o = start[2];
o < end[2];
o++)
570 for (
int n = start[1]; n < end[1]; n++)
571 for (
int m = start[0]; m < end[0]; m++) {
591 for (
auto i = 0
u; i < 3u; i++) {
630 for (
auto coord : {0
u, 1u, 2u}) {
643 auto cell = &
cells.at(
704 .at(
static_cast<std::size_t
>(
715 std::vector<std::pair<int, ParticleList *>> recv;
716 std::vector<std::pair<int, ParticleList *>> send;
718 std::map<int, PeerBucket>
peers;
719 std::vector<std::pair<int, LocalComm>> local;
743 bool is_ghost =
false;
744 for (
auto d = 0
u; d < 3u; ++d) {
777 for (
auto d = 0
u; d < 3u; ++d) {
780 else if (
side[d] == +1)
790 auto const by_key = [](
auto const &a,
auto const &b) {
791 return a.first < b.first;
798 nc.recv.reserve(
bucket.recv.size());
799 for (
auto const &[
key, cell] :
bucket.recv)
801 nc.send.reserve(
bucket.send.size());
802 for (
auto const &[
key, cell] :
bucket.send)
804 plan.neighbors.push_back(std::move(
nc));
810 local, [](
auto const &a,
auto const &b) {
return a.first < b.first; });
811 plan.local.reserve(local.size());
812 for (
auto &[
key,
lc] : local)
822 : m_comm(
std::
move(comm)), m_box(box_geo), m_local_box(local_geo),
826 create_cell_grid(
range);
829 init_cell_interactions();
856 return static_cast<int>(c -
cells.data());
870 for (
int i = 0; i < 3; ++i)
881 for (
int i = 0; i < 3; ++i) {
911 for (
int i = 0; i < 3; ++i) {
914 c->m_is_boundary =
true;
927#ifdef ESPRESSO_ADDITIONAL_CHECKS
930 "RegularDecomposition"));
static int coord(std::string const &s)
auto make_flat_set(Comparator &&comp)
static void fold_and_reset(Particle &p, BoxGeometry const &box_geo)
Fold coordinates to box and reset the old position.
Vector implementation and trait types for boost qvm interoperability.
Utils::Vector3d const & length() const
Box length.
constexpr bool periodic(unsigned coord) const
Check periodicity in direction.
T get_mi_coord(T a, T b, unsigned coord) const noexcept
Get the minimum-image distance between two coordinates.
void fold_position(Utils::Vector3d &pos, Utils::Vector3i &image_box) const
Fold coordinates to primary simulation box in-place.
auto const & my_right() const
Right (top, back) corner of this nodes local box.
auto const & boundary() const
Boundary information for the local box.
auto const & my_left() const
Left (bottom, front) corner of this nodes local box.
auto const & length() const
Dimensions of the box a single node is responsible for.
void clear()
Remove all elements form container.
T & insert(T const &v)
Insert an element into the container.
iterator erase(iterator it)
Remove element from the list.
DEVICE_QUALIFIER constexpr size_type size() const noexcept
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value) noexcept
Create a vector that has all entries set to the same value.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
Communicator communicator
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeErrorMsg()
ParticleRange particles(std::span< Cell *const > cells)
void mark_boundary_cells(std::span< Cell *const > local_cells, std::span< Cell *const > ghost_cells, std::function< bool(Cell const *, Cell const *)> wrap_predicate=nullptr)
Classify each local cell as interior or boundary.
void mark_plan_cells_boundary(HaloPlan const &plan, std::span< Cell *const > local_cells)
Mark plan-exported local cells as boundary (source 2, see mark_boundary_cells).
bool report_violations(std::vector< std::string > const &violations, char const *context)
Print violations to stderr and return whether the list was empty.
std::vector< std::string > validate_halo_plan(HaloPlan const &plan, std::span< Cell *const > local_cells, std::span< Cell *const > ghost_cells)
Validate a HaloPlan for correctness.
std::array< mpi::request, 2 > isendrecv(mpi::communicator const &comm, int dest, int stag, const T &sval, int src, int rtag, T &rval)
mpi::status sendrecv(mpi::communicator const &comm, int dest, int stag, const T &sval, int src, int rtag, T &rval)
T product(Vector< T, N > const &v)
DEVICE_QUALIFIER int get_linear_index(int a, int b, int c, Vector3i const &adim)
auto hadamard_division(Vector< T, N > const &a, Vector< U, N > const &b)
auto hadamard_product(Vector< T, N > const &a, Vector< U, N > const &b)
Utils::Vector3i node_grid
Struct holding all information for one particle.
constexpr auto const & pos() const
constexpr auto & pos_at_last_verlet_update()
constexpr auto const & image_box() const
Utils::Vector3i ghost_cell_grid
linked cell grid with ghost frame.
Utils::Vector3d max_cutoff() const override
Utils::Vector3d inv_cell_size
inverse cell_size.
std::vector< Cell * > m_ghost_cells
BoxGeometry const & m_box
Cell * particle_to_cell(Particle const &p) override
auto fully_connected_boundary() const
void resort(bool global, std::vector< ParticleChange > &diff) override
std::vector< Cell > cells
RegularDecomposition(boost::mpi::communicator comm, double range, BoxGeometry const &box_geo, LocalBox const &local_geo, std::optional< std::pair< int, int > > fully_connected)
Utils::Vector3d cell_size
Cell size.
std::span< Cell *const > local_cells() const override
GhostComm::HaloPlan m_halo_plan
Topology-agnostic direct-neighbor halo plan (see make_halo_plan).
std::vector< Cell * > m_local_cells
Utils::Vector3d max_range() const override
std::span< Cell *const > ghost_cells() const override
Utils::Vector3i cell_grid
Grid dimensions per node.
Utils::Vector3i cell_offset
Offset in global grid.
boost::mpi::communicator m_comm