Skip to content

Commit

Permalink
playing with pyopencl
Browse files Browse the repository at this point in the history
  • Loading branch information
enjalot committed Feb 15, 2011
1 parent 22a85a4 commit 89de546
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
9 changes: 7 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ Florida State University Department of Scientific Computing
http://sc.fsu.edu

Tutorials
part1 - getting started: making and running a minimal opencl kernel
part1.5 - getting started with C++ bindings: making and running a minimal opencl kernel
C:
part1 - getting started: making and running a minimal opencl kernel
C++:
part1.5 - getting started with C++ bindings: making and running a minimal opencl kernel
part2 - using OpenGL and OpenCL together, illustrated with a simple particle system
Python:
part1 - getting started, simple port of part1

Supporting directories
cmake - cmake modules for finding necessary libraries
Expand Down
2 changes: 1 addition & 1 deletion experiments/forcefield/ff.cl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ __kernel void ff(__global float4* pos, __global float4* color, __global float4*
if(colx > 1) {colx = 1.0f;}
if(coly < 0) {coly = -1.0f*coly;}
if(coly > 1) {coly = 1.0f;}
color[i].x = .5*(1.0f - colx);
color[i].x = (1.0f - colx);
color[i].y = coly;
color[i].z = colx;
color[i].w = life;
Expand Down
48 changes: 48 additions & 0 deletions python/part1/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#Port from Adventures in OpenCL Part1 to PyOpenCL
# http://enja.org/2010/07/13/adventures-in-opencl-part-1-getting-started/
# http://documen.tician.de/pyopencl/

import pyopencl as cl
import numpy

class CL:
def __init__(self):
self.ctx = cl.create_some_context()
self.queue = cl.CommandQueue(self.ctx)

def loadProgram(self, filename):
#read in the OpenCL source file as a string
f = open(filename, 'r')
fstr = "".join(f.readlines())
print fstr
#create the program
self.program = cl.Program(self.ctx, fstr).build()

def popCorn(self):
mf = cl.mem_flags

#initialize client side (CPU) arrays
self.a = numpy.array(range(10), dtype=numpy.float32)
self.b = numpy.array(range(10), dtype=numpy.float32)

#create OpenCL buffers
self.a_buf = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.a)
self.b_buf = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.b)
self.dest_buf = cl.Buffer(self.ctx, mf.WRITE_ONLY, self.b.nbytes)

def execute(self):
self.program.part1(self.queue, self.a.shape, None, self.a_buf, self.b_buf, self.dest_buf)
c = numpy.empty_like(self.a)
cl.enqueue_read_buffer(self.queue, self.dest_buf, c).wait()
print "a", self.a
print "b", self.b
print "c", c



if __name__ == "__main__":
example = CL()
example.loadProgram("part1.cl")
example.popCorn()
example.execute()

6 changes: 6 additions & 0 deletions python/part1/part1.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__kernel void part1(__global float* a, __global float* b, __global float* c)
{
unsigned int i = get_global_id(0);

c[i] = a[i] + b[i];
}
48 changes: 48 additions & 0 deletions python/part2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#Port from Adventures in OpenCL Part2 to PyOpenCL
# http://enja.org/2010/08/27/adventures-in-opencl-part-2-particles-with-opengl/
# http://documen.tician.de/pyopencl/

import pyopencl as cl
import numpy

class CL:
def __init__(self):
self.ctx = cl.create_some_context()
self.queue = cl.CommandQueue(self.ctx)

def loadProgram(self, filename):
#read in the OpenCL source file as a string
f = open(filename, 'r')
fstr = "".join(f.readlines())
print fstr
#create the program
self.program = cl.Program(self.ctx, fstr).build()

def popCorn(self):
mf = cl.mem_flags

#initialize client side (CPU) arrays
self.a = numpy.array(range(10), dtype=numpy.float32)
self.b = numpy.array(range(10), dtype=numpy.float32)

#create OpenCL buffers
self.a_buf = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.a)
self.b_buf = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.b)
self.dest_buf = cl.Buffer(self.ctx, mf.WRITE_ONLY, self.b.nbytes)

def execute(self):
self.program.part1(self.queue, self.a.shape, None, self.a_buf, self.b_buf, self.dest_buf)
c = numpy.empty_like(self.a)
cl.enqueue_read_buffer(self.queue, self.dest_buf, c).wait()
print "a", self.a
print "b", self.b
print "c", c



if __name__ == "__main__":
example = CL()
example.loadProgram("part1.cl")
example.popCorn()
example.execute()

0 comments on commit 89de546

Please sign in to comment.