forked from enjalot/adventures_in_opencl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
enjalot
committed
Feb 15, 2011
1 parent
22a85a4
commit 89de546
Showing
5 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|