Search code examples
pythonopenclpyopencl

Use OpenCl Global ID as an integer in the kernel


I just started looking into OpenCl in python using the pyopencl module.

I am interested in generating stuff without any inputs, generating samples of a sine wave for instance.

To do so, all I needed was the global id to make my calculations, but returning the global id result in some fancy numbers. I use the code below:

import numpy as np
import pyopencl as cl

Size = Width*Height

# Get platforms, both CPU and GPU
plat = cl.get_platforms()
GPU = plat[0].get_devices()

#Create context for GPU
ctx = cl.Context(GPU)

# Create queue for each kernel execution
queue = cl.CommandQueue(ctx)

mf = cl.mem_flags

# Kernel function
src = '''
__kernel void shader(__global float *result, __global int *width){

int w = *width;
size_t gid = get_global_id(0);

result[gid] = gid;
}
'''

#Kernel function instantiation
prg = cl.Program(ctx, src).build()

#Allocate memory for variables on the device
width_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=np.int32(Width))
result_g = cl.Buffer(ctx, mf.WRITE_ONLY, Size*8)

# Call Kernel. Automatically takes care of block/grid distribution
prg.shader(queue, (Size,), None , result_g, width_g)
result = np.empty((Size,))
cl.enqueue_copy(queue, result, result_g)

Image = result

All I do is copy the global id to the buffer object result_d, but when I inspect result, I get some numbers that are not even integers. I also tried setting my buffer to an integer instead of float, but the result was still the same.

What am I doing wrong?


Solution

  • The problem is that the result in the OpenCL kernel is of type float and the result on the host side is of type double.

    Specify the host buffer to be float to fix the problem:

    result = np.empty((Size,),dtype=np.float32)