[PyOpenCL] PyOpenCL Question

Andrea Borsic Andrea.Borsic at Dartmouth.edu
Fri Apr 27 10:46:06 PDT 2012


Hi Andreas,

Thanks very much for your suggestion. Unfortunately I still did not 
manage to get this working.

For example this small test:

------------------------------------------
import pyopencl as cl
import numpy as np

ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)

mf = cl.mem_flags

# 3D sorce array 10x10x200
source_buff_host=np.zeros((10,10,200),dtype=np.float32)

# 2D "slice" dev array 10x10x1
dest_buff_dev = cl.Buffer(ctx, mf.READ_ONLY, 
source_buff_host.shape[0]*source_buff_host.shape[1]*source_buff_host.itemsize) 


# copy one 2D slice to the dev buffer
cl.enqueue_copy(queue, dest_buff_dev, source_buff_host[:,:,1])
------------------------------------------

Results in the following error:

File "/Users/andrea_borsic/Dropbox/ImageAnalisisAndVisualization/mCT/VES 
PyOpenCL/SliceCopyTest.py", line 15, in <module>
     cl.enqueue_copy(queue, dest_buff_dev, source_buff_host[:,:,1])
   File 
"/Library/Python/2.7/site-packages/pyopencl-2011.2-py2.7-macosx-10.7-intel.egg/pyopencl/__init__.py", 
line 755, in enqueue_copy
     return _cl._enqueue_write_buffer(queue, dest, src, **kwargs)
TypeError: expected a single-segment buffer object

Any suggestion is really appreciated !

Best Regards,

Andrea



On 4/25/12 12:56 PM, Andreas Kloeckner wrote:
> On Wed, 25 Apr 2012 12:17:33 -0400, Andrea Borsic<Andrea.Borsic at dartmouth.edu>  wrote:
>> Hi Andreas,
>>
>> I'd like to ask you a question (but I don't plan to bother you in the
>> future with PyOpnCL questions), I hope you don't mind.
>>
>> I am at a stage where I am trying to figure out whether I can use
>> PyOpenCL for my work, and wanted to ask you one information regarding
>> copying only parts of a buffer from host to device.
>>
>> In most of the problems in medical imaging I am dealing with I need to
>> copy data from a 3D array to the GPU in a slice by slice fashion. Whole
>> 3D volumes do not fit on the GPU, but fortunately many algorithms can
>> work in "sliding slice" way, where only a certain thickness of the
>> volume is copied to the GPU, and as the algorithm proceeds a new slice
>> is added and one is discarded from the back of the stack. The bottom
>> line is that I need to copy only one 2D slice at a time from host to
>> device, from a 3D array.
>>
>> I currently use, in C, clEnqueueWriteBuffer pointing to the base of the
>> 3D buffer, specifying the current slice offset and numbers of bytes to copy.
>>
>> I am looking to do the same with PyOpenCL, I did various searches on the
>> internet, but I am not quite sure that I understand how to specify an
>> offset and num_bytes in a memory transfer operation.
>>
>>   From the documentation of pyopencl.enqueue_copy and it seems that for
>> host<->  Buffer copies the supported parameter is "device_offset", but I
>> would need actually to specify a host_offset, to address the single
>> slice, and a byte_count, to read only 1 slice. These parameters seem to
>> be available only for Buffer<->  Buffer transfers (which I assume is a
>> GPU<->  GPU transfer ?)
>>
>> What's the correct way of copying only a subset of a host memory buffer
>> to device and vice-versa (writing back from device to host to a
>> particular range within a buffer ?)
> (cc'ing pyopencl list)
>
> I assume your data is sitting in a numpy array on the host. Then all you
> need to do is enqueue_copy(dev_buf, host[1000:2000]), i.e. pass the
> desired slice of the numpy array to enqueue_copy, instead of the whole
> array. Obviously, this will only work if the numpy array resulting from
> the slice access is contiguous in host memory.
>
> Hope this helps!
>
> Andreas





More information about the PyOpenCL mailing list