# How to use the basic Sparse matrix operations (multiplication, .etc) in PyCUDA

I try to use sparse matrix operations in GPU in Python and now try to use PyCUDA with theano. But I can't find how to do sparse matrix and vector multiplication. I only got an example showing how to solve $A\cdot x=b$ using CG from the library document. Is there any document or example detailing how to do some basic sparse matrix operations?

By the way, is there another python library that can do sparse matrix operations in GPU?

This example from the PyCUDA demonstrates how to do sparse matrix vector multiplication.

• Isn't this an example for solving a sparse linear system with CG? I don't see any explicit matrix-vector products.
– cfh
May 23 '15 at 14:46
• Yes, but CG is built on (sparse) matrix-vector multiplication. The relevant lines are 30-31, where the sparse matvec operator spmv is defined for a matrix $A$ given in scipy.sparse.crs format. If you look at the cg source (line 123), you see that $b=Ax$ is then computed via b=spmv(x). May 23 '15 at 15:17
• It would be good to put that into the answer since it's definitely not easy to spot in the code (and in fact spmv is never explicitly invoked in that source file, only passed to CG).
– cfh
May 23 '15 at 22:05

I think the most used libraries for sparse matrix operations using CUDA is cuSPARSE, which already comes included in the CUDA toolkit and supports all common sparse matrix formats. There is a Python wrapper for it here.

Overall, the Python/CUDA ecosystem still seems weirdly fractured, with no obvious choice existing for many common tasks. That said, most of my Python/CUDA experience comes from writing custom kernels using PyCUDA, which worked well.