PETSc4Py Creating dense distributed matrix

83 Views Asked by At

I'm trying to create a new distributed matrix initialized by NumPy array using petsc4py.

My intention was to distribute every row of the matrix to different process and let them solve a linear system afterwards, but the distribution itself is not working...

My code looks like this

import numpy as np
import petsc4py.PETSc as PETSc

comm = PETSc.COMM_WORLD
rank = comm.Get_rank()

arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

n = arr.shape[0]

if rank < arr.shape[0]:
    A = PETSc.Mat().createDense((1, 3), array=arr[rank].reshape(1, 3), comm=PETSc.COMM_WORLD)
    print(rank, A)

And when I try to run the code mpirun -np 4 python3 main.py

I'm getting this error

    A = PETSc.Mat().createDense((1, 3), array=arr[rank].reshape(1, 3), comm=PETSc.COMM_WORLD)
    A = PETSc.Mat().createDense((1, 3), array=arr[rank].reshape(1, 3), comm=PETSc.COMM_WORLD)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "petsc4py/PETSc/Mat.pyx", line 465, in petsc4py.PETSc.Mat.createDense
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "petsc4py/PETSc/Mat.pyx", line 465, in petsc4py.PETSc.Mat.createDense
  File "petsc4py/PETSc/petscmat.pxi", line 849, in petsc4py.PETSc.Mat_AllocDense
  File "petsc4py/PETSc/petscmat.pxi", line 849, in petsc4py.PETSc.Mat_AllocDense
ValueError: size(array) is 3, expected 0x3=0
ValueError: size(array) is 3, expected 0x3=0

I can see, that the problem is with the specification of the local matrix size, but I have no idea, why PETSc expects it to be 0x3 now... What am I doing incorrectly?

0

There are 0 best solutions below