Skip to content

Stop on failed output writes, and fix the output write time and size - #3028

Open
rois1995 wants to merge 2 commits into
developfrom
output_write_checks
Open

rois1995 wants to merge 2 commits into
developfrom
output_write_checks

Conversation

@rois1995

Copy link
Copy Markdown
Contributor

Problem

  • A failed write (for example a full disk) was ignored. SU2 carried on with a broken output or restart file.
  • On very large meshes, node numbers and MPI counts of the output can overflow an int, with no error.
  • The write time and bandwidth printed by SU2 were wrong. A header written by one rank was counted once per rank. The time missed the work just before the close. Without MPI and OpenMP, the timer measured CPU time, not real time.

Fix

  • Stop with an error when a write, a view change or a close fails. At close, also check the file size: some MPI libraries report success after a short write.
  • Stop with an error before an output node number or MPI count gets too large for an int.
  • Count each byte once. Time the write from open to close with a wall clock. Use the longest time over the ranks for the bandwidth.
  • Forward AD builds now use the passive MPI wrapper for passive data, like reverse AD builds.
  • Results do not change.

Tests

  • Unit test CFileWriter_tests.cpp checks the file size, write time and bandwidth.

Notes

Related

PR Checklist

  • I am submitting my contribution to the develop branch.
  • My contribution generates no new compiler warnings (try with --warnlevel=3 when using meson).
  • My contribution is commented and consistent with SU2 style (https://su2code.github.io/docs_v7/Style-Guide/).
  • I used the pre-commit hook to prevent dirty commits and used pre-commit run --all to format old commits.
  • I have added a test case that demonstrates my contribution, if necessary.
  • I have updated appropriate documentation (Tutorials, Docs Page, config_template.cpp), if necessary.

🤖 Generated with Claude Code

- Stop with an error when an MPI or POSIX output write, view change or close
  fails, and check the final file size at close (some MPI backends report
  success after a short write).
- Stop with an error before node IDs or int MPI counts of the output sorters
  and writers overflow.
- Count bytes written by one rank once, time the whole write from open to
  close with a wall-clock timer, and use the largest time over the ranks for
  the bandwidth.
- Use the passive MPI wrapper for passive data also in forward AD builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bigfooted

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

# Conflicts:
#	SU2_CFD/src/output/filewriter/CParallelFileWriter.cpp

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

Copilot AI commented Nov 30, 2026

Copy link
Copy Markdown

@copilot resolve the merge conflicts in this pull request

Resolved in merge commit 5b89572. The writer source and existing writer test both pass C++ syntax checks; the full unit tests could not run because build dependencies are not initialized.

/*--- Check the final extent: some MPI backends report success even after a short write. ---*/
MPI_Offset expectedEnd = 0, actualEnd = 0;
CBaseMPIWrapper::Allreduce(&disp, &expectedEnd, 1, MPI_OFFSET, MPI_MAX, SU2_MPI::GetComm());
if (MPI_File_get_size(fhw, &actualEnd) != MPI_SUCCESS || actualEnd != expectedEnd)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important: CloseMPIFile can stop a run that wrote a complete file. Each rank calls MPI_File_get_size straight after its own writes, with no MPI_File_sync between them. In MPI-IO's default nonatomic mode, a rank may not yet see the file growth from writes made by other ranks. For example, a header written by the master with WriteMPIBinaryData/WriteMPIString (MPI_File_write_at, which is not collective), or the tail of a write_all on Lustre/GPFS with client caching. That rank sees actualEnd < expectedEnd and calls SU2_MPI::Error.

Fix: call MPI_File_sync(fhw) and a barrier (sync-barrier-sync) before MPI_File_get_size, or have only rank 0 query the size after the sync and broadcast the result.

#if MPI_VERSION >= 3
MPI_Count written = 0;
if (MPI_Get_elements_x(&status, MPI_BYTE, &written) != MPI_SUCCESS ||
written < 0 || static_cast<unsigned long>(written) != expected)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important: Every write now stops the run unless the MPI_Status reports exactly expected bytes. That status is filled from a write_all of a derived datatype (WriteMPIBinaryDataAll) or from an MPI_CHAR write (WriteMPIString), but the code queries it with MPI_BYTE. The MPI standard requires the datatype passed to MPI_Get_elements[_x] to match the one used in the operation. Some MPI-IO layers also leave the status incomplete for collective writes, for example on a rank that contributes 0 bytes. A successful write then aborts with "MPI output write failed or was incomplete". Before this change these writes only returned a bool.

Fix: query the status with the datatype that was written (datatype for the collective write, MPI_CHAR for strings). Otherwise, rely only on ierr plus the extent check at close.

void CSurfaceFVMDataSorter::SortConnectivity(CConfig *config, CGeometry *geometry, const vector<string> &markerList) {
unsigned long boundaryElements = 0;
for (unsigned short marker = 0; marker < geometry->GetnMarker(); ++marker) {
CheckIntCount(geometry->GetnElem_Bound(marker), N_POINTS_QUADRILATERAL);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit (optional): The surface sorter checks the boundary elements of every marker in the geometry, not only the ones in markerList that are actually sorted. It also uses the quadrilateral node count for every element. A case that plots one small marker on a mesh with a huge unplotted boundary (more than about 536M local boundary elements) stops with "Output exceeds the supported 32-bit ... range", even though the surface output would fit.

Fix: sum only the markers whose tag is in markerList.

/*--- The largest counts that fit, larger ones stop the run. ---*/
CParallelDataSorter::CheckIntCount(INT_MAX);
CParallelDataSorter::CheckIntCount(INT_MAX / 8, 8);
CParallelDataSorter::CheckIntCount(0, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit (optional): The test is meant to check that counts above the limit stop the run, but it only exercises values that pass. An off-by-one or an inverted condition in CheckIntCount, such as >= or a dropped / entries, would still pass as long as the boundary values do not trip. The comment also says "larger ones stop the run", which the test never checks.

Fix: add a check that INT_MAX + 1ul and INT_MAX / 8 + 1, 8 exceed the limit. Because SU2_MPI::Error aborts, this needs a pure predicate that the test can call.


TEST_CASE("File writer accounting", "[Output]") {
const int rank = SU2_MPI::GetRank(), size = SU2_MPI::GetSize();
const std::string fileName = "file_writer_accounting";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit (optional): The test writes to a fixed file name, file_writer_accounting.bin, in the current directory. When the serial and MPI unit-test binaries run at the same time from the same build directory (for example meson test in parallel), one run can delete the other's file, or hit the MPI_MODE_EXCL delete/recreate path. The size or extent checks then fail at random.

Fix: make the name unique per run, for example by appending the PID and the communicator size.


nElemGlobal = std::accumulate(nElemPerTypeGlobal.begin(), nElemPerTypeGlobal.end(), 0ul);
nElem = std::accumulate(nElemPerType.begin(), nElemPerType.end(), 0ul);
CheckIntCount(nElemGlobal);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Large meshes are rejected although the global element count is never stored as an int.

CheckIntCount(nElemGlobal) limits the global element count to INT_MAX, but nElemGlobal is unsigned long, and the Paraview XML writer already switches to int64 connectivity and offsets (connInt64). Only node IDs and per-rank counts are int.

Example: a tet mesh with 2.3e9 elements and ~4e8 nodes fits everywhere it matters, but this check aborts the run at its first output.

Suggestion: drop the global element check, and check only the per-rank element/connectivity counts and the global node count.

usedTime += stopTime - startTime;

return (bytesWritten == sizeInBytes);
if (bytesWritten != sizeInBytes) SU2_MPI::Error("Output write failed", CURRENT_FUNCTION);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Every write failure now aborts the solver, and the bool return value is dead.

WriteMPIBinaryDataAll, WriteMPIBinaryData, WriteMPIString and CloseMPIFile call SU2_MPI::Error instead of returning false (here and at lines 208 and 258). Before this PR, a disk-quota failure while writing e.g. a surface CSV left a short file and the solver kept running. Now MPI_Abort kills the run, and everything since the last restart is lost.

If the abort is intended, it would be good to say so and remove the return true / bool API. Otherwise, return false and let callers decide.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So here the question is: when su2 fails to write a file, do we stop or do we continue the run?
I am inclined to say we stop and this is the intended behavior.

stopTime = SU2_MPI::Wtime();
stopTime = WallTime();

usedTime += stopTime - startTime;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The usedTime accumulation is dead code.

CloseMPIFile overwrites usedTime via UpdateBandwidth(elapsed since open), so the per-write usedTime += stopTime - startTime here (and at lines 157, 189, 206, 240 and 256) never reaches the reported value. The startTime/stopTime updates in these functions can go too.

* \brief Set the write time to the largest one over all ranks and compute the bandwidth with the file size.
* \param[in] elapsed - Write time of this rank.
*/
void UpdateBandwidth(passivedouble elapsed);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pre-existing, but this PR now feeds the result into the bandwidth report.

DetermineFilesize (line 86) returns -1 cast to unsigned long when stat fails. That value now reaches UpdateBandwidth. For example, a build without TecIO that asks for TECPLOT_BINARY writes no file, so the file size becomes ~1.8e19 and the reported bandwidth is absurd.

Suggestion: return 0 (or report the error) when stat fails.

public:
/*!
* \brief Stop with an error if count * entries does not fit in an int (node IDs and MPI counts are int).
* \param[in] count - Number of items.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor: \param[in] count - Number of items. only restates the parameter name. Either drop the line or say what is counted (elements, nodes, ...).

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants