2

I'm using petsc4py and now face some problems. I have a number of small petsc dense matrices mij, and I want to construct them to a big matrix M like this:

     [  m11  m12  m13  ]
M =  |  m21  m22  m23  |   ,
     [  m31  m32  m33  ]

A mcve code is showing below, and I'm using a python wrap of PETSc, however, their grammars are similar.

import numpy as np
from petsc4py import PETSc

mSizes = (5, 8, 6)
mij = []

# create sub-matrices mij
for i in range(len(mSizes)):
    for j in range(len(mSizes)):
        temp_m = PETSc.Mat().create(comm=PETSc.COMM_WORLD)
        temp_m.setSizes(((None, mSizes[i]), (None, mSizes[j])))
        temp_m.setType('mpidense')
        temp_m.setFromOptions()
        temp_m.setUp()
        temp_m[:, :] = np.random.random_sample((mSizes[i], mSizes[j]))
        temp_m.assemble()
        mij.append(temp_m)

# Now we have four sub-matrices. 
# I would like to construct them into a big matrix M.
M = PETSc.Mat().create(comm=PETSc.COMM_WORLD)
M.setSizes(((None, np.sum(mSizes)), (None, np.sum(mSizes))))
M.setType('mpidense')
M.setFromOptions()
M.setUp()
mLocations = np.insert(np.cumsum(mSizes), 0, 0)    # mLocations = [0, mSizes]
for i in range(len(mSizes)):
    for j in range(len(mSizes)):
        M[mLocations[i]:mLocations[i+1], mLocations[j]:mLocations[j+1]] = \ 
            mij[i*len(mSizes)+j][:, :]
M.assemble()

which report such error messages:

Traceback (most recent call last):
  File "tryMatConstuct.py", line 29, in <module>
    mij[i*len(mSizes)+j][:, :]
  File "PETSc/Mat.pyx", line 227, in petsc4py.PETSc.Mat.__getitem__ (src/petsc4py.PETSc.c:110477)
  File "PETSc/petscmat.pxi", line 997, in petsc4py.PETSc.mat_getitem (src/petsc4py.PETSc.c:30481)
  File "PETSc/petscmat.pxi", line 917, in petsc4py.PETSc.matgetvalues (src/petsc4py.PETSc.c:29242)
petsc4py.PETSc.Error: error code 56
[1] MatGetValues() line 1818 in /home/zhangji/PycharmProjects/petsc-petsc-31a1859eaff6/src/mat/interface/matrix.c
[1] MatGetValues_MPIDense() line 154 in /home/zhangji/PycharmProjects/petsc-petsc-31a1859eaff6/src/mat/impls/dense/mpi/mpidense.c
[1] No support for this operation for this object type
[1] Only local values currently supported
Ji Zhang
  • 128
  • 1
  • 11
  • The format MATMPIDENSE corresponds to sliced matrices : each process stores a given set of rows of the overall matrix as a MATSEQDENSE matrix. The function `MatGetValues()` only allows access to the local data : this is the reason why the error message gets printed. Similarly `MatDenseGetArray_MPIDense()` enables access to the local data. See [mpidense.c](http://www.mcs.anl.gov/petsc/petsc-current/src/mat/impls/dense/mpi/mpidense.c.html). The MPI communication needed to dispatch the data is left to you! We will need a [mcve](http://stackoverflow.com/help/mcve) to provide you with an answer! – francis Sep 13 '16 at 18:54

1 Answers1

1

I have to use a 2D array instead of a petsc matrix to short sub-matrix mij.

In petsc4py the function is PETSc.Mat().getDenseArray(), i.e.

temp_m = mij[i*len(mSizes)+j].getDenseArray()
Ji Zhang
  • 128
  • 1
  • 11