Transfer and access of OpenCL imageNd_t in Python using PyOpenCL -
i'm trying simple data transfer of data input array output array in pyopencl. have following python script (test.py):
import numpy np import pyopencl cl # input array # in_arr = np.arange(27).astype(np.float32).reshape((3,3,3)) in_arr = np.arange(9).astype(np.float32).reshape((3,3)) # find list of devices. don't use iris pro, use nvidia gpu. platforms = cl.get_platforms() devicelist = platforms[0].get_devices(cl.device_type.gpu) testdevice in devicelist: if 'gt' in testdevice.name: device = testdevice # initialisation of context work within. context = cl.context(devices=[device]) # create queue in current context add commands to. queue = cl.commandqueue(context) # output array. out_arr = np.zeros(in_arr.shape).astype(np.float32) # memory flags mf = cl.mem_flags # buffers. in_buf = cl.image(context, mf.read_only, cl.imageformat(cl.channel_order.intensity,cl.channel_type.float), shape=in_arr.shape) out_buf = cl.image(context, mf.write_only, cl.imageformat(cl.channel_order.intensity,cl.channel_type.float), shape=out_arr.shape) # upstream queue. cl.enqueue_copy(queue,in_buf,in_arr,origin=(0,0,0),region=in_arr.shape) # read kernel. kernel = open("test.cl", "r").read() # compile program program = cl.program(context,kernel).build() # run program. program.test(queue, out_arr.shape, none, in_buf, out_buf) # downstream queue. cl.enqueue_copy(queue,out_arr,out_buf,origin=(0,0,0),region=out_arr.shape) print('results:') print('-- in --------') print(in_arr) print('-- out -------') print(out_arr) and following kernel (test.cl):
const sampler_t sampler = clk_normalized_coords_false | clk_filter_nearest | clk_address_clamp_to_edge; __kernel void test(read_only image2d_t arr_in, write_only image2d_t arr_out) { int x = get_global_id(0); int y = get_global_id(1); // int z = get_global_id(2); int2 pos = (x,y); // cl_intensity(i, i, i, i) float4 val = read_imagef(arr_in, sampler, pos); printf("point: %i %i, array value: %f \n",x,y,val[0]); write_imagef(arr_out,pos,val); } the current output getting is:
point: 0 0, array value: 0.000000 point: 1 0, array value: 0.000000 point: 2 0, array value: 0.000000 point: 0 1, array value: 4.000000 point: 1 1, array value: 4.000000 point: 2 1, array value: 4.000000 point: 0 2, array value: 8.000000 point: 1 2, array value: 8.000000 point: 2 2, array value: 8.000000 results: -- in -------- [[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.]] -- out ------- [[ 0. 0. 0.] [ 0. 4. 0.] [ 0. 0. 8.]] the problem: reason, , don't understand why, (x,y) coordinate doesn't seem reading right values? or perhaps there data copy issue?
the intent make work 3d assume pretty straight forward once working in 2d. please offer assistance or insight data transfer , access issues?
in advance: thank kindly, guys huge help!
--
thank you @jensmunk picking on enforcing 32 bit floats on:
out_arr = np.zeros(in_arr.shape).astype(np.float32)
Comments
Post a Comment