Skip to content

Remove neighbour duplication#87

Open
TomMelt wants to merge 2 commits into
mainfrom
remove-neighbour-duplication
Open

Remove neighbour duplication#87
TomMelt wants to merge 2 commits into
mainfrom
remove-neighbour-duplication

Conversation

@TomMelt

@TomMelt TomMelt commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Unify periodic and non-periodic neighbour info

Closes #84

Summary

Merged periodic neighbour tracking into the same data structures used for non-periodic neighbours. Since a neighbour cannot be both periodic and non-periodic at the same time, separate _p suffixed maps were redundant.

Changes

  • Partitioner.cpp — removed getNeighbourInfoPeriodic() and merged all periodic neighbour data (halo sizes, send/recv buffer positions, corner neighbours) into the existing _neighbours, _sendPos, _recvPos, _cornerNeighbours, and _cornerSendPos maps.
  • Partitioner.hpp — removed _neighbours_p, _sendPos_p, _recvPos_p, _cornerNeighbours_p, _cornerSendPos_p member variables and the periodic overload of getNeighbourInfoPeriodic().
  • saveMetadata() — removed periodic dimension declarations and periodic variable writes from the NetCDF output.
  • Refactor saveMetadata — replace duplicated edge/corner loops with a templated compute_dims() function; introduce DimInfo struct to group neighbour dimension data; use lambda helpers (def_dim, write_array, write_scalar) to reduce repetitive nc_* calls

Tests

Updated all reference CDL files to match the unified neighbour output (periodic neighbour IDs now appear in the standard (non-periodic) fields)

TODO

  • check halo exchange tests in nextsim still work after changing decomp

@TomMelt TomMelt self-assigned this Jun 19, 2026
@TomMelt TomMelt added the ICCS label Jun 19, 2026
@TomMelt TomMelt marked this pull request as draft June 19, 2026 13:27
Previously, neighbours across periodic boundaries were tracked in
separate data structures (_neighbours_p, _sendPos_p, _recvPos_p,
_cornerNeighbours_p, _cornerSendPos_p) with a dedicated
getNeighbourInfoPeriodic() method and separate NetCDF output paths.

This change merges periodic neighbour info into the same maps used
for non-periodic neighbours, removing redundant code and data structures:

- Removed getNeighbourInfoPeriodic() entirely
- Removed periodic dimension/variable definitions from saveMetadata()
- Removed periodic data writes from saveMetadata()
- Consolidated periodic and regular neighbours into _neighbours,
  _sendPos, _recvPos, _cornerNeighbours, _cornerSendPos
@TomMelt TomMelt force-pushed the remove-neighbour-duplication branch from d7ac15e to 8a28401 Compare June 22, 2026 15:19
…bdas

- Replace duplicated edge/corner loop logic with templated compute_dims()
- Introduce DimInfo struct to group neighbour dimension data
- Add lambda helpers (def_dim, write_array, write_scalar) to reduce
  repetitive nc_* calls
@TomMelt TomMelt force-pushed the remove-neighbour-duplication branch from 8a28401 to b154a94 Compare June 22, 2026 15:31
@TomMelt TomMelt moved this to Review required in neXtSIM_DG overview Jun 23, 2026
@TomMelt TomMelt marked this pull request as ready for review June 23, 2026 13:34

@Mikolaj-A-Kowalski Mikolaj-A-Kowalski left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just couple of comments that came to mind after a first look.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if my impression is correct. But the _px and _py suffixed partition tests differ from their base version only by being periodic?

I it is the case I am thinking we should probably remove them.

Also maybe open a followup issue to provide some description of the tests (i.e. what they intend to test). Unless I missed something, at the moment, it is missing.

Comment thread Partitioner.cpp
info.dims.resize(items.size(), 0);
info.offsets.resize(items.size(), 0);
for (std::size_t i = 0; i < items.size(); i++) {
info.numNeighbours[i] = (int)data[items[i]].size();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
info.numNeighbours[i] = (int)data[items[i]].size();
info.numNeighbours[i] = static_cast<int>(data[items[i]].size());

since C++ style cast offer additional type safety and are much easier to 'grep' for?

Comment thread Partitioner.cpp
Comment on lines +355 to +357
auto def_dim = [&](const std::string& name, int len, int& dimid) {
NC_CHECK(nc_def_dim(nc_id, name.c_str(), len, &dimid));
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto def_dim = [&](const std::string& name, int len, int& dimid) {
NC_CHECK(nc_def_dim(nc_id, name.c_str(), len, &dimid));
};
auto def_dim = [&](const std::string& name, int len) {
int dimid;
NC_CHECK(nc_def_dim(nc_id, name.c_str(), len, &dimid));
return dimid;
};

It may be personal bias, but mutable references as arguments always feel to me a bit off 😅

The logic being is that a call site it is never obvious in C++ that a particular I will quote what made argument can be modified by a function. Since we are dealing with a pure intent(out) situation here, returning feels more explicit in terms of the intention.

Comment thread Partitioner.cpp
NC_CHECK(nc_close(nc_id));
}

struct DimInfo {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking this helper class could use a short doc-comment to describe its intent a bit.

To a novice reader (like myself) it is not completely obvious what each field represents.

I will stare at it a bit more and try to phrase.

Comment thread Partitioner.cpp

// Define periodic dimensions in netCDF file
std::vector<int> dimids_p(N_EDGE);
NC_CHECK(nc_def_dim(nc_id, "P", _totalNumProcs, &dimid));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the def_dim lambda here as well?

Comment thread Partitioner.cpp
// Define groups in netCDF file
int bbox_gid, connectivity_gid;
// ---- Define groups ----
int bbox_gid, c_grid;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to say I liked the old name better :-(

I am not sure why did we change it?

@Mikolaj-A-Kowalski Mikolaj-A-Kowalski left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I kind of have a feeling that we can spend eternity trying to improve this function ;-), but ultimately it does not matter much.

So I would say it is ready to go just with the few suggestions for consideration ;-)

What I am thinking we REALLY need to do is to have some proper description of the output format (i.e. what are the groups/variables in the net cdf file and what do they mean). I think it is outside the scope of this PR, but if you agree we should open an issue and try to address it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Review required

Development

Successfully merging this pull request may close these issues.

reduce duplication for periodic neighbours in domain_decomp

2 participants