26#include "communication.hpp"
29#include "system/System.hpp"
36#include <boost/container/flat_set.hpp>
37#include <boost/mpi/collectives.hpp>
38#include <boost/mpi/communicator.hpp>
39#include <boost/range/algorithm/reverse.hpp>
40#include <boost/range/numeric.hpp>
51int RegularDecomposition::position_to_cell_index(
55 for (
auto i = 0
u; i < 3u; i++) {
82void RegularDecomposition::move_if_local(
84 std::vector<ParticleChange> &modified_cells) {
85 for (
auto &part : src) {
86 auto target_cell = position_to_cell(part.pos());
89 target_cell->particles().insert(std::move(part));
90 modified_cells.emplace_back(
ModifiedList{target_cell->particles()});
92 rest.
insert(std::move(part));
99void RegularDecomposition::move_left_or_right(
ParticleList &src,
105 auto const can_move_left =
m_box.
periodic(dir) or not is_open_boundary_left;
106 auto const can_move_right =
m_box.
periodic(dir) or not is_open_boundary_right;
109 for (
auto it = src.
begin(); it != src.
end();) {
110 auto const pos = it->pos()[dir];
112 right.
insert(std::move(*it));
115 left.
insert(std::move(*it));
123void RegularDecomposition::exchange_neighbors(
124 ParticleList &pl, std::vector<ParticleChange> &modified_cells) {
125 auto const node_neighbors = Utils::Mpi::cart_neighbors<3>(
m_comm);
126 static ParticleList send_buf_l, send_buf_r, recv_buf_l, recv_buf_r;
128 for (
int dir = 0; dir < 3; dir++) {
130 if (Utils::Mpi::cart_get<3>(
m_comm).dims[dir] == 1) {
135 if (Utils::Mpi::cart_get<3>(
m_comm).dims[dir] == 2) {
136 move_left_or_right(pl, send_buf_l, send_buf_l, dir);
139 node_neighbors[2 * dir], 0, recv_buf_l);
143 using boost::mpi::request;
146 move_left_or_right(pl, send_buf_l, send_buf_r, dir);
149 node_neighbors[2 * dir], 0, recv_buf_l);
150 auto req_r =
isendrecv(
m_comm, node_neighbors[2 * dir + 1], 0, send_buf_r,
151 node_neighbors[2 * dir + 1], 0, recv_buf_r);
153 std::array<request, 4> reqs{{req_l[0], req_l[1], req_r[0], req_r[1]}};
154 boost::mpi::wait_all(reqs.begin(), reqs.end());
160 move_if_local(recv_buf_l, pl, modified_cells);
161 move_if_local(recv_buf_r, pl, modified_cells);
175 std::vector<ParticleChange> &diff) {
179 for (
auto it = c->particles().begin(); it != c->particles().end();) {
185 if (target_cell == c) {
190 auto p = std::move(*it);
191 it = c->particles().erase(it);
195 if (target_cell ==
nullptr) {
197 displaced_parts.
insert(std::move(p));
200 else if (target_cell != c) {
201 target_cell->particles().insert(std::move(p));
202 diff.emplace_back(
ModifiedList{target_cell->particles()});
208 auto const grid = Utils::Mpi::cart_get<3>(
m_comm).dims;
212 int rounds_left = grid[0] + grid[1] + grid[2] - 3;
213 for (; rounds_left > 0; rounds_left--) {
214 exchange_neighbors(displaced_parts, diff);
216 auto left_over = boost::mpi::all_reduce(
m_comm, displaced_parts.
size(),
217 std::plus<std::size_t>());
219 if (left_over == 0) {
224 exchange_neighbors(displaced_parts, diff);
227 if (not displaced_parts.
empty()) {
230 for (
auto &part : displaced_parts) {
232 <<
"than one local box length in one timestep";
233 sort_cell->particles().insert(std::move(part));
235 diff.emplace_back(
ModifiedList{sort_cell->particles()});
240void RegularDecomposition::mark_cells() {
256void RegularDecomposition::fill_comm_cell_lists(
ParticleList **part_lists,
259 for (
int o = lc[0]; o <= hc[0]; o++)
260 for (
int n = lc[1]; n <= hc[1]; n++)
261 for (
int m = lc[2]; m <= hc[2]; m++) {
264 *part_lists++ = &(
cells.at(i).particles());
269 auto dir_max_range = [
this](
unsigned int i) {
273 return {dir_max_range(0
u), dir_max_range(1u), dir_max_range(2u)};
277int RegularDecomposition::calc_processor_min_num_cells()
const {
283 return boost::accumulate(Utils::Mpi::cart_get<3>(
m_comm).dims, 1,
284 [](
int n_cells,
int grid) {
285 return (grid == 1) ? 2 * n_cells : n_cells;
289void RegularDecomposition::create_cell_grid(
double range) {
290 auto const cart_info = Utils::Mpi::cart_get<3>(
m_comm);
294 auto const min_num_cells = calc_processor_min_num_cells();
298 auto const cells_per_dir =
299 static_cast<int>(std::ceil(std::cbrt(min_num_cells)));
307 auto const scale = std::cbrt(RegularDecomposition::max_num_cells / volume);
309 for (
auto i = 0
u; i < 3u; i++) {
311 cell_grid[i] =
static_cast<int>(std::ceil(local_box_l[i] * scale));
312 cell_range[i] = local_box_l[i] /
static_cast<double>(
cell_grid[i]);
314 if (cell_range[i] < range) {
316 cell_grid[i] =
static_cast<int>(std::floor(local_box_l[i] / range));
319 <<
"interaction range " << range <<
" in direction " << i
320 <<
" is larger than the local box size " << local_box_l[i];
323 cell_range[i] = local_box_l[i] /
static_cast<double>(
cell_grid[i]);
335 if (n_local_cells <= RegularDecomposition::max_num_cells)
340 auto min_size = cell_range[0];
342 for (
auto i = 1u; i < 3u; ++i) {
343 if (
cell_grid[i] > 1 and cell_range[i] < min_size) {
345 min_size = cell_range[i];
354 if (n_local_cells < min_num_cells) {
356 <<
" is smaller than minimum " << min_num_cells
357 <<
": either interaction range is too large for "
358 <<
"the current skin (range=" << range <<
", "
359 <<
"half_local_box_l=[" << local_box_l / 2. <<
"]) "
360 <<
"or min_num_cells too large";
364 if (n_local_cells > RegularDecomposition::max_num_cells) {
368 auto const node_pos = cart_info.coords;
372 for (
auto i = 0
u; i < 3u; i++) {
382 cells.resize(
static_cast<unsigned int>(new_cells));
388 return boost::container::flat_set<K, std::remove_reference_t<Comparator>>(
389 std::forward<Comparator>(comp));
392void RegularDecomposition::init_cell_interactions() {
395 auto const cart_info = Utils::Mpi::cart_get<3>(
m_comm);
396 auto const &node_pos = cart_info.coords;
405 return (global_halo_offset + local_index);
410 auto const folded_index = (global_index + global_size) % global_size;
418 return (global_index - global_halo_offset);
427 for (
int o = start[2]; o < end[2]; o++)
428 for (
int n = start[1]; n < end[1]; n++)
429 for (
int m = start[0]; m < end[0]; m++) {
451 for (
int i = 0; i < 3; i++) {
453 lower_index[i] = std::max(0, lower_index[i]);
454 upper_index[i] = std::min(global_size[i] - 1, upper_index[i]);
460 auto neighbors = make_flat_set<Utils::Vector3i>(
462 return folded_linear_index(a) < folded_linear_index(b);
466 for (
int p = lower_index[2]; p <= upper_index[2]; p++)
467 for (
int q = lower_index[1]; q <= upper_index[1]; q++)
468 for (
int r = lower_index[0]; r <= upper_index[0]; r++) {
473 auto const ind1 = folded_linear_index({m, n, o});
475 std::vector<Cell *> red_neighbors;
476 std::vector<Cell *> black_neighbors;
477 for (
auto const &neighbor : neighbors) {
478 auto const ind2 = folded_linear_index(neighbor);
483 auto cell = &
cells.at(
486 red_neighbors.push_back(cell);
488 black_neighbors.push_back(cell);
513 boost::reverse(c.part_lists);
524 auto next = std::next(it);
535 int dir, lr, i, cnt, n_comm_cells[3];
538 auto const comm_info = Utils::Mpi::cart_get<3>(
m_comm);
539 auto const node_neighbors = Utils::Mpi::cart_neighbors<3>(
m_comm);
543 for (dir = 0; dir < 3; dir++) {
544 for (lr = 0; lr < 2; lr++) {
546 if (comm_info.dims[dir] == 1)
563 for (dir = 0; dir < 3; dir++) {
564 lc[(dir + 1) % 3] = 1 - done[(dir + 1) % 3];
565 lc[(dir + 2) % 3] = 1 - done[(dir + 2) % 3];
566 hc[(dir + 1) % 3] =
cell_grid[(dir + 1) % 3] + done[(dir + 1) % 3];
567 hc[(dir + 2) % 3] =
cell_grid[(dir + 2) % 3] + done[(dir + 2) % 3];
572 for (lr = 0; lr < 2; lr++) {
573 if (comm_info.dims[dir] == 1) {
575 ghost_comm.communications[cnt].type =
GHOST_LOCL;
576 ghost_comm.communications[cnt].node =
m_comm.rank();
579 ghost_comm.communications[cnt].part_lists.resize(2 * n_comm_cells[dir]);
582 lc[dir] = hc[dir] = 1 + lr * (
cell_grid[dir] - 1);
584 fill_comm_cell_lists(ghost_comm.communications[cnt].part_lists.data(),
588 lc[dir] = hc[dir] = 0 + (1 - lr) * (
cell_grid[dir] + 1);
591 fill_comm_cell_lists(
592 &ghost_comm.communications[cnt].part_lists[n_comm_cells[dir]], lc,
598 for (i = 0; i < 2; i++) {
599 if ((comm_info.coords[dir] + i) % 2 == 0) {
600 ghost_comm.communications[cnt].type =
GHOST_SEND;
601 ghost_comm.communications[cnt].node = node_neighbors[2 * dir + lr];
602 ghost_comm.communications[cnt].part_lists.resize(n_comm_cells[dir]);
605 lc[dir] = hc[dir] = 1 + lr * (
cell_grid[dir] - 1);
607 fill_comm_cell_lists(
608 ghost_comm.communications[cnt].part_lists.data(), lc, hc);
611 if ((comm_info.coords[dir] + (1 - i)) % 2 == 0) {
612 ghost_comm.communications[cnt].type =
GHOST_RECV;
613 ghost_comm.communications[cnt].node =
614 node_neighbors[2 * dir + (1 - lr)];
615 ghost_comm.communications[cnt].part_lists.resize(n_comm_cells[dir]);
617 lc[dir] = hc[dir] = (1 - lr) * (
cell_grid[dir] + 1);
619 fill_comm_cell_lists(
620 ghost_comm.communications[cnt].part_lists.data(), lc, hc);
636 : m_comm(std::move(comm)), m_box(box_geo), m_local_box(local_geo) {
638 create_cell_grid(range);
641 init_cell_interactions();
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.
__shared__ int pos[MAXDEPTH *THREADS5/WARPSIZE]
T get_mi_coord(T a, T b, unsigned coord) const
Get the minimum-image distance between two coordinates.
Utils::Vector3d const & length() const
Box length.
constexpr bool periodic(unsigned coord) const
Check periodicity in direction.
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.
bool empty() const
Is the container empty?
T & insert(T const &v)
Insert an element into the container.
iterator erase(iterator it)
Remove element from the list.
std::size_t size() const
Number of elements in the container.
static Vector< T, N > broadcast(T const &s)
Create a vector that has all entries set to one value.
Communicator communicator
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeErrorMsg()
#define GHOST_RECV
recv from a single node
#define GHOST_PSTSTORE
additional flag for poststoring
#define GHOST_LOCL
transfer data from cell to cell on this node
#define GHOST_SEND
send to a single node
#define GHOST_PREFETCH
additional flag for prefetching
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)
int get_linear_index(int a, int b, int c, const Vector3i &adim, MemoryOrder memory_order=MemoryOrder::COLUMN_MAJOR)
get the linear index from the position (a,b,c) in a 3D grid of dimensions adim.
auto hadamard_product(Vector< T, N > const &a, Vector< U, N > const &b)
void assign_prefetches(GhostCommunicator &comm)
Of every two communication rounds, set the first receivers to prefetch and poststore.
void revert_comm_order(GhostCommunicator &comm)
Revert the order of a communicator: After calling this the communicator is working in reverted order ...
Utils::Vector3i node_grid
Properties for a ghost communication.
std::vector< GhostCommunication > communications
List of ghost communications.
Struct holding all information for one particle.
auto & pos_at_last_verlet_update()
auto const & image_box() const
GhostCommunicator m_exchange_ghosts_comm
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
void resort(bool global, std::vector< ParticleChange > &diff) override
Utils::Span< Cell *const > local_cells() const override
std::vector< Cell > cells
Utils::Vector3d cell_size
Cell size.
std::vector< Cell * > m_local_cells
Utils::Vector3d max_range() const override
GhostCommunicator m_collect_ghost_force_comm
RegularDecomposition(boost::mpi::communicator comm, double range, BoxGeometry const &box_geo, LocalBox const &local_geo)
Utils::Vector3i cell_grid
Grid dimensions per node.
Utils::Vector3i cell_offset
Offset in global grid.
boost::mpi::communicator m_comm