Skip to content

Commit

Permalink
Merge pull request #48 from nextsimhub/neighbour
Browse files Browse the repository at this point in the history
Fix typo and use "neighbour"
  • Loading branch information
jwallwork23 authored Sep 11, 2024
2 parents 629b625 + 88cba68 commit 82329d1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 130 deletions.
119 changes: 60 additions & 59 deletions Partitioner.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* @file Partitioner.cpp
* @author Athena Elafrou <ae488@cam.ac.uk>
* @date 23 Aug 2024
* @date 11 Sep 2024
*/

#include "Partitioner.hpp"
Expand Down Expand Up @@ -30,33 +30,33 @@ void Partitioner::get_bounding_box(
local_ext_1 = _local_ext_1_new;
}

void Partitioner::get_top_neighbors(std::vector<int>& ids, std::vector<int>& halo_sizes) const
void Partitioner::get_top_neighbours(std::vector<int>& ids, std::vector<int>& halo_sizes) const
{
for (auto it = _top_neighbors.begin(); it != _top_neighbors.end(); ++it) {
for (auto it = _top_neighbours.begin(); it != _top_neighbours.end(); ++it) {
ids.push_back(it->first);
halo_sizes.push_back(it->second);
}
}

void Partitioner::get_bottom_neighbors(std::vector<int>& ids, std::vector<int>& halo_sizes) const
void Partitioner::get_bottom_neighbours(std::vector<int>& ids, std::vector<int>& halo_sizes) const
{
for (auto it = _bottom_neighbors.begin(); it != _bottom_neighbors.end(); ++it) {
for (auto it = _bottom_neighbours.begin(); it != _bottom_neighbours.end(); ++it) {
ids.push_back(it->first);
halo_sizes.push_back(it->second);
}
}

void Partitioner::get_left_neighbors(std::vector<int>& ids, std::vector<int>& halo_sizes) const
void Partitioner::get_left_neighbours(std::vector<int>& ids, std::vector<int>& halo_sizes) const
{
for (auto it = _left_neighbors.begin(); it != _left_neighbors.end(); ++it) {
for (auto it = _left_neighbours.begin(); it != _left_neighbours.end(); ++it) {
ids.push_back(it->first);
halo_sizes.push_back(it->second);
}
}

void Partitioner::get_right_neighbors(std::vector<int>& ids, std::vector<int>& halo_sizes) const
void Partitioner::get_right_neighbours(std::vector<int>& ids, std::vector<int>& halo_sizes) const
{
for (auto it = _right_neighbors.begin(); it != _right_neighbors.end(); ++it) {
for (auto it = _right_neighbours.begin(); it != _right_neighbours.end(); ++it) {
ids.push_back(it->first);
halo_sizes.push_back(it->second);
}
Expand Down Expand Up @@ -106,31 +106,31 @@ void Partitioner::save_metadata(const std::string& filename) const
nc_mode = NC_MPIIO | NC_NETCDF4;
NC_CHECK(nc_create_par(filename.c_str(), nc_mode, _comm, MPI_INFO_NULL, &nc_id));

// Prepare neighbor data
// Prepare neighbour data
std::vector<int> top_ids, bottom_ids, left_ids, right_ids;
std::vector<int> top_halos, bottom_halos, left_halos, right_halos;
get_top_neighbors(top_ids, top_halos);
get_bottom_neighbors(bottom_ids, bottom_halos);
get_left_neighbors(left_ids, left_halos);
get_right_neighbors(right_ids, right_halos);
int top_num_neighbors = top_ids.size();
int bottom_num_neighbors = bottom_ids.size();
int left_num_neighbors = left_ids.size();
int right_num_neighbors = right_ids.size();
get_top_neighbours(top_ids, top_halos);
get_bottom_neighbours(bottom_ids, bottom_halos);
get_left_neighbours(left_ids, left_halos);
get_right_neighbours(right_ids, right_halos);
int top_num_neighbours = top_ids.size();
int bottom_num_neighbours = bottom_ids.size();
int left_num_neighbours = left_ids.size();
int right_num_neighbours = right_ids.size();

// Compute global dimensions
int top_dim, bottom_dim, left_dim, right_dim;
CHECK_MPI(MPI_Allreduce(&top_num_neighbors, &top_dim, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Allreduce(&bottom_num_neighbors, &bottom_dim, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Allreduce(&left_num_neighbors, &left_dim, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Allreduce(&right_num_neighbors, &right_dim, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Allreduce(&top_num_neighbours, &top_dim, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Allreduce(&bottom_num_neighbours, &bottom_dim, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Allreduce(&left_num_neighbours, &left_dim, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Allreduce(&right_num_neighbours, &right_dim, 1, MPI_INT, MPI_SUM, _comm));

// Compute global offsets
int top_offset = 0, bottom_offset = 0, left_offset = 0, right_offset = 0;
CHECK_MPI(MPI_Exscan(&top_num_neighbors, &top_offset, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Exscan(&bottom_num_neighbors, &bottom_offset, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Exscan(&left_num_neighbors, &left_offset, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Exscan(&right_num_neighbors, &right_offset, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Exscan(&top_num_neighbours, &top_offset, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Exscan(&bottom_num_neighbours, &bottom_offset, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Exscan(&left_num_neighbours, &left_offset, 1, MPI_INT, MPI_SUM, _comm));
CHECK_MPI(MPI_Exscan(&right_num_neighbours, &right_offset, 1, MPI_INT, MPI_SUM, _comm));

// Create 2 dimensions
// The values to be written are associated with the netCDF variable by
Expand Down Expand Up @@ -166,25 +166,26 @@ void Partitioner::save_metadata(const std::string& filename) const
NC_CHECK(nc_def_var(bbox_gid, "domain_extent_x", NC_INT, 1, &dimid, &cnt_x_vid));
NC_CHECK(nc_def_var(bbox_gid, "domain_extent_y", NC_INT, 1, &dimid, &cnt_y_vid));
// Connectivity group
NC_CHECK(nc_def_var(connectivity_gid, "top_neighbors", NC_INT, 1, &dimid, &top_num_vid));
NC_CHECK(nc_def_var(connectivity_gid, "top_neighbor_ids", NC_INT, 1, &top_dimid, &top_ids_vid));
NC_CHECK(nc_def_var(connectivity_gid, "top_neighbours", NC_INT, 1, &dimid, &top_num_vid));
NC_CHECK(
nc_def_var(connectivity_gid, "top_neighbor_halos", NC_INT, 1, &top_dimid, &top_halos_vid));
NC_CHECK(nc_def_var(connectivity_gid, "bottom_neighbors", NC_INT, 1, &dimid, &bottom_num_vid));
nc_def_var(connectivity_gid, "top_neighbour_ids", NC_INT, 1, &top_dimid, &top_ids_vid));
NC_CHECK(
nc_def_var(connectivity_gid, "top_neighbour_halos", NC_INT, 1, &top_dimid, &top_halos_vid));
NC_CHECK(nc_def_var(connectivity_gid, "bottom_neighbours", NC_INT, 1, &dimid, &bottom_num_vid));
NC_CHECK(nc_def_var(
connectivity_gid, "bottom_neighbor_ids", NC_INT, 1, &bottom_dimid, &bottom_ids_vid));
connectivity_gid, "bottom_neighbour_ids", NC_INT, 1, &bottom_dimid, &bottom_ids_vid));
NC_CHECK(nc_def_var(
connectivity_gid, "bottom_neighbor_halos", NC_INT, 1, &bottom_dimid, &bottom_halos_vid));
NC_CHECK(nc_def_var(connectivity_gid, "left_neighbors", NC_INT, 1, &dimid, &left_num_vid));
connectivity_gid, "bottom_neighbour_halos", NC_INT, 1, &bottom_dimid, &bottom_halos_vid));
NC_CHECK(nc_def_var(connectivity_gid, "left_neighbours", NC_INT, 1, &dimid, &left_num_vid));
NC_CHECK(
nc_def_var(connectivity_gid, "left_neighbor_ids", NC_INT, 1, &left_dimid, &left_ids_vid));
nc_def_var(connectivity_gid, "left_neighbour_ids", NC_INT, 1, &left_dimid, &left_ids_vid));
NC_CHECK(nc_def_var(
connectivity_gid, "left_neighbor_halos", NC_INT, 1, &left_dimid, &left_halos_vid));
NC_CHECK(nc_def_var(connectivity_gid, "right_neighbors", NC_INT, 1, &dimid, &right_num_vid));
connectivity_gid, "left_neighbour_halos", NC_INT, 1, &left_dimid, &left_halos_vid));
NC_CHECK(nc_def_var(connectivity_gid, "right_neighbours", NC_INT, 1, &dimid, &right_num_vid));
NC_CHECK(nc_def_var(
connectivity_gid, "right_neighbor_ids", NC_INT, 1, &right_dimid, &right_ids_vid));
connectivity_gid, "right_neighbour_ids", NC_INT, 1, &right_dimid, &right_ids_vid));
NC_CHECK(nc_def_var(
connectivity_gid, "right_neighbor_halos", NC_INT, 1, &right_dimid, &right_halos_vid));
connectivity_gid, "right_neighbour_halos", NC_INT, 1, &right_dimid, &right_halos_vid));

// Write metadata to file
NC_CHECK(nc_enddef(nc_id));
Expand All @@ -204,35 +205,35 @@ void Partitioner::save_metadata(const std::string& filename) const
NC_CHECK(nc_put_var1_int(bbox_gid, cnt_y_vid, &start, &_local_ext_1_new));

NC_CHECK(nc_var_par_access(connectivity_gid, top_num_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_var1_int(connectivity_gid, top_num_vid, &start, &top_num_neighbors));
NC_CHECK(nc_put_var1_int(connectivity_gid, top_num_vid, &start, &top_num_neighbours));
NC_CHECK(nc_var_par_access(connectivity_gid, bottom_num_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_var1_int(connectivity_gid, bottom_num_vid, &start, &bottom_num_neighbors));
NC_CHECK(nc_put_var1_int(connectivity_gid, bottom_num_vid, &start, &bottom_num_neighbours));
NC_CHECK(nc_var_par_access(connectivity_gid, left_num_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_var1_int(connectivity_gid, left_num_vid, &start, &left_num_neighbors));
NC_CHECK(nc_put_var1_int(connectivity_gid, left_num_vid, &start, &left_num_neighbours));
NC_CHECK(nc_var_par_access(connectivity_gid, right_num_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_var1_int(connectivity_gid, right_num_vid, &start, &right_num_neighbors));
NC_CHECK(nc_put_var1_int(connectivity_gid, right_num_vid, &start, &right_num_neighbours));

start = top_offset;
count = top_num_neighbors;
count = top_num_neighbours;
NC_CHECK(nc_var_par_access(connectivity_gid, top_ids_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_vara_int(connectivity_gid, top_ids_vid, &start, &count, top_ids.data()));
NC_CHECK(nc_var_par_access(connectivity_gid, top_halos_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_vara_int(connectivity_gid, top_halos_vid, &start, &count, top_halos.data()));
start = bottom_offset;
count = bottom_num_neighbors;
count = bottom_num_neighbours;
NC_CHECK(nc_var_par_access(connectivity_gid, bottom_ids_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_vara_int(connectivity_gid, bottom_ids_vid, &start, &count, bottom_ids.data()));
NC_CHECK(nc_var_par_access(connectivity_gid, bottom_halos_vid, NC_COLLECTIVE));
NC_CHECK(
nc_put_vara_int(connectivity_gid, bottom_halos_vid, &start, &count, bottom_halos.data()));
start = left_offset;
count = left_num_neighbors;
count = left_num_neighbours;
NC_CHECK(nc_var_par_access(connectivity_gid, left_ids_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_vara_int(connectivity_gid, left_ids_vid, &start, &count, left_ids.data()));
NC_CHECK(nc_var_par_access(connectivity_gid, left_halos_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_vara_int(connectivity_gid, left_halos_vid, &start, &count, left_halos.data()));
start = right_offset;
count = right_num_neighbors;
count = right_num_neighbours;
NC_CHECK(nc_var_par_access(connectivity_gid, right_ids_vid, NC_COLLECTIVE));
NC_CHECK(nc_put_vara_int(connectivity_gid, right_ids_vid, &start, &count, right_ids.data()));
NC_CHECK(nc_var_par_access(connectivity_gid, right_halos_vid, NC_COLLECTIVE));
Expand All @@ -251,7 +252,7 @@ Partitioner* Partitioner::Factory::create(
throw std::runtime_error("Invalid partitioner!");
}

void Partitioner::discover_neighbors()
void Partitioner::discover_neighbours()
{
// Gather bounding boxes for all processes
std::vector<int> top_left_0(_num_procs, -1);
Expand Down Expand Up @@ -281,74 +282,74 @@ void Partitioner::discover_neighbors()
for (int i = 0; i < _num_procs; i++)
bottom_right_1[i] += top_left_1[i] - 1;

// Find my top neighbors and their halo sizes
// Find my top neighbours and their halo sizes
for (int i = 0; i < _num_procs; i++) {
if (i != _rank) {
if (top_left_1[_rank] >= bottom_left_1[i] && top_left_1[_rank] <= bottom_right_1[i]
&& bottom_right_1[i] <= top_right_1[_rank]
&& (top_left_0[_rank] - bottom_left_0[i] == 1)) {
int halo_size = bottom_right_1[i] - top_left_1[_rank] + 1;
_top_neighbors.insert(std::pair<int, int>(i, halo_size));
_top_neighbours.insert(std::pair<int, int>(i, halo_size));
}
if (top_right_1[_rank] >= bottom_left_1[i] && top_right_1[_rank] <= bottom_right_1[i]
&& bottom_left_1[i] >= top_left_1[_rank]
&& (top_right_0[_rank] - bottom_right_0[i] == 1)) {
int halo_size = top_right_1[_rank] - bottom_left_1[i] + 1;
_top_neighbors.insert(std::pair<int, int>(i, halo_size));
_top_neighbours.insert(std::pair<int, int>(i, halo_size));
}
}
}

// Find my bottom neighbors
// Find my bottom neighbours
for (int i = 0; i < _num_procs; i++) {
if (i != _rank) {
if (bottom_left_1[_rank] >= top_left_1[i] && bottom_left_1[_rank] <= top_right_1[i]
&& top_right_1[i] <= bottom_right_1[_rank]
&& (top_left_0[i] - bottom_left_0[_rank] == 1)) {
int halo_size = top_right_1[i] - bottom_left_1[_rank] + 1;
_bottom_neighbors.insert(std::pair<int, int>(i, halo_size));
_bottom_neighbours.insert(std::pair<int, int>(i, halo_size));
}
if (bottom_right_1[_rank] >= top_left_1[i] && bottom_right_1[_rank] <= top_right_1[i]
&& top_left_1[i] >= bottom_left_1[_rank]
&& (top_right_0[i] - bottom_right_0[_rank] == 1)) {
int halo_size = bottom_right_1[_rank] - top_left_1[i] + 1;
_bottom_neighbors.insert(std::pair<int, int>(i, halo_size));
_bottom_neighbours.insert(std::pair<int, int>(i, halo_size));
}
}
}

// Find my left neighbors
// Find my left neighbours
for (int i = 0; i < _num_procs; i++) {
if (i != _rank) {
if (top_left_0[_rank] >= top_right_0[i] && top_left_0[_rank] <= bottom_right_0[i]
&& bottom_left_0[_rank] <= bottom_right_0[i]
&& (top_left_1[_rank] - top_right_1[i] == 1)) {
int halo_size = bottom_right_0[i] - top_left_0[_rank] + 1;
_left_neighbors.insert(std::pair<int, int>(i, halo_size));
_left_neighbours.insert(std::pair<int, int>(i, halo_size));
}
if (bottom_left_0[_rank] >= top_right_0[i] && bottom_left_0[_rank] <= bottom_right_0[i]
&& top_left_0[_rank] <= top_right_0[i]
&& (bottom_left_1[_rank] - top_right_1[i] == 1)) {
int halo_size = bottom_left_0[_rank] - top_right_0[i] + 1;
_left_neighbors.insert(std::pair<int, int>(i, halo_size));
_left_neighbours.insert(std::pair<int, int>(i, halo_size));
}
}
}

// Find my right neighbors
// Find my right neighbours
for (int i = 0; i < _num_procs; i++) {
if (i != _rank) {
if (top_right_0[_rank] >= top_left_0[i] && top_right_0[_rank] <= bottom_left_0[i]
&& bottom_right_0[_rank] >= bottom_left_0[i]
&& (top_left_1[i] - top_right_1[_rank] == 1)) {
int halo_size = bottom_left_0[i] - top_right_0[_rank] + 1;
_right_neighbors.insert(std::pair<int, int>(i, halo_size));
_right_neighbours.insert(std::pair<int, int>(i, halo_size));
}
if (bottom_right_0[_rank] >= top_left_0[i] && bottom_right_0[_rank] <= bottom_left_0[i]
&& top_right_0[_rank] <= top_left_0[i]
&& (top_left_1[i] - top_right_1[_rank] == 1)) {
int halo_size = bottom_right_0[_rank] - top_left_0[i] + 1;
_right_neighbors.insert(std::pair<int, int>(i, halo_size));
_right_neighbours.insert(std::pair<int, int>(i, halo_size));
}
}
}
Expand Down
44 changes: 22 additions & 22 deletions Partitioner.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* @file Partitioner.hpp
* @author Athena Elafrou <ae488@cam.ac.uk>
* @date 23 Aug 2024
* @date 11 Sep 2024
*/

#pragma once
Expand Down Expand Up @@ -56,28 +56,28 @@ class LIB_EXPORT Partitioner {
void get_bounding_box(int& global_0, int& global_1, int& local_ext_0, int& local_ext_1) const;

/*!
* @brief Returns the MPI ranks and halo sizes of the top neighbors for this
* @brief Returns the MPI ranks and halo sizes of the top neighbours for this
* process after partitioning.
*/
void get_top_neighbors(std::vector<int>& ids, std::vector<int>& halo_sizes) const;
void get_top_neighbours(std::vector<int>& ids, std::vector<int>& halo_sizes) const;

/*!
* @brief Returns the MPI ranks and halo sizes of the bottom neighbors for
* @brief Returns the MPI ranks and halo sizes of the bottom neighbours for
* this process after partitioning.
*/
void get_bottom_neighbors(std::vector<int>& ids, std::vector<int>& halo_sizes) const;
void get_bottom_neighbours(std::vector<int>& ids, std::vector<int>& halo_sizes) const;

/*!
* @brief Returns the MPI ranks and halo sizes of the left neighbors for this
* @brief Returns the MPI ranks and halo sizes of the left neighbours for this
* process after partitioning.
*/
void get_left_neighbors(std::vector<int>& ids, std::vector<int>& halo_sizes) const;
void get_left_neighbours(std::vector<int>& ids, std::vector<int>& halo_sizes) const;

/*!
* @brief Returns the MPI ranks and halo sizes of the right neighbors for this
* @brief Returns the MPI ranks and halo sizes of the right neighbours for this
* process after partitioning.
*/
void get_right_neighbors(std::vector<int>& ids, std::vector<int>& halo_sizes) const;
void get_right_neighbours(std::vector<int>& ids, std::vector<int>& halo_sizes) const;

/*!
* @brief Saves the partition IDs of the latest 2D domain decomposition in a
Expand All @@ -102,9 +102,9 @@ class LIB_EXPORT Partitioner {
* and domain_y are defined as the coordinates of the upper left corner of the
* box for each partition, while the domain_extent_x and domain_extent_y
* variables define the local extent of the x and y dimensions respectively.
* The file also defines the variables X_neighbors(P), X_neighbor_ids(X_dim)
* and X_neighbor_halos(X_dim), where X is top/bottom/left/right, which
* correspond to the number of neighbors per process, the neighbor IDs and
* The file also defines the variables X_neighbours(P), X_neighbour_ids(X_dim)
* and X_neighbour_halos(X_dim), where X is top/bottom/left/right, which
* correspond to the number of neighbours per process, the neighbour IDs and
* halo sizes of each process sorted from lower to higher MPI rank.
*
* @param filename Name of the NetCDF file.
Expand All @@ -117,8 +117,8 @@ class LIB_EXPORT Partitioner {
// created in the heap to ensure it's dtor is executed before MPI_Finalize()
Partitioner(MPI_Comm comm);

// Discover the processe's neighbors and halo sizes after partitioning
void discover_neighbors();
// Discover the processe's neighbours and halo sizes after partitioning
void discover_neighbours();

protected:
MPI_Comm _comm; // MPI communicator
Expand All @@ -143,14 +143,14 @@ class LIB_EXPORT Partitioner {
int _global_1_new = -1; /* Global coordinate in 2nd dimension of upper left
corner (after partitioning) */
std::vector<int> _proc_id = {}; // Process ids of partition (dense form)
std::map<int, int> _top_neighbors
= {}; // Map of top neighbors to their halo sizes after partitioning
std::map<int, int> _bottom_neighbors
= {}; // Map of bottom neighbors to their halo sizes after partitioning
std::map<int, int> _right_neighbors
= {}; // Map of bottom neighbors to their halo sizes after partitioning
std::map<int, int> _left_neighbors
= {}; // Map of bottom neighbors to their halo sizes after partitioning
std::map<int, int> _top_neighbours
= {}; // Map of top neighbours to their halo sizes after partitioning
std::map<int, int> _bottom_neighbours
= {}; // Map of bottom neighbours to their halo sizes after partitioning
std::map<int, int> _right_neighbours
= {}; // Map of bottom neighbours to their halo sizes after partitioning
std::map<int, int> _left_neighbours
= {}; // Map of bottom neighbours to their halo sizes after partitioning

public:
struct LIB_EXPORT Factory {
Expand Down
Loading

0 comments on commit 82329d1

Please sign in to comment.