From d9eb72d78ce75110fb3439d68c0ed35808382e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20R=2E=20Brodtkorb?= Date: Thu, 1 Nov 2018 20:43:28 +0100 Subject: [PATCH] Refactoring --- GPUSimulators/Common.py | 11 +- GPUSimulators/EE2D_KP07_dimsplit.py | 10 +- GPUSimulators/Simulator.py | 13 +- GPUSimulators/cuda/EE2D_KP07_dimsplit.cu | 175 ++++++++++----------- GPUSimulators/cuda/common.h | 192 +++++++++++------------ GPUSimulators/cuda/limiters.h | 40 ++--- 6 files changed, 220 insertions(+), 221 deletions(-) diff --git a/GPUSimulators/Common.py b/GPUSimulators/Common.py index b973692..a0c2fb3 100644 --- a/GPUSimulators/Common.py +++ b/GPUSimulators/Common.py @@ -92,7 +92,7 @@ class CudaArray2D: #self.logger.debug("Allocating [%dx%d] buffer", self.nx, self.ny) #Should perhaps use pycuda.driver.mem_alloc_data.pitch() here - self.data = pycuda.gpuarray.empty((ny_halo, nx_halo), dtype) + self.data = pycuda.gpuarray.zeros((ny_halo, nx_halo), dtype) #If we don't have any data, just allocate and return if cpu_data is None: @@ -310,5 +310,12 @@ class ArakawaA2D: stream.synchronize() return cpu_variables - + """ + Checks that data is still sane + """ + def check(self): + for i, gpu_variable in enumerate(self.gpu_variables): + var_sum = pycuda.gpuarray.sum(gpu_variable.data).get() + self.logger.debug("Data %d with size [%d x %d] has sum %f", i, gpu_variable.nx, gpu_variable.ny, var_sum) + assert np.isnan(var_sum) == False, "Data contains NaN values!" \ No newline at end of file diff --git a/GPUSimulators/EE2D_KP07_dimsplit.py b/GPUSimulators/EE2D_KP07_dimsplit.py index 84fcc33..99ba8c7 100644 --- a/GPUSimulators/EE2D_KP07_dimsplit.py +++ b/GPUSimulators/EE2D_KP07_dimsplit.py @@ -57,7 +57,7 @@ class EE2D_KP07_dimsplit (Simulator.BaseSimulator): dx, dy, dt, \ gamma, \ theta=1.3, \ - block_width=8, block_height=4): + block_width=16, block_height=8): # Call super constructor super().__init__(context, \ @@ -65,10 +65,8 @@ class EE2D_KP07_dimsplit (Simulator.BaseSimulator): dx, dy, dt, \ block_width, block_height) self.gamma = np.float32(gamma) - self.theta = np.float32(theta) - #Get kernels #Get kernels self.kernel = context.get_prepared_kernel("cuda/EE2D_KP07_dimsplit.cu", "KP07DimsplitKernel", \ "iifffffiPiPiPiPiPiPiPiPi", \ @@ -135,4 +133,8 @@ class EE2D_KP07_dimsplit (Simulator.BaseSimulator): self.t += dt def download(self): - return self.u0.download(self.stream) \ No newline at end of file + return self.u0.download(self.stream) + + def check(self): + self.u0.check() + self.u1.check() \ No newline at end of file diff --git a/GPUSimulators/Simulator.py b/GPUSimulators/Simulator.py index 3792a01..94ef35b 100644 --- a/GPUSimulators/Simulator.py +++ b/GPUSimulators/Simulator.py @@ -122,6 +122,7 @@ class BaseSimulator: if (t.elapsed() >= next_print): self.logger.info("%s simulated %d of %d steps (Euler)", self, i, n) next_print += self.log_every + self.check() self.logger.info("%s simulated %f seconds to %f with %d steps (Euler)", self, t_end, self.t, n) @@ -153,6 +154,7 @@ class BaseSimulator: if (t.elapsed() >= next_print): self.logger.info("%s simulated %d of %d steps (RK2)", self, i, n) next_print += self.log_every + self.check() self.logger.info("%s simulated %f seconds to %f with %d steps (RK2)", self, t_end, self.t, n) return self.t, n @@ -184,6 +186,7 @@ class BaseSimulator: if (t.elapsed() >= next_print): self.logger.info("%s simulated %d of %d steps (Dimsplit)", self, i, n) next_print += self.log_every + self.check() self.logger.info("%s simulated %f seconds to %f with %d steps (Dimsplit)", self, t_end, self.t, 2*n) return self.t, 2*n @@ -203,13 +206,17 @@ class BaseSimulator: def stepDimsplitYX(self, dt): raise(NotImplementedError("Needs to be implemented in subclass")) - - def sim_time(self): - return self.t def download(self): raise(NotImplementedError("Needs to be implemented in subclass")) + + def check(self): + raise(NotImplementedError("Needs to be implemented in subclass")) + + def sim_time(self): + return self.t def synchronize(self): self.stream.synchronize() + diff --git a/GPUSimulators/cuda/EE2D_KP07_dimsplit.cu b/GPUSimulators/cuda/EE2D_KP07_dimsplit.cu index 370efbd..2397712 100644 --- a/GPUSimulators/cuda/EE2D_KP07_dimsplit.cu +++ b/GPUSimulators/cuda/EE2D_KP07_dimsplit.cu @@ -27,97 +27,95 @@ along with this program. If not, see . __device__ void computeFluxF(float Q[4][BLOCK_HEIGHT+4][BLOCK_WIDTH+4], - float Qx[4][BLOCK_HEIGHT+2][BLOCK_WIDTH+2], - float F[4][BLOCK_HEIGHT+1][BLOCK_WIDTH+1], - const float gamma_, const float dx_, const float dt_) { - int j=threadIdx.y; - const int l = j + 2; //Skip ghost cells - for (int i=threadIdx.x; i( E0_ptr_, E0_pitch_, Q[3], nx_+4, ny_+4); __syncthreads(); - //Fix boundary conditions noFlowBoundary(Q[0], nx_, ny_); - noFlowBoundary(Q[1], nx_, ny_); + noFlowBoundary(Q[1], nx_, ny_); noFlowBoundary(Q[2], nx_, ny_); noFlowBoundary(Q[3], nx_, ny_); __syncthreads(); - //Step 0 => evolve x first, then y @@ -186,12 +182,11 @@ __global__ void KP07DimsplitKernel( //Set boundary conditions noFlowBoundary(Q[0], nx_, ny_); - noFlowBoundary(Q[1], nx_, ny_); + noFlowBoundary(Q[1], nx_, ny_); noFlowBoundary(Q[2], nx_, ny_); noFlowBoundary(Q[3], nx_, ny_); __syncthreads(); - //Compute fluxes along the y axis and evolve minmodSlopeY(Q, Qx, theta_); __syncthreads(); @@ -200,7 +195,7 @@ __global__ void KP07DimsplitKernel( __syncthreads(); evolveG(Q, F, dy_, dt_); - __syncthreads(); + __syncthreads(); } //Step 1 => evolve y first, then x @@ -208,14 +203,16 @@ __global__ void KP07DimsplitKernel( //Compute fluxes along the y axis and evolve minmodSlopeY(Q, Qx, theta_); __syncthreads(); + computeFluxG(Q, Qx, F, gamma_, dy_, dt_); __syncthreads(); + evolveG(Q, F, dy_, dt_); __syncthreads(); - + //Set boundary conditions noFlowBoundary(Q[0], nx_, ny_); - noFlowBoundary(Q[1], nx_, ny_); + noFlowBoundary(Q[1], nx_, ny_); noFlowBoundary(Q[2], nx_, ny_); noFlowBoundary(Q[3], nx_, ny_); __syncthreads(); @@ -223,8 +220,10 @@ __global__ void KP07DimsplitKernel( //Compute fluxes along the x axis and evolve minmodSlopeX(Q, Qx, theta_); __syncthreads(); + computeFluxF(Q, Qx, F, gamma_, dx_, dt_); __syncthreads(); + evolveF(Q, F, dx_, dt_); __syncthreads(); } diff --git a/GPUSimulators/cuda/common.h b/GPUSimulators/cuda/common.h index 4a251e3..f936fce 100644 --- a/GPUSimulators/cuda/common.h +++ b/GPUSimulators/cuda/common.h @@ -151,86 +151,91 @@ inline __device__ void writeBlock(float* ptr_, int pitch_, template __device__ void noFlowBoundary(float Q[block_height+2*ghost_cells][block_width+2*ghost_cells], const int nx_, const int ny_) { - const int ti = blockDim.x*blockIdx.x + threadIdx.x + ghost_cells; - const int tj = blockDim.y*blockIdx.y + threadIdx.y + ghost_cells; - const int i = threadIdx.x + ghost_cells; - const int j = threadIdx.y + ghost_cells; - - - // West boundary - if (ti == ghost_cells) { - Q[j][i-1] = scale_east_west*Q[j][i]; - } - if (ghost_cells >= 2 && ti == ghost_cells + 1) { - Q[j][i-3] = scale_east_west*Q[j][i]; - } - if (ghost_cells >= 3 && ti == ghost_cells + 2) { - Q[j][i-5] = scale_east_west*Q[j][i]; - } - if (ghost_cells >= 4 && ti == ghost_cells + 3) { - Q[j][i-7] = scale_east_west*Q[j][i]; - } - if (ghost_cells >= 5 && ti == ghost_cells + 4) { - Q[j][i-9] = scale_east_west*Q[j][i]; + for (int j=threadIdx.y; j= 2 && ti == ghost_cells + 1) { + Q[j][i-3] = scale_east_west*Q[j][i]; + } + if (ghost_cells >= 3 && ti == ghost_cells + 2) { + Q[j][i-5] = scale_east_west*Q[j][i]; + } + if (ghost_cells >= 4 && ti == ghost_cells + 3) { + Q[j][i-7] = scale_east_west*Q[j][i]; + } + if (ghost_cells >= 5 && ti == ghost_cells + 4) { + Q[j][i-9] = scale_east_west*Q[j][i]; + } + + + + // East boundary + if (ti == nx_ + ghost_cells - 1) { + Q[j][i+1] = scale_east_west*Q[j][i]; + } + if (ghost_cells >= 2 && ti == nx_ + ghost_cells - 2) { + Q[j][i+3] = scale_east_west*Q[j][i]; + } + if (ghost_cells >= 3 && ti == nx_ + ghost_cells - 3) { + Q[j][i+5] = scale_east_west*Q[j][i]; + } + if (ghost_cells >= 4 && ti == nx_ + ghost_cells - 4) { + Q[j][i+7] = scale_east_west*Q[j][i]; + } + if (ghost_cells >= 5 && ti == nx_ + ghost_cells - 5) { + Q[j][i+9] = scale_east_west*Q[j][i]; + } } - - // East boundary - if (ti == nx_ + ghost_cells - 1) { - Q[j][i+1] = scale_east_west*Q[j][i]; - } - if (ghost_cells >= 2 && ti == nx_ + ghost_cells - 2) { - Q[j][i+3] = scale_east_west*Q[j][i]; - } - if (ghost_cells >= 3 && ti == nx_ + ghost_cells - 3) { - Q[j][i+5] = scale_east_west*Q[j][i]; - } - if (ghost_cells >= 3 && ti == nx_ + ghost_cells - 4) { - Q[j][i+7] = scale_east_west*Q[j][i]; - } - if (ghost_cells >= 3 && ti == nx_ + ghost_cells - 5) { - Q[j][i+9] = scale_east_west*Q[j][i]; - } - - - - - // South boundary - if (tj == ghost_cells) { - Q[j-1][i] = scale_north_south*Q[j][i]; - } - if (ghost_cells >= 2 && tj == ghost_cells + 1) { - Q[j-3][i] = scale_north_south*Q[j][i]; - } - if (ghost_cells >= 3 && tj == ghost_cells + 2) { - Q[j-5][i] = scale_north_south*Q[j][i]; - } - if (ghost_cells >= 4 && tj == ghost_cells + 3) { - Q[j-7][i] = scale_north_south*Q[j][i]; - } - if (ghost_cells >= 5 && tj == ghost_cells + 4) { - Q[j-9][i] = scale_north_south*Q[j][i]; - } - - - - // North boundary - if (tj == ny_ + ghost_cells - 1) { - Q[j+1][i] = scale_north_south*Q[j][i]; - } - if (ghost_cells >= 2 && tj == ny_ + ghost_cells - 2) { - Q[j+3][i] = scale_north_south*Q[j][i]; - } - if (ghost_cells >= 3 && tj == ny_ + ghost_cells - 3) { - Q[j+5][i] = scale_north_south*Q[j][i]; - } - if (ghost_cells >= 3 && tj == ny_ + ghost_cells - 4) { - Q[j+7][i] = scale_north_south*Q[j][i]; - } - if (ghost_cells >= 3 && tj == ny_ + ghost_cells - 5) { - Q[j+9][i] = scale_north_south*Q[j][i]; + + for (int i=threadIdx.x; i= 2 && tj == ghost_cells + 1) { + Q[j-3][i] = scale_north_south*Q[j][i]; + } + if (ghost_cells >= 3 && tj == ghost_cells + 2) { + Q[j-5][i] = scale_north_south*Q[j][i]; + } + if (ghost_cells >= 4 && tj == ghost_cells + 3) { + Q[j-7][i] = scale_north_south*Q[j][i]; + } + if (ghost_cells >= 5 && tj == ghost_cells + 4) { + Q[j-9][i] = scale_north_south*Q[j][i]; + } + + + + // North boundary + if (tj == ny_ + ghost_cells - 1) { + Q[j+1][i] = scale_north_south*Q[j][i]; + } + if (ghost_cells >= 2 && tj == ny_ + ghost_cells - 2) { + Q[j+3][i] = scale_north_south*Q[j][i]; + } + if (ghost_cells >= 3 && tj == ny_ + ghost_cells - 3) { + Q[j+5][i] = scale_north_south*Q[j][i]; + } + if (ghost_cells >= 4 && tj == ny_ + ghost_cells - 4) { + Q[j+7][i] = scale_north_south*Q[j][i]; + } + if (ghost_cells >= 5 && tj == ny_ + ghost_cells - 5) { + Q[j+9][i] = scale_north_south*Q[j][i]; + } } } @@ -247,21 +252,14 @@ __device__ void noFlowBoundary(float Q[block_height+2*ghost_cells][block_width+2 template __device__ void evolveF(float Q[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], - float F[vars][block_height+1][block_width+1], + float F[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], const float dx_, const float dt_) { - //Index of thread within block - const int tx = threadIdx.x; - const int ty = threadIdx.y; - - const int i = tx + ghost_cells; //Skip local ghost cells - const int j = ty + ghost_cells; - - //Index of cell within domain - //const int ti = blockDim.x*blockIdx.x + threadIdx.x + ghost_cells; //Skip global ghost cells, i.e., +1 - //const int tj = blockDim.y*blockIdx.y + threadIdx.y + ghost_cells; - //if (ti > ghost_cells-1 && ti < nx_+ghost_cells && tj > ghost_cells-1 && tj < ny_+ghost_cells) { for (int var=0; var < vars; ++var) { - Q[var][j][i] = Q[var][j][i] + (F[var][ty][tx] - F[var][ty][tx+1]) * dt_ / dx_; + for (int j=threadIdx.y; j __device__ void evolveG(float Q[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], - float G[vars][block_height+1][block_width+1], + float G[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], const float dy_, const float dt_) { - //Index of thread within block - const int tx = threadIdx.x; - const int ty = threadIdx.y; - - const int i = tx + ghost_cells; //Skip local ghost cells, i.e., +1 - const int j = ty + ghost_cells; - for (int var=0; var < vars; ++var) { - Q[var][j][i] = Q[var][j][i] + (G[var][ty][tx] - G[var][ty+1][tx]) * dt_ / dy_; + for (int j=threadIdx.y+1; j -__device__ void minmodSlopeX(float Q[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], - float Qx[vars][block_height+2*(ghost_cells-1)][block_width+2*(ghost_cells-1)], +__device__ void minmodSlopeX(float Q[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], + float Qx[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], const float theta_) { - //Index of thread within block - const int tx = threadIdx.x; - const int ty = threadIdx.y; - - const int j = ty; - const int l = j + ghost_cells; //Skip ghost cells - //Reconstruct slopes along x axis - for (int i=tx; i -__device__ void minmodSlopeY(float Q[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], - float Qy[vars][block_height+2*(ghost_cells-1)][block_width+2*(ghost_cells-1)], +__device__ void minmodSlopeY(float Q[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], + float Qy[vars][block_height+2*ghost_cells][block_width+2*ghost_cells], const float theta_) { - //Index of thread within block - const int tx = threadIdx.x; - const int ty = threadIdx.y; - - const int i = tx; - const int k = i + ghost_cells; //Skip ghost cells - //Reconstruct slopes along y axis - for (int j=ty; j