Introduction
This article is to introduce you PyCUDA, which is a python framework allows you write NVidia’s GPU program in a pythonic way. This framework is consisted of two major parts. The first part is a cuda source code compiler, which compiles the embedded cuda code in python at runtime. The second part is to do the computing by leveraging the numpy, which is also a famous python library for scientific computing.
Start with your first Pycuda program
This website gives out the PyCUDA’s documentation.
Here is an example from the above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
In this example, the SourceModule
is used to create a python Module
from the cuda source code. This involves the Just-in-time compilation. Then, get_function
is used to extract the function from that compiled module, hence the following program can directly call the function like a normal python function.
pycuda.driver
helps to copy-in and copy-out the data between the main memory and the GPU memory. drv.Out
function wraps the variable, which is used in the main memory, in contrast if the variable will be used in GPU memory, use drv.In
. Other parameters, like block
and grid
are the same as used in the normal c cuda program. This blog post gives nice explanation about them: http://www.resultsovercoffee.com/2011/02/cuda-blocks-and-grids.html.
Conclusion
With the help of the some libraries, such as numpy, scipy and etc., PyCuda gives us another handful method to do the scientific computing. By leveraging the GPU computing power, we can not only process and analyze data sets but also do it efficiently.