Asynchronous Writing

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

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.

1 Like

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.

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.

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.