Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a local buffer in unique_writer for better write to disk performance #3322

Open
SteveBronder opened this issue Dec 10, 2024 · 0 comments · May be fixed by #3325
Open

Use a local buffer in unique_writer for better write to disk performance #3322

SteveBronder opened this issue Dec 10, 2024 · 0 comments · May be fixed by #3325

Comments

@SteveBronder
Copy link
Collaborator

SteveBronder commented Dec 10, 2024

Summary:

Just a note on two optimizations we could do when writing parameters.

Description:

When sampling with nuts the mcmc_writer generates the parameters it needs to write and then sends the parameters as a std::vector to a writer. For cmdstan the writer is the unique_stream_writer that writes to disk.

We call this function decently often and I think we could do a few things to both speed it up while being a little nicer to the system so we can write faster.

  1. unique_stream_writer for a std::vector calls its write_vector member function. This function takes each value in the vector and one at a time write them to disk. Instead of many small calls for writing to disk we should have a string buffer inside of the unique_stream_writer that we write all of the values to first. Then we just write one large buffer to disk once instead of many times.

  2. Inside of mcmc_writer::write_sample_params we keep making the std::vector<double> values for each call. Instead we can put those vectors directly in the mcmc_writer class. That would make mcmc_writer stateful in terms of memory, but to my knowledge we never call an mcmc_writer instance from multiple threads and idt it would make sense to. If we did not want to add state to mcmc_writer we could also pass mcmc_writer::write_sample_params a local allocator to reuse memory.

These optimizations would not really matter for small problems. But once we start getting to 100K+ parameter models reading and writing to disk can be an issue.

Current Version:

v2.36.0

@SteveBronder SteveBronder linked a pull request Dec 13, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant