<p>Let's go through the paper you mentioned and try to list the factors which will affect your decision on whether to use GPU or CPU for simulation.
I will also add some comments about my opinion for each factor.</p>

<h2>Computation bound</h2>

<p>A GPU program runs in a SIMT(Single instruction, multiple threads) model.
In this model, if some threads stall due to memory access or divergence,
thread scheduler will schedule another set of threads to run
to hide the memory latency.</p>

<p>As we can see it requires adequate computation load to make this mechanism work.
If one implementation is memory bound, we hardly benefit from the SIMT programming model.</p>

<p>Fortunately, fluid simulation is a computation bound task for most of the cases.</p>

<h2>Locality</h2>

<p>In the paper you mentioned we can observe perfect locality:<br>
for each grid point we only have to examine its neighbors.</p>

<p>The actual memory access pattern depends on the finite difference scheme you use.
But for all grid based simulation methods(Eulerian or Lattice-Boltzmann method), I think the locality always presents.</p>

<h2>Divergence</h2>

<p>A warp (a group of 32 threads) in a GPU program can be executed in parallel if they follow the same code path. Otherwise the program diverges and the execution of each branch will be serialized. Divergence will hurt the performance.</p>

<p>In particle based simulations (such as SPH method) we usually have to query the nearest N particles. A tree-like data structure will be used.</p>

<p>In GPU a tree data structure may introduce divergence because particles may fall into different branches.</p>

<p>The paper you mentioned uses grid based methods, where all threads execute along the same code path (Jacobi or Gauss-Seidel iteration), so divergence is not a problem here.</p>

<h2>Interactivity</h2>

<p>In the paper it uses static boundary conditions.<br>
For this case you just need to upload the boundary conditions to GPU memory only once.</p>

<p>If you have dynamic boundary conditions, for example, which are specified by the user at each frame, the overhead of memory transfer between GPU and CPU should be taken into consideration.</p>

<h2>Spatial complexity</h2>

<p>Uniform grid based simulations cannot handle very large computation volume, because the spatial complexity is cubic.</p>

<p>There are adaptive grid methods, which use relatively higher resolution for the computation region we are most interested in. But that would also bring in divergence.</p>

<h2>Numerical issues</h2>

<p>For fluid simulation you cannot decide the number of steps solely based on the performance/frame rate.</p>

<p>In grid based methods, the Courant–Friedrichs–Lewy (CFL) condition puts a constraint on how large simulation step you can have.
Given a fixed time step, too large or too small steps would cause instability and blow up the simulation.</p>

<p>In particle based methods and some stable grid based methods (such as the semi-Lagrangian method used in the paper), we can use relatively larger simulation steps.</p>

<p>But we still cannot increase the step size with no bound because of the accuracy.
For example in grid based methods a large step size would introduce too much numerical viscosity.</p>

<h2>Summary</h2>

<p>As a summary, if your simulation has strong convergence and locality, prefer a GPU implementation; otherwise, prefer a CPU one.</p>

<p>In practice, I will always implement a CPU version because it is easier to debug and it will be useful as a baseline.
Then I will profile it to locate the bottleneck and decide whether to have a GPU version.</p>

CC BY-SA 3.0
2016-10-04T18:31:23.817Z
Thanks, this will help me to work out how and what to implement and will give me some foundation to work from. Also, the last paragraph is a good advice!
Karlovsky120
CC BY-SA 3.0
2016-10-06T00:04:26.267Z
@DavidLively I just rephrased that paragraph. > The SIMT and SIMD models have nothing to do with bandwidth / "transfer latency.". I actually intended to describe how SIMT hides the memory access latency, not the transfer overhead. I think this is a major difference between SIMT and SIMD. I hope it is more clear now. Thank you for pointing out these issues.
TheBusyTypist
CC BY-SA 3.0
2016-10-06T14:55:52.160Z
@TheyBusyTypist Much better. Thanks for updating. :)
3Dave
CC BY-SA 3.0
2016-10-04T17:49:05.087Z
2016-10-05T23:50:44.620Z
2016-10-05T23:50:44.620Z
TheBusyTypist
<p>I have very recently started looking into real-time fluid simulation. Idea is to make a game that would use 3D fluid simulation as a core gameplay mechanic.</p>

<p>Looking around the web, I found <strong><a href="http://www.it.ubi.pt/17epcg/Actas/artigos/17epcg_submission_39.pdf" rel="nofollow">this</a></strong> article that compares the performance of fluid simulations on CPU and GPU. While the article itself is a bit dated, I assume that while CPUs and GPUs today are faster, the performance ration between the two has stayed similar enough to keep the article relevant. Actually, comparing their hardware (Core 2 Quad Q6600 and GeForce 8800 GT) to today's hardware (eg. i5 6600K and GTX 1070), CPU has <a href="http://cpu.userbenchmark.com/Compare/Intel-Core2-Quad-Q6600-vs-Intel-Core-i5-6600K/1980vs3503" rel="nofollow">230% performance gain</a>, while GPU has staggering <a href="http://gpu.userbenchmark.com/Compare/Nvidia-GeForce-8800-GT-vs-Nvidia-GTX-1070/m7722vs3609" rel="nofollow">1670% performance gain</a>. This would make the article more relevant today than it was before (more than six times).</p>

<p>Now, using the biggest grid they tested with (GS, 128^3, page 7, table 2), GPU simulation was over 3000 times faster than the CPU simulation. Even if utilizing 4 cores (they were using only a single thread), it's still over 700-fold performance gain (back in the day, today that would go up to 5000-fold). Given the results for smaller grids, that difference would only increase with bigger grids than 128^3.</p>

<p>The question is, along with all the normal rendering the GPU is tasked with doing, is it viable to burden it with the task of computing the simulation too, <strong>in any case at all</strong>?</p>

<p>What's the use of rendering the graphics at 60FPS if fluid simulation runs at 10 steps per second, effectively making the game look like it's running at 10FPS? Wouldn't it be better if I rendered graphics at 30FPS, but could do the simulation at 30 steps per second as well?</p>

<p>Basically, the question is, what would take longer:</p>

<ul>
<li>For the GPU to render a frame AND compute the simulation step</li>
</ul>

<p>OR</p>

<ul>
<li>For the CPU to compute rest of the game logic AND compute the simulation step.</li>
</ul>

<p>I know that this heavily depends on the GPU involved and the remainder of the GPU and CPU load, but look at the whole thing this way:</p>

<p>Even if I used just 1% of the power of GPU, the speed would still be 7(50!) times greater than using the CPU.</p>

<p>I'm a big novice in when it comes to computer graphics, so I may have gotten it all wrong. Does anything I wrote make any sense, or is it just gibberish?</p>

CC BY-SA 3.0
2016-10-02T14:52:09.190Z
This isn't really the type of question anyone else can answer. It's going to depend entirely on how your fluid sim and rendering are written and the hardware it's running on. Maybe you should put together a prototype and profile it. That could give you data to push you in one direction or the other.
user1118321
CC BY-SA 3.0
2016-10-02T15:57:11.243Z
I would suggest to provide more information to make this question specific. For example, how do you simulate your fluid? Do you use a particle based method such as SPH? Do you use a grid base method(the benchmark you mentioned is a grid based method)? How is your fluid coupled with other objects? All of these will impact the stability, convergence rate, and performance.
TheBusyTypist
CC BY-SA 3.0
2016-10-02T18:10:29.233Z
Well you did answer my question. The fact that you can't give me a (specific) answer tells me that there, in fact, are scenarios where GPU powered sim is viable and/or desirable. Look at the bold text.
Karlovsky120
CC BY-SA 3.0
2016-10-03T02:47:14.500Z
_What's the use of rendering the graphics at 60FPS if fluid simulation runs at 10 steps per second,_ Even if the simulation is slower, 60fps gives a better interactivity (think of rotating the scene). You could also for example interpolate between simulation steps.
Julien Guertault
CC BY-SA 3.0
2016-10-05T22:38:06.607Z
What @JulienGuertault said. There's a difference between a  physically correct simulation, and something that's good for gameplay. Also, this isn't really a graphics question. Have you tried gamedev.stackexchange?
3Dave
<fluid-sim>
Fluid sim: CPU vs. GPU
Karlovsky120
# Fluid sim: CPU vs. GPU

(Asked by: username_0 on 2016-10-02)

I have very recently started looking into real-time fluid simulation. Idea is to make a game that would use 3D fluid simulation as a core gameplay mechanic.

Looking around the web, I found **[this](http://www.it.ubi.pt/17epcg/Actas/artigos/17epcg_submission_39.pdf)** article that compares the performance of fluid simulations on CPU and GPU. While the article itself is a bit dated, I assume that while CPUs and GPUs today are faster, the performance ration between the two has stayed similar enough to keep the article relevant. Actually, comparing their hardware (Core 2 Quad Q6600 and GeForce 8800 GT) to today's hardware (eg. i5 6600K and GTX 1070), CPU has [230% performance gain](http://cpu.userbenchmark.com/Compare/Intel-Core2-Quad-Q6600-vs-Intel-Core-i5-6600K/1980vs3503), while GPU has staggering [1670% performance gain](http://gpu.userbenchmark.com/Compare/Nvidia-GeForce-8800-GT-vs-Nvidia-GTX-1070/m7722vs3609). This would make the article more relevant today than it was before (more than six times).

Now, using the biggest grid they tested with (GS, 128^3, page 7, table 2), GPU simulation was over 3000 times faster than the CPU simulation. Even if utilizing 4 cores (they were using only a single thread), it's still over 700-fold performance gain (back in the day, today that would go up to 5000-fold). Given the results for smaller grids, that difference would only increase with bigger grids than 128^3.

The question is, along with all the normal rendering the GPU is tasked with doing, is it viable to burden it with the task of computing the simulation too, **in any case at all**?

What's the use of rendering the graphics at 60FPS if fluid simulation runs at 10 steps per second, effectively making the game look like it's running at 10FPS? Wouldn't it be better if I rendered graphics at 30FPS, but could do the simulation at 30 steps per second as well?

Basically, the question is, what would take longer:

* For the GPU to render a frame AND compute the simulation step

OR

* For the CPU to compute rest of the game logic AND compute the simulation step.

I know that this heavily depends on the GPU involved and the remainder of the GPU and CPU load, but look at the whole thing this way:

Even if I used just 1% of the power of GPU, the speed would still be 7(50!) times greater than using the CPU.

I'm a big novice in when it comes to computer graphics, so I may have gotten it all wrong. Does anything I wrote make any sense, or is it just gibberish?

**Tags:** fluid-sim

> **Comments:**
> **username_1**: This isn't really the type of question anyone else can answer. It's going to depend entirely on how your fluid sim and rendering are written and the hardware it's running on. Maybe you should put together a prototype and profile it. That could give you data to push you in one direction or the other.  
> **username_2**: I would suggest to provide more information to make this question specific. For example, how do you simulate your fluid? Do you use a particle based method such as SPH? Do you use a grid base method(the benchmark you mentioned is a grid based method)? How is your fluid coupled with other objects? All of these will impact the stability, convergence rate, and performance.  
> **username_0**: Well you did answer my question. The fact that you can't give me a (specific) answer tells me that there, in fact, are scenarios where GPU powered sim is viable and/or desirable. Look at the bold text.  
> **username_3**: _What's the use of rendering the graphics at 60FPS if fluid simulation runs at 10 steps per second,_ Even if the simulation is slower, 60fps gives a better interactivity (think of rotating the scene). You could also for example interpolate between simulation steps.  
> **username_4**: What @username_3 said. There's a difference between a  physically correct simulation, and something that's good for gameplay. Also, this isn't really a graphics question. Have you tried gamedev.stackexchange?  

---

## 1 answers

### Answer by username_2 on 2016-10-04. Score: 3 | Accepted answer

Let's go through the paper you mentioned and try to list the factors which will affect your decision on whether to use GPU or CPU for simulation.
I will also add some comments about my opinion for each factor.

## Computation bound

A GPU program runs in a SIMT(Single instruction, multiple threads) model.
In this model, if some threads stall due to memory access or divergence,
thread scheduler will schedule another set of threads to run
to hide the memory latency.

As we can see it requires adequate computation load to make this mechanism work.
If one implementation is memory bound, we hardly benefit from the SIMT programming model.

Fortunately, fluid simulation is a computation bound task for most of the cases.

## Locality

In the paper you mentioned we can observe perfect locality:  
for each grid point we only have to examine its neighbors.

The actual memory access pattern depends on the finite difference scheme you use.
But for all grid based simulation methods(Eulerian or Lattice-Boltzmann method), I think the locality always presents.

## Divergence

A warp (a group of 32 threads) in a GPU program can be executed in parallel if they follow the same code path. Otherwise the program diverges and the execution of each branch will be serialized. Divergence will hurt the performance.

In particle based simulations (such as SPH method) we usually have to query the nearest N particles. A tree-like data structure will be used.

In GPU a tree data structure may introduce divergence because particles may fall into different branches.

The paper you mentioned uses grid based methods, where all threads execute along the same code path (Jacobi or Gauss-Seidel iteration), so divergence is not a problem here.

## Interactivity

In the paper it uses static boundary conditions.  
For this case you just need to upload the boundary conditions to GPU memory only once.

If you have dynamic boundary conditions, for example, which are specified by the user at each frame, the overhead of memory transfer between GPU and CPU should be taken into consideration.

## Spatial complexity

Uniform grid based simulations cannot handle very large computation volume, because the spatial complexity is cubic.

There are adaptive grid methods, which use relatively higher resolution for the computation region we are most interested in. But that would also bring in divergence.

## Numerical issues

For fluid simulation you cannot decide the number of steps solely based on the performance/frame rate.

In grid based methods, the Courant–Friedrichs–Lewy (CFL) condition puts a constraint on how large simulation step you can have.
Given a fixed time step, too large or too small steps would cause instability and blow up the simulation.

In particle based methods and some stable grid based methods (such as the semi-Lagrangian method used in the paper), we can use relatively larger simulation steps.

But we still cannot increase the step size with no bound because of the accuracy.
For example in grid based methods a large step size would introduce too much numerical viscosity.

## Summary

As a summary, if your simulation has strong convergence and locality, prefer a GPU implementation; otherwise, prefer a CPU one.

In practice, I will always implement a CPU version because it is easier to debug and it will be useful as a baseline.
Then I will profile it to locate the bottleneck and decide whether to have a GPU version.

> **Comments:**
> **username_0**: Thanks, this will help me to work out how and what to implement and will give me some foundation to work from. Also, the last paragraph is a good advice!  
> **username_2**: @DavidLively I just rephrased that paragraph. > The SIMT and SIMD models have nothing to do with bandwidth / "transfer latency.". I actually intended to describe how SIMT hides the memory access latency, not the transfer overhead. I think this is a major difference between SIMT and SIMD. I hope it is more clear now. Thank you for pointing out these issues.  
> **username_4**: @TheyBusyTypist Much better. Thanks for updating. :)  

---

