Writing data from GPU

5 次查看(过去 30 天)
Benjamin Ballintyn
Benjamin Ballintyn 2020-1-21
回答: Erik Meade 2020-12-2
Hi,
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!

回答(1 个)

Erik Meade
Erik Meade 2020-12-2
Hi,
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.
-Erik

类别

Help CenterFile Exchange 中查找有关 Get Started with GPU Coder 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by