On Tue, Aug 9, 2022 at 11:12 AM Stefano Zampini <stefano.zamp...@gmail.com> wrote:
> PETSc distributes matrices and vectors in parallel. Take a look at > https://petsc.org/release/docs/manualpages/Vec/VecGetOwnershipRange.html Longer explanation: Vectors are distributed by giving a stretch of contiguous rows to each process. Dense matrices also give a stretch of contiguous rows to each process. This means that each matrix column would correspond to a vector. The easiest way I see to do this is to use https://petsc.org/main/docs/manualpages/Mat/MatDenseGetColumnVec/ and then VecCopy() each vector into the correct column. If you want to share memory, allocate the dense matrix first, and make you vectors using this routine. Thanks, Matt > > > On Aug 9, 2022, at 11:21 AM, Thomas Saigre <sai...@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 > > -- 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/>