Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
b78efb9
feat: added metadata info on neighbours
TomMelt Aug 23, 2024
0c63053
feat: additional metadata for send recv buffers
TomMelt Jan 17, 2025
cec50d7
tests: update to reflect new metadata
TomMelt Jan 17, 2025
057d69c
tests: update metadata file for testRectGrid
TomMelt Jan 17, 2025
dca50ff
chore: fix mpi.h include error
TomMelt Jan 17, 2025
672e637
chore: fix CMakeLists for mpi tests
TomMelt May 15, 2025
847ba27
test: working example of halo exchange
TomMelt Dec 20, 2024
fd4c916
feat: separate halo exchange logic into class
TomMelt Jan 28, 2025
b00e907
chore: fixes for halo logic after merge request
TomMelt May 16, 2025
5467eb1
feat: convert ModelMetadata to singleton
TomMelt May 19, 2025
1af24e4
Merge branch 'develop' into halo-exchange
TomMelt May 21, 2025
e217392
chore: simplify ParaGrid_test to use same metadata throughout
TomMelt May 23, 2025
d70ad3d
feat: refactor MPI metadata into singleton
TomMelt Jun 4, 2025
da75bdd
chore: remove unused code
TomMelt Jun 4, 2025
d841540
feat: only write inner block to disk
TomMelt Jun 10, 2025
e57ce15
test: update halo tests
TomMelt Jun 10, 2025
baea962
bug: forcing data is now offset correctly
TomMelt Jun 16, 2025
0bbf09a
Set all ModelArray values to zero on resize()
einola Jun 13, 2025
786c157
feat: refactor periodic and non-periodic metadata
TomMelt Jun 19, 2025
6ae20c8
wip: initial working impl of eigenSlice
TomMelt Jul 15, 2025
58c1077
wip: weird memory bug for DGField
TomMelt Jul 16, 2025
3497706
wip: weird memory bug for DGField
TomMelt Jul 16, 2025
02f2711
wip: tidied eigenslice test
TomMelt Jul 16, 2025
d7224c6
wip: fix testParaGrid_MPI2
TomMelt Aug 4, 2025
271ce05
wip: eigen slice dump before removing it
TomMelt Aug 19, 2025
ea17449
wip: remove eigenslice
TomMelt Aug 19, 2025
59b25ef
wip: halo exchange now supports all field types
TomMelt Aug 19, 2025
1be8230
wip: fix most halo but some tests still broken
TomMelt Aug 19, 2025
afc3d55
test: ConfigOutput now uses partition_metadata for example
TomMelt Aug 20, 2025
604bee4
test: fix paragrid test to run for serial case
TomMelt Aug 21, 2025
2135cd1
test: remove physic tests from MPI as they are serial
TomMelt Aug 21, 2025
f142a56
test: remove frontmatter date and file
TomMelt Aug 21, 2025
4ca7363
test: remove frontmatter blank lines
TomMelt Aug 21, 2025
5ea65d0
test: author ensure 2 spaces
TomMelt Aug 21, 2025
b408562
Merge branch 'develop' into halo-exchange
TomMelt Aug 26, 2025
ae15458
tests: XIOS now uses metadata/MPI singletons
TomMelt Aug 26, 2025
aa02b7b
docs: add doc strings to halo.hpp
TomMelt Aug 26, 2025
6863c7a
bug: make sure halo region is initialized to zero
TomMelt Sep 1, 2025
cae32e7
feat: use Halo::haloWidth instead of HALOWIDTH
TomMelt Sep 1, 2025
bed5b93
bug: remove erroneous mpi include
TomMelt Sep 1, 2025
8502246
chore: remove stale comments
TomMelt Sep 1, 2025
6191a33
chore: remove stale comments
TomMelt Sep 1, 2025
e36db54
chore: remove unnecessary ifdefs
TomMelt Sep 1, 2025
6a4b03a
feat: change constructor of halo to use datatype
TomMelt Sep 1, 2025
dc5d320
docs: add description to halo tests
TomMelt Sep 1, 2025
f38263b
feat: add isDimLateral to simplify logic in paragridIO
TomMelt Sep 19, 2025
80f159b
chore: remove old TODO
TomMelt Sep 19, 2025
9ebbb9c
chore: remove unused file
TomMelt Sep 19, 2025
769b0e0
chore: remove duplicate include
TomMelt Sep 19, 2025
1830de1
feat: add runtime error if partition file is empty
TomMelt Sep 29, 2025
c683062
bugfix: globalLength shouldnt be padded with Halo cells
TomMelt Sep 29, 2025
ae334ca
docs: add doxygen info Halo, ModelMPI and ModelMetadata
TomMelt Sep 29, 2025
4c39c88
docs: fix typo in comment ParaGrid_test.cpp
TomMelt Oct 2, 2025
231126c
docs: fix typos raised by Joe in PR review
TomMelt Oct 2, 2025
653d2b7
merge: resolve merge conflicts with develop
TomMelt Oct 2, 2025
069d0b9
fix: fix Model.cpp after merge of develop
TomMelt Oct 2, 2025
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ __pycache__/
*.sif
*.smesh
.*sw?
/run/*.cdl
.cache/
.clangd
compile_commands.json
*.vtk

# Specific files
Expand Down
1 change: 1 addition & 0 deletions core/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ else()
endif()

set(BaseSources
"ModelMPI.cpp"
"Logged.cpp"
"Model.cpp"
"Iterator.cpp"
Expand Down
9 changes: 5 additions & 4 deletions core/src/CommonRestartMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#include "include/CommonRestartMetadata.hpp"
#include "include/ModelMetadata.hpp"

#include "include/MissingData.hpp"

Expand All @@ -15,17 +16,17 @@

namespace Nextsim {

netCDF::NcFile& CommonRestartMetadata::writeStructureType(
netCDF::NcFile& ncFile, const ModelMetadata& metadata)
netCDF::NcFile& CommonRestartMetadata::writeStructureType(netCDF::NcFile& ncFile)
{
auto& metadata = ModelMetadata::getInstance();
ncFile.putAtt(IStructure::structureNodeName(), metadata.structureName());
return ncFile;
}

netCDF::NcFile& CommonRestartMetadata::writeRestartMetadata(
netCDF::NcFile& ncFile, const ModelMetadata& metadata)
netCDF::NcFile& CommonRestartMetadata::writeRestartMetadata(netCDF::NcFile& ncFile)
{
// Structure type
auto& metadata = ModelMetadata::getInstance();
ncFile.putAtt(IStructure::structureNodeName(), metadata.structureName());

// As Unix time
Expand Down
14 changes: 7 additions & 7 deletions core/src/DevStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Nextsim {

DevStep::DevStep()
: pData(nullptr)
, mData(nullptr)
, m_restartPeriod(0)
{
}
Expand All @@ -38,14 +37,15 @@ void DevStep::iterate(const TimestepTime& tst)
pData->update(tst);
// The state of the model has now advanced by one timestep, so update the
// model metadata timestamp.
mData->incrementTime(tst.step);
if ((m_restartPeriod.seconds() > 0) && (mData->time() >= lastOutput + m_restartPeriod)) {
std::string currentFileName = mData->time().format(m_restartFileName);
pData->writeRestartFile(currentFileName, *mData);
lastOutput = mData->time();
auto& mData = ModelMetadata::getInstance();
mData.incrementTime(tst.step);
if ((m_restartPeriod.seconds() > 0) && (mData.time() >= lastOutput + m_restartPeriod)) {
std::string currentFileName = mData.time().format(m_restartFileName);
pData->writeRestartFile(currentFileName);
lastOutput = mData.time();
}
// XIOS wants all the fields, every timestep, so I guess that's what everyone gets
Module::getImplementation<IDiagnosticOutput>().outputState(pData->getStateDiagnostic(), *mData);
Module::getImplementation<IDiagnosticOutput>().outputState(pData->getStateDiagnostic());
}

void DevStep::setRestartDetails(const Duration& restartPeriod, const std::string& fileName)
Expand Down
63 changes: 27 additions & 36 deletions core/src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,9 @@ static const std::map<int, std::string> keyMap = {
{ Model::RESTARTOUTFILE_KEY, "model.restart_file" },
};

#ifdef USE_MPI
Model::Model(MPI_Comm comm)
#else
Model::Model()
#endif
: iterator(modelStep)
{
#ifdef USE_MPI
m_etadata.setMpiMetadata(comm);
#endif
finalFileName = std::string("restart") + TimePoint::ymdhmsFormat + ".nc";
}

Expand All @@ -62,25 +55,6 @@ void Model::configure()
// Configure logging
Logged::configure();

// Start/stop times. Run length will override stop time, if present.
std::string startTimeStr
= Configured::getConfiguration(keyMap.at(STARTTIME_KEY), std::string());
std::string stopTimeStr = Configured::getConfiguration(keyMap.at(STOPTIME_KEY), std::string());
std::string runLengthStr
= Configured::getConfiguration(keyMap.at(RUNLENGTH_KEY), std::string());
std::string stepStr = Configured::getConfiguration(keyMap.at(TIMESTEP_KEY), std::string());

if (runLengthStr.empty()) {
if (stopTimeStr.empty()) {
throw std::invalid_argument(std::string("At least one of ") + keyMap.at(STOPTIME_KEY)
+ " or " + keyMap.at(RUNLENGTH_KEY) + " must be set");
} else {
m_etadata.setTimes(startTimeStr, TimePoint(stopTimeStr), stepStr);
}
} else {
m_etadata.setTimes(startTimeStr, Duration(runLengthStr), stepStr);
}
iterator.setStartStopStep(m_etadata.startTime(), m_etadata.stopTime(), m_etadata.stepLength());
// Configure the missing data value
MissingData::setValue(
Configured::getConfiguration(keyMap.at(MISSINGVALUE_KEY), MissingData::defaultValue));
Expand All @@ -97,25 +71,42 @@ void Model::configure()
#ifdef USE_MPI
std::string partitionFile
= Configured::getConfiguration(keyMap.at(PARTITIONFILE_KEY), std::string("partition.nc"));
m_etadata.getPartitionMetadata(partitionFile);
auto& metadata = ModelMetadata::getInstance(partitionFile);
#else
auto& metadata = ModelMetadata::getInstance();
#endif

#ifdef USE_MPI
ModelState initialState(StructureFactory::stateFromFile(initialFileName, m_etadata));
#else
// Start/stop times. Run length will override stop time, if present.
std::string startTimeStr
= Configured::getConfiguration(keyMap.at(STARTTIME_KEY), std::string());
std::string stopTimeStr = Configured::getConfiguration(keyMap.at(STOPTIME_KEY), std::string());
std::string runLengthStr
= Configured::getConfiguration(keyMap.at(RUNLENGTH_KEY), std::string());
std::string stepStr = Configured::getConfiguration(keyMap.at(TIMESTEP_KEY), std::string());

if (runLengthStr.empty()) {
if (stopTimeStr.empty()) {
throw std::invalid_argument(std::string("At least one of ") + keyMap.at(STOPTIME_KEY)
+ " or " + keyMap.at(RUNLENGTH_KEY) + " must be set");
} else {
metadata.setTimes(startTimeStr, TimePoint(stopTimeStr), stepStr);
}
} else {
metadata.setTimes(startTimeStr, Duration(runLengthStr), stepStr);
}
iterator.setStartStopStep(metadata.startTime(), metadata.stopTime(), metadata.stepLength());

ModelState initialState(StructureFactory::stateFromFile(initialFileName));
#endif

// The period with which to write restart files.
std::string restartPeriodStr
= Configured::getConfiguration(keyMap.at(RESTARTPERIOD_KEY), std::string("0"));
restartPeriod = Duration(restartPeriodStr);

// Get the coordinates from the ModelState for persistence
m_etadata.extractCoordinates(initialState);
metadata.extractCoordinates(initialState);

modelStep.setData(pData);
modelStep.setMetadata(m_etadata);
modelStep.setRestartDetails(restartPeriod, finalFileName);
pData.setData(initialState.data);
}
Expand Down Expand Up @@ -186,9 +177,9 @@ void Model::run()
//! Write a restart file for the model.
void Model::writeRestartFile()
{
std::string formattedFileName = m_etadata.time().format(finalFileName);
pData.writeRestartFile(formattedFileName, m_etadata);
auto& metadata = ModelMetadata::getInstance();
std::string formattedFileName = metadata.time().format(finalFileName);
pData.writeRestartFile(formattedFileName);
}

ModelMetadata& Model::metadata() { return m_etadata; }
} /* namespace Nextsim */
36 changes: 36 additions & 0 deletions core/src/ModelMPI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*!
* @author Tom Meltzer <tdm39@cam.ac.uk>
*/

#ifdef USE_MPI
#include "include/ModelMPI.hpp"

namespace Nextsim {

ModelMPI::ModelMPI(MPI_Comm comm)
: m_comm(comm)
{
MPI_Comm_size(m_comm, &m_size);
MPI_Comm_rank(m_comm, &m_rank);
static bool doneOnce = doOnce();
isInitialized = true;
}

MPI_Comm ModelMPI::getComm() const { return m_comm; }

int ModelMPI::getSize() const { return m_size; }

int ModelMPI::getRank() const { return m_rank; }

bool ModelMPI::doOnce()
{
// Register the finalization function here
Finalizer::registerUnique(finalize);
return true;
}

void ModelMPI::finalize() { }

} // namespace Nextsim

#endif // USE_MPI
120 changes: 108 additions & 12 deletions core/src/ModelMetadata.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
/*!
* @author Tim Spain <timothy.spain@nersc.no>
* @author Tom Meltzer <tdm39@cam.ac.uk>
*/

#include "include/ModelMetadata.hpp"

#include "include/Finalizer.hpp"
#include "include/IStructure.hpp"
#include "include/ModelMPI.hpp"
#include "include/NextsimModule.hpp"
#ifdef USE_XIOS
#include "include/Xios.hpp"
#endif
#include "include/gridNames.hpp"
#include <array>
#include <cstddef>
#include <functional>
#include <vector>

#ifdef USE_MPI
#include "mpi.h"
#include <ncDim.h>
#include <ncFile.h>
#include <ncGroup.h>
Expand All @@ -26,25 +34,86 @@ const std::string& ModelMetadata::structureName() const
}

#ifdef USE_MPI
ModelMetadata::ModelMetadata(std::string partitionFile, MPI_Comm comm)
ModelMetadata::ModelMetadata(std::string partitionFile)
{
setMpiMetadata(comm);
if (partitionFile.empty()) {
throw std::runtime_error(
"ModelMetadata :: getInstance() called without partition file in MPI build.");
}
getPartitionMetadata(partitionFile);
static bool doneOnce = doOnce();
isInitialized = true;
}

void ModelMetadata::setMpiMetadata(MPI_Comm comm)
void ModelMetadata::readNeighbourData(netCDF::NcFile& ncFile)
{
mpiComm = comm;
MPI_Comm_size(mpiComm, &mpiSize);
MPI_Comm_rank(mpiComm, &mpiMyRank);
netCDF::NcGroup neighbourGroup(ncFile.getGroup(neighbourName));
std::string varName {};
auto& modelMPI = ModelMPI::getInstance();
auto mpiSize = modelMPI.getSize();
auto mpiMyRank = modelMPI.getRank();
enum BoundaryType { nonPeriodic, periodic };
for (BoundaryType btype : { nonPeriodic, periodic }) {
// Use btype as needed
std::array<std::string, 4> suffixes = { "_neighbour_ids", "_neighbour_halos",
"_neighbour_halo_send", "_neighbour_halo_recv" };
if (btype == periodic) {
for (auto& suffix : suffixes) {
suffix += "_periodic";
}
}
for (auto edge : edges) {
size_t nStart = 0; // start point in metadata arrays
size_t count = 0; // number of elements to read from metadata arrays
std::vector<int> numNeighbours = std::vector<int>(mpiSize, 0);
std::vector<int> offsets = std::vector<int>(mpiSize, 0);
std::vector<std::reference_wrapper<std::vector<int>>> arrays;

if (btype == nonPeriodic) {
arrays = { neighbourRanks[edge], neighbourExtents[edge], neighbourHaloSend[edge],
neighbourHaloRecv[edge] };
} else if (btype == periodic) {
arrays = { neighbourRanksPeriodic[edge], neighbourExtentsPeriodic[edge],
neighbourHaloSendPeriodic[edge], neighbourHaloRecvPeriodic[edge] };
}

varName = edgeNames[edge] + "_neighbours";
if (btype == periodic) {
varName += "_periodic";
}
neighbourGroup.getVar(varName).getVar(
{ 0 }, { static_cast<size_t>(mpiSize) }, numNeighbours.data());

// compute start index for each process
MPI_Exscan(&numNeighbours[mpiMyRank], &nStart, 1, MPI_INT, MPI_SUM, modelMPI.getComm());
if (mpiMyRank == 0) {
// MPI_Exscan is undefined on the first rank. So to be safe we manually set nStart
// to 0. (see e.g., https://www.open-mpi.org/doc/v4.1/man3/MPI_Exscan.3.php)
nStart = 0;
}
// how many elements to read for each process
count = numNeighbours[mpiMyRank];

if (count) {
// initialize neighbour info to zero
for (size_t i = 0; i < arrays.size(); ++i) {
arrays[i].get().resize(count, 0);
varName = edgeNames[edge] + suffixes[i];
neighbourGroup.getVar(varName).getVar(
{ nStart }, { count }, arrays[i].get().data());
}
}
}
}
}

void ModelMetadata::getPartitionMetadata(std::string partitionFile)
{
// TODO: Move the reading of the partition file to its own class
netCDF::NcFile ncFile(partitionFile, netCDF::NcFile::read);
int sizes = ncFile.getDim("L").getSize();
int nBoxes = ncFile.getDim("P").getSize();
auto& modelMPI = ModelMPI::getInstance();
auto mpiSize = modelMPI.getSize();
if (nBoxes != mpiSize) {
std::string errorMsg = "Number of MPI ranks " + std::to_string(mpiSize) + " <> "
+ std::to_string(nBoxes) + "\n";
Expand All @@ -53,14 +122,32 @@ void ModelMetadata::getPartitionMetadata(std::string partitionFile)
globalExtentX = ncFile.getDim("NX").getSize();
globalExtentY = ncFile.getDim("NY").getSize();
netCDF::NcGroup bboxGroup(ncFile.getGroup(bboxName));
std::vector<size_t> index(1, mpiMyRank);
bboxGroup.getVar("domain_x").getVar(index, &localCornerX);
bboxGroup.getVar("domain_y").getVar(index, &localCornerY);
bboxGroup.getVar("domain_extent_x").getVar(index, &localExtentX);
bboxGroup.getVar("domain_extent_y").getVar(index, &localExtentY);

std::vector<size_t> rank(1, modelMPI.getRank());
bboxGroup.getVar("domain_x").getVar(rank, &localCornerX);
bboxGroup.getVar("domain_y").getVar(rank, &localCornerY);
bboxGroup.getVar("domain_extent_x").getVar(rank, &localExtentX);
bboxGroup.getVar("domain_extent_y").getVar(rank, &localExtentY);

readNeighbourData(ncFile);

ncFile.close();
}

int ModelMetadata::getLocalCornerX() const { return localCornerX; }
int ModelMetadata::getLocalCornerY() const { return localCornerY; }
int ModelMetadata::getLocalExtentX() const { return localExtentX; }
int ModelMetadata::getLocalExtentY() const { return localExtentY; }
int ModelMetadata::getGlobalExtentX() const { return globalExtentX; }
int ModelMetadata::getGlobalExtentY() const { return globalExtentY; }
#else

ModelMetadata::ModelMetadata()
{
isInitialized = true;
static bool doneOnce = doOnce();
}

#endif

const ModelState& ModelMetadata::extractCoordinates(const ModelState& state)
Expand Down Expand Up @@ -146,4 +233,13 @@ void ModelMetadata::incrementTime(const Duration& step)
#endif
}

void ModelMetadata::finalize() { }

bool ModelMetadata::doOnce()
{
// Register the finalization function here
Finalizer::registerUnique(finalize);
return true;
}

} /* namespace Nextsim */
Loading
Loading