diff --git a/GPUSimulators/common/arrays/array2d.py b/GPUSimulators/common/arrays/array2d.py index b63acf1..03452ad 100644 --- a/GPUSimulators/common/arrays/array2d.py +++ b/GPUSimulators/common/arrays/array2d.py @@ -36,4 +36,14 @@ class BaseArray2D(object): raise ValueError("Wrong size of data type") if np.isfortran(cpu_data): - raise TypeError("Wrong datatype (Fortran, expected C)") \ No newline at end of file + raise TypeError("Wrong datatype (Fortran, expected C)") + + def check(self, x, y, nx, ny, cpu_data): + if nx != cpu_data.shape[1]: + raise ValueError + if ny != cpu_data.shape[0]: + raise ValueError + if x + nx > self.nx + 2 * self.x_halo: + raise ValueError + if y + ny > self.ny + 2 * self.y_halo: + raise ValueError diff --git a/GPUSimulators/common/arrays/cuda/arkawa2d.py b/GPUSimulators/common/arrays/cuda/arkawa2d.py index 23730ed..df5f2d4 100644 --- a/GPUSimulators/common/arrays/cuda/arkawa2d.py +++ b/GPUSimulators/common/arrays/cuda/arkawa2d.py @@ -25,4 +25,4 @@ class ArakawaA2D(BaseArakawaA2D): + f"has average {var_sum / (gpu_variable.nx * gpu_variable.ny)}") if np.isnan(var_sum): - raise ValueError("Data contains NaN values!") \ No newline at end of file + raise ValueError("Data contains NaN values!") diff --git a/GPUSimulators/common/arrays/cuda/array2d.py b/GPUSimulators/common/arrays/cuda/array2d.py index 19a0140..99d913e 100644 --- a/GPUSimulators/common/arrays/cuda/array2d.py +++ b/GPUSimulators/common/arrays/cuda/array2d.py @@ -58,10 +58,7 @@ class CudaArray2D(BaseArray2D): # Non-pagelocked: cpu_data = np.empty((ny, nx), dtype=np.float32) # cpu_data = self.memorypool.allocate((ny, nx), dtype=np.float32) - assert nx == cpu_data.shape[1] - assert ny == cpu_data.shape[0] - assert x + nx <= self.nx + 2 * self.x_halo - assert y + ny <= self.ny + 2 * self.y_halo + self.check(x, y, nx, ny, cpu_data) # Create a copy object from device to host copy = cuda.Memcpy2D() @@ -93,10 +90,7 @@ class CudaArray2D(BaseArray2D): else: x, y, nx, ny = extent - assert (nx == cpu_data.shape[1]) - assert (ny == cpu_data.shape[0]) - assert (x + nx <= self.nx + 2 * self.x_halo) - assert (y + ny <= self.ny + 2 * self.y_halo) + self.check(x, y, nx, ny, cpu_data) # Create a copy object from device to host copy = cuda.Memcpy2D() @@ -113,4 +107,4 @@ class CudaArray2D(BaseArray2D): copy.width_in_bytes = int(nx) * cpu_data.itemsize copy.height = int(ny) - copy(stream) \ No newline at end of file + copy(stream)