# Understading memory sharing in a GPGPU using a lattice example

I am new in the GPUs world, I used them in Matlab ambient so I didn't need to appreciate the subtleties of these devices.

I know that a GPU can be divided into multiprocessors (also called Streaming Multiprocessors) whose single processors share a cache memory. I don't understand if the host is required for the communication between multiprocessors. In order to center my question I am going to give an example.

Let us take a fluid dynamics simulation in which I perform a discretization of my domain into $$N$$ cells. After that I assign to each multiprocessor $$M$$ cells. In this ultra-generic simulation I have some continuity equations, between one cell and the neighbors, which must be updated for each time step. If the cells belong to the same multiprocessor it is ok since they can share memory using the common cache. On the contrary, the situation is different for those cells whose (part of their) neighbors cells belong to a different multiprocessor. My question is:

How can cells belonging to different multiprocessors communicate for each time step?

I could use the memory transfer to the CPU but I think it would not be efficient.

In my opinion, this is the central point of the computational aspect of Lattice Boltzmann Methods.

Help me to understand that.

• Depending on the numerical scheme perhaps you could get away with computing the overlaps in each procesor? (I was going to ask the same question yesterday actually :')) – Emil Sep 13 '20 at 9:09
• @Emil yes of course it depends on the numerical scheme.But my question was focused on data transfer between the whole SM and the global memory. – Siderius Sep 13 '20 at 13:44

## 1 Answer

The GPU consists of several streaming multiprocessors.

Each SM has 64-96kB of shared memory that can be accessed by up to 1024-2048 threads. This shared memory allows these threads to communicate.

To communicate between SMs you must write to and read from the GPU's global memory, which is 4-32GB in size.

But it is best to think of your problem as consisting of a large number of 1D or 2D chunks which may be assigned to arbitrary SMs and the "communication" as storage of intermediate results. That is, the SM may "communicate" with itself.

Thus, because the GPU's memory is used for storing these intermediates, communication with the host in the middle of a calculation is not necessary.

• So you are saying that we define an array on the global memory and we assing several components to each SM. In this way for each time step, SMs access to the global memory and update that array. Is it for this point that the debate about the structure of arrays vs array of structures and the coalescence optimization arise? – Siderius Sep 12 '20 at 15:33
• If your computation overlaps you'd need to write to a second array to avoid mutating your input. The SoA Vs AoS isn't a debate so much as a difficult design choice. – Richard Sep 12 '20 at 15:48
• I think in compute shaders modifiers groupshared (or shared?) needs to be used or type RWStructuredBuffer to share between threads. (Surprisingly hard to find answers about this in documentation) – Emil Sep 13 '20 at 8:52