November 01, 2008
CUDA, Supercomputing for the Masses: Part 9Introducing SWIG
An excellent software development tool that connects modules written in C and C++ to a wide variety of high-level programming languages is SWIG which supports Perl, PHP, Python, Tcl, Java, C#, Common Lisp, Octave, R and many more (see www.swig.org/compat.html#SupportedLanguages for more languages.
Here are some links to get you started for three common languages. Check out the web for your favorite if not listed below:
The following is a simple Python example, contributed by a colleague at NVIDIA, which demonstrates the simplicity and speed of calling a CUDA kernel from Python. This example actually implements a useful method for financial applications -- namely matrix exponentiation. Unfortunately, the reasoning behind why such a method is useful is beyond the scope of this article. See the discussion starting on page 19 in the paper at http://arxiv.org/pdf/0710.1606 for more information. Be forewarned, this paper is quite dense.
In the spirit of this article, this example module makes efficient use of the GPU. The reason it performs so well is because this module lets Python programmers call SGEMM, a high flop per data item level-3 BLAS routine in the NVIDIA CUBLAS library. It also demonstrates that it is possible to map variables -- in this case an array -- very efficiently between Python and CUDA.
The full listing for the Python code exponentiationTest.py is:
#! /usr/bin/env python
Within the exponentiationTest.py, a custom module is imported with the line:
import FastMatrixExp
The reader is required to define its own Python method to input a matrix into variable a, which is then duplicated in variable b for purposes of comparing the speed and accuracy of the CPU and GPU:
Matrix a is then raised to the power specified in the variable steps (specifically 100) on the host processor with this code snippet:
After which the SGEMM routine from the CUBLAS library is called from Python and utilized on the GPU to perform the matrix exponentiation with the following:
Both the GPU and CPU generated results are then checked to see if they are equal within a reasonable tolerance via a numpy comparision as seen below. (Numpy is an excellent numerical Python package that has matrix operations.
The following is the SWIG interface code:
%module FastMatrixExp
The module name, FastMatrixExp, is defined in the first line of CUBLAS.i:
The iterated calls to cublasSgemm occur in the following C subroutine, which is defined between the %{ and %} for SWIG:
%{
void matrixMulLoop(int steps, float *u, int n)
{
int i;
float *ud;
cublasStatus status;
To gain a greater understanding of the remaining parts of the SWIG file, I recommend consulting the SWIG documentation. You can also find out more about SWIG in David Beazley's article SWIG and Automated C/C++ Scripting Extensions, and Daniel Blezek's article Rapid Prototyping with SWIG.
For more advanced numerical packages that combine Python and CUDA, checkout pystream or GPUlib (which can be downloaded after submitting an email request).
|
|
||||||||||||||||||||||||||||||
|
|
|
|