def execute(workerCount): n = 1000000000 delta = 1.0 / n startTime = time() sliceSize = n // workerCount context = create_some_context() queue = CommandQueue(context) with open('processSlice_opencl.cl', 'r') as f: kernel = Program(context, f.read()).build() results = numpy.array(range(workerCount), dtype=numpy.float64) buffer = Buffer(context, mem_flags.WRITE_ONLY, results.nbytes) kernel.processSlice(queue, results.shape, None, numpy.int32(sliceSize), numpy.float64(delta), buffer) enqueue_read_buffer(queue, buffer, results).wait() pi = 4.0 * delta * numpy.sum(results) elapseTime = time() - startTime out(__file__, pi, n, elapseTime)
def load_program(self): #Read all the lines of the cl file into one string (safely) with open("raytraced/Raytracer.cl", "r") as file: source = ''.join(file.readlines()) #Create the opencl program program = Program(self.context, source) #make program options options = "-cl-mad-enable -cl-fast-relaxed-math -Werror -I %s" % os.path.dirname(os.path.abspath(__file__)) #build program program.build(options=options) self.kernel = program.raytrace self.kernel.set_scalar_arg_dtypes([None, None, None, numpy.int32]) #Match OpenCL Dtype. May not work everywhere cltypes.Vertex, c_decl = OpenCL.tools.match_dtype_to_c_struct(self.context.devices[0], 'Vertex', cltypes.Vertex)
#! /usr/bin/env python3 # Calculation of π using quadrature. Using PyOpenCL. # # Copyright © 2012, 2014 Russel Winder from time import time from pyopencl import create_some_context, CommandQueue, Program, Buffer, mem_flags, enqueue_read_buffer import numpy from output import out n = 1000000000 delta = 1.0 / n startTime = time() context = create_some_context() queue = CommandQueue(context) with open('processSlice_opencl.cl', 'r') as f: kernel = Program(context, f.read()).build() # Quadro FX 570 card on Anglides only supports 32-bit operations, hence float not double. results = numpy.array(n, dtype=numpy.float32) buffer = Buffer(context, mem_flags.WRITE_ONLY, results.nbytes) kernel.processSlice(queue, results.shape, None, numpy.int32(n), numpy.float32(delta), buffer) enqueue_read_buffer(queue, buffer, results).wait() pi = 4.0 * delta * numpy.sum(results) elapseTime = time() - startTime out(__file__, pi, n, elapseTime)
def get_elwise_program(context, arguments, operation, name="elwise_kernel", options=[], preamble="", loop_prep="", after_loop="", use_range=False): if use_range: body = r"""//CL// if (step < 0) { for (i = start + (work_group_start + lid)*step; i > stop; i += gsize*step) { %(operation)s; } } else { for (i = start + (work_group_start + lid)*step; i < stop; i += gsize*step) { %(operation)s; } } """ else: body = """//CL// for (i = work_group_start + lid; i < n; i += gsize) { %(operation)s; } """ import re return_match = re.search(r"\breturn\b", operation) if return_match is not None: from warnings import warn warn("Using a 'return' statement in an element-wise operation will " "likely lead to incorrect results. Use " "PYOPENCL_ELWISE_CONTINUE instead.", stacklevel=3) source = ("""//CL// %(preamble)s #define PYOPENCL_ELWISE_CONTINUE continue __kernel void %(name)s(%(arguments)s) { int lid = get_local_id(0); int gsize = get_global_size(0); int work_group_start = get_local_size(0)*get_group_id(0); long i; %(loop_prep)s; %(body)s %(after_loop)s; } """ % { "arguments": ", ".join(arg.declarator() for arg in arguments), "name": name, "preamble": preamble, "loop_prep": loop_prep, "after_loop": after_loop, "body": body % dict(operation=operation), }) from pyopencl import Program return Program(context, source).build(options)
self.angle += self.ch_angles.get(key, 0.) return {"img": rotate(self.angle, *self.params)[:, :, 0]} def __del__(self): print("delete rotate interactor") self.in_img_buf.release() self.out_img_buf.release() if __name__ == "__main__": from sys import argv try: angle = -float(argv[1]) / 180. * pi except (IndexError, ValueError): angle = pi / 4 ctx = create_some_context() in_img = lena() h, w = map(int32, in_img.shape[:2]) # in pyopencl 2018.2.2 channel orders other than RGBA # cause segmentation fault i4 = zeros((h, w, 4), dtype=uint8) i4[:, :, 0] = in_img in_img_buf = image_from_array(ctx, i4, 4) fmt = ImageFormat(CHO.RGBA, CHANNEL.UNSIGNED_INT8) out_img_buf = Image(ctx, MEM.WRITE_ONLY, fmt, shape=(w, h)) prg = Program(ctx, load_cl_text("rotation.cl")).build() res = rotate(angle, ctx, in_img_buf, out_img_buf, h, w, prg) in_img_buf.release() out_img_buf.release() show_img(res[:, :, 0])