Reputation: 20895
I am teaching myself CUDA from the ground up. I made this simple kernel that adds 1 to each of the relevant elements within a 2D array. The elements of the 2D array stem from the red channel of an image (zebra.jpg).
from pycuda.compiler import SourceModule
import matplotlib.image as img
import pycuda.driver as cuda
import pycuda.autoinit
import numpy as np
my_image = img.imread("zebra.jpg")[:,:,0]
block = (32, 32, 1)
grid = (8, 8)
if __name__ == '__main__':
width, height = np.int32(my_image.shape)
im = np.array(my_image)
print 'original sum: ' + str(np.sum(im))
# Create the CUDA kernel, and run it.
mod = SourceModule("""
__global__ void add1ToEverything(float* r, int w, int h) {
int rowID = blockDim.y * blockDim.y + threadIdx.y;
int colID = blockDim.x * blockIdx.x + threadIdx.x;
if (rowID > 0 && rowID < h - 2 && colID > 0 && colID < w - 2)
r[rowID * w + colID] += 1.0;
}
""")
func = mod.get_function('add1ToEverything')
for i in range(0, 5):
func(cuda.InOut(im), width, height, block=block, grid=grid)
print 'new sum: ' + str(np.sum(im))
However, then I run this program, I get the following results.
original sum: 1828815
new sum: 1828815
Why is my original sum identical to my new sum? Shouldn't the new sum be greater?
Here is zebra.jpg.
Upvotes: 2
Views: 306
Reputation: 2870
your problem is at the line:
int rowID = blockDim.y * blockDim.y + threadIdx.y;
it should be:
int rowID = blockDim.y * blockIdx.y + threadIdx.y;
Upvotes: 4