# Asynchronous Writing

**URL:** <https://gpusph.discourse.group/t/asynchronous-writing/233>\
**Category:** Development\
**Created:** [November 9, 2026, 6:30pm UTC](https://gpusph.discourse.group/t/asynchronous-writing/233 "2026-11-09T18:30:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ethan\_Evans](https://yyz2.discourse-cdn.com/free1/user_avatar/gpusph.discourse.group/ethan_evans/32/141_2.png) [@Ethan\_Evans](https://gpusph.discourse.group/u/Ethan_Evans)\
**Post date:** [November 9, 2026, 6:30pm UTC](https://gpusph.discourse.group/t/asynchronous-writing/233/1 "2026-11-09T18:30:20Z")

</div>

For large simulations, writing can seriously slow performance. Does a method of writing vtp outputs asynchronously (i.e. without pausing the entire simulation) already exist?

Cheers,

Ethan

---

<div class="post-metadata">

**Author:** ![giuseppe.bilotta](https://yyz2.discourse-cdn.com/free1/user_avatar/gpusph.discourse.group/giuseppe.bilotta/32/72_2.png) [@giuseppe.bilotta](https://gpusph.discourse.group/u/giuseppe.bilotta)\
**Post date:** [November 10, 2026, 11:59am UTC](https://gpusph.discourse.group/t/asynchronous-writing/233/2 "2026-11-10T11:59:13Z")

</div>

Hi @Ethan_Evans, I’m afraid not. This is a long-standing issue, and definitely worth looking into.

I think a good starting point for this would be to have `Writer::Write` run on a separate thread. Optionally, each writer could be run on a separate thread, but some care must be taken with the special writers (COMMON and CALLBACK).

The main thing that needs taking care of is that the host buffers and any other host-side data that is being used by the writers need to be locked while the writers are writing. This includes also things such as the current time and iteration, the gravity, and everything else. For things outside of the buffers we might want to make a copy, but we should double-check that there is no interference.

---

<div class="post-metadata">

**Author:** ![giuseppe.bilotta](https://yyz2.discourse-cdn.com/free1/user_avatar/gpusph.discourse.group/giuseppe.bilotta/32/72_2.png) [@giuseppe.bilotta](https://gpusph.discourse.group/u/giuseppe.bilotta)\
**Post date:** [November 11, 2026, 7:49am UTC](https://gpusph.discourse.group/t/asynchronous-writing/233/3 "2026-11-11T07:49:27Z")

</div>

Some additional thoughts I had about the async writing these days.

We have a list of the buffers that are made available for writing: it’s the `which_buffers` variable in `GPUSPH::saveParticles`. This could become a `volatile` variable in `GlobalData` (e.g. `buffes_being_saved` or whatever) that the `GPUWorker::runCommand<DUMP>` could check against its `cmd.reads` list to ensure no dumping is done on a buffer that is being saved.

The idea is that the variable is set in `saveParticles`, and then reset to `BUFFER_NONE` after the writing thread(s) finish. the `DUMP` command does a busy loop until `cmd.reads & buffers_being_saved == BUFFER_NONE` (no buffers being saved are in the list of buffers to be dumped). This ensures that the host copy of the data is not overwritten by a subsequent iteration, and solves at least _that_ part of the asynchronicity.

The other thing to make sure is that _either_ no varying `GlobalData` member is accessed in the writers _or_ that any changes to them are similarly fenced. This is more subtle work because it requires going over each writer and ensuring they don’t read varying `gdata` members.

BTW the `VTKWriter` isn’t the only culprit, probably. I’m guessing the `HotWriter` also takes a nontrivial amount of time. Additionally, we have the issue that the two are often staggered with respect with each other: most writers write at predefined intervals in simulated time (e.g. every 0.1 seconds of simulated time), but the `HotWriter` _must_ be called at a neighbors list update because otherwise resumes are not reproducible (the first thing that gets done on reload is to rebuild the neighbors list, since otherwise we’d have to write that too, which is _huge_).

You will see that regular writing is handled in `TIME_STEP_EPILOGUE`, through the final `check_write()`, which among other things polls the writers to see if they want to write at this time-step: this is basically the save frequency check, _but_ if the hotwrite wants to write it will just e marked “pending”, because it has a dedicated `HANDLE_HOTWRITE` command, which is called right after `CHECK_NEIBSNUM` at the end of `Integrator::buildNeibsPhase` in `src/Integrator.cc`.

This means that, even if both have the same frequency, they can still be staggered with respect to each other. Of course they can be both made async, but e.g. if the hotwrite happens shortly after the VTK write it will stil have to finish for the VTK to complete before dumping the new data for the hotwrite. We need to check.

And yes, we could have an option to delay all writers until the next neighbors list construction, but e.g. if you want sampling of gages or testpoints at specific intervals you’d want this to be at the closest possible time, so you don’t want to delay. It’s an issue with more hidden complexity that it seems.

---

<div class="post-metadata">

**Author:** ![michaelthomas](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@michaelthomas](https://gpusph.discourse.group/u/michaelthomas)\
**Post date:** [November 26, 2026, 7:49pm UTC](https://gpusph.discourse.group/t/asynchronous-writing/233/4 "2026-11-26T19:49:40Z")

</div>

One alternative to busy-waiting on the buffer mask is a small pool of host staging buffers. In saveParticles, copy the relevant particle ranges into a free staging slot, mark that slot busy, and queue a job containing a snapshot of the varying GlobalData fields the writer needs (time, iteration, gravity, etc.). The writer thread then reads only the immutable snapshot, so DUMP can proceed on the live buffers as soon as the copy is done. The trade-off is extra host memory and copy time, but it decouples GPU advancement from disk I/O more cleanly than locking the original buffers until writing finishes.

---

<div class="post-metadata">

**Author:** ![giuseppe.bilotta](https://yyz2.discourse-cdn.com/free1/user_avatar/gpusph.discourse.group/giuseppe.bilotta/32/72_2.png) [@giuseppe.bilotta](https://gpusph.discourse.group/u/giuseppe.bilotta)\
**Post date:** [November 27, 2026, 6:03pm UTC](https://gpusph.discourse.group/t/asynchronous-writing/233/5 "2026-11-27T18:03:28Z")

</div>

Hello @michaelthomas,

that’s a very good idea, although I’m not sure how “small’ the host staging buffers would be, since we usually dump most buffers with the main writers. For starters we could even just duplicate the shared host buffers and proceed with the duplicates, or ping-pong between the two.

This approachfits very well with one of the wishlist features that we for writers, which is to indicate exactly which buffer(s) they need/want, so the staging area could be assembled from the requirements of the writers. Moreover, on host we currently have already some amount of waste because IIRC the vectors used to generate the particle system are never freed. Freeing those vectors would allow us to implement this without significantly growing the host-side memory footprint.
