diff options
Diffstat (limited to 'silx/opencl/processing.py')
-rw-r--r-- | silx/opencl/processing.py | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/silx/opencl/processing.py b/silx/opencl/processing.py index 045a9d3..707aa72 100644 --- a/silx/opencl/processing.py +++ b/silx/opencl/processing.py @@ -168,7 +168,7 @@ class OpenclProcessing(object): paramatrized buffers. :param use_array: allocate memory as pyopencl.array.Array instead of pyopencl.Buffer - + Note that an OpenCL context also requires some memory, as well as Event and other OpenCL functionalities which cannot and are not taken into account here. The memory required by a context @@ -290,6 +290,59 @@ class OpenclProcessing(object): else: self.queue = pyopencl.CommandQueue(self.ctx) + def profile_add(self, event, desc): + """ + Add an OpenCL event to the events lists, if profiling is enabled. + + :param event: silx.opencl.processing.EventDescription. + :param desc: event description + """ + if self.profile: + self.events.append(EventDescription(desc, event)) + + def allocate_texture(self, shape, hostbuf=None, support_1D=False): + """ + Allocate an OpenCL image ("texture"). + + :param shape: Shape of the image. Note that pyopencl and OpenCL < 1.2 + do not support 1D images, so 1D images are handled as 2D with one row + :param support_1D: force the image to be 1D if the shape has only one dim + """ + if len(shape) == 1 and not(support_1D): + shape = (1,) + shape + return pyopencl.Image( + self.ctx, + pyopencl.mem_flags.READ_ONLY | pyopencl.mem_flags.USE_HOST_PTR, + pyopencl.ImageFormat( + pyopencl.channel_order.INTENSITY, + pyopencl.channel_type.FLOAT + ), + hostbuf=numpy.zeros(shape[::-1], dtype=numpy.float32) + ) + + def transfer_to_texture(self, arr, tex_ref): + """ + Transfer an array to a texture. + + :param arr: Input array. Can be a numpy array or a pyopencl array. + :param tex_ref: texture reference (pyopencl._cl.Image). + """ + copy_args = [self.queue, tex_ref, arr] + shp = arr.shape + ndim = arr.ndim + if ndim == 1: + # pyopencl and OpenCL < 1.2 do not support image1d_t + # force 2D with one row in this case + #~ ndim = 2 + shp = (1,) + shp + copy_kwargs = {"origin":(0,) * ndim, "region": shp[::-1]} + if not(isinstance(arr, numpy.ndarray)): # assuming pyopencl.array.Array + # D->D copy + copy_args[2] = arr.data + copy_kwargs["offset"] = 0 + ev = pyopencl.enqueue_copy(*copy_args, **copy_kwargs) + self.profile_add(ev, "Transfer to texture") + def log_profile(self): """If we are in profiling mode, prints out all timing for every single OpenCL call """ @@ -328,7 +381,7 @@ class OpenclProcessing(object): def get_compiler_options(self, x87_volatile=False): """Provide the default OpenCL compiler options - + :param x87_volatile: needed for Kahan summation :return: string with compiler option """ |