def set_opencl(self): #Get all devices that fit requirements #from one platform good_devices = [] good_platform = None for platform in OpenCL.get_platforms(): for device in platform.get_devices(): if device.meets_requirements(): good_devices.append(device) if len(good_devices) > 0: good_platform = platform break #Raise a not-supported exception if there are no good devices if len(good_devices) == 0: raise Exception("This program is not supported on your hardware") #Create a OpenCL context with platform specific properties properties = self.get_context_properties(good_platform) self.context = Context(good_devices, properties=properties) #Create the context queue self.queue = CommandQueue(self.context) #print OpenCL version print "Using OpenCL version: " + str( good_platform.get_info(platform_info.VERSION))
def set_opencl(self): #Get all devices that fit requirements #from one platform good_devices = [] good_platform = None for platform in OpenCL.get_platforms(): for device in platform.get_devices(): if device.meets_requirements(): good_devices.append(device) if len(good_devices) > 0: good_platform = platform break #Raise a not-supported exception if there are no good devices if len(good_devices) == 0: raise Exception("This program is not supported on your hardware") #Create a OpenCL context with platform specific properties properties = self.get_context_properties(good_platform) self.context = Context(good_devices, properties=properties) #Create the context queue self.queue = CommandQueue(self.context) #print OpenCL version print "Using OpenCL version: " + str(good_platform.get_info(platform_info.VERSION))
def main(): devices = get_devices() try: debug(CL_DEVICE_TYPE(devices[0].type)) except IndexError as ie: exception(ie) context = Context(devices) queue = CommandQueue(context) # Create queue for each kernel execution source = read_kernel_source("median_filter.cl") program = Program(context, source).build() # Kernel function instantiation image = imread('../data/noisyImage.jpg', flatten=True).astype(float32) # Read in image imshow(image) start_usec = perf_counter() args = allocate_variables(context, image) program.medianFilter(queue, image.shape, None, *args) # Call Kernel. # Automatically takes care of block/grid distribution. Note explicit naming of kernel to execute. result = copy_from_buffer(queue, args[1], image.shape, image.dtype) # Copy the result back from buffer debug("%g milliseconds" % (1e3 * (perf_counter() - start_usec))) imshow(result) imsave('../data/medianFilter-OpenCL.jpg', result) # Show the blurred image
def setUp(self): numpy.random.seed(42) self.ctx = create_some_context(answers=[0, 0]) self.queue = CommandQueue(self.ctx) self.image = CLImage(self.ctx, (256, 256)) self.solver = FastBoxCounting()
def rotate(angle, ctx, in_img_buf, out_img_buf, h, w, prg): times = {} pt = perf_counter() with CommandQueue(ctx) as queue: prg.rotate_(queue, (w, h), None, in_img_buf, out_img_buf, w, h, float32(angle)) times["Execution"] = perf_counter() - pt pt = perf_counter() dest = zeros((w, h, 4), dtype=uint8) enqueue_copy(queue, dest, out_img_buf, origin=(0, 0), region=(w, h)) times["Copying"] = perf_counter() - pt print("\n".join("%s:\t%g" % i for i in times.items())) return dest
def find_set(x0, y0, dx, dy, w, h, ctx, buf, prg): """Find Mandelbrot set""" times = {} pt = perf_counter() with CommandQueue(ctx) as queue: prg.mandelbrot(queue, (w, h), None, buf, int32(w), int32(h), float64(x0), float64(y0), float64(dx), float64(dy)) times["Execution"] = perf_counter() - pt pt = perf_counter() dest = zeros((h, w, 4), dtype=uint8) enqueue_copy(queue, dest, buf, origin=(0, 0), region=(w, h)) times["Copying"] = perf_counter() - pt print("\n".join("%s:\t%g" % i for i in times.items())) return dest
def __init__(self, model, Observer=CLObserver, Propagator=CL1Propagator, ctx=None): if not ctx: ctx = create_some_context() self.model = model self.grid_array = self.model.build_grid() with CommandQueue(ctx) as queue: self.grid = to_device(queue, self.grid_array) self.observer = Observer(model, ctx=ctx) self.propagator = Propagator(model, ctx=ctx) self.candidate = self.observer.observe(self.grid)[1:] self.done = False
def __init__(self, **ctx_kw_args): print(""" \t############ WELCOME TO CHIMERA.CL ############ """) if ctx_kw_args == {}: print(""" \t CONTEXT IS NOT CHOSEN, PLEASE, DO IT NOW. \t TO AVOID BEING ASKED IN THE FUTURE, YOU MAY \t SPECIFY ARGUMENT OF COMMUNICATOR, e.g. \t comm = Communicator(answers=[0,2]) \t################################################ """) ctx_kw_args['interactive'] = True self.ctx = create_some_context(**ctx_kw_args) self.queue = CommandQueue(self.ctx) api = ocl_api() self.thr = api.Thread(cqd=self.queue) selected_dev = self.queue.device self.dev_type = device_type.to_string(selected_dev.type) self.dev_name = self.queue.device.name self.plat_name = selected_dev.platform.vendor self.ocl_version = selected_dev.opencl_c_version print(""" \t {} DEVICE {} IS CHOSEN \t ON {} PLATFORM \t WITH {} COMPILER """.format(self.dev_type, self.dev_name, self.plat_name, self.ocl_version)) if self.dev_type == 'CPU' and self.plat_name == 'Apple': print('\t\tReikna FFT is replaced by pyFFTW') self.fft_method = 'pyFFTW' else: self.fft_method = 'Reikna' if self.dev_type == 'CPU': print('\t\tReikna MatrixMul is replaced by numpy.dot') self.dot_method = 'NumPy' else: self.dot_method = 'Reikna'
#! /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)
n, m, p = 3, 4, 5 a = random.randint(2, size=(n * m)).astype(float32) b = random.randint(2, size=(m * p)).astype(float32) c = zeros((n * p), dtype=float32) TIMES = {} ctx = create_some_context() a_buf = Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a) b_buf = Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b) c_buf = Buffer(ctx, mf.WRITE_ONLY, c.nbytes) pt = perf_counter() prg = Program(ctx, load_cl_text("multiply_matr.cl")).build() TIMES["Compilation"] = perf_counter() - pt pt = perf_counter() with CommandQueue(ctx) as queue: prg.multiply(queue, c.shape, None, uint16(n), uint16(m), uint16(p), a_buf, b_buf, c_buf) TIMES["Execution"] = perf_counter() - pt pt = perf_counter() enqueue_copy(queue, c, c_buf) TIMES["Copying"] = perf_counter() - pt a_buf.release() b_buf.release() c_buf.release() print("matrix A:") print(a.reshape(n, m)) print("matrix B:") print(b.reshape(m, p)) print("multiplied A*B:") print(c.reshape(n, p))
class Raytracer: """Raytraced Renderer""" """ INITIALISATION """ def __init__(self, resolution, object): #initialize display self.set_display(resolution) self.width, self.height = resolution #initialize opengl self.set_opengl() #initialize opencl self.set_opencl() #build program self.load_program() #create the texture we render to self.create_texture() #Create global OpenCL buffers self.create_buffers() self.object = object self.load_object() #print OpenGL Version print "Using OpenGL version: " + glGetString(GL_VERSION) def set_display(self, resolution): #Create a pygame window pygame.display.init() pygame.display.set_mode(resolution, pygame.OPENGL | pygame.DOUBLEBUF) def set_opengl(self): #create shader program vertex_shader = Shader("raytraced/vertex.vert") fragment_shader = Shader("raytraced/fragment.frag") self.draw_program = ShaderProgram(vertex_shader.id, fragment_shader.id) #create render quad self.render_quad = QUAD() def set_opencl(self): #Get all devices that fit requirements #from one platform good_devices = [] good_platform = None for platform in OpenCL.get_platforms(): for device in platform.get_devices(): if device.meets_requirements(): good_devices.append(device) if len(good_devices) > 0: good_platform = platform break #Raise a not-supported exception if there are no good devices if len(good_devices) == 0: raise Exception("This program is not supported on your hardware") #Create a OpenCL context with platform specific properties properties = self.get_context_properties(good_platform) self.context = Context(good_devices, properties=properties) #Create the context queue self.queue = CommandQueue(self.context) #print OpenCL version print "Using OpenCL version: " + str( good_platform.get_info(platform_info.VERSION)) def get_context_properties(self, plat): #reference context properties enumeration out = [] #link OpenCL context platform out.append((context_properties.PLATFORM, plat)) #link OpenGL context out.append( (context_properties.GL_CONTEXT_KHR, platform.GetCurrentContext())) #link platform specific window contexts if "GLX" in globals(): out.append((context_properties.GLX_DISPLAY_KHR, GLX.glXGetCurrentDisplay())) if "WGL" in globals(): out.append((context_properties.WGL_HDC_KHR, WGL.wglGetCurrentDC())) #return context properties return out 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) def create_texture(self): #Grab the screen size width, height = self.width, self.height #Create a GL texture objects self.gl_texture = texture = glGenTextures(1) #Bind the texture so we can change things glBindTexture(GL_TEXTURE_2D, texture) #Add parameters for texture access glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) #upload texture to GPU glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, None) #wait for OpenGL to finish executing every command glFinish() #Link the opengl texture to opencl self.render_texture = GLTexture(self.context, mem_flags.READ_WRITE, GL_TEXTURE_2D, 0, texture, 2) def create_buffers(self): self.meshes_buffer = None def load_object(self): vertices = [] for tri in self.object.triangles: #Built vertex object position = tuple(list(self.object.vertices[tri]) + [0.0]) normal = tuple(list(self.object.normals[tri]) + [0.0]) vertex = (position, normal) vertices.append(vertex) self.meshes_array = numpy.array(vertices, dtype=cltypes.Vertex) #Make buffers self.meshes_buffer = Buffer(self.context, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=self.meshes_array) def render(self, camera): camera_info = camera.getCl_info() #Grab the screen size width, height = self.width, self.height #wait for OpenGL to finish all functions glFinish() #Bind OpenGL texture for OpenCL OpenCL.enqueue_acquire_gl_objects(self.queue, [self.render_texture]) #Queue Raytrace self.raytrace(camera_info) #Unbind OpenGL texture from OpenCL OpenCL.enqueue_release_gl_objects(self.queue, [self.render_texture]) #Render rendered texture to back-buffer self.render_render_texture() #Swap back and front buffers pygame.display.flip() def raytrace(self, camera_info): #Grab the global memory size (screen size) global_size = (self.width, self.height) #Execute OpenCL kernel with arguments self.kernel(self.queue, global_size, None, self.render_texture, camera_info, self.meshes_buffer, len(self.meshes_array)) #Wait for OpenCL to finish rendering self.queue.finish() def render_render_texture(self): #Reset projection matrix glMatrixMode(GL_PROJECTION) glLoadIdentity() #Bind shader program glUseProgram(self.draw_program.id) #Bind render-texture for reading glActiveTexture(GL_TEXTURE0 + self.gl_texture) glBindTexture(GL_TEXTURE_2D, self.gl_texture) #Set argument in shader program glUniform1i(self.draw_program.renderTexture, self.gl_texture) #Render render-quad with texture to back-buffer glCallList(self.render_quad) def close(self): #close the pygame window pygame.quit()
class Raytracer: """Raytraced Renderer""" """ INITIALISATION """ def __init__(self, resolution, object): #initialize display self.set_display(resolution) self.width, self.height = resolution #initialize opengl self.set_opengl() #initialize opencl self.set_opencl() #build program self.load_program() #create the texture we render to self.create_texture() #Create global OpenCL buffers self.create_buffers() self.object = object self.load_object() #print OpenGL Version print "Using OpenGL version: " + glGetString(GL_VERSION) def set_display(self, resolution): #Create a pygame window pygame.display.init() pygame.display.set_mode(resolution, pygame.OPENGL|pygame.DOUBLEBUF) def set_opengl(self): #create shader program vertex_shader = Shader("raytraced/vertex.vert") fragment_shader = Shader("raytraced/fragment.frag") self.draw_program = ShaderProgram(vertex_shader.id, fragment_shader.id) #create render quad self.render_quad = QUAD() def set_opencl(self): #Get all devices that fit requirements #from one platform good_devices = [] good_platform = None for platform in OpenCL.get_platforms(): for device in platform.get_devices(): if device.meets_requirements(): good_devices.append(device) if len(good_devices) > 0: good_platform = platform break #Raise a not-supported exception if there are no good devices if len(good_devices) == 0: raise Exception("This program is not supported on your hardware") #Create a OpenCL context with platform specific properties properties = self.get_context_properties(good_platform) self.context = Context(good_devices, properties=properties) #Create the context queue self.queue = CommandQueue(self.context) #print OpenCL version print "Using OpenCL version: " + str(good_platform.get_info(platform_info.VERSION)) def get_context_properties(self, plat): #reference context properties enumeration out = [] #link OpenCL context platform out.append((context_properties.PLATFORM, plat)) #link OpenGL context out.append((context_properties.GL_CONTEXT_KHR, platform.GetCurrentContext())) #link platform specific window contexts if "GLX" in globals(): out.append((context_properties.GLX_DISPLAY_KHR, GLX.glXGetCurrentDisplay())) if "WGL" in globals(): out.append((context_properties.WGL_HDC_KHR, WGL.wglGetCurrentDC())) #return context properties return out 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) def create_texture(self): #Grab the screen size width, height = self.width, self.height #Create a GL texture objects self.gl_texture = texture = glGenTextures(1) #Bind the texture so we can change things glBindTexture(GL_TEXTURE_2D, texture) #Add parameters for texture access glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) #upload texture to GPU glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, None) #wait for OpenGL to finish executing every command glFinish() #Link the opengl texture to opencl self.render_texture = GLTexture(self.context, mem_flags.READ_WRITE, GL_TEXTURE_2D, 0, texture, 2) def create_buffers(self): self.meshes_buffer = None def load_object(self): vertices = [] for tri in self.object.triangles: #Built vertex object position = tuple(list(self.object.vertices[tri]) + [0.0]) normal = tuple(list(self.object.normals[tri]) + [0.0]) vertex = (position, normal) vertices.append(vertex) self.meshes_array = numpy.array(vertices, dtype=cltypes.Vertex) #Make buffers self.meshes_buffer = Buffer(self.context, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=self.meshes_array) def render(self, camera): camera_info = camera.getCl_info() #Grab the screen size width, height = self.width, self.height #wait for OpenGL to finish all functions glFinish() #Bind OpenGL texture for OpenCL OpenCL.enqueue_acquire_gl_objects(self.queue, [self.render_texture]) #Queue Raytrace self.raytrace(camera_info) #Unbind OpenGL texture from OpenCL OpenCL.enqueue_release_gl_objects(self.queue, [self.render_texture]) #Render rendered texture to back-buffer self.render_render_texture() #Swap back and front buffers pygame.display.flip() def raytrace(self, camera_info): #Grab the global memory size (screen size) global_size = (self.width, self.height) #Execute OpenCL kernel with arguments self.kernel(self.queue, global_size, None, self.render_texture, camera_info, self.meshes_buffer, len(self.meshes_array)) #Wait for OpenCL to finish rendering self.queue.finish() def render_render_texture(self): #Reset projection matrix glMatrixMode(GL_PROJECTION) glLoadIdentity() #Bind shader program glUseProgram(self.draw_program.id) #Bind render-texture for reading glActiveTexture(GL_TEXTURE0 + self.gl_texture) glBindTexture(GL_TEXTURE_2D, self.gl_texture) #Set argument in shader program glUniform1i(self.draw_program.renderTexture, self.gl_texture) #Render render-quad with texture to back-buffer glCallList(self.render_quad) def close(self): #close the pygame window pygame.quit()
def setUp(self): numpy.random.seed(42) self.ctx = create_some_context(answers=[0, 0]) self.queue = CommandQueue(self.ctx)
def setUp(self): self.ctx = create_some_context(answers=[0, 0]) self.queue = CommandQueue(self.ctx)