Overview of an N-body simulation implementation

Let's review the implementation of this in pseudocode format, followed by explaining its logic. In this example, we use gravitational potential to illustrate the basic form of computation in an all pairs N-body simulation. The implemented code can be found in 07_parallel_programming_pattern/05_n-body. Follow these steps to get started:

  1. Initialize n-space with random variables:
data[i] = 2.0f * (rand() / max) - 1.0f
  1. Declare and store the data in an intermediate shared memory space for efficient reuse. Synchronize it to guarantee that all the threads within the block see the updated values in shared memory:
for (int tile = 0; tile < gridDim.x; tile++) {
...
__shared__ float3 shared_position[blockDim.x];
float4 temp_position = p[tile * blockDim.x + threadIdx.x];
shared_position[threadIdx.x] = make_float3(temp_position.x, temp_position.y, temp_position.z);
__syncthreads();
...
}
  1. Calculate the force by iterating every block:
for (int j = 0; j < BLOCK_SIZE; j++) {
//Calculate Force
__syncthreads();
}
  1. Finally, compile the application with the nvcc compiler with the following command:
$nvcc -run --gpu-architecture=sm_70 -o n-body n_body.cu

As you can see, implementing an N-body simulation is an embarrassingly parallel task and quite straightforward. While we have implemented the basic version of code here, there are various algorithmic variations that exist. You can make use of this version as a template that you can improve, based on changes that are made to the algorithm.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.69.143