Skip to main content
=========
py2OpenCL
=========

OpenCL is a powerful means of applying the same simple function (a "kernel") to
large arrays of similar data.

py2OpenCL uses Python's AST module to convert a Python function to an OpenCL
kernel (written in a C-like language), then uses Andreas Klöckner's PyOpenCL
module to submit the kernel to the GPU. It is not meant to convert arbitrary
Python code to OpenCL, as that would be impossible. Instead, it is limited to
simple Python lambdas and functions containing only simple mathematical operations
and built-in OpenCL functions.


Examples
========

The following code returns a new numpy array holding the results of the lambda function:


import numpy as np
from py2opencl import Py2OpenCL, F

py2 = Py2OpenCL( lambda x: -x if x < 0.5 else F.sin(x) )
a = py2.map( np.random.rand(10000000) )

print py2.kernel

>> __kernel void sum( __global const float *x, __global float *res_g) {
>> int gid = get_global_id(0);
>> res_g[gid] = (((x[gid] < 0.5)) ? -x[gid] : sin( x[gid] ));
>> }


More complex functions are supported, though there are many constraints. The following function
averages the pixels of an image:

import numpy as np
from py2opencl import Py2OpenCL, F
import Image

img_path = 'py2opencl/test/Lenna.png'

img = np.array( Image.open( img_path ).convert('RGB') )

def avg( x, y, z, dest, src ):
# note that the C code produced will handle wrapping automatically
right = src[ x+1, y, z ]
left = src[ x-1, y, z ]
up = src[ x, y-1, z ]
down = src[ x, y+1, z ]
dest[x,y,z] = (right / 4) + (left / 4) + (up / 4) + (down / 4)

dest = Py2OpenCL( avg ).map( img )
Image.fromarray( dest, 'RGB').save('foo.png')


OpenCL Drivers
==============

Py2OpenCL should work out-of-the-box on Mac OS X. If you're on Linux and don't have a fancy GPU,
I'd suggest AMD's ICD, found `here http://developer.amd.com/tools-and-sdks/opencl-zone/opencl-tools-sdks/amd-accelerated-parallel-processing-app-sdk/`
(as of 26 Jul 2014). It supports modern Intel CPU's, no GPU required. (Presumably it supports AMD CPUs as well.)

As of this writing, Intel's beignet driver appears to be broken on Ubuntu 14.04.


Tested Platforms
================

- NVIDIA CUDA / nvidia-opencl-icd-331 on Ubuntu 14.04
- AMD Accelerated Parallel Processing (AMD-APP-SDK-v2.9) on Intel Core i7-3610QM, Ubuntu 14.04
- Apple's OpenCL drivers for the Intel Core i5-4258U, OS X 10.9
- Apple's OpenCL drivers for the Intel "Iris" (Intel HD 4000), OS X 10.9


TODO
====

- support while loops and C-style for loops (ie, 'for i in range(n)')
- performance writeup

Release files for py2opencl 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for py2opencl 0.4.0
File Interpreter ABI Platform
py2opencl-0.4.0.linux-x86_64.tar.gz Details

Release files / py2opencl-0.4.0.linux-x86_64.tar.gz

Download URL py2opencl-0.4.0.linux-x86_64.tar.gz
Size 26.6 kB
Tags Source
SHA-256 checksum
How to use checksums
890d7b60aa892e274a9f1a9e70dd1b7e747317301eb8a1cec3f96da082f1bb41
BLAKE2b-256 checksum
How to use checksums
e0a572e5e8d26511149ffbd0fecdf62cf7123ccea4a3c2cbb314ec18742f0d0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.4.0 This release

1 release file

0.3.0

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page