[petsc-users] Question about petsc4py with cuda

Matthew Knepley knepley at gmail.com
Mon Jan 15 11:23:25 CST 2024


On Mon, Jan 15, 2024 at 11:57 AM MIA via petsc-users <
petsc-users at mcs.anl.gov> wrote:

> Hi,
>
> I am trying to solve a large linear equation, which needs a GPU solver as
> comparison. I install a CUDA-enabled PETSc and petsc4py from sources using
> the release tarball. According to the test results after installation, the
> PETSc can successfully work with cuda.
>

Here is a How-To for GPUs: https://petsc.org/main/faq/#doc-faq-gpuhowto

  Thanks,

     Matt


> All my programs are written in python, so I turn to petsc4py. But I do not
> find any commands that define variables on coda device or define where the
> direct solver is executed. I check `nvidia-smi` and find my cuda does not
> work at all when executing my python script:
>
> from petsc4py import PETSc
> import numpy as np
>
> n = 1000
>
> nnz = 3 * np.ones(n, dtype=np.int32)
> nnz[0] = nnz[-1] = 2
>
> A = PETSc.Mat()
> A.createAIJ([n, n], nnz=nnz)
>
> # First set the first row
> A.setValue(0, 0, 2)
> A.setValue(0, 1, -1)
> # Now we fill the last row
> A.setValue(999, 998, -1)
> A.setValue(999, 999, 2)
>
> # And now everything else
> for index in range(1, n - 1):
>     A.setValue(index, index - 1, -1)
>     A.setValue(index, index, 2)
>     A.setValue(index, index + 1, -1)
>
> A.assemble()
>
> indexptr, indices, data = A.getValuesCSR()
> b = A.createVecLeft()
> b.array[:] = 1
> for i in range(10):
>     ksp = PETSc.KSP().create()
>     ksp.setOperators(A)
>     ksp.setType('preonly')
>     ksp.setConvergenceHistory()
>     ksp.getPC().setType('lu')
>     x = A.createVecRight()
>     ksp.solve(2*b, x)
>     residual = A * x - 2*b
>     if i % 10 == 0:
>         print(f"The relative residual is: {residual.norm() / b.norm()}.”)
>
> What should I do to utlize GPU to execute the KSP task? Are there some
> settings to be modified?
>
> Looking forward to your early reply. Thanks a lot.
>
>

-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.cse.buffalo.edu/~knepley/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20240115/0fdcec20/attachment-0001.html>


More information about the petsc-users mailing list