Writing data from GPU

4 views (last 30 days)
Benjamin Ballintyn
Benjamin Ballintyn on 21 Jan 2020
Answered: Erik Meade on 2 Dec 2020
I am trying to find the fastest way to write data to a binary file (currently using fwrite), from within a function that gets compiled by GPU Coder into a mex file. I currently have a function that simulates a system for N timesteps. At each timestep I need to save some data. Running the mex function without saving runs ~150x faster than real time. However, when saving data each timestep, it runs ~2.3x slower than real time. This means the vast amount of time is being spent writing to the file. My question is, what is the absolute fastest way to save data from within a function compiled by GPU Coder? Thanks!

Answers (1)

Erik Meade
Erik Meade on 2 Dec 2020
It is quite possible that after each timestep, your program is incurring cudaMemcpy's to copy memory from the GPU to CPU every timestep and then writing the results to a file. cudaMemcpy requires the GPU to synchronize all threads and is thus a very expensive operation.
If it's possible, you could just keep all the data in GPU memory in a separate variable after each step, and then after the N-steps, write all the data to your file at once. The downside is you will use more GPU memory saving , but if you are not writing large amounts of data per timestep, it should be fine.
Let me know if this works. If not, feel free to share your code and I'll take a further look at it.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!