I have a fluid dynamic solver written in python which I want to accelerate by moving the most expensive computations to the GPU. Ideally all arrays and sparse matrices used in my code should remain on the gpu, and matrices in COO format should be built directly from arrays on the gpu.

From my search it seems that there are two python packages which may help; pyviennacl and petsc4py. Is there a reason I should choose one over the other?

  • 1
    $\begingroup$ Have you considered pycuda? $\endgroup$
    – nluigi
    Commented Mar 6, 2018 at 17:46
  • $\begingroup$ Can you call cusp routines from pycuda in a straight forward way $\endgroup$
    – hakeem
    Commented Mar 6, 2018 at 18:16
  • $\begingroup$ there have been a couple of questions posted here, e.g. this one. Googling "pycuda cusp" shows a number of results aswell $\endgroup$
    – nluigi
    Commented Mar 6, 2018 at 19:07
  • $\begingroup$ @nluigi you might want to convert your comments to the answer, as it seemed to offer an answer. $\endgroup$
    – Anton Menshov
    Commented Mar 11, 2018 at 8:44

2 Answers 2


I have found pycuda particularly useful as wrapper for cuda in python. Especially the section on metaprogramming is useful if you are interested in building more sophisticated frameworks. It's a mature package, there is an active mailing list and the developer gives very useful advice.


Pycuda is one of the more pythonic way to handle cuda in python as @nluigi suggested.

If you are open to call C/C++ code inside python there is also CUSP:

Cusp is a library for sparse linear algebra and graph computations based on Thrust. Cusp provides a flexible, high-level interface for manipulating sparse matrices and solving sparse linear systems.

This is a template library that I found user friendly and with this you can do with it some heavy task. I use both pycuda and CUSP some time ago for a GPU solver for Navier-Stokes.

There are also other library I cite two:

scikit-cuda provides Python interfaces to many of the functions in the CUDA device/runtime, CUBLAS, CUFFT, and CUSOLVER libraries distributed as part of NVIDIA's CUDA Programming Toolkit, as well as interfaces to select functions in the CULA Dense Toolkit. Both low-level wrapper functions similar to their C counterparts and high-level functions comparable to those in NumPy and Scipy are provided.

PyCULA provides an efficient and simple CUDA GPU environment for python. PyCULA accomplishes this feat by combining the power of driver based PyCUDA with nVidia’s runtime libraries and, most importantly, CULA GPU-LAPACK functionality in a single environment. We aim to hide complications without limiting the enduser.

  • $\begingroup$ What about cupy? More specifically cupyx.scipy.sparse.linalg.spsolve? $\endgroup$
    – C-3PO
    Commented Jul 19, 2021 at 11:22
  • $\begingroup$ Hi @C-3PO, I never used it so I can't help you, but it looks like interesting. Note only one things, the function you cite recall an internal function csrlsvqr. From a quick check the name, and the cuda doc, tell it is a solver with QR. Consider if for your problem is better a direct or iterative solver. $\endgroup$ Commented Jul 21, 2021 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.