Skip to content

Ensure vs30 values are written to disk and verified before exit#540

Closed
AndrewRidden-Harper wants to merge 1 commit intomasterfrom
ensure-vs30-write-completion
Closed

Ensure vs30 values are written to disk and verified before exit#540
AndrewRidden-Harper wants to merge 1 commit intomasterfrom
ensure-vs30-write-completion

Conversation

@AndrewRidden-Harper
Copy link

This adds to @sungeunbae 's recent HF fixes
We were also finding that Vs30 values were sometimes missing from the output file, causing the BB stage to crash with a math error. The root cause seems to be that vs30 values were not completely written to the file before the program finished. To fix this, I've added flush and fsync, which should ensure that the writes have completed before the program finishes. I also added some validation checks for missing vs30 values at the end.

Add flush and fsync after writing vs30 values to prevent incomplete
writes when MPI terminates. Add post-completion validation on master
rank to check file size and detect zero vs30 headers.
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue where Vs30 values were sometimes missing from output files, leading to crashes in downstream processing. It enhances data integrity by ensuring that Vs30 values are completely written to disk and then validated immediately after the simulation completes, thereby preventing data corruption and improving the robustness of the workflow.

Highlights

  • Data Persistence: Implemented flush() and fsync() calls to ensure Vs30 values are fully written to disk before the process terminates, preventing data loss and subsequent crashes.
  • Data Validation: Added a post-write validation step to re-read Vs30 values from the output file and check for any zero values, providing critical error messages if found.
Changelog
  • workflow/calculation/hf_sim.py
    • Added explicit flush() and os.fsync() calls after writing Vs30 data to guarantee disk persistence.
    • Introduced a post-processing validation block to re-open the output file, fsync it to invalidate caches, read the Vs30 values, and check for any zero values, aborting if critical issues are found.
Activity
  • No human activity has been recorded for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue where Vs30 values were sometimes missing from the output file. The changes introduce flush and fsync calls to ensure data is written to disk after each station is processed. Additionally, a verification step is added at the end of the simulation on the master process to check for any missing Vs30 values in the output file, aborting if any are found. The changes are logical and directly address the problem. I have one suggestion to slightly refactor the new verification code for better efficiency.

Comment on lines +560 to +573
with open(args.out_file, "r+b") as hff:
os.fsync(hff.fileno())
with open(args.out_file, "rb") as hff:
hff.seek(HEAD_SIZE)
vs_values = np.fromfile(
hff,
count=stations.size,
dtype={
"names": ["vs"],
"formats": ["f4"],
"offsets": [HEAD_STAT - FLOAT_SIZE],
"itemsize": HEAD_STAT,
},
)["vs"]

Choose a reason for hiding this comment

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

medium

For efficiency, you can combine these two with open(...) blocks. Opening the file in r+b mode allows for both calling os.fsync() and then reading from the file without needing to close and reopen it.

            with open(args.out_file, "r+b") as hff:
                os.fsync(hff.fileno())
                hff.seek(HEAD_SIZE)
                vs_values = np.fromfile(
                    hff,
                    count=stations.size,
                    dtype={
                        "names": ["vs"],
                        "formats": ["f4"],
                        "offsets": [HEAD_STAT - FLOAT_SIZE],
                        "itemsize": HEAD_STAT,
                    },
                )["vs"]

Copy link
Member

@sungeunbae sungeunbae left a comment

Choose a reason for hiding this comment

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

I am a little confused here - do you mean HF.bin output file?

out.seek(HEAD_STAT - 2 * FLOAT_SIZE, 1)
e_dist[i].tofile(out)
vs.tofile(out)
# Ensure vs30 values are fully written to disk before MPI terminates the process
Copy link
Member

Choose a reason for hiding this comment

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

vs30 values?

Copy link
Author

Choose a reason for hiding this comment

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

@sungeunbae Sorry, I meant vs values

@sungeunbae sungeunbae reopened this Mar 5, 2026
@AndrewRidden-Harper AndrewRidden-Harper marked this pull request as draft March 5, 2026 01:42
@AndrewRidden-Harper
Copy link
Author

I just discovered a problem with the extra validation check I put in hf_sim.py. I've converted this back to draft until my fix is confirmed, then I will include it and mark is as ready for review again

@AndrewRidden-Harper
Copy link
Author

This PR actually doesn't reliably solve the problem of vs values not being written to the HF.bin file on RCH and we actually don't need it to work on RCH because we can just run it on NeSI where it always works properly, so I'll close this PR

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants