tag:blogger.com,1999:blog-61153974851219571442018-03-07T15:36:29.700-08:00kgochevkgochevnoreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6115397485121957144.post-85678535459380631672011-05-02T20:20:00.000-07:002011-05-02T20:20:53.259-07:00CIS 565 - GPU Programming and Architecture Project<b>Optimization 2: Sparsity Optimization of the Conjugate Gradient Solver</b><br /><br />It is interesting to note that the conjugate gradient solver needs to solve a system of equations in the form A p = d, where A is a very sparse matrix computed based on the boundary conditions of the underlying grid, p is unknown pressure vector needed to make the velocity field divergence-free, and d is the current velocity divergence of each cell. A is quadratically proportional to the grid size and it has at most 7 non-zero entries in each row and each column.<br /><br />Therefore, I decided to take advantage of the sparsity of A in the conjugate gradient solver. Instead of doing large matrix-vector multiplications of matrix rows consisting of predominantly 0s, I decided to make the GPU-based conjugate gradient solver compute the non-zero coefficients of A directly from the grid. Thus, instead of computing A on the CPU and passing it down to the GPU, I decided to send the grid information (namely whether a cell is fluid or solid) to the GPU. The grid information is much smaller data set than A, which should save on transfer times. However, the more important benefit of this optimization is that each matrix-vector multiplication has to do only 7 multiplications per row of A.<br /><br />The table below shows a performance comparison between simulations using the CPU implementation of the conjugate gradient solver, the naive GPU implementation, and the GPU implementation optimized for sparsity. The averages were computed over 100 frames of simulation. The grid contained 2048 cells, which means that the conjugate gradient solver in the projection step calculated the inverse of a 2048x2048 sparse matrix. In all cases the conjugate gradient solver was allowed a maximum of 200 iterations and a target error tolerance of 0.01.<br /><br /><table><tbody><tr> <td></td><td style="border: 1px solid white;"><b><span style="color: lightblue;">CPU solver*</span></b></td><td style="border: 1px solid white;"><b><span style="color: lime;">GPU solver version 1**</span></b></td><td style="border: 1px solid white;"><b><span style="color: lime;">GPU solver version 2**</span></b></td></tr> <tr> <td style="border: 1px solid white;">Grid Size</td><td style="border: 1px solid white;">64x32x1 (2048 cells)</td><td style="border: 1px solid white;">64x32x1 (2048 cells)</td> <td style="border: 1px solid white;">64x32x1 (2048 cells)</td> </tr> <tr> <td style="border: 1px solid white;">Avg. time per frame</td><td style="border: 1px solid white;">33774.14 msec</td><td style="border: 1px solid white;">1852.69 msec</td> <td style="border: 1px solid white;">249.55 msec</td> </tr> <tr> <td style="border: 1px solid white;">Avg. time per projection step</td><td style="border: 1px solid white;">33754.32 msec</td><td style="border: 1px solid white;"><span style="color: red;">***</span>1832.78 msec</td> <td style="border: 1px solid white;"><span style="color: red;">***</span>230.95 msec</td> </tr> <tr> <td style="border: 1px solid white;">Speedup (proj. step)</td><td style="border: 1px solid white;">x1.0 (base)</td><td style="border: 1px solid white;">x18.42</td> <td style="border: 1px solid white;">x146.15</td> </tr> <tr> <td style="border: 1px solid white;">Speedup (frame)</td><td style="border: 1px solid white;">x1.0 (base)</td><td style="border: 1px solid white;"><b><span style="color: green;">x18.23</span></b></td> <td style="border: 1px solid white;"><b><span style="color: green;">x135.34</span></b></td> </tr> </tbody></table><br /><span style="color: lightblue;">*</span> Intel Core i7 (2.67 GHz)<br /><span style="color: lime;">**</span> nVidia GeForce GTX 295<br /><span style="color: red;">***</span> The reported time per projection step includes the time it takes to transfer data from the CPU to the GPU.<br /><br />Even with the sparsity optimization, the bulk of time is spent in the projection step (92.55%). My future goals are to move the entire simulation to the GPU and implement volumetric rendering for better visualization of the results. However, I don't expect any significant improvement in the running time after I move the rest of the simulation to the GPU as the CPU part of the implementation currently takes about 20msec., which is not at all significant compared to the 230msec required for the projection step.<br /><br />Link to my final presentation poster: <a href="http://www.seas.upenn.edu/~kgochev/CIS565/project/poster.pdf">poster</a><br />The video below is of a simulation on a 32x32x5 grid (5120 cells) and was running at about 750 msec per frame. The smoke in the simulation was visualized using translucent cubes.<br /><br /><div class="separator" style="clear: both; text-align: center;"><iframe allowFullScreen='true' webkitallowfullscreen='true' mozallowfullscreen='true' width='480' height='320' src='https://www.blogger.com/video.g?token=AD6v5dwHVq7mcQjTBurNc3neoy80ZGAS6SAt_qzvGB6pO-r1yie6ZP4a0PcsuIbnTZbD67SEHcEVKRaJgmcd0f8e5w' class='b-hbp-video b-uploaded' FRAMEBORDER='0' /></div>kgochevnoreply@blogger.com0tag:blogger.com,1999:blog-6115397485121957144.post-88878629894922493052011-04-07T14:37:00.000-07:002011-04-07T14:49:05.384-07:00CIS 565 - GPU Programming and Architecture Project<b>Optimization 1: Moving the conjugate-gradient solver to the GPU</b><br /><br />I successfully moved the CPU conjugate gradient solver to the GPU. The process involved writing a bunch of small (and as of yet unoptimized) linear algebra kernels, such as matrix-vector multiplication, vector inner product, vector L2-norm, etc. I ran unit tests on the individual kernels to make sure they were working properly, but I left benchmarking and optimizations of the kernels for a later stage of the project.<br /><br />The table below shows a performance comparison between simulations using the CPU implementation of the conjugate gradient solver I had before, and the naive (unoptimized) GPU implementation of the solver. The averages were computed over 100 frames of simulation. The grid contained 2048 cells, which means that the conjugate gradient solver in the projection step calculated the inverse of a 2048x2048 sparse matrix. In both cases the conjugate gradient solver was allowed a maximum of 200 iterations and a target error tolerance of 0.01.<br /><br /><table><tr> <td></td><td style="border: 1px solid white"><b><font color=lightblue>CPU solver*</font></b></td><td style="border: 1px solid white"><b><font color=lime>GPU solver**</font></b></td> </tr><tr> <td style="border: 1px solid white">Grid Size</td><td style="border: 1px solid white">64x32x1 (2048 cells)</td><td style="border: 1px solid white">64x32x1 (2048 cells)</td> </tr><tr> <td style="border: 1px solid white">Avg. time per frame</td><td style="border: 1px solid white">33774.14 msec</td><td style="border: 1px solid white">1852.69 msec</td> </tr><tr> <td style="border: 1px solid white">Avg. time per projection step</td><td style="border: 1px solid white">33754.32 msec</td><td style="border: 1px solid white"><font color=red>***</font>1832.78 msec</td> </tr><tr> <td style="border: 1px solid white">Speedup (proj. step)</td><td style="border: 1px solid white">x1.0 (base)</td><td style="border: 1px solid white">x18.42</td> </tr><tr> <td style="border: 1px solid white">Speedup (frame)</td><td style="border: 1px solid white">x1.0 (base)</td><td style="border: 1px solid white"><b><font color=green>x18.23</font></b></td> </tr></table><br /><font color=lightblue>*</font> Intel Core i7 (2.67 GHz)<br /><font color=lime>**</font> nVidia GeForce GTX 295<br /><font color=red>***</font> The reported time per projection step includes the time it takes to transfer data from the CPU to the GPU.<br /><br />As seen from the table above, the projection step takes more than 99% of the time per frame. Thus, achieving a speedup of x18.42 on the projection step computations results in roughly the same speedup of the overall simulation.<br /><br />(to be continued ...)kgochevnoreply@blogger.com2tag:blogger.com,1999:blog-6115397485121957144.post-72245714543574315702011-03-22T22:21:00.000-07:002011-04-07T13:54:06.368-07:00CIS 565 - GPU Programming and Architecture ProjectI am embarking on a new project for my GPU Programming and Architecture class. The project goal is to produce a GPU implementation of a physically-based fluid simulator, using semi-Lagrangian advection scheme on a staggered grid. Technically I will be modifying and extending the CPU implementation that I made for another class (Physically-Based Animation).<br /><br />The first goal of the project is to convert the projection step (i.e. the pressure update), which is the bottleneck of the CPU implementation, to run on the GPU. The projection step's purpose is to compute pressure differences between neighboring cells and adjust the fluid's velocity field, so that it stays incompressible and divergence-free. The step involves the solving of a linear system Ap = d, where A is n x n sparse matrix (where n is the number of cells in the simulation), d is a vector containing the velocity divergence of each of the n cells, and p is a unknown vector containing the pressures in each of the n cells. A and d are known, and the goal is to compute p, by computing the inverse of A (p = A<span class="Apple-style-span" style="font-size: xx-small;">inv</span> d) . Once the pressures for each of the cells are computed, the velocity field can be adjusted to become divergence free.<br /><br /><div class="separator" style="clear: both; text-align: center;"><iframe allowFullScreen='true' webkitallowfullscreen='true' mozallowfullscreen='true' width='480' height='360' src='https://www.youtube.com/embed/BymKekf3g-Q?feature=player_embedded' FRAMEBORDER='0' /></div><br /><div class="separator" style="clear: both; text-align: center;"><iframe allowFullScreen='true' webkitallowfullscreen='true' mozallowfullscreen='true' width='480' height='360' src='https://www.youtube.com/embed/u7wNJwkPgd4?feature=player_embedded' FRAMEBORDER='0' /></div><br />These videos are from my CPU implementation, which ran far from real-time even on small grids.<br /><br />(to be continued...)kgochevnoreply@blogger.com0