[petsc-users] Issue to set values to a matrix in parallel in python
Stefano Zampini
stefano.zampini at gmail.com
Tue Aug 9 10:08:36 CDT 2022
PETSc distributes matrices and vectors in parallel. Take a look at https://petsc.org/release/docs/manualpages/Vec/VecGetOwnershipRange.html
> On Aug 9, 2022, at 11:21 AM, Thomas Saigre <saigre at math.unistra.fr> wrote:
>
> Hi,
>
> I've been trying for a few weeks to construct a matrix from a list of vectors, unsuccessfully.
> Here is my problem : I have a list l a petsc4py.Vec, each vector has a size n, and I have d vectors. I want to "cast" these vectors to a petsc4py.Mat Z of shape (n,d), where Z[:, i] = l[i] (using NumPy notation)
>
> Here is the code I'm using :
>
> import sys
> from petsc4py import PETSc
> n = 5
> d = 10
>
> l = [] # creation of the list of vectors
> for i in range(d):
> v = PETSc.Vec().create()
> v.setSizes(n)
> v.setFromOptions()
> v.set(i)
> l.append(v)
>
> Z = PETSc.Mat().create()
> Z.setSizes([n, d])
> Z.setFromOptions()
> Z.setUp()
> for i, v in enumerate(l):
> Z.setValues(range(n), i, v)
> Z.assemble()
> Z.view() # to display the matrix in the terminal
>
> In sequential, the result is correct :
>
> Mat Object: 1 MPI processes
> type: seqaij
> row 0: (0, 0.) (1, 1.) (2, 2.) (3, 3.) (4, 4.) (5, 5.) (6, 6.) (7, 7.) (8, 8.) (9, 9.)
> row 1: (0, 0.) (1, 1.) (2, 2.) (3, 3.) (4, 4.) (5, 5.) (6, 6.) (7, 7.) (8, 8.) (9, 9.)
> row 2: (0, 0.) (1, 1.) (2, 2.) (3, 3.) (4, 4.) (5, 5.) (6, 6.) (7, 7.) (8, 8.) (9, 9.)
> row 3: (0, 0.) (1, 1.) (2, 2.) (3, 3.) (4, 4.) (5, 5.) (6, 6.) (7, 7.) (8, 8.) (9, 9.)
> row 4: (0, 0.) (1, 1.) (2, 2.) (3, 3.) (4, 4.) (5, 5.) (6, 6.) (7, 7.) (8, 8.) (9, 9.)
>
> but when I run it using the command mpirun -np 2 python3 file.py, I get the following error, about incompatible array sizes (I did not manage to understand what ni, nj and nv correspond to...)
>
> Traceback (most recent call last):
> File "/home/Documents/code/tests/file.py", line 31, in <module>
> Z.setValues(list(range(n)), i, v)
> File "PETSc/Mat.pyx", line 888, in petsc4py.PETSc.Mat.setValues
> File "PETSc/petscmat.pxi", line 828, in petsc4py.PETSc.matsetvalues
> ValueError: incompatible array sizes: ni=5, nj=1, nv=3
> Traceback (most recent call last):
> File "/home/saigre/Documents/code/tests/t2.py", line 31, in <module>
> Z.setValues(list(range(n)), i, v)
> File "PETSc/Mat.pyx", line 888, in petsc4py.PETSc.Mat.setValues
> File "PETSc/petscmat.pxi", line 828, in petsc4py.PETSc.matsetvalues
> ValueError: incompatible array sizes: ni=5, nj=1, nv=2
>
> Two weeks ago, I made a post on stack overflow (https://stackoverflow.com/questions/73124230/convert-a-list-of-vector-to-a-matrix-with-petsc4py). I tried using the apt packages, and I also compiled from the sources, but I get the same error.
>
> I someone has an idea how to succeed in it, I'm all ears !
>
> Thanks,
>
> Thomas
More information about the petsc-users
mailing list