def __init__(self): super().__init__() self.setWindowTitle("Plot some examples") self.setAttribute(Qt.WA_DeleteOnClose) self.canvas = Canvas(self) self.toolbar = NavigationToolbar(self.canvas, self) self.addToolBar(self.toolbar) self.setCentralWidget(self.canvas) self.mbar = self.menuBar() self.interactor = None examples = self.mbar.addMenu("Examples") examples.addAction(QAction("Rotate", self, triggered=self._set_rotate)) examples.addAction(QAction("Mandelbrot", self, triggered=self._set_mandelbrot)) self.draw({"img": lena()})
def __init__(self): self.angle = 0. self.ch_angles = { "Key_UP": pi / 18., "Key_Down": -pi / 18., "Key_Right": -pi / 180., "Key_Left": pi / 180. } 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 self.in_img_buf = image_from_array(ctx, i4, 4) fmt = ImageFormat(CHO.RGBA, CHANNEL.UNSIGNED_INT8) self.out_img_buf = Image(ctx, MEM.WRITE_ONLY, fmt, shape=(w, h)) prg = Program(ctx, load_cl_text("rotation.cl")).build() self.params = (ctx, self.in_img_buf, self.out_img_buf, h, w, prg)
TIMES = {} GAUSSIAN_BLUR = array([ 1, 4, 7, 4, 1, 4, 16, 26, 16, 4, 7, 26, 41, 26, 7, 4, 16, 26, 16, 4, 1, 4, 7, 4, 1 ], dtype=float32) GAUSSIAN_BLUR /= 273. try: angle = -float(argv[1]) / 180. * pi except (IndexError, ValueError): angle = pi / 4 ctx = create_some_context() filter_buf = Buffer(ctx, MEM.READ_ONLY | MEM.COPY_HOST_PTR, hostbuf=GAUSSIAN_BLUR) 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)) pt = perf_counter() prg = Program(ctx, load_cl_text("convolution.cl")).build() TIMES["Compilation"] = perf_counter() - pt pt = perf_counter() with CommandQueue(ctx) as queue: prg.convolution(queue, (w, h), None, in_img_buf, out_img_buf, filter_buf, int32(5), Sampler(ctx, False, ADDRESS.CLAMP_TO_EDGE, FILTER.NEAREST))
from pyopencl import (CommandQueue, mem_flags as mf, Buffer, Program, enqueue_copy) from numpy import zeros, int32 from misc import create_some_context, load_cl_text, lena import matplotlib.pyplot as plt try: from time import process_time as perf_counter except ImportError: from time import perf_counter TIMES = {} ctx = create_some_context() lenar = lena().astype(int32).flatten() len_buf = Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=lenar) histogram = zeros(256, dtype=int32) h_buf = Buffer(ctx, mf.WRITE_ONLY | mf.COPY_HOST_PTR, hostbuf=histogram) pt = perf_counter() prg = Program(ctx, load_cl_text("histogram.cl")).build() TIMES["Compilation"] = perf_counter() - pt pt = perf_counter() with CommandQueue(ctx) as queue: prg.histogram(queue, lenar.shape, None, len_buf, int32(len(lenar)), h_buf) TIMES["Execution"] = perf_counter() - pt pt = perf_counter() enqueue_copy(queue, histogram, h_buf) TIMES["Copying"] = perf_counter() - pt h_buf.release() len_buf.release() print("\n".join("%s:\t%g" % i for i in TIMES.items())) plt.plot(histogram, ",") plt.show()