36

I guess the question speaks for itself. I'm interested in doing some serious computations but am not a programmer by trade. I can string enough python together to get done what I want. But can I write a program in python and have the GPU execute it using CUDA? Or do I have to use some mix of python and C?

The examples on Klockner's (sp) "pyCUDA" webpage had a mix of both python and C, so I'm not sure what the answer is.

If anyone wants to chime in about Opencl, feel free. I heard about this CUDA business only a couple of weeks ago and didn't know you could use your video cards like this.

hichris123
  • 10,145
  • 15
  • 56
  • 70
Aquateenfan
  • 401
  • 1
  • 4
  • 5
  • I suppose you could have a python library that takes code written in a subset of Python and compiles it into PTX instructions that can be sent to the driver with the CUDA API. Some of the approaches to GPU programming in Haskell takes this approach. Not sure if it's been don in Python yet, though – Gautham Ganapathy Jun 02 '10 at 06:14

8 Answers8

20

You should take a look at CUDAmat and Theano. Both are approaches to writing code that executes on the GPU without really having to know much about GPU programming.

dwf
  • 3,503
  • 1
  • 20
  • 25
18

I believe that, with PyCUDA, your computational kernels will always have to be written as "CUDA C Code". PyCUDA takes charge of a lot of otherwise-tedious book-keeping, but does not build computational CUDA kernels from Python code.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Indeed, though there are interesting projects that do; see my answer. ;) – dwf Jun 20 '10 at 00:54
  • 2
    We use PyCUDA at work and get good results with it. The advantage of writing plain CUDA C is that you can get help and snippets from other people without translating from your API specific python code. – Ron Warholic Sep 11 '11 at 01:41
8

pyopencl offers an interesting alternative to PyCUDA. It is described as a "sister project" to PyCUDA. It is a complete wrapper around OpenCL's API.

As far as I understand, OpenCL has the advantage of running on GPUs beyond Nvidia's.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • OpenCL in theory can execute on supported CPUs as well. However, support outside of Nvidia's hardware is rather half-assed so far. – dwf Jun 20 '10 at 17:52
5

Great answers already, but another option is Clyther. It will let you write OpenCL programs without even using C, by compiling a subset of Python into OpenCL kernels.

4

A promising library is Copperhead (alternative link), you just need to decorate the function that you want to be run by the GPU (and then you can opt-in / opt-out it to see what's best between cpu or gpu for that function)

root-11
  • 1,727
  • 1
  • 19
  • 33
Riccardo Galli
  • 12,419
  • 6
  • 64
  • 62
1

There is a good, basic set of math constructs with compute kernels already written that can be accessed through pyCUDA's cumath module. If you want to do more involved or specific/custom stuff you will have to write a touch of C in the kernel definition, but the nice thing about pyCUDA is that it will do the heavy C-lifting for you; it does a lot of meta-programming on the back-end so you don't have to worry about serious C programming, just the little pieces. One of the examples given is a Map/Reduce kernel to calculate the dot product:

dot_krnl = ReductionKernel(np.float32, neutral="0", reduce_expr="a+b", map_expr="x[i]*y[i]", arguments="float *x, float *y")

The little snippets of code inside each of those arguments are C lines, but it actually writes the program for you. the ReductionKernel is a custom kernel type for map/reducish type functions, but there are different types. The examples portion of the official pyCUDA documentation goes into more detail.

Good luck!

Jeff
  • 445
  • 1
  • 5
  • 9
0

Scikits CUDA package could be a better option, provided that it doesn't require any low-level knowledge or C code for any operation that can be represented as numpy array manipulation.

chiffa
  • 2,026
  • 3
  • 26
  • 41
0

I was wondering the same thing and carried a few searches. I found the article linked below which seems to answer your question. However, you asked this back in 2014 and the Nvidia article does not have a date.

https://developer.nvidia.com/how-to-cuda-python

The video goes through the set up, an initial example and, quite importantly, profiliing. However, I do not know if you can implement all of the usual general compute patterns. I would think you can because as far as I could there are no limitations in NumPy.

atirado
  • 311
  • 2
  • 2