How to use the pyopencl.Context function in pyopencl

To help you get started, we’ve selected a few pyopencl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github inducer / pyopencl / examples / gl_particle_animation.py View on Github external
# Draw the VBOs
    glDrawArrays(GL_POINTS, 0, num_particles)

    glDisableClientState(GL_COLOR_ARRAY)
    glDisableClientState(GL_VERTEX_ARRAY)

    glDisable(GL_BLEND)

    glutSwapBuffers()

window = glut_window()

(np_position, np_velocity, gl_position, gl_color) = initial_buffers(num_particles)

platform = cl.get_platforms()[0]
context = cl.Context(properties=[(cl.context_properties.PLATFORM, platform)] + get_gl_sharing_context_properties())  
queue = cl.CommandQueue(context)

cl_velocity = cl.Buffer(context, mf.COPY_HOST_PTR, hostbuf=np_velocity)
cl_start_position = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=np_position)
cl_start_velocity = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=np_velocity)

cl_gl_position = cl.GLBuffer(context, mf.READ_WRITE, int(gl_position))
cl_gl_color = cl.GLBuffer(context, mf.READ_WRITE, int(gl_color))

kernel = """__kernel void particle_fountain(__global float4* position, 
                                            __global float4* color, 
                                            __global float4* velocity, 
                                            __global float4* start_position, 
                                            __global float4* start_velocity, 
                                            float time_step)
{
github harvard-cs205 / cs205-homework / HW3 / P3 / tune.py View on Github external
import pyopencl as cl
import numpy as np

def create_data(N):
    return host_x, x

if __name__ == "__main__":
    N = 1e7

    platforms = cl.get_platforms()
    devices = [d for platform in platforms for d in platform.get_devices()]
    for i, d in enumerate(devices):
        print("#{0}: {1} on {2}".format(i, d.name, d.platform.name))
    ctx = cl.Context(devices)

    queue = cl.CommandQueue(ctx, properties=cl.command_queue_properties.PROFILING_ENABLE)

    program = cl.Program(ctx, open('sum.cl').read()).build(options='')

    host_x = np.random.rand(N).astype(np.float32)
    x = cl.Buffer(ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=host_x)

    times = {}

    for num_workgroups in 2 ** np.arange(3, 10):
        partial_sums = cl.Buffer(ctx, cl.mem_flags.READ_WRITE, 4 * num_workgroups)
        host_partial = np.empty(num_workgroups).astype(np.float32)
        for num_workers in 2 ** np.arange(2, 8):
            local = cl.LocalMemory(num_workers * 4)
            event = program.sum_coalesced(queue, (num_workgroups * num_workers,), (num_workers,),
github theMarix / clBandwidth / runner.py View on Github external
def __init__(self, device = None, local_threads = LOCAL_THREADS, global_threads = GLOBAL_THREADS, max_mem_size = MAX_MEM_SIZE):
		if device != None:
			platforms = cl.get_platforms()
			if len(platforms) > 1:
				raise Exception('Found more then one platform, giving up.')
			platform = platforms[0]
			properties = [(cl.context_properties.PLATFORM, platform)]
			devices = [platform.get_devices()[device]]
			self.ctx = cl.Context(devices, properties)
		else:
			self.ctx = cl.create_some_context()
		self.queue = cl.CommandQueue(self.ctx, properties=cl.command_queue_properties.PROFILING_ENABLE)

		self.device = self.queue.device
		print '#Device: {0}'.format(self.device.name)
		print '#Memory size: {0} KiB'.format(self.device.global_mem_size / 1024)
		print '#Maximum buffer size: {0} KiB'.format(self.device.max_mem_alloc_size / 1024)

		f = open('kernels.cl', 'r')
		fstr = "".join(f.readlines())
		self.prg = cl.Program(self.ctx, fstr).build()

		self.in_buf = cl.Buffer(self.ctx, cl.mem_flags.READ_ONLY, max_mem_size)
		self.out_buf = cl.Buffer(self.ctx, cl.mem_flags.WRITE_ONLY, max_mem_size)
github inducer / pyopencl / examples / median-filter.py View on Github external
#Read in image
img = imread('noisyImage.jpg', flatten=True).astype(np.float32)

# Get platforms, both CPU and GPU
plat = cl.get_platforms()
CPU = plat[0].get_devices()
try:
    GPU = plat[1].get_devices()
except IndexError:
    GPU = "none"

#Create context for GPU/CPU
if GPU!= "none":
    ctx = cl.Context(GPU)
else:
    ctx = cl.Context(CPU)

# Create queue for each kernel execution
queue = cl.CommandQueue(ctx)

mf = cl.mem_flags

# Kernel function
src = '''
void sort(int *a, int *b, int *c) {
   int swap;
   if(*a > *b) {
      swap = *a;
      *a = *b;
      *b = swap;
   }
   if(*a > *c) {
github oysstu / pyopencl-in-action / ch14 / fft.py View on Github external
__kernel void fft_scale(__global float2* g_data, uint points_per_group, uint scale) {

   uint points_per_item, addr, i;

   points_per_item = points_per_group/get_local_size(0);
   addr = get_group_id(0) * points_per_group + get_local_id(0) * points_per_item;

   for(i=addr; i
github enjalot / adventures_in_opencl / experiments / bitonic / bitonic.py View on Github external
def __init__(self, max_elements, cta_size, dtype):

        plat = cl.get_platforms()[0]
        device = plat.get_devices()[0]
        self.ctx = cl.Context(devices=[device])
        self.queue = cl.CommandQueue(self.ctx, device)



        self.loadProgram()
        self.uintsz = dtype.itemsize
        self.d_tempKeys = cl.Buffer(self.ctx, mf.READ_WRITE, size=self.uintsz * max_elements)
        self.d_tempValues = cl.Buffer(self.ctx, mf.READ_WRITE, size=self.uintsz * max_elements)
github benma / pysph / src / fluid_rendering / fluid_renderer.py View on Github external
def cl_init_context(self):
        """
        Define context and queue.
        """
        device = self.cl_pick_device()
        platform = device.platform
        additional_properties = []

        from pyopencl.tools import get_gl_sharing_context_properties
        additional_properties = get_gl_sharing_context_properties()
        # Some OSs prefer clCreateContextFromType, some prefer clCreateContext. Try both.
        try: 
            self.ctx = cl.Context(properties=[(cl.context_properties.PLATFORM, platform)] + additional_properties)
        except:
            self.ctx = cl.Context(properties=[(cl.context_properties.PLATFORM, platform)] + additional_properties, devices=[device])
        self.queue = cl.CommandQueue(self.ctx, device=device, properties=cl.command_queue_properties.OUT_OF_ORDER_EXEC_MODE_ENABLE)
github rcloud / PyOpenCL-OpenCL-Programming-Guide-Examples / ch2 / HelloWorld.py View on Github external
gpu_device = gpu_platform.get_devices()[index]
        

#create contexts for the CPU and GPU devices if available
#the context constructor requires a list
#command queues are limited to one device, but each device may have multiple command queues
if has_cpu:
    cpu_list = []
    cpu_list.append(cpu_device)
    cpu_context = cl.Context(devices=cpu_list)
    cpu_queue = cl.CommandQueue(cpu_context)

if has_gpu:
    gpu_list = []
    gpu_list.append(gpu_device)
    gpu_context = cl.Context(devices=gpu_list)
    gpu_queue = cl.CommandQueue(gpu_context)

size = 1000
a = np.ones(size, dtype=np.float32)
b = np.ones(size, dtype=np.float32) * 2
c = np.zeros_like(a)

#lets first run this on the CPU, so create memory objects for the cpu_context
if has_cpu:
    a_dev = cl.Buffer(cpu_context, cl.mem_flags.READ_WRITE, a.nbytes)
    b_dev = cl.Buffer(cpu_context, cl.mem_flags.READ_WRITE, b.nbytes)
    c_dev = cl.Buffer(cpu_context, cl.mem_flags.READ_WRITE, c.nbytes)

    cpu_program = cl.Program(cpu_context, kernel_source).build()
#copy memory objects to the device
    cl.enqueue_copy(cpu_queue, a_dev, a, is_blocking=True)
github inducer / pyopencl / examples / gl_interop_demo.py View on Github external
platform = cl.get_platforms()[0]

    from pyopencl.tools import get_gl_sharing_context_properties
    import sys
    if sys.platform == "darwin":
        ctx = cl.Context(properties=get_gl_sharing_context_properties(),
                devices=[])
    else:
        # Some OSs prefer clCreateContextFromType, some prefer
        # clCreateContext. Try both.
        try:
            ctx = cl.Context(properties=[
                (cl.context_properties.PLATFORM, platform)]
                + get_gl_sharing_context_properties())
        except:
            ctx = cl.Context(properties=[
                (cl.context_properties.PLATFORM, platform)]
                + get_gl_sharing_context_properties(),
                devices = [platform.get_devices()[0]])

    glClearColor(1, 1, 1, 1)
    glColor(0, 0, 1)
    vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    rawGlBufferData(GL_ARRAY_BUFFER, n_vertices * 2 * 4, None, GL_STATIC_DRAW)
    glEnableClientState(GL_VERTEX_ARRAY)
    glVertexPointer(2, GL_FLOAT, 0, None)
    coords_dev = cl.GLBuffer(ctx, cl.mem_flags.READ_WRITE, int(vbo))
    prog = cl.Program(ctx, src).build()
    queue = cl.CommandQueue(ctx)
    cl.enqueue_acquire_gl_objects(queue, [coords_dev])
    prog.generate_sin(queue, (n_vertices,), None, coords_dev)