Skip to content

Commit ad7aa43

Browse files
author
Jonathan Rocher
committed
Adding pyopencl simple demo.
1 parent d799adc commit ad7aa43

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

ipython_parallel_demo.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
1-
"""
2-
$ ipcluster start -n 4
1+
""" Before this code is run in a given session, an ipython cluster session needs
2+
to be started. This can be done with 4 engines using the ipcluter command:
3+
4+
$ ipcluster start -n 4
5+
6+
More engines can be added on the fly by starting another 1
7+
8+
$ ipengine
39
"""
410
from IPython.parallel import Client
11+
import time
12+
13+
# This assumes that the client will connect to the default profile. If not, a
14+
# profile or a json file can be provided to the client at creation.
15+
c = Client()
16+
print "The client contains 4 different engines with different IDs", c.ids
17+
18+
# 2 different kinds of view on the engines:
19+
direct_view_engine_0 = c[0]
20+
load_balanced_view = c.load_balanced_view()
21+
22+
# Let's define what we want our engines to do for us
23+
mul = lambda x,y: x*y
24+
direct_view_engine_0.apply(mul, 4, 5)
25+
26+
# The loadbalanced view can receive a map_sync or map_async with a callable and
27+
# a list of arguments.
28+
load_balanced_view.map_sync(mul, range(5), range(10, 15))
29+
30+
# c[:] is a (multiplexer) direct view on all 4 engines. It can apply a target
31+
# function to all engines. This version is asynchronous: the result will be
32+
# stored separately for each engine.
33+
c[:].apply_async(mul, 4, 5).get_dict()
534

6-
c = Client()
735

8-
print c.ids
9-
c[:].apply_sync(lambda : "Hello, World")
36+
def test_latency(view, nb_tasks):
37+
""" Measure the latency of a given direct view on an engine for a given
38+
setup where nb_tasks tasks are sent to it.
39+
"""
40+
tic = time.time()
41+
echo = lambda x: x
42+
tic = time.time()
43+
for i in xrange(nb_tasks):
44+
view.apply_async(echo, '')
45+
toc = time.time()
46+
view.wait()
47+
tac = time.time()
48+
sent = toc-tic
49+
roundtrip = tac-tic
50+
return sent, roundtrip
51+
52+
# test overhead related to
53+
for client in c:
54+
print

py_open_cl_demo.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
""" Demo of PyOpenCL that multiply a numpy array by 2
2+
"""
3+
4+
import pyopencl as cl
5+
import numpy
6+
7+
# Create some 1D data
8+
a = numpy.random.rand(256**3).astype("float32")
9+
10+
# Create a GPU context to contain that data and a queue to receive commands
11+
ctx = cl.create_some_context()
12+
queue = cl.CommandQueue(ctx)
13+
14+
# Allocate a chunk of memory of the size of a and copy the data
15+
a_dev = cl.Buffer(ctx, cl.mem.flags.READ_WRITE, size = a.bytes)
16+
cl.enqueue_write_buffer(queue, a_dev, a)
17+
18+
# create the pyopencl function a little like weave.inline
19+
# Just in Time compiler
20+
prg = cl.Program(ctx, """
21+
_kernel void twice(_global float *a)
22+
{a[get_global_id(0)] *= 2;}
23+
""").build()
24+
25+
# Call the twice function
26+
prg.twice(queue, a.shape, (1,), a_dev)
27+
28+
# Retrieve result
29+
result = numpy.empty_like(a)
30+
cl.enqueue_read_buffer(queue, a.dev, result).wait()
31+
print "Succes?", numpy.all(result-2*a == 0.)

0 commit comments

Comments
 (0)